Implement HashOf statement and op (#217)

This commit is contained in:
Ahmad Afuni 2025-05-06 19:14:53 +10:00 committed by GitHub
parent 53ade6ea26
commit 8cc090c5e0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 139 additions and 6 deletions

View file

@ -58,7 +58,7 @@ use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use super::serialization::*;
use crate::middleware::{Params, ToFields};
use crate::middleware::{Params, ToFields, Value};
/// F is the native field we use everywhere. Currently it's Goldilocks from plonky2
pub type F = GoldilocksField;
@ -164,6 +164,10 @@ pub fn hash_fields(input: &[F]) -> Hash {
Hash(PoseidonHash::hash_no_pad(input).elements)
}
pub fn hash_values(input: &[Value]) -> Hash {
hash_fields(&input.iter().flat_map(|v| v.raw().0).collect::<Vec<_>>())
}
impl From<RawValue> for Hash {
fn from(v: RawValue) -> Self {
Hash(v.0)

View file

@ -69,6 +69,7 @@ pub enum NativeOperation {
SumOf = 11,
ProductOf = 12,
MaxOf = 13,
HashOf = 14,
// Syntactic sugar operations. These operations are not supported by the backend. The
// frontend compiler is responsible of translating these operations into the operations above.
@ -119,6 +120,7 @@ impl OperationType {
NativeOperation::SumOf => Some(Predicate::Native(NativePredicate::SumOf)),
NativeOperation::ProductOf => Some(Predicate::Native(NativePredicate::ProductOf)),
NativeOperation::MaxOf => Some(Predicate::Native(NativePredicate::MaxOf)),
NativeOperation::HashOf => Some(Predicate::Native(NativePredicate::HashOf)),
no => unreachable!("Unexpected syntactic sugar op {:?}", no),
},
OperationType::Custom(cpr) => Some(Predicate::Custom(cpr.clone())),
@ -152,6 +154,7 @@ pub enum Operation {
SumOf(Statement, Statement, Statement),
ProductOf(Statement, Statement, Statement),
MaxOf(Statement, Statement, Statement),
HashOf(Statement, Statement, Statement),
Custom(CustomPredicateRef, Vec<Statement>),
}
@ -174,6 +177,7 @@ impl Operation {
Self::SumOf(_, _, _) => OT::Native(SumOf),
Self::ProductOf(_, _, _) => OT::Native(ProductOf),
Self::MaxOf(_, _, _) => OT::Native(MaxOf),
Self::HashOf(_, _, _) => OT::Native(HashOf),
Self::Custom(cpr, _) => OT::Custom(cpr.clone()),
}
}
@ -194,6 +198,7 @@ impl Operation {
Self::SumOf(s1, s2, s3) => vec![s1, s2, s3],
Self::ProductOf(s1, s2, s3) => vec![s1, s2, s3],
Self::MaxOf(s1, s2, s3) => vec![s1, s2, s3],
Self::HashOf(s1, s2, s3) => vec![s1, s2, s3],
Self::Custom(_, args) => args,
}
}
@ -250,6 +255,9 @@ impl Operation {
Self::ProductOf(s1, s2, s3)
}
(NO::MaxOf, (Some(s1), Some(s2), Some(s3)), OA::None, 3) => Self::MaxOf(s1, s2, s3),
(NO::HashOf, (Some(s1), Some(s2), Some(s3)), OA::None, 3) => {
Self::HashOf(s1, s2, s3)
}
_ => Err(Error::custom(format!(
"Ill-formed operation {:?} with arguments {:?}.",
op_code, args

View file

@ -32,6 +32,7 @@ pub enum NativePredicate {
SumOf = 8,
ProductOf = 9,
MaxOf = 10,
HashOf = 11,
// Syntactic sugar predicates. These predicates are not supported by the backend. The
// frontend compiler is responsible of translating these predicates into the predicates above.
@ -102,6 +103,7 @@ pub enum Statement {
SumOf(AnchoredKey, AnchoredKey, AnchoredKey),
ProductOf(AnchoredKey, AnchoredKey, AnchoredKey),
MaxOf(AnchoredKey, AnchoredKey, AnchoredKey),
HashOf(AnchoredKey, AnchoredKey, AnchoredKey),
Custom(CustomPredicateRef, Vec<WildcardValue>),
}
@ -123,6 +125,7 @@ impl Statement {
Self::SumOf(_, _, _) => Native(NativePredicate::SumOf),
Self::ProductOf(_, _, _) => Native(NativePredicate::ProductOf),
Self::MaxOf(_, _, _) => Native(NativePredicate::MaxOf),
Self::HashOf(_, _, _) => Native(NativePredicate::HashOf),
Self::Custom(cpr, _) => Custom(cpr.clone()),
}
}
@ -140,6 +143,7 @@ impl Statement {
Self::SumOf(ak1, ak2, ak3) => vec![Key(ak1), Key(ak2), Key(ak3)],
Self::ProductOf(ak1, ak2, ak3) => vec![Key(ak1), Key(ak2), Key(ak3)],
Self::MaxOf(ak1, ak2, ak3) => vec![Key(ak1), Key(ak2), Key(ak3)],
Self::HashOf(ak1, ak2, ak3) => vec![Key(ak1), Key(ak2), Key(ak3)],
Self::Custom(_, args) => Vec::from_iter(args.into_iter().map(WildcardLiteral)),
}
}