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

@ -4,3 +4,4 @@ BA = "BA"
Ded = "Ded" # "ANDed", it thought "Ded" should be "Dead" Ded = "Ded" # "ANDed", it thought "Ded" should be "Dead"
OT = "OT" OT = "OT"
aks = "aks" # anchored keys aks = "aks" # anchored keys
nin = "nin" # not in

View file

@ -491,7 +491,10 @@ impl MainPodVerifyCircuit {
mod tests { mod tests {
use super::*; use super::*;
use crate::backends::plonky2::mock::mainpod; use crate::backends::plonky2::mock::mainpod;
use crate::backends::plonky2::{basetypes::C, mock::mainpod::OperationArg}; use crate::backends::plonky2::{
basetypes::C,
mock::mainpod::{OperationArg, OperationAux},
};
use crate::middleware::{OperationType, PodId}; use crate::middleware::{OperationType, PodId};
use plonky2::plonk::{circuit_builder::CircuitBuilder, circuit_data::CircuitConfig}; use plonky2::plonk::{circuit_builder::CircuitBuilder, circuit_data::CircuitConfig};
@ -571,20 +574,29 @@ mod tests {
fn test_operation_verify() -> Result<()> { fn test_operation_verify() -> Result<()> {
// None // None
let st: mainpod::Statement = Statement::None.into(); let st: mainpod::Statement = Statement::None.into();
let op = mainpod::Operation(OperationType::Native(NativeOperation::None), vec![]); let op = mainpod::Operation(
OperationType::Native(NativeOperation::None),
vec![],
OperationAux::None,
);
let prev_statements = vec![Statement::None.into()]; let prev_statements = vec![Statement::None.into()];
operation_verify(st.clone(), op, prev_statements.clone())?; operation_verify(st.clone(), op, prev_statements.clone())?;
// NewEntry // NewEntry
let st1: mainpod::Statement = let st1: mainpod::Statement =
Statement::ValueOf(AnchoredKey(SELF, "hello".into()), 55.into()).into(); Statement::ValueOf(AnchoredKey(SELF, "hello".into()), 55.into()).into();
let op = mainpod::Operation(OperationType::Native(NativeOperation::NewEntry), vec![]); let op = mainpod::Operation(
OperationType::Native(NativeOperation::NewEntry),
vec![],
OperationAux::None,
);
operation_verify(st1.clone(), op, vec![])?; operation_verify(st1.clone(), op, vec![])?;
// Copy // Copy
let op = mainpod::Operation( let op = mainpod::Operation(
OperationType::Native(NativeOperation::CopyStatement), OperationType::Native(NativeOperation::CopyStatement),
vec![OperationArg::Index(0)], vec![OperationArg::Index(0)],
OperationAux::None,
); );
operation_verify(st, op, prev_statements)?; operation_verify(st, op, prev_statements)?;
@ -602,6 +614,7 @@ mod tests {
let op = mainpod::Operation( let op = mainpod::Operation(
OperationType::Native(NativeOperation::EqualFromEntries), OperationType::Native(NativeOperation::EqualFromEntries),
vec![OperationArg::Index(0), OperationArg::Index(1)], vec![OperationArg::Index(0), OperationArg::Index(1)],
OperationAux::None,
); );
let prev_statements = vec![st1.clone(), st2]; let prev_statements = vec![st1.clone(), st2];
operation_verify(st, op, prev_statements)?; operation_verify(st, op, prev_statements)?;
@ -620,6 +633,7 @@ mod tests {
let op = mainpod::Operation( let op = mainpod::Operation(
OperationType::Native(NativeOperation::LtFromEntries), OperationType::Native(NativeOperation::LtFromEntries),
vec![OperationArg::Index(0), OperationArg::Index(1)], vec![OperationArg::Index(0), OperationArg::Index(1)],
OperationAux::None,
); );
let prev_statements = vec![st1.clone(), st2]; let prev_statements = vec![st1.clone(), st2];
operation_verify(st, op, prev_statements)?; operation_verify(st, op, prev_statements)?;

View file

@ -8,10 +8,13 @@ use serde::{Deserialize, Serialize};
use std::any::Any; use std::any::Any;
use std::fmt; use std::fmt;
use crate::middleware::{ use crate::{
self, hash_str, AnchoredKey, Hash, MainPodInputs, NativeOperation, NativePredicate, NonePod, backends::plonky2::primitives::merkletree::MerkleProof,
OperationType, Params, Pod, PodId, PodProver, Predicate, StatementArg, ToFields, KEY_TYPE, middleware::{
SELF, self, hash_str, AnchoredKey, Hash, MainPodInputs, NativeOperation, NativePredicate,
NonePod, OperationType, Params, Pod, PodId, PodProver, Predicate, StatementArg, ToFields,
KEY_TYPE, SELF,
},
}; };
mod operation; mod operation;
@ -41,6 +44,9 @@ pub struct MockMainPod {
operations: Vec<Operation>, operations: Vec<Operation>,
// All statements (inherited + new) // All statements (inherited + new)
statements: Vec<Statement>, statements: Vec<Statement>,
// All Merkle proofs
// TODO: Use a backend-specific representation
merkle_proofs: Vec<MerkleProof>,
} }
impl fmt::Display for MockMainPod { impl fmt::Display for MockMainPod {
@ -243,9 +249,28 @@ impl MockMainPod {
} }
} }
fn find_op_aux(
merkle_proofs: &[MerkleProof],
op_aux: &middleware::OperationAux,
) -> Result<OperationAux> {
match op_aux {
middleware::OperationAux::None => Ok(OperationAux::None),
middleware::OperationAux::MerkleProof(pf_arg) => merkle_proofs
.iter()
.enumerate()
.find_map(|(i, pf)| (pf == pf_arg).then_some(i))
.map(OperationAux::MerkleProofIndex)
.ok_or(anyhow!(
"Merkle proof corresponding to op arg {} not found",
op_aux
)),
}
}
fn process_private_statements_operations( fn process_private_statements_operations(
params: &Params, params: &Params,
statements: &[Statement], statements: &[Statement],
merkle_proofs: &[MerkleProof],
input_operations: &[middleware::Operation], input_operations: &[middleware::Operation],
) -> Result<Vec<Operation>> { ) -> Result<Vec<Operation>> {
let mut operations = Vec::new(); let mut operations = Vec::new();
@ -259,8 +284,12 @@ impl MockMainPod {
.iter() .iter()
.map(|mid_arg| Self::find_op_arg(statements, mid_arg)) .map(|mid_arg| Self::find_op_arg(statements, mid_arg))
.collect::<Result<Vec<_>>>()?; .collect::<Result<Vec<_>>>()?;
let mid_aux = op.aux();
let aux = Self::find_op_aux(merkle_proofs, &mid_aux)?;
Self::pad_operation_args(params, &mut args); Self::pad_operation_args(params, &mut args);
operations.push(Operation(op.op_type(), args)); operations.push(Operation(op.op_type(), args, aux));
} }
Ok(operations) Ok(operations)
} }
@ -278,17 +307,22 @@ impl MockMainPod {
operations.push(Operation( operations.push(Operation(
OperationType::Native(NativeOperation::NewEntry), OperationType::Native(NativeOperation::NewEntry),
vec![], vec![],
OperationAux::None,
)); ));
for i in 0..(params.max_public_statements - 1) { for i in 0..(params.max_public_statements - 1) {
let st = &statements[offset_public_statements + i + 1]; let st = &statements[offset_public_statements + i + 1];
let mut op = if st.is_none() { let mut op = if st.is_none() {
Operation(OperationType::Native(NativeOperation::None), vec![]) Operation(
OperationType::Native(NativeOperation::None),
vec![],
OperationAux::None,
)
} else { } else {
let mid_arg = st.clone(); let mid_arg = st.clone();
Operation( Operation(
OperationType::Native(NativeOperation::CopyStatement), OperationType::Native(NativeOperation::CopyStatement),
// TODO vec![Self::find_op_arg(statements, &mid_arg.try_into()?)?],
vec![Self::find_op_arg(statements, &mid_arg.try_into().unwrap())?], OperationAux::None,
) )
}; };
fill_pad(&mut op.1, OperationArg::None, params.max_operation_args); fill_pad(&mut op.1, OperationArg::None, params.max_operation_args);
@ -304,8 +338,21 @@ impl MockMainPod {
// TODO: Insert a new public statement of ValueOf with `key=KEY_TYPE, // TODO: Insert a new public statement of ValueOf with `key=KEY_TYPE,
// value=PodType::MockMainPod` // value=PodType::MockMainPod`
let statements = Self::layout_statements(params, &inputs); let statements = Self::layout_statements(params, &inputs);
let operations = let merkle_proofs = inputs
Self::process_private_statements_operations(params, &statements, inputs.operations)?; .operations
.iter()
.flat_map(|op| match op {
middleware::Operation::ContainsFromEntries(_, _, _, pf) => Some(pf.clone()),
middleware::Operation::NotContainsFromEntries(_, _, pf) => Some(pf.clone()),
_ => None,
})
.collect::<Vec<_>>();
let operations = Self::process_private_statements_operations(
params,
&statements,
&merkle_proofs,
inputs.operations,
)?;
let operations = let operations =
Self::process_public_statements_operations(params, &statements, operations)?; Self::process_public_statements_operations(params, &statements, operations)?;
@ -340,6 +387,7 @@ impl MockMainPod {
public_statements, public_statements,
statements, statements,
operations, operations,
merkle_proofs,
}) })
} }
@ -350,7 +398,11 @@ impl MockMainPod {
} }
fn operation_none(params: &Params) -> Operation { fn operation_none(params: &Params) -> Operation {
let mut op = Operation(OperationType::Native(NativeOperation::None), vec![]); let mut op = Operation(
OperationType::Native(NativeOperation::None),
vec![],
OperationAux::None,
);
fill_pad(&mut op.1, OperationArg::None, params.max_operation_args); fill_pad(&mut op.1, OperationArg::None, params.max_operation_args);
op op
} }
@ -444,7 +496,10 @@ impl Pod for MockMainPod {
.enumerate() .enumerate()
.map(|(i, s)| { .map(|(i, s)| {
self.operations[i] self.operations[i]
.deref(&self.statements[..input_statement_offset + i]) .deref(
&self.statements[..input_statement_offset + i],
&self.merkle_proofs,
)
.unwrap() .unwrap()
.check_and_log(&self.params, &s.clone().try_into().unwrap()) .check_and_log(&self.params, &s.clone().try_into().unwrap())
}) })
@ -513,13 +568,18 @@ pub mod tests {
zu_kyc_sign_pod_builders, zu_kyc_sign_pod_builders,
}; };
use crate::middleware; use crate::middleware;
use crate::middleware::containers::Set;
#[test] #[test]
fn test_mock_main_zu_kyc() -> Result<()> { fn test_mock_main_zu_kyc() -> Result<()> {
let params = middleware::Params::default(); let params = middleware::Params::default();
let sanctions_values = ["A343434340"].map(|s| crate::frontend::Value::from(s));
let sanction_set = crate::frontend::Value::Set(crate::frontend::containers::Set::new(
sanctions_values.to_vec(),
)?);
let (gov_id_builder, pay_stub_builder, sanction_list_builder) = let (gov_id_builder, pay_stub_builder, sanction_list_builder) =
zu_kyc_sign_pod_builders(&params); zu_kyc_sign_pod_builders(&params, &sanction_set);
let mut signer = MockSigner { let mut signer = MockSigner {
pk: "ZooGov".into(), pk: "ZooGov".into(),
}; };

View file

@ -1,6 +1,9 @@
use super::Statement; use super::Statement;
use crate::middleware::{self, OperationType, Params, ToFields, F}; use crate::{
use anyhow::Result; backends::plonky2::primitives::merkletree::MerkleProof,
middleware::{self, OperationType, Params, ToFields, F},
};
use anyhow::{anyhow, Result};
use plonky2::field::types::{Field, PrimeField64}; use plonky2::field::types::{Field, PrimeField64};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::fmt; use std::fmt;
@ -28,7 +31,13 @@ impl OperationArg {
} }
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Operation(pub OperationType, pub Vec<OperationArg>); pub enum OperationAux {
None,
MerkleProofIndex(usize),
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Operation(pub OperationType, pub Vec<OperationArg>, pub OperationAux);
impl Operation { impl Operation {
pub fn op_type(&self) -> OperationType { pub fn op_type(&self) -> OperationType {
@ -37,7 +46,11 @@ impl Operation {
pub fn args(&self) -> &[OperationArg] { pub fn args(&self) -> &[OperationArg] {
&self.1 &self.1
} }
pub fn deref(&self, statements: &[Statement]) -> Result<crate::middleware::Operation> { pub fn deref(
&self,
statements: &[Statement],
merkle_proofs: &[MerkleProof],
) -> Result<crate::middleware::Operation> {
let deref_args = self let deref_args = self
.1 .1
.iter() .iter()
@ -45,8 +58,16 @@ impl Operation {
OperationArg::None => None, OperationArg::None => None,
OperationArg::Index(i) => Some(statements[*i].clone().try_into()), OperationArg::Index(i) => Some(statements[*i].clone().try_into()),
}) })
.collect::<Result<Vec<crate::middleware::Statement>>>()?; .collect::<Result<Vec<_>>>()?;
middleware::Operation::op(self.0.clone(), &deref_args) let deref_aux = match self.2 {
OperationAux::None => Ok(crate::middleware::OperationAux::None),
OperationAux::MerkleProofIndex(i) => merkle_proofs
.get(i)
.cloned()
.ok_or(anyhow!("Missing Merkle proof index {}", i))
.map(crate::middleware::OperationAux::MerkleProof),
}?;
middleware::Operation::op(self.0.clone(), &deref_args, &deref_aux)
} }
} }
@ -64,6 +85,10 @@ impl fmt::Display for Operation {
} }
} }
} }
match self.2 {
OperationAux::None => (),
OperationAux::MerkleProofIndex(i) => write!(f, "merkle_proof_{:02}", i)?,
}
Ok(()) Ok(())
} }
} }

