Skip to content

Commit 7d13617

Browse files
committed
Incorporate Merkle proof op arg into frontend
1 parent 4d98c86 commit 7d13617

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

src/examples.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,16 @@ pub fn tickets_pod_builder(
372372
signed_pod: &SignedPod,
373373
expected_event_id: i64,
374374
expect_consumed: bool,
375-
blacklisted_emails: &Value,
375+
blacklisted_emails: &Dictionary,
376376
) -> Result<MainPodBuilder> {
377+
let blacklisted_email_root = Value::Dictionary(blacklisted_emails.clone());
378+
let attendee_email_statement: Statement = (signed_pod, "attendeeEmail").into();
379+
let attendee_email_value = match attendee_email_statement.1.get(1).cloned() {
380+
Some(crate::frontend::StatementArg::Literal(v)) => Ok(v),
381+
_ => Err(anyhow!(
382+
"Unexpected error reading attendee email from signed POD."
383+
)),
384+
}?;
377385
// Create a main pod referencing this signed pod with some statements
378386
let mut builder = MainPodBuilder::new(params);
379387
builder.add_signed_pod(signed_pod);
@@ -382,8 +390,9 @@ pub fn tickets_pod_builder(
382390
builder.pub_op(op!(eq, (signed_pod, "isRevoked"), false))?;
383391
builder.pub_op(op!(
384392
not_contains,
385-
blacklisted_emails,
386-
(signed_pod, "attendeeEmail")
393+
blacklisted_email_root,
394+
attendee_email_statement,
395+
blacklisted_emails.prove_nonexistence(&(&attendee_email_value).into())?
387396
))?;
388397
Ok(builder)
389398
}
@@ -397,6 +406,6 @@ pub fn tickets_pod_full_flow() -> Result<MainPodBuilder> {
397406
&signed_pod,
398407
123,
399408
true,
400-
&Value::Dictionary(Dictionary::new(&HashMap::new())?),
409+
&Dictionary::new(&HashMap::new())?,
401410
)
402411
}

src/frontend/mod.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,8 @@ impl MainPodBuilder {
343343
)));
344344
st_args.push(StatementArg::Literal(v.clone()))
345345
}
346+
// Merkle proofs are never arguments to statements.
347+
OperationArg::MerkleProof(_) => (),
346348
};
347349
}
348350
Ok(st_args)
@@ -802,9 +804,12 @@ impl MainPodCompiler {
802804
self.operations.push(op);
803805
}
804806

805-
fn compile_op_arg(&self, op_arg: &OperationArg) -> Option<middleware::Statement> {
807+
fn compile_op_arg(&self, op_arg: &OperationArg) -> Option<middleware::OperationArg> {
806808
match op_arg {
807-
OperationArg::Statement(s) => self.compile_st(s).ok(),
809+
OperationArg::Statement(s) => self
810+
.compile_st(s)
811+
.ok()
812+
.map(|s| middleware::OperationArg::Statement(s)),
808813
OperationArg::Literal(_v) => {
809814
// OperationArg::Literal is a syntax sugar for the frontend. This is translated to
810815
// a new ValueOf statement and it's key used instead.
@@ -816,6 +821,9 @@ impl MainPodCompiler {
816821
// statement doesn't have any requirement on the key and value.
817822
None
818823
}
824+
OperationArg::MerkleProof(pf) => {
825+
Some(middleware::OperationArg::MerkleProof(pf.clone()))
826+
}
819827
}
820828
}
821829

@@ -829,10 +837,7 @@ impl MainPodCompiler {
829837
// TODO: Take Merkle proof into account.
830838
let mop_args =
831839
op.1.iter()
832-
.flat_map(|arg| {
833-
self.compile_op_arg(arg)
834-
.map(|s| Ok(middleware::OperationArg::Statement(s.try_into()?)))
835-
})
840+
.flat_map(|arg| self.compile_op_arg(arg).map(|op_arg| Ok(op_arg)))
836841
.collect::<Result<Vec<_>>>()?;
837842
middleware::Operation::op(mop_code, &mop_args)
838843
}

src/frontend/operation.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use std::fmt;
22

33
use super::{SignedPod, Statement, Value};
4-
use crate::middleware::OperationType;
4+
use crate::{backends::plonky2::primitives::merkletree::MerkleProof, middleware::OperationType};
55

66
#[derive(Clone, Debug, PartialEq, Eq)]
77
pub enum OperationArg {
88
Statement(Statement),
99
Literal(Value),
1010
Entry(String, Value),
11+
MerkleProof(MerkleProof),
1112
}
1213

1314
impl fmt::Display for OperationArg {
@@ -16,6 +17,7 @@ impl fmt::Display for OperationArg {
1617
OperationArg::Statement(s) => write!(f, "{}", s),
1718
OperationArg::Literal(v) => write!(f, "{}", v),
1819
OperationArg::Entry(k, v) => write!(f, "({}, {})", k, v),
20+
OperationArg::MerkleProof(pf) => write!(f, "merkle_proof({})", pf),
1921
}
2022
}
2123
}
@@ -68,6 +70,12 @@ impl<V: Into<Value>> From<(&str, V)> for OperationArg {
6870
}
6971
}
7072

73+
impl From<MerkleProof> for OperationArg {
74+
fn from(pf: MerkleProof) -> Self {
75+
Self::MerkleProof(pf)
76+
}
77+
}
78+
7179
#[derive(Clone, Debug, PartialEq, Eq)]
7280
pub struct Operation(pub OperationType, pub Vec<OperationArg>);
7381

0 commit comments

Comments
 (0)