Fe contains (#145)

* Contains should take three arguments (root, key, value)

* Add a test for frontend Dictionaries

* Separate frontend and middleware operations

* Make tests pass: add arg to contains

* Cargo fmt

* Merkleproof verify circuit (#143)

* merkletree: add keypath circuit

* merkletree-circuit: implement proof of existence verification in-circuit

* parametrize max_depth at the tree circuit

* Constrain selectors in-circuit

* implement merketree nonexistence proof circuit, and add edgecase tests

* add non-existence proofs documentation in the mdbook, mv EMPTY->EMPTY_VALUE & NULL->EMPTY_HASH, dependency clean and public exposure methods

* review comments, some extra polishing and add a test that expects wrong proofs to fail

* Add circuit to check only merkleproofs-of-existence

With this, the merkletree_circuit module offers two different circuits:
- `MerkleProofCircuit`: allows to verify both proofs of existence and proofs
non-existence with the same circuit.
- `MerkleProofExistenceCircuit`: allows to verify proofs of existence only.

In this way, if only proofs of existence are needed,
`MerkleProofExistenceCircuit` should be used, which requires less amount
of constraints than `MerkleProofCircuit`.

* Code review

---------

Co-authored-by: Ahmad <root@ahmadafuni.com>

* Towards Contains/NotContains in middleware and backend

* Fix build

* Adding error handling to deal with op compile introduce extra ops

* Incorporate Merkle proofs into MockMainPod

* Merkleproof verify circuit (#143)

* merkletree: add keypath circuit

* merkletree-circuit: implement proof of existence verification in-circuit

* parametrize max_depth at the tree circuit

* Constrain selectors in-circuit

* implement merketree nonexistence proof circuit, and add edgecase tests

* add non-existence proofs documentation in the mdbook, mv EMPTY->EMPTY_VALUE & NULL->EMPTY_HASH, dependency clean and public exposure methods

* review comments, some extra polishing and add a test that expects wrong proofs to fail

* Add circuit to check only merkleproofs-of-existence

With this, the merkletree_circuit module offers two different circuits:
- `MerkleProofCircuit`: allows to verify both proofs of existence and proofs
non-existence with the same circuit.
- `MerkleProofExistenceCircuit`: allows to verify proofs of existence only.

In this way, if only proofs of existence are needed,
`MerkleProofExistenceCircuit` should be used, which requires less amount
of constraints than `MerkleProofCircuit`.

* Code review

---------

Co-authored-by: Ahmad <root@ahmadafuni.com>

* Towards Contains/NotContains in middleware and backend

* Frontend compound types -- allow one frontend operation to produce multiple middleware statements (in progress)

* Incorporate Merkle proofs into MockMainPod

* Incorporate Merkle proof op arg into frontend

* Compile one statement to many, in progress

* Fix remaining tests

* Minor clean-up

* Oops I did a bunch of work in the middle of a rebase, committing

* Incorporate Merkle proof op arg into frontend

* still working on frontend compound types, refactor compile() to output multiple statements

* Contains statements for frontend types: code compiles

* Tests pass

* Examples use front-end compound types

* Remove old Contains and NotContains from frontend

* Add nin to typos

* Code review

---------

Co-authored-by: arnaucube <git@arnaucube.com>
Co-authored-by: Ahmad <root@ahmadafuni.com>
This commit is contained in:
tideofwords 2025-03-26 17:54:58 -07:00 committed by GitHub
parent d6033b7090
commit d00ff95f41
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 789 additions and 162 deletions

View file

@ -6,7 +6,10 @@ use std::fmt;
use std::iter;
use super::{CustomPredicateRef, NativePredicate, Statement, StatementArg, ToFields, F};
use crate::middleware::{AnchoredKey, Params, Predicate, Value, SELF};
use crate::{
backends::plonky2::primitives::merkletree::{MerkleProof, MerkleTree},
middleware::{AnchoredKey, Params, Predicate, Value, SELF},
};
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum OperationType {
@ -14,6 +17,22 @@ pub enum OperationType {
Custom(CustomPredicateRef),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum OperationAux {
None,
MerkleProof(MerkleProof),
}
impl fmt::Display for OperationAux {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::None => write!(f, "<no aux>")?,
Self::MerkleProof(pf) => write!(f, "merkle_proof({})", pf)?,
}
Ok(())
}
}
impl ToFields for OperationType {
fn to_fields(&self, params: &Params) -> Vec<F> {
let mut fields: Vec<F> = match self {
@ -106,8 +125,17 @@ pub enum Operation {
TransitiveEqualFromStatements(Statement, Statement),
GtToNotEqual(Statement),
LtToNotEqual(Statement),
ContainsFromEntries(Statement, Statement),
NotContainsFromEntries(Statement, Statement),
ContainsFromEntries(
/* root */ Statement,
/* key */ Statement,
/* value */ Statement,
/* proof */ MerkleProof,
),
NotContainsFromEntries(
/* root */ Statement,
/* key */ Statement,
/* proof */ MerkleProof,
),
SumOf(Statement, Statement, Statement),
ProductOf(Statement, Statement, Statement),
MaxOf(Statement, Statement, Statement),
@ -129,8 +157,8 @@ impl Operation {
Self::TransitiveEqualFromStatements(_, _) => OT::Native(TransitiveEqualFromStatements),
Self::GtToNotEqual(_) => OT::Native(GtToNotEqual),
Self::LtToNotEqual(_) => OT::Native(LtToNotEqual),
Self::ContainsFromEntries(_, _) => OT::Native(ContainsFromEntries),
Self::NotContainsFromEntries(_, _) => OT::Native(NotContainsFromEntries),
Self::ContainsFromEntries(_, _, _, _) => OT::Native(ContainsFromEntries),
Self::NotContainsFromEntries(_, _, _) => OT::Native(NotContainsFromEntries),
Self::SumOf(_, _, _) => OT::Native(SumOf),
Self::ProductOf(_, _, _) => OT::Native(ProductOf),
Self::MaxOf(_, _, _) => OT::Native(MaxOf),
@ -150,16 +178,27 @@ impl Operation {
Self::TransitiveEqualFromStatements(s1, s2) => vec![s1, s2],
Self::GtToNotEqual(s) => vec![s],
Self::LtToNotEqual(s) => vec![s],
Self::ContainsFromEntries(s1, s2) => vec![s1, s2],
Self::NotContainsFromEntries(s1, s2) => vec![s1, s2],
Self::ContainsFromEntries(s1, s2, s3, pf) => vec![s1, s2, s3],
Self::NotContainsFromEntries(s1, s2, pf) => vec![s1, s2],
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::Custom(_, args) => args,
}
}
/// Extracts auxiliary data from operation.
pub fn aux(&self) -> OperationAux {
match self {
Self::ContainsFromEntries(_, _, _, mp) => OperationAux::MerkleProof(mp.clone()),
Self::NotContainsFromEntries(_, _, mp) => OperationAux::MerkleProof(mp.clone()),
_ => OperationAux::None,
}
}
/// Forms operation from op-code and arguments.
pub fn op(op_code: OperationType, args: &[Statement]) -> Result<Self> {
pub fn op(op_code: OperationType, args: &[Statement], aux: &OperationAux) -> Result<Self> {
type OA = OperationAux;
type NO = NativeOperation;
let arg_tup = (
args.first().cloned(),
@ -167,27 +206,39 @@ impl Operation {
args.get(2).cloned(),
);
Ok(match op_code {
OperationType::Native(o) => match (o, arg_tup, args.len()) {
(NO::None, (None, None, None), 0) => Self::None,
(NO::NewEntry, (None, None, None), 0) => Self::NewEntry,
(NO::CopyStatement, (Some(s), None, None), 1) => Self::CopyStatement(s),
(NO::EqualFromEntries, (Some(s1), Some(s2), None), 2) => {
OperationType::Native(o) => match (o, arg_tup, aux.clone(), args.len()) {
(NO::None, (None, None, None), OA::None, 0) => Self::None,
(NO::NewEntry, (None, None, None), OA::None, 0) => Self::NewEntry,
(NO::CopyStatement, (Some(s), None, None), OA::None, 1) => Self::CopyStatement(s),
(NO::EqualFromEntries, (Some(s1), Some(s2), None), OA::None, 2) => {
Self::EqualFromEntries(s1, s2)
}
(NO::NotEqualFromEntries, (Some(s1), Some(s2), None), 2) => {
(NO::NotEqualFromEntries, (Some(s1), Some(s2), None), OA::None, 2) => {
Self::NotEqualFromEntries(s1, s2)
}
(NO::GtFromEntries, (Some(s1), Some(s2), None), 2) => Self::GtFromEntries(s1, s2),
(NO::LtFromEntries, (Some(s1), Some(s2), None), 2) => Self::LtFromEntries(s1, s2),
(NO::ContainsFromEntries, (Some(s1), Some(s2), None), 2) => {
Self::ContainsFromEntries(s1, s2)
(NO::GtFromEntries, (Some(s1), Some(s2), None), OA::None, 2) => {
Self::GtFromEntries(s1, s2)
}
(NO::NotContainsFromEntries, (Some(s1), Some(s2), None), 2) => {
Self::NotContainsFromEntries(s1, s2)
(NO::LtFromEntries, (Some(s1), Some(s2), None), OA::None, 2) => {
Self::LtFromEntries(s1, s2)
}
(NO::SumOf, (Some(s1), Some(s2), Some(s3)), 3) => Self::SumOf(s1, s2, s3),
(NO::ProductOf, (Some(s1), Some(s2), Some(s3)), 3) => Self::ProductOf(s1, s2, s3),
(NO::MaxOf, (Some(s1), Some(s2), Some(s3)), 3) => Self::MaxOf(s1, s2, s3),
(
NO::ContainsFromEntries,
(Some(s1), Some(s2), Some(s3)),
OA::MerkleProof(pf),
3,
) => Self::ContainsFromEntries(s1, s2, s3, pf),
(
NO::NotContainsFromEntries,
(Some(s1), Some(s2), None),
OA::MerkleProof(pf),
2,
) => Self::NotContainsFromEntries(s1, s2, pf),
(NO::SumOf, (Some(s1), Some(s2), Some(s3)), OA::None, 3) => Self::SumOf(s1, s2, s3),
(NO::ProductOf, (Some(s1), Some(s2), Some(s3)), OA::None, 3) => {
Self::ProductOf(s1, s2, s3)
}
(NO::MaxOf, (Some(s1), Some(s2), Some(s3)), OA::None, 3) => Self::MaxOf(s1, s2, s3),
_ => Err(anyhow!(
"Ill-formed operation {:?} with arguments {:?}.",
op_code,
@ -270,20 +321,25 @@ impl Operation {
Self::LtToNotEqual(_) => {
return Err(anyhow!("Invalid operation"));
}
Self::ContainsFromEntries(ValueOf(ak1, v1), ValueOf(ak2, v2)) =>
/* TODO */
Self::ContainsFromEntries(ValueOf(ak1, v1), ValueOf(ak2, v2), ValueOf(ak3, v3), pf)
if MerkleTree::verify(pf.siblings.len(), (*v1).into(), &pf, v2, v3)? == () =>
{
Some(vec![StatementArg::Key(*ak1), StatementArg::Key(*ak2)])
Some(vec![
StatementArg::Key(*ak1),
StatementArg::Key(*ak2),
StatementArg::Key(*ak3),
])
}
Self::ContainsFromEntries(_, _) => {
Self::ContainsFromEntries(_, _, _, _) => {
return Err(anyhow!("Invalid operation"));
}
Self::NotContainsFromEntries(ValueOf(ak1, v1), ValueOf(ak2, v2)) =>
/* TODO */
Self::NotContainsFromEntries(ValueOf(ak1, v1), ValueOf(ak2, v2), pf)
if MerkleTree::verify_nonexistence(pf.siblings.len(), (*v1).into(), &pf, v2)?
== () =>
{
Some(vec![StatementArg::Key(*ak1), StatementArg::Key(*ak2)])
}
Self::NotContainsFromEntries(_, _) => {
Self::NotContainsFromEntries(_, _, _) => {
return Err(anyhow!("Invalid operation"));
}
Self::SumOf(ValueOf(ak1, v1), ValueOf(ak2, v2), ValueOf(ak3, v3)) => {
@ -361,12 +417,12 @@ impl Operation {
(Self::LtFromEntries(ValueOf(ak1, v1), ValueOf(ak2, v2)), Lt(ak3, ak4)) => {
Ok(v1 < v2 && ak3 == ak1 && ak4 == ak2)
}
(Self::ContainsFromEntries(_, _), Contains(_, _)) =>
(Self::ContainsFromEntries(_, _, _, _), Contains(_, _, _)) =>
/* TODO */
{
Ok(true)
}
(Self::NotContainsFromEntries(_, _), NotContains(_, _)) =>
(Self::NotContainsFromEntries(_, _, _), NotContains(_, _)) =>
/* TODO */
{
Ok(true)