View file

@ -60,8 +60,8 @@ impl TryFrom<Statement> for middleware::Statement {
} }
(NP::Gt, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), None), 2) => S::Gt(ak1, ak2), (NP::Gt, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), None), 2) => S::Gt(ak1, ak2),
(NP::Lt, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), None), 2) => S::Lt(ak1, ak2), (NP::Lt, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), None), 2) => S::Lt(ak1, ak2),
(NP::Contains, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), None), 2) => { (NP::Contains, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), Some(SA::Key(ak3))), 3) => {
S::Contains(ak1, ak2) S::Contains(ak1, ak2, ak3)
} }
(NP::NotContains, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), None), 2) => { (NP::NotContains, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), None), 2) => {
S::NotContains(ak1, ak2) S::NotContains(ak1, ak2)

View file

@ -2,6 +2,7 @@
//! https://0xparc.github.io/pod2/merkletree.html . //! https://0xparc.github.io/pod2/merkletree.html .
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use plonky2::field::types::Field; use plonky2::field::types::Field;
use serde::{Deserialize, Serialize};
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt; use std::fmt;
use std::iter::IntoIterator; use std::iter::IntoIterator;
@ -207,7 +208,7 @@ impl fmt::Display for MerkleTree {
} }
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct MerkleProof { pub struct MerkleProof {
// note: currently we don't use the `_existence` field, we would use if we merge the methods // note: currently we don't use the `_existence` field, we would use if we merge the methods
// `verify` and `verify_nonexistence` into a single one // `verify` and `verify_nonexistence` into a single one

View file

@ -17,6 +17,7 @@ use crate::op;
pub fn zu_kyc_sign_pod_builders( pub fn zu_kyc_sign_pod_builders(
params: &Params, params: &Params,
sanction_set: &Value,
) -> (SignedPodBuilder, SignedPodBuilder, SignedPodBuilder) { ) -> (SignedPodBuilder, SignedPodBuilder, SignedPodBuilder) {
let mut gov_id = SignedPodBuilder::new(params); let mut gov_id = SignedPodBuilder::new(params);
gov_id.insert("idNumber", "4242424242"); gov_id.insert("idNumber", "4242424242");
@ -44,17 +45,27 @@ pub fn zu_kyc_pod_builder(
pay_stub: &SignedPod, pay_stub: &SignedPod,
sanction_list: &SignedPod, sanction_list: &SignedPod,
) -> Result<MainPodBuilder> { ) -> Result<MainPodBuilder> {
let sanction_set = match sanction_list.kvs.get("sanctionList") {
Some(Value::Set(s)) => Ok(s),
_ => Err(anyhow!("Missing sanction list!")),
}?;
let now_minus_18y: i64 = 1169909388; let now_minus_18y: i64 = 1169909388;
let now_minus_1y: i64 = 1706367566; let now_minus_1y: i64 = 1706367566;
let gov_id_kvs = gov_id.kvs();
let id_number_value = gov_id_kvs.get(&"idNumber".into()).unwrap();
let mut kyc = MainPodBuilder::new(params); let mut kyc = MainPodBuilder::new(params);
kyc.add_signed_pod(gov_id); kyc.add_signed_pod(gov_id);
kyc.add_signed_pod(pay_stub); kyc.add_signed_pod(pay_stub);
kyc.add_signed_pod(sanction_list); kyc.add_signed_pod(sanction_list);
kyc.pub_op(op!( kyc.pub_op(op!(
not_contains, set_not_contains,
(sanction_list, "sanctionList"), (sanction_list, "sanctionList"),
(gov_id, "idNumber") (gov_id, "idNumber"),
sanction_set
.middleware_set()
.prove_nonexistence(id_number_value)?
))?; ))?;
kyc.pub_op(op!(lt, (gov_id, "dateOfBirth"), now_minus_18y))?; kyc.pub_op(op!(lt, (gov_id, "dateOfBirth"), now_minus_18y))?;
kyc.pub_op(op!( kyc.pub_op(op!(
@ -251,6 +262,8 @@ pub fn great_boy_pod_builder(
PodType::MockSigned as i64 PodType::MockSigned as i64
))?; ))?;
for issuer_idx in 0..2 { for issuer_idx in 0..2 {
let pod_kvs = good_boy_pods[good_boy_idx * 2 + issuer_idx].kvs();
// Type check // Type check
great_boy.pub_op(op!( great_boy.pub_op(op!(
eq, eq,
@ -258,10 +271,19 @@ pub fn great_boy_pod_builder(
PodType::MockSigned as i64 PodType::MockSigned as i64
))?; ))?;
// Each good boy POD comes from a valid issuer // Each good boy POD comes from a valid issuer
let good_boy_proof = match good_boy_issuers {
Value::Dictionary(dict) => Ok(dict),
_ => Err(anyhow!("Invalid good boy issuers!")),
}?
.middleware_dict()
.prove(pod_kvs.get(&KEY_SIGNER.into()).unwrap())?
.1;
great_boy.pub_op(op!( great_boy.pub_op(op!(
contains, dict_contains,
good_boy_issuers, good_boy_issuers,
(good_boy_pods[good_boy_idx * 2 + issuer_idx], KEY_SIGNER) (good_boy_pods[good_boy_idx * 2 + issuer_idx], KEY_SIGNER),
0,
good_boy_proof
))?; ))?;
// Each good boy has 2 good boy pods // Each good boy has 2 good boy pods
great_boy.pub_op(op!( great_boy.pub_op(op!(
@ -338,7 +360,13 @@ pub fn great_boy_pod_full_flow() -> Result<MainPodBuilder> {
alice_friend_pods.push(friend.sign(&mut bob_signer).unwrap()); alice_friend_pods.push(friend.sign(&mut bob_signer).unwrap());
alice_friend_pods.push(friend.sign(&mut charlie_signer).unwrap()); alice_friend_pods.push(friend.sign(&mut charlie_signer).unwrap());
let good_boy_issuers_dict = Value::Dictionary(Dictionary::new(HashMap::new()).unwrap()); // empty let good_boy_issuers = Value::Dictionary(Dictionary::new(
good_boy_issuers
.into_iter()
.map(|issuer| (issuer.to_string(), 0.into()))
.collect(),
)?);
great_boy_pod_builder( great_boy_pod_builder(
&params, &params,
[ [
@ -348,7 +376,7 @@ pub fn great_boy_pod_full_flow() -> Result<MainPodBuilder> {
&charlie_good_boys[1], &charlie_good_boys[1],
], ],
[&alice_friend_pods[0], &alice_friend_pods[1]], [&alice_friend_pods[0], &alice_friend_pods[1]],
&good_boy_issuers_dict, &good_boy_issuers,
alice, alice,
) )
} }
@ -372,8 +400,13 @@ pub fn tickets_pod_builder(
signed_pod: &SignedPod, signed_pod: &SignedPod,
expected_event_id: i64, expected_event_id: i64,
expect_consumed: bool, expect_consumed: bool,
blacklisted_emails: &Value, blacklisted_emails: &Dictionary,
) -> Result<MainPodBuilder> { ) -> Result<MainPodBuilder> {
let attendee_email_value = signed_pod.kvs.get("attendeeEmail").unwrap();
let attendee_nin_blacklist_pf = blacklisted_emails
.middleware_dict()
.prove_nonexistence(&attendee_email_value.into())?;
let blacklisted_email_dict_value = Value::Dictionary(blacklisted_emails.clone());
// Create a main pod referencing this signed pod with some statements // Create a main pod referencing this signed pod with some statements
let mut builder = MainPodBuilder::new(params); let mut builder = MainPodBuilder::new(params);
builder.add_signed_pod(signed_pod); builder.add_signed_pod(signed_pod);
@ -381,9 +414,10 @@ pub fn tickets_pod_builder(
builder.pub_op(op!(eq, (signed_pod, "isConsumed"), expect_consumed))?; builder.pub_op(op!(eq, (signed_pod, "isConsumed"), expect_consumed))?;
builder.pub_op(op!(eq, (signed_pod, "isRevoked"), false))?; builder.pub_op(op!(eq, (signed_pod, "isRevoked"), false))?;
builder.pub_op(op!( builder.pub_op(op!(
not_contains, dict_not_contains,
blacklisted_emails, blacklisted_email_dict_value,
(signed_pod, "attendeeEmail") (signed_pod, "attendeeEmail"),
attendee_nin_blacklist_pf
))?; ))?;
Ok(builder) Ok(builder)
} }
@ -397,6 +431,6 @@ pub fn tickets_pod_full_flow() -> Result<MainPodBuilder> {
&signed_pod, &signed_pod,
123, 123,
true, true,
&Value::Dictionary(Dictionary::new(HashMap::new()).unwrap()), &Dictionary::new(HashMap::new())?,
) )
} }

View file

@ -19,7 +19,12 @@ pub struct Set(Vec<Value>, #[serde(skip)] MiddlewareSet);
impl Set { impl Set {
pub fn new(values: Vec<Value>) -> Result<Self> { pub fn new(values: Vec<Value>) -> Result<Self> {
let set = MiddlewareSet::new(&values.iter().map(|v| MiddlewareValue::from(v)).collect())?; let set = MiddlewareSet::new(
&values
.iter()
.map(|v| MiddlewareValue::from(v))
.collect::<Vec<_>>(),
)?;
Ok(Self(values, set)) Ok(Self(values, set))
} }
@ -85,8 +90,12 @@ pub struct Array(Vec<Value>, #[serde(skip)] MiddlewareArray);
impl Array { impl Array {
pub fn new(values: Vec<Value>) -> Result<Self> { pub fn new(values: Vec<Value>) -> Result<Self> {
let array = let array = MiddlewareArray::new(
MiddlewareArray::new(&values.iter().map(|v| MiddlewareValue::from(v)).collect())?; &values
.iter()
.map(|v| MiddlewareValue::from(v))
.collect::<Vec<_>>(),
)?;
Ok(Self(values, array)) Ok(Self(values, array))
} }

View file

@ -3,10 +3,9 @@
use crate::frontend::serialization::*; use crate::frontend::serialization::*;
use crate::middleware::{ use crate::middleware::{
self, hash_str, Hash, MainPodInputs, NativeOperation, NativePredicate, Params, PodId, self, hash_str, Hash, MainPodInputs, Params, PodId, PodProver, PodSigner, SELF,
PodProver, PodSigner, SELF,
}; };
use crate::middleware::{OperationType, Predicate, KEY_SIGNER, KEY_TYPE}; use crate::middleware::{KEY_SIGNER, KEY_TYPE};
use anyhow::{anyhow, Error, Result}; use anyhow::{anyhow, Error, Result};
use containers::{Array, Dictionary, Set}; use containers::{Array, Dictionary, Set};
use env_logger; use env_logger;
@ -18,13 +17,17 @@ use std::collections::HashMap;
use std::convert::From; use std::convert::From;
use std::{fmt, hash as h}; use std::{fmt, hash as h};
use crate::middleware::{hash_value, OperationAux, EMPTY_VALUE};
pub mod containers; pub mod containers;
mod custom; mod custom;
mod operation; mod operation;
mod predicate;
mod serialization; mod serialization;
mod statement; mod statement;
pub use custom::*; pub use custom::*;
pub use operation::*; pub use operation::*;
pub use predicate::*;
pub use statement::*; pub use statement::*;
/// This type is just for presentation purposes. /// This type is just for presentation purposes.
@ -122,6 +125,12 @@ impl From<middleware::Value> for Value {
} }
} }
impl From<middleware::Hash> for Value {
fn from(v: middleware::Hash) -> Self {
Self::Raw(v.into())
}
}
impl TryInto<i64> for Value { impl TryInto<i64> for Value {
type Error = Error; type Error = Error;
fn try_into(self) -> std::result::Result<i64, Self::Error> { fn try_into(self) -> std::result::Result<i64, Self::Error> {
@ -405,9 +414,9 @@ impl MainPodBuilder {
self.op(false, op) self.op(false, op)
} }
fn op(&mut self, public: bool, mut op: Operation) -> Result<Statement> { fn op(&mut self, public: bool, mut op: Operation) -> Result<Statement, anyhow::Error> {
use NativeOperation::*; use NativeOperation::*;
let Operation(op_type, ref mut args) = &mut op; let Operation(op_type, ref mut args, _) = &mut op;
// TODO: argument type checking // TODO: argument type checking
let pred = op_type let pred = op_type
.output_predicate() .output_predicate()
@ -486,8 +495,6 @@ impl MainPodBuilder {
return Err(anyhow!("Invalid arguments to lt-to-neq operation")); return Err(anyhow!("Invalid arguments to lt-to-neq operation"));
} }
}, },
ContainsFromEntries => self.op_args_entries(public, args)?,
NotContainsFromEntries => self.op_args_entries(public, args)?,
SumOf => match (args[0].clone(), args[1].clone(), args[2].clone()) { SumOf => match (args[0].clone(), args[1].clone(), args[2].clone()) {
( (
OperationArg::Statement(Statement { OperationArg::Statement(Statement {
@ -635,12 +642,17 @@ impl MainPodBuilder {
return Err(anyhow!("Invalid arguments to operation")); return Err(anyhow!("Invalid arguments to operation"));
} }
}, },
DictContainsFromEntries => self.op_args_entries(public, args)?,
DictNotContainsFromEntries => self.op_args_entries(public, args)?,
SetContainsFromEntries => self.op_args_entries(public, args)?,
SetNotContainsFromEntries => self.op_args_entries(public, args)?,
ArrayContainsFromEntries => self.op_args_entries(public, args)?,
}, },
OperationType::Custom(cpr) => { OperationType::Custom(cpr) => {
// All args should be statements to be pattern matched against statement templates. // All args should be statements to be pattern matched against statement templates.
let args = args.iter().map( let args = args.iter().map(
|a| match a { |a| match a {
OperationArg::Statement(s) => middleware::Statement::try_from(s.clone()), OperationArg::Statement(s) => Ok(middleware::Statement::try_from(s.clone())?),
_ => Err(anyhow!("Invalid argument {} to operation corresponding to custom predicate {:?}.", a, cpr)) _ => Err(anyhow!("Invalid argument {} to operation corresponding to custom predicate {:?}.", a, cpr))
} }
).collect::<Result<Vec<_>>>()?; ).collect::<Result<Vec<_>>>()?;
@ -712,6 +724,7 @@ impl MainPodBuilder {
Operation( Operation(
OperationType::Native(NativeOperation::NewEntry), OperationType::Native(NativeOperation::NewEntry),
vec![OperationArg::Entry(k.clone(), v)], vec![OperationArg::Entry(k.clone(), v)],
OperationAux::None,
), ),
) )
} }
@ -843,6 +856,13 @@ struct MainPodCompiler {
// Output // Output
statements: Vec<middleware::Statement>, statements: Vec<middleware::Statement>,
operations: Vec<middleware::Operation>, operations: Vec<middleware::Operation>,
// Internal state
// Tracks literal constants assigned to ValueOf statements by self.literal()
// If `val` has been added as a literal,
// then `self.literals.get(&val)` returns `Some(idx)`, and
// then `self.statements[idx]` is the ValueOf statement
// where it was introduced.
literals: HashMap<middleware::Value, usize>,
} }
impl MainPodCompiler { impl MainPodCompiler {
@ -851,6 +871,7 @@ impl MainPodCompiler {
params: params.clone(), params: params.clone(),
statements: Vec::new(), statements: Vec::new(),
operations: Vec::new(), operations: Vec::new(),
literals: HashMap::new(),
} }
} }
@ -876,22 +897,138 @@ impl MainPodCompiler {
} }
} }
fn compile_st(&self, st: &Statement) -> Result<middleware::Statement> { // Introduces a literal value if it hasn't been introduced,
// or else returns the existing ValueOf statement where it was first introduced.
// TODO: this might produce duplicate keys, fix
fn literal<V: Clone + Into<middleware::Value>>(&mut self, val: V) -> &middleware::Statement {
let val: middleware::Value = val.into();
match self.literals.get(&val) {
Some(idx) => &self.statements[*idx],
None => {
let ak = middleware::AnchoredKey(SELF, hash_value(&val));
let st = middleware::Statement::ValueOf(ak, val);
let op = middleware::Operation::NewEntry;
self.statements.push(st);
self.operations.push(op);
self.statements.last().unwrap()
}
}
}
// Returns the existing ValueOf statement where it was first introduced,
// or None if it does not exist.
fn get_literal<V: Clone + Into<middleware::Value>>(
&self,
val: V,
) -> Option<&middleware::Statement> {
let val: middleware::Value = val.into();
match self.literals.get(&val) {
Some(idx) => Some(&self.statements[*idx]),
None => None,
}
}
// This function handles cases where one frontend statement
// compiles to multiple middleware statements.
// For example: DictContains(x, y) on the frontend compiles to:
// ValueOf(empty, EMPTY_VALUE)
// Contains(x, y, empty)
fn manual_compile_st_op(&mut self, st: &Statement, op: &Operation) -> Result<()> {
match st.predicate {
Predicate::Native(NativePredicate::DictContains) => {
let empty_st = self.literal(EMPTY_VALUE).clone();
let empty_ak = match empty_st {
middleware::Statement::ValueOf(ak, _) => ak,
_ => unreachable!(),
};
let (ak1, ak2) = match (st.args.get(0).cloned(), st.args.get(1).cloned()) {
(Some(StatementArg::Key(ak1)), Some(StatementArg::Key(ak2))) => (ak1, ak2),
_ => Err(anyhow!("Ill-formed statement: {}", st))?,
};
let middle_st =
middleware::Statement::Contains(ak1.into(), ak2.into(), empty_ak.clone());
let middle_op = middleware::Operation::ContainsFromEntries(
match &op.1[0] {
OperationArg::Statement(s) => self.compile_st(&s)?,
_ => Err(anyhow!("Statement compile failed in manual compile"))?,
},
match &op.1[1] {
OperationArg::Statement(s) => self.compile_st(&s)?,
_ => Err(anyhow!("Statement compile failed in manual compile"))?,
},
empty_st,
match &op.2 {
OperationAux::MerkleProof(mp) => mp.clone(),
_ => {
return Err(anyhow!(
"Auxiliary argument to DictContainsFromEntries must be Merkle proof"
));
}
},
);
self.statements.push(middle_st);
self.operations.push(middle_op);
assert_eq!(self.statements.len(), self.operations.len());
Ok(())
}
_ => unreachable!(),
}
}
// If the frontend statement `st` compiles to a single middleware statement,
// returns that middleware statement.
// If it compiles to multiple middlewarestatements, returns StatementConversionError.
// This is only a helper method within compile_st_op().
// If you want to compile a statement in general, run compile_st().
fn compile_st_try_simple(
&self,
st: &Statement,
) -> Result<middleware::Statement, StatementConversionError> {
st.clone().try_into() st.clone().try_into()
} }
// Compiles the frontend statement `st` to a middleware statement.
// This function assumes the middleware statement already exists --
// it should not be called from compile_st_op.
fn compile_st(&self, st: &Statement) -> Result<middleware::Statement> {
match self.compile_st_try_simple(st) {
Ok(s) => Ok(s),
Err(StatementConversionError::Error(e)) => Err(e),
Err(StatementConversionError::MCR(_)) => {
let empty_st = self
.get_literal(EMPTY_VALUE)
.clone()
.ok_or(anyhow!("Literal value not found for empty literal."))?;
let empty_ak = match empty_st {
middleware::Statement::ValueOf(ak, _) => ak,
_ => unreachable!(),
};
let (ak1, ak2) = match (st.args.get(0).cloned(), st.args.get(1).cloned()) {
(Some(StatementArg::Key(ak1)), Some(StatementArg::Key(ak2))) => (ak1, ak2),
_ => Err(anyhow!("Ill-formed statement: {}", st))?,
};
let middle_st =
middleware::Statement::Contains(ak1.into(), ak2.into(), empty_ak.clone());
Ok(middle_st)
}
}
}
fn compile_op(&self, op: &Operation) -> Result<middleware::Operation> { fn compile_op(&self, op: &Operation) -> Result<middleware::Operation> {
// TODO let mop_code: middleware::OperationType = op.0.clone().try_into()?;
let mop_code: OperationType = op.0.clone();
// TODO: Take Merkle proof into account.
let mop_args = let mop_args =
op.1.iter() op.1.iter()
.flat_map(|arg| self.compile_op_arg(arg).map(|s| Ok(s.try_into()?))) .flat_map(|arg| self.compile_op_arg(arg).map(|op_arg| Ok(op_arg)))
.collect::<Result<Vec<middleware::Statement>>>()?; .collect::<Result<Vec<_>>>()?;
middleware::Operation::op(mop_code, &mop_args) middleware::Operation::op(mop_code, &mop_args, &op.2)
} }
fn compile_st_op(&mut self, st: &Statement, op: &Operation, params: &Params) -> Result<()> { fn compile_st_op(&mut self, st: &Statement, op: &Operation, params: &Params) -> Result<()> {
let middle_st = self.compile_st(st)?; let middle_st_res = self.compile_st_try_simple(st);
match middle_st_res {
Ok(middle_st) => {
let middle_op = self.compile_op(op)?; let middle_op = self.compile_op(op)?;
let is_correct = middle_op.check(params, &middle_st)?; let is_correct = middle_op.check(params, &middle_st)?;
if !is_correct { if !is_correct {
@ -906,6 +1043,10 @@ impl MainPodCompiler {
Ok(()) Ok(())
} }
} }
Err(StatementConversionError::Error(e)) => Err(e),
Err(StatementConversionError::MCR(_)) => self.manual_compile_st_op(st, op),
}
}
pub fn compile( pub fn compile(
mut self, mut self,
@ -950,55 +1091,66 @@ pub mod build_utils {
#[macro_export] #[macro_export]
macro_rules! op { macro_rules! op {
(new_entry, ($key:expr, $value:expr)) => { $crate::frontend::Operation( (new_entry, ($key:expr, $value:expr)) => { $crate::frontend::Operation(
$crate::middleware::OperationType::Native($crate::middleware::NativeOperation::NewEntry), $crate::frontend::OperationType::Native($crate::frontend::NativeOperation::NewEntry),
$crate::op_args!(($key, $value))) }; $crate::op_args!(($key, $value)), crate::middleware::OperationAux::None) };
(eq, $($arg:expr),+) => { $crate::frontend::Operation( (eq, $($arg:expr),+) => { $crate::frontend::Operation(
$crate::middleware::OperationType::Native($crate::middleware::NativeOperation::EqualFromEntries), $crate::frontend::OperationType::Native($crate::frontend::NativeOperation::EqualFromEntries),
$crate::op_args!($($arg),*)) }; $crate::op_args!($($arg),*), crate::middleware::OperationAux::None) };
(ne, $($arg:expr),+) => { $crate::frontend::Operation( (ne, $($arg:expr),+) => { $crate::frontend::Operation(
$crate::middleware::OperationType::Native($crate::middleware::NativeOperation::NotEqualFromEntries), $crate::frontend::OperationType::Native($crate::frontend::NativeOperation::NotEqualFromEntries),
$crate::op_args!($($arg),*)) }; $crate::op_args!($($arg),*), crate::middleware::OperationAux::None) };
(gt, $($arg:expr),+) => { crate::frontend::Operation( (gt, $($arg:expr),+) => { crate::frontend::Operation(
crate::middleware::OperationType::Native(crate::middleware::NativeOperation::GtFromEntries), crate::frontend::OperationType::Native(crate::frontend::NativeOperation::GtFromEntries),
crate::op_args!($($arg),*)) }; crate::op_args!($($arg),*), crate::middleware::OperationAux::None) };
(lt, $($arg:expr),+) => { crate::frontend::Operation( (lt, $($arg:expr),+) => { crate::frontend::Operation(
crate::middleware::OperationType::Native(crate::middleware::NativeOperation::LtFromEntries), crate::frontend::OperationType::Native(crate::frontend::NativeOperation::LtFromEntries),
crate::op_args!($($arg),*)) }; crate::op_args!($($arg),*), crate::middleware::OperationAux::None) };
(transitive_eq, $($arg:expr),+) => { crate::frontend::Operation( (transitive_eq, $($arg:expr),+) => { crate::frontend::Operation(
crate::middleware::OperationType::Native(crate::middleware::NativeOperation::TransitiveEqualFromStatements), crate::frontend::OperationType::Native(crate::frontend::NativeOperation::TransitiveEqualFromStatements),
crate::op_args!($($arg),*)) }; crate::op_args!($($arg),*), crate::middleware::OperationAux::None) };
(gt_to_ne, $($arg:expr),+) => { crate::frontend::Operation( (gt_to_ne, $($arg:expr),+) => { crate::frontend::Operation(
crate::middleware::OperationType::Native(crate::middleware::NativeOperation::GtToNotEqual), crate::frontend::OperationType::Native(crate::frontend::NativeOperation::GtToNotEqual),
crate::op_args!($($arg),*)) }; crate::op_args!($($arg),*), crate::middleware::OperationAux::None) };
(lt_to_ne, $($arg:expr),+) => { crate::frontend::Operation( (lt_to_ne, $($arg:expr),+) => { crate::frontend::Operation(
crate::middleware::OperationType::Native(crate::middleware::NativeOperation::LtToNotEqual), crate::frontend::OperationType::Native(crate::frontend::NativeOperation::LtToNotEqual),
crate::op_args!($($arg),*)) }; crate::op_args!($($arg),*), crate::middleware::OperationAux::None) };
(contains, $($arg:expr),+) => { crate::frontend::Operation(
crate::middleware::OperationType::Native(crate::middleware::NativeOperation::ContainsFromEntries),
crate::op_args!($($arg),*)) };
(not_contains, $($arg:expr),+) => { crate::frontend::Operation(
crate::middleware::OperationType::Native(crate::middleware::NativeOperation::NotContainsFromEntries),
crate::op_args!($($arg),*)) };
(sum_of, $($arg:expr),+) => { crate::frontend::Operation( (sum_of, $($arg:expr),+) => { crate::frontend::Operation(
crate::middleware::OperationType::Native(crate::middleware::NativeOperation::SumOf), crate::frontend::OperationType::Native(crate::frontend::NativeOperation::SumOf),
crate::op_args!($($arg),*)) }; crate::op_args!($($arg),*), crate::middleware::OperationAux::None) };
(product_of, $($arg:expr),+) => { crate::frontend::Operation( (product_of, $($arg:expr),+) => { crate::frontend::Operation(
crate::middleware::OperationType::Native(crate::middleware::NativeOperation::ProductOf), crate::frontend::OperationType::Native(crate::frontend::NativeOperation::ProductOf),
crate::op_args!($($arg),*)) }; crate::op_args!($($arg),*), crate::middleware::OperationAux::None) };
(max_of, $($arg:expr),+) => { crate::frontend::Operation( (max_of, $($arg:expr),+) => { crate::frontend::Operation(
crate::middleware::OperationType::Native(crate::middleware::NativeOperation::MaxOf), crate::frontend::OperationType::Native(crate::frontend::NativeOperation::MaxOf),
crate::op_args!($($arg),*)) }; crate::op_args!($($arg),*), crate::middleware::OperationAux::None) };
(custom, $op:expr, $($arg:expr),+) => { $crate::frontend::Operation( (custom, $op:expr, $($arg:expr),+) => { $crate::frontend::Operation(
$crate::middleware::OperationType::Custom($op), $crate::frontend::OperationType::Custom($op),
$crate::op_args!($($arg),*)) }; $crate::op_args!($($arg),*), crate::middleware::OperationAux::None) };
(dict_contains, $dict:expr, $key:expr, $value:expr, $aux:expr) => { crate::frontend::Operation(
crate::frontend::OperationType::Native(crate::frontend::NativeOperation::DictContainsFromEntries),
crate::op_args!($dict, $key, $value), crate::middleware::OperationAux::MerkleProof($aux)) };
(dict_not_contains, $dict:expr, $key:expr, $aux:expr) => { crate::frontend::Operation(
crate::frontend::OperationType::Native(crate::frontend::NativeOperation::DictNotContainsFromEntries),
crate::op_args!($dict, $key), crate::middleware::OperationAux::MerkleProof($aux)) };
(set_contains, $set:expr, $value:expr, $aux:expr) => { crate::frontend::Operation(
crate::frontend::OperationType::Native(crate::frontend::NativeOperation::SetContainsFromEntries),
crate::op_args!($set, $value), crate::middleware::OperationAux::MerkleProof($aux)) };
(set_not_contains, $set:expr, $value:expr, $aux:expr) => { crate::frontend::Operation(
crate::frontend::OperationType::Native(crate::frontend::NativeOperation::SetNotContainsFromEntries),
crate::op_args!($set, $value), crate::middleware::OperationAux::MerkleProof($aux)) };
(array_contains, $array:expr, $value:expr, $aux:expr) => { crate::frontend::Operation(
crate::frontend::OperationType::Native(crate::frontend::NativeOperation::ArrayContainsFromEntries),
crate::op_args!($array, $value), crate::middleware::OperationAux::MerkleProof($aux)) };
} }
} }
#[cfg(test)] #[cfg(test)]
pub mod tests { pub mod tests {
use super::*; use super::*;
use crate::backends::plonky2::basetypes;
use crate::backends::plonky2::mock::mainpod::MockProver; use crate::backends::plonky2::mock::mainpod::MockProver;
use crate::backends::plonky2::mock::signedpod::MockSigner; use crate::backends::plonky2::mock::signedpod::MockSigner;
use crate::backends::plonky2::primitives::merkletree::MerkleTree;
use crate::examples::{ use crate::examples::{
eth_dos_pod_builder, eth_friend_signed_pod_builder, great_boy_pod_full_flow, eth_dos_pod_builder, eth_friend_signed_pod_builder, great_boy_pod_full_flow,
tickets_pod_full_flow, zu_kyc_pod_builder, zu_kyc_sign_pod_builders, tickets_pod_full_flow, zu_kyc_pod_builder, zu_kyc_sign_pod_builders,
@ -1007,8 +1159,12 @@ pub mod tests {
// Check that frontend public statements agree with those // Check that frontend public statements agree with those
// embedded in a MainPod. // embedded in a MainPod.
fn check_public_statements(pod: &MainPod) -> Result<()> { fn check_public_statements(pod: &MainPod) -> Result<()> {
Ok(
std::iter::zip(pod.public_statements.clone(), pod.pod.pub_statements()).try_for_each( std::iter::zip(pod.public_statements.clone(), pod.pod.pub_statements()).try_for_each(
|(fes, s)| crate::middleware::Statement::try_from(fes).map(|fes| assert_eq!(fes, s)), |(fes, s)| {
crate::middleware::Statement::try_from(fes).map(|fes| assert_eq!(fes, s))
},
)?,
) )
} }
@ -1041,7 +1197,9 @@ pub mod tests {
#[test] #[test]
fn test_front_zu_kyc() -> Result<()> { fn test_front_zu_kyc() -> Result<()> {
let params = Params::default(); let params = Params::default();
let (gov_id, pay_stub, sanction_list) = zu_kyc_sign_pod_builders(&params); let sanctions_values = vec!["A343434340".into()];
let sanction_set = Value::Set(Set::new(sanctions_values)?);
let (gov_id, pay_stub, sanction_list) = zu_kyc_sign_pod_builders(&params, &sanction_set);
println!("{}", gov_id); println!("{}", gov_id);
println!("{}", pay_stub); println!("{}", pay_stub);
@ -1164,6 +1322,7 @@ pub mod tests {
OperationArg::from((&signed_pod, "a")), OperationArg::from((&signed_pod, "a")),
OperationArg::from((&signed_pod, "b")), OperationArg::from((&signed_pod, "b")),
], ],
OperationAux::None,
); );
let st1 = builder.op(true, op_eq1).unwrap(); let st1 = builder.op(true, op_eq1).unwrap();
let op_eq2 = Operation( let op_eq2 = Operation(
@ -1172,12 +1331,14 @@ pub mod tests {
OperationArg::from((&signed_pod, "b")), OperationArg::from((&signed_pod, "b")),
OperationArg::from((&signed_pod, "a")), OperationArg::from((&signed_pod, "a")),
], ],
OperationAux::None,
); );
let st2 = builder.op(true, op_eq2).unwrap(); let st2 = builder.op(true, op_eq2).unwrap();
let op_eq3 = Operation( let op_eq3 = Operation(
OperationType::Native(NativeOperation::TransitiveEqualFromStatements), OperationType::Native(NativeOperation::TransitiveEqualFromStatements),
vec![OperationArg::Statement(st1), OperationArg::Statement(st2)], vec![OperationArg::Statement(st1), OperationArg::Statement(st2)],
OperationAux::None,
); );
let st3 = builder.op(true, op_eq3); let st3 = builder.op(true, op_eq3);
@ -1214,6 +1375,58 @@ pub mod tests {
} }
#[test] #[test]
fn test_dictionaries() -> Result<()> {
let params = Params::default();
let mut builder = SignedPodBuilder::new(&params);
type BeValue = basetypes::Value;
let mut my_dict_kvs: HashMap<String, Value> = HashMap::new();
my_dict_kvs.insert("a".to_string(), Value::from(1));
my_dict_kvs.insert("b".to_string(), Value::from(2));
my_dict_kvs.insert("c".to_string(), Value::from(3));
// let my_dict_as_mt = MerkleTree::new(5, &my_dict_kvs).unwrap();
// let dict = Dictionary { mt: my_dict_as_mt };
let dict = Dictionary::new(my_dict_kvs)?;
let dict_root = Value::Dictionary(dict.clone());
builder.insert("dict", dict_root);
let mut signer = MockSigner {
pk: "signer".into(),
};
let pod = builder.sign(&mut signer).unwrap();
let mut builder = MainPodBuilder::new(&params);
builder.add_signed_pod(&pod);
let st0 = Statement::from((&pod, "dict"));
let st1 = builder.op(true, op!(new_entry, ("key", "a"))).unwrap();
let st2 = builder.literal(false, &Value::Int(1)).unwrap();
builder
.pub_op(Operation(
// OperationType
OperationType::Native(NativeOperation::DictContainsFromEntries),
// Vec<OperationArg>
vec![
OperationArg::Statement(st0),
OperationArg::Statement(st1),
OperationArg::Statement(st2),
],
OperationAux::MerkleProof(
dict.middleware_dict()
.prove(&Hash::from("a").into())
.unwrap()
.1,
),
))
.unwrap();
let mut main_prover = MockProver {};
let main_pod = builder.prove(&mut main_prover, &params).unwrap();
println!("{}", main_pod);
Ok(())
}
#[should_panic] #[should_panic]
fn test_incorrect_pod() { fn test_incorrect_pod() {
// try to insert the same key multiple times // try to insert the same key multiple times
@ -1234,7 +1447,11 @@ pub mod tests {
StatementArg::Literal(Value::Int(3)), StatementArg::Literal(Value::Int(3)),
], ],
), ),
Operation(OperationType::Native(NativeOperation::NewEntry), vec![]), Operation(
OperationType::Native(NativeOperation::NewEntry),
vec![],
OperationAux::None,
),
)); ));
builder.insert(( builder.insert((
Statement::new( Statement::new(
@ -1247,7 +1464,11 @@ pub mod tests {
StatementArg::Literal(Value::Int(28)), StatementArg::Literal(Value::Int(28)),
], ],
), ),
Operation(OperationType::Native(NativeOperation::NewEntry), vec![]), Operation(
OperationType::Native(NativeOperation::NewEntry),
vec![],
OperationAux::None,
),
)); ));
let mut prover = MockProver {}; let mut prover = MockProver {};
@ -1283,11 +1504,19 @@ pub mod tests {
builder.insert(( builder.insert((
value_of_a.clone(), value_of_a.clone(),
Operation(OperationType::Native(NativeOperation::NewEntry), vec![]), Operation(
OperationType::Native(NativeOperation::NewEntry),
vec![],
OperationAux::None,
),
)); ));
builder.insert(( builder.insert((
value_of_b.clone(), value_of_b.clone(),
Operation(OperationType::Native(NativeOperation::NewEntry), vec![]), Operation(
OperationType::Native(NativeOperation::NewEntry),
vec![],
OperationAux::None,
),
)); ));
builder.insert(( builder.insert((
Statement::new( Statement::new(
@ -1300,6 +1529,7 @@ pub mod tests {
OperationArg::Statement(value_of_a), OperationArg::Statement(value_of_a),
OperationArg::Statement(value_of_b), OperationArg::Statement(value_of_b),
], ],
OperationAux::None,
), ),
)); ));

View file

@ -1,7 +1,10 @@
use std::fmt; use std::fmt;
use super::{SignedPod, Statement, Value}; use super::{NativePredicate, Predicate, SignedPod, Statement, Value};
use crate::middleware::OperationType; use crate::{
backends::plonky2::primitives::merkletree::MerkleProof,
middleware::{self, OperationAux},
};
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub enum OperationArg { pub enum OperationArg {
@ -69,7 +72,120 @@ impl<V: Into<Value>> From<(&str, V)> for OperationArg {
} }
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub struct Operation(pub OperationType, pub Vec<OperationArg>); pub enum OperationType {
Native(NativeOperation),
Custom(middleware::CustomPredicateRef),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum NativeOperation {
None = 0,
NewEntry = 1,
CopyStatement = 2,
EqualFromEntries = 3,
NotEqualFromEntries = 4,
GtFromEntries = 5,
LtFromEntries = 6,
TransitiveEqualFromStatements = 7,
GtToNotEqual = 8,
LtToNotEqual = 9,
SumOf = 13,
ProductOf = 14,
MaxOf = 15,
DictContainsFromEntries = 16,
DictNotContainsFromEntries = 17,
SetContainsFromEntries = 18,
SetNotContainsFromEntries = 19,
ArrayContainsFromEntries = 20,
}
impl TryFrom<OperationType> for middleware::OperationType {
type Error = anyhow::Error;
fn try_from(fe_ot: OperationType) -> Result<Self, Self::Error> {
type FeOT = OperationType;
type FeNO = NativeOperation;
type MwOT = middleware::OperationType;
type MwNO = middleware::NativeOperation;
let mw_ot = match fe_ot {
FeOT::Native(FeNO::None) => MwOT::Native(MwNO::None),
FeOT::Native(FeNO::NewEntry) => MwOT::Native(MwNO::NewEntry),
FeOT::Native(FeNO::CopyStatement) => MwOT::Native(MwNO::CopyStatement),
FeOT::Native(FeNO::EqualFromEntries) => MwOT::Native(MwNO::EqualFromEntries),
FeOT::Native(FeNO::NotEqualFromEntries) => MwOT::Native(MwNO::NotEqualFromEntries),
FeOT::Native(FeNO::GtFromEntries) => MwOT::Native(MwNO::GtFromEntries),
FeOT::Native(FeNO::LtFromEntries) => MwOT::Native(MwNO::LtFromEntries),
FeOT::Native(FeNO::TransitiveEqualFromStatements) => {
MwOT::Native(MwNO::TransitiveEqualFromStatements)
}
FeOT::Native(FeNO::GtToNotEqual) => MwOT::Native(MwNO::GtToNotEqual),
FeOT::Native(FeNO::LtToNotEqual) => MwOT::Native(MwNO::LtToNotEqual),
FeOT::Native(FeNO::SumOf) => MwOT::Native(MwNO::SumOf),
FeOT::Native(FeNO::ProductOf) => MwOT::Native(MwNO::ProductOf),
FeOT::Native(FeNO::MaxOf) => MwOT::Native(MwNO::MaxOf),
FeOT::Native(FeNO::DictContainsFromEntries) => MwOT::Native(MwNO::ContainsFromEntries),
FeOT::Native(FeNO::DictNotContainsFromEntries) => {
MwOT::Native(MwNO::NotContainsFromEntries)
}
FeOT::Native(FeNO::SetContainsFromEntries) => MwOT::Native(MwNO::ContainsFromEntries),
FeOT::Native(FeNO::SetNotContainsFromEntries) => {
MwOT::Native(MwNO::NotContainsFromEntries)
}
FeOT::Native(FeNO::ArrayContainsFromEntries) => MwOT::Native(MwNO::ContainsFromEntries),
FeOT::Custom(mw_cpr) => MwOT::Custom(mw_cpr),
};
Ok(mw_ot)
}
}
impl OperationType {
/// Gives the type of predicate that the operation will output, if known.
/// CopyStatement may output any predicate (it will match the statement copied),
/// so output_predicate returns None on CopyStatement.
pub fn output_predicate(&self) -> Option<Predicate> {
match self {
OperationType::Native(native_op) => match native_op {
NativeOperation::None => Some(Predicate::Native(NativePredicate::None)),
NativeOperation::NewEntry => Some(Predicate::Native(NativePredicate::ValueOf)),
NativeOperation::CopyStatement => None,
NativeOperation::EqualFromEntries => {
Some(Predicate::Native(NativePredicate::Equal))
}
NativeOperation::NotEqualFromEntries => {
Some(Predicate::Native(NativePredicate::NotEqual))
}
NativeOperation::GtFromEntries => Some(Predicate::Native(NativePredicate::Gt)),
NativeOperation::LtFromEntries => Some(Predicate::Native(NativePredicate::Lt)),
NativeOperation::TransitiveEqualFromStatements => {
Some(Predicate::Native(NativePredicate::Equal))
}
NativeOperation::GtToNotEqual => Some(Predicate::Native(NativePredicate::NotEqual)),
NativeOperation::LtToNotEqual => Some(Predicate::Native(NativePredicate::NotEqual)),
NativeOperation::SumOf => Some(Predicate::Native(NativePredicate::SumOf)),
NativeOperation::ProductOf => Some(Predicate::Native(NativePredicate::ProductOf)),
NativeOperation::MaxOf => Some(Predicate::Native(NativePredicate::MaxOf)),
NativeOperation::DictContainsFromEntries => {
Some(Predicate::Native(NativePredicate::DictContains))
}
NativeOperation::DictNotContainsFromEntries => {
Some(Predicate::Native(NativePredicate::DictNotContains))
}
NativeOperation::SetContainsFromEntries => {
Some(Predicate::Native(NativePredicate::SetContains))
}
NativeOperation::SetNotContainsFromEntries => {
Some(Predicate::Native(NativePredicate::SetNotContains))
}
NativeOperation::ArrayContainsFromEntries => {
Some(Predicate::Native(NativePredicate::ArrayContains))
}
},
OperationType::Custom(cpr) => Some(Predicate::Custom(cpr.clone())),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Operation(pub OperationType, pub Vec<OperationArg>, pub OperationAux);
impl fmt::Display for Operation { impl fmt::Display for Operation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

39
src/frontend/predicate.rs Normal file
View file

@ -0,0 +1,39 @@
use anyhow::{anyhow, Result};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::fmt;
use super::{AnchoredKey, SignedPod, Value};
//use crate::middleware::{self, NativePredicate, Predicate};
use crate::middleware::{self, CustomPredicateRef};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
pub enum NativePredicate {
None = 0,
ValueOf = 1,
Equal = 2,
NotEqual = 3,
Gt = 4,
Lt = 5,
SumOf = 8,
ProductOf = 9,
MaxOf = 10,
DictContains = 11,
DictNotContains = 12,
SetContains = 13,
SetNotContains = 14,
ArrayContains = 15, // there is no ArrayNotContains
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub enum Predicate {
Native(NativePredicate),
BatchSelf(usize),
Custom(CustomPredicateRef),
}
impl From<NativePredicate> for Predicate {
fn from(v: NativePredicate) -> Self {
Self::Native(v)
}
}

View file

@ -151,6 +151,8 @@ pub fn transform_value_schema(schema: &mut Schema) {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use anyhow::Result;
use crate::{ use crate::{
backends::plonky2::mock::{mainpod::MockProver, signedpod::MockSigner}, backends::plonky2::mock::{mainpod::MockProver, signedpod::MockSigner},
examples::{zu_kyc_pod_builder, zu_kyc_sign_pod_builders}, examples::{zu_kyc_pod_builder, zu_kyc_sign_pod_builders},
@ -260,11 +262,13 @@ mod tests {
} }
#[test] #[test]
fn test_main_pod_serialization() { fn test_main_pod_serialization() -> Result<()> {
let params = middleware::Params::default(); let params = middleware::Params::default();
let sanctions_values = vec!["A343434340".into()];
let sanction_set = Value::Set(Set::new(sanctions_values)?);
let (gov_id_builder, pay_stub_builder, sanction_list_builder) = let (gov_id_builder, pay_stub_builder, sanction_list_builder) =
zu_kyc_sign_pod_builders(&params); zu_kyc_sign_pod_builders(&params, &sanction_set);
let mut signer = MockSigner { let mut signer = MockSigner {
pk: "ZooGov".into(), pk: "ZooGov".into(),
}; };
@ -289,9 +293,8 @@ mod tests {
assert_eq!(kyc_pod.public_statements, deserialized.public_statements); assert_eq!(kyc_pod.public_statements, deserialized.public_statements);
assert_eq!(kyc_pod.pod.id(), deserialized.pod.id()); assert_eq!(kyc_pod.pod.id(), deserialized.pod.id());
assert_eq!( assert_eq!(kyc_pod.pod.verify()?, deserialized.pod.verify()?);
kyc_pod.pod.verify().is_ok(),
deserialized.pod.verify().is_ok() Ok(())
);
} }
} }

View file

@ -1,5 +1,5 @@
use super::{AnchoredKey, SignedPod, Value}; use super::{AnchoredKey, NativePredicate, Predicate, SignedPod, Value};
use crate::middleware::{self, NativePredicate, Predicate}; use crate::middleware;
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use schemars::JsonSchema; use schemars::JsonSchema;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -50,9 +50,35 @@ impl From<(&SignedPod, &str)> for Statement {
} }
} }
#[derive(Debug)]
pub struct ManualConversionRequired();
impl std::fmt::Display for StatementConversionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Statement conversion error: statement conversion must be implemented manually."
)
}
}
impl std::error::Error for StatementConversionError {}
#[derive(Debug)]
pub enum StatementConversionError {
MCR(ManualConversionRequired),
Error(anyhow::Error),
}
impl From<anyhow::Error> for StatementConversionError {
fn from(value: anyhow::Error) -> Self {
Self::Error(value)
}
}
impl TryFrom<Statement> for middleware::Statement { impl TryFrom<Statement> for middleware::Statement {
type Error = anyhow::Error; type Error = StatementConversionError;
fn try_from(s: Statement) -> Result<Self> { fn try_from(s: Statement) -> Result<Self, StatementConversionError> {
type MS = middleware::Statement; type MS = middleware::Statement;
type NP = NativePredicate; type NP = NativePredicate;
type SA = StatementArg; type SA = StatementArg;
@ -79,12 +105,6 @@ impl TryFrom<Statement> for middleware::Statement {
(NP::Lt, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), None)) => { (NP::Lt, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), None)) => {
MS::Lt(ak1.into(), ak2.into()) MS::Lt(ak1.into(), ak2.into())
} }
(NP::Contains, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), None)) => {
MS::Contains(ak1.into(), ak2.into())
}
(NP::NotContains, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), None)) => {
MS::NotContains(ak1.into(), ak2.into())
}
(NP::SumOf, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), Some(SA::Key(ak3)))) => { (NP::SumOf, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), Some(SA::Key(ak3)))) => {
MS::SumOf(ak1.into(), ak2.into(), ak3.into()) MS::SumOf(ak1.into(), ak2.into(), ak3.into())
} }
@ -94,6 +114,19 @@ impl TryFrom<Statement> for middleware::Statement {
(NP::MaxOf, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), Some(SA::Key(ak3)))) => { (NP::MaxOf, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), Some(SA::Key(ak3)))) => {
MS::MaxOf(ak1.into(), ak2.into(), ak3.into()) MS::MaxOf(ak1.into(), ak2.into(), ak3.into())
} }
(
NP::DictContains,
(Some(SA::Key(ak1)), Some(SA::Key(ak2)), Some(SA::Key(ak3))),
) => MS::Contains(ak1.into(), ak2.into(), ak3.into()),
(NP::DictNotContains, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), None)) => {
MS::NotContains(ak1.into(), ak2.into())
}
(NP::SetContains, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), None)) => {
return Err(StatementConversionError::MCR(ManualConversionRequired()));
}
(NP::SetNotContains, (Some(SA::Key(ak1)), Some(SA::Key(ak2)), None)) => {
MS::NotContains(ak1.into(), ak2.into())
}
_ => Err(anyhow!("Ill-formed statement: {}", s))?, _ => Err(anyhow!("Ill-formed statement: {}", s))?,
}, },
Predicate::Custom(cpr) => MS::Custom( Predicate::Custom(cpr) => MS::Custom(

View file

@ -73,7 +73,7 @@ pub struct Set {
} }
impl Set { impl Set {
pub fn new(set: &Vec<Value>) -> Result<Self> { pub fn new(set: &[Value]) -> Result<Self> {
let kvs: HashMap<Value, Value> = set let kvs: HashMap<Value, Value> = set
.iter() .iter()
.map(|e| { .map(|e| {
@ -126,7 +126,7 @@ pub struct Array {
} }
impl Array { impl Array {
pub fn new(array: &Vec<Value>) -> Result<Self> { pub fn new(array: &[Value]) -> Result<Self> {
let kvs: HashMap<Value, Value> = array let kvs: HashMap<Value, Value> = array
.iter() .iter()
.enumerate() .enumerate()

View file

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

View file

@ -44,8 +44,12 @@ pub enum Statement {
NotEqual(AnchoredKey, AnchoredKey), NotEqual(AnchoredKey, AnchoredKey),
Gt(AnchoredKey, AnchoredKey), Gt(AnchoredKey, AnchoredKey),
Lt(AnchoredKey, AnchoredKey), Lt(AnchoredKey, AnchoredKey),
Contains(AnchoredKey, AnchoredKey), Contains(
NotContains(AnchoredKey, AnchoredKey), /* root */ AnchoredKey,
/* key */ AnchoredKey,
/* value */ AnchoredKey,
),
NotContains(/* root */ AnchoredKey, /* key */ AnchoredKey),
SumOf(AnchoredKey, AnchoredKey, AnchoredKey), SumOf(AnchoredKey, AnchoredKey, AnchoredKey),
ProductOf(AnchoredKey, AnchoredKey, AnchoredKey), ProductOf(AnchoredKey, AnchoredKey, AnchoredKey),
MaxOf(AnchoredKey, AnchoredKey, AnchoredKey), MaxOf(AnchoredKey, AnchoredKey, AnchoredKey),
@ -65,7 +69,7 @@ impl Statement {
Self::NotEqual(_, _) => Native(NativePredicate::NotEqual), Self::NotEqual(_, _) => Native(NativePredicate::NotEqual),
Self::Gt(_, _) => Native(NativePredicate::Gt), Self::Gt(_, _) => Native(NativePredicate::Gt),
Self::Lt(_, _) => Native(NativePredicate::Lt), Self::Lt(_, _) => Native(NativePredicate::Lt),
Self::Contains(_, _) => Native(NativePredicate::Contains), Self::Contains(_, _, _) => Native(NativePredicate::Contains),
Self::NotContains(_, _) => Native(NativePredicate::NotContains), Self::NotContains(_, _) => Native(NativePredicate::NotContains),
Self::SumOf(_, _, _) => Native(NativePredicate::SumOf), Self::SumOf(_, _, _) => Native(NativePredicate::SumOf),
Self::ProductOf(_, _, _) => Native(NativePredicate::ProductOf), Self::ProductOf(_, _, _) => Native(NativePredicate::ProductOf),
@ -82,7 +86,7 @@ impl Statement {
Self::NotEqual(ak1, ak2) => vec![Key(ak1), Key(ak2)], Self::NotEqual(ak1, ak2) => vec![Key(ak1), Key(ak2)],
Self::Gt(ak1, ak2) => vec![Key(ak1), Key(ak2)], Self::Gt(ak1, ak2) => vec![Key(ak1), Key(ak2)],
Self::Lt(ak1, ak2) => vec![Key(ak1), Key(ak2)], Self::Lt(ak1, ak2) => vec![Key(ak1), Key(ak2)],
Self::Contains(ak1, ak2) => vec![Key(ak1), Key(ak2)], Self::Contains(ak1, ak2, ak3) => vec![Key(ak1), Key(ak2), Key(ak3)],
Self::NotContains(ak1, ak2) => vec![Key(ak1), Key(ak2)], Self::NotContains(ak1, ak2) => vec![Key(ak1), Key(ak2)],
Self::SumOf(ak1, ak2, ak3) => vec![Key(ak1), Key(ak2), Key(ak3)], 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::ProductOf(ak1, ak2, ak3) => vec![Key(ak1), Key(ak2), Key(ak3)],
@ -130,8 +134,10 @@ impl Statement {
} }
} }
Native(NativePredicate::Contains) => { Native(NativePredicate::Contains) => {
if let (StatementArg::Key(a0), StatementArg::Key(a1)) = (args[0], args[1]) { if let (StatementArg::Key(a0), StatementArg::Key(a1), StatementArg::Key(a2)) =
Ok(Self::Contains(a0, a1)) (args[0], args[1], args[2])
{
Ok(Self::Contains(a0, a1, a2))
} else { } else {
Err(anyhow!("Incorrect statement args")) Err(anyhow!("Incorrect statement args"))
} }