From e396bd2701fa6f7bdcea21f13bbf9ef25c993df7 Mon Sep 17 00:00:00 2001 From: ihateface Date: Wed, 24 Aug 2022 15:16:44 +0200 Subject: [PATCH] daod: merge zk_public_values() and zk_proof_addrs() into a single function, and so deprecate zip! macro --- bin/daod/src/dao_contract/mint/validate.rs | 8 +--- bin/daod/src/dao_contract/propose/validate.rs | 36 +++++++---------- bin/daod/src/dao_contract/vote/validate.rs | 40 ++++++++----------- bin/daod/src/demo.rs | 31 ++++---------- bin/daod/src/example_contract/foo/validate.rs | 8 ++-- .../src/money_contract/transfer/validate.rs | 17 ++------ 6 files changed, 47 insertions(+), 93 deletions(-) diff --git a/bin/daod/src/dao_contract/mint/validate.rs b/bin/daod/src/dao_contract/mint/validate.rs index c6ad8c690..a8afba837 100644 --- a/bin/daod/src/dao_contract/mint/validate.rs +++ b/bin/daod/src/dao_contract/mint/validate.rs @@ -50,12 +50,8 @@ pub struct CallData { } impl CallDataBase for CallData { - fn zk_public_values(&self) -> Vec> { - vec![vec![self.dao_bulla.0]] - } - - fn zk_proof_addrs(&self) -> Vec { - vec!["dao-mint".to_string()] + fn zk_public_values(&self) -> Vec<(String, Vec)> { + vec![("dao-mint".to_string(), vec![self.dao_bulla.0])] } fn as_any(&self) -> &dyn Any { diff --git a/bin/daod/src/dao_contract/propose/validate.rs b/bin/daod/src/dao_contract/propose/validate.rs index 4eeeee6cc..6b17a1d4f 100644 --- a/bin/daod/src/dao_contract/propose/validate.rs +++ b/bin/daod/src/dao_contract/propose/validate.rs @@ -52,7 +52,7 @@ pub struct CallData { } impl CallDataBase for CallData { - fn zk_public_values(&self) -> Vec> { + fn zk_public_values(&self) -> Vec<(String, Vec)> { let mut zk_publics = Vec::new(); let mut total_funds_commit = pallas::Point::identity(); @@ -67,21 +67,24 @@ impl CallDataBase for CallData { let sigpub_x = *sigpub_coords.x(); let sigpub_y = *sigpub_coords.y(); - zk_publics.push(vec![ - value_commit_x, - value_commit_y, - self.header.token_commit, - input.merkle_root.0, - sigpub_x, - sigpub_y, - ]); + zk_publics.push(( + "dao-propose-burn".to_string(), + vec![ + value_commit_x, + value_commit_y, + self.header.token_commit, + input.merkle_root.0, + sigpub_x, + sigpub_y, + ], + )); } let total_funds_coords = total_funds_commit.to_affine().coordinates().unwrap(); let total_funds_x = *total_funds_coords.x(); let total_funds_y = *total_funds_coords.y(); - zk_publics.push( - // dao-propose-main proof + zk_publics.push(( + "dao-propose-main".to_string(), vec![ self.header.token_commit, self.header.dao_merkle_root.0, @@ -89,20 +92,11 @@ impl CallDataBase for CallData { total_funds_x, total_funds_y, ], - ); + )); zk_publics } - fn zk_proof_addrs(&self) -> Vec { - let mut zk_addrs = Vec::new(); - for input in &self.inputs { - zk_addrs.push("dao-propose-burn".to_string()); - } - zk_addrs.push("dao-propose-main".to_string()); - zk_addrs - } - fn as_any(&self) -> &dyn Any { self } diff --git a/bin/daod/src/dao_contract/vote/validate.rs b/bin/daod/src/dao_contract/vote/validate.rs index a52ae4b74..c2201cfd6 100644 --- a/bin/daod/src/dao_contract/vote/validate.rs +++ b/bin/daod/src/dao_contract/vote/validate.rs @@ -59,7 +59,7 @@ pub struct CallData { } impl CallDataBase for CallData { - fn zk_public_values(&self) -> Vec> { + fn zk_public_values(&self) -> Vec<(String, Vec)> { let mut zk_publics = Vec::new(); let mut total_value_commit = pallas::Point::identity(); @@ -74,16 +74,18 @@ impl CallDataBase for CallData { let sigpub_x = *sigpub_coords.x(); let sigpub_y = *sigpub_coords.y(); - // dao-vote-burn proof - zk_publics.push(vec![ - input.nullifier.0, - value_commit_x, - value_commit_y, - self.header.token_commit, - input.merkle_root.0, - sigpub_x, - sigpub_y, - ]); + zk_publics.push(( + "dao-vote-burn".to_string(), + vec![ + input.nullifier.0, + value_commit_x, + value_commit_y, + self.header.token_commit, + input.merkle_root.0, + sigpub_x, + sigpub_y, + ], + )); } let vote_commit_coords = self.header.vote_commit.to_affine().coordinates().unwrap(); @@ -93,8 +95,9 @@ impl CallDataBase for CallData { let value_commit_coords = total_value_commit.to_affine().coordinates().unwrap(); let value_commit_x = *value_commit_coords.x(); let value_commit_y = *value_commit_coords.y(); - zk_publics.push( - // dao-vote-main proof + + zk_publics.push(( + "dao-vote-main".to_string(), vec![ self.header.token_commit, self.header.proposal_bulla, @@ -103,20 +106,11 @@ impl CallDataBase for CallData { value_commit_x, value_commit_y, ], - ); + )); zk_publics } - fn zk_proof_addrs(&self) -> Vec { - let mut zk_addrs = Vec::new(); - for input in &self.inputs { - zk_addrs.push("dao-vote-burn".to_string()); - } - zk_addrs.push("dao-vote-main".to_string()); - zk_addrs - } - fn as_any(&self) -> &dyn Any { self } diff --git a/bin/daod/src/demo.rs b/bin/daod/src/demo.rs index 2a3a24170..fa45e149b 100644 --- a/bin/daod/src/demo.rs +++ b/bin/daod/src/demo.rs @@ -108,14 +108,6 @@ impl ZkContractTable { } } -macro_rules! zip { - ($x: expr) => ($x); - ($x: expr, $($y: expr), +) => ( - $x.iter().zip( - zip!($($y), +)) - ) -} - pub struct Transaction { pub func_calls: Vec, } @@ -127,24 +119,18 @@ impl Transaction { fn zk_verify(&self, zk_bins: &ZkContractTable) { for func_call in &self.func_calls { let proofs_public_vals = &func_call.call_data.zk_public_values(); - let proofs_addrs = &func_call.call_data.zk_proof_addrs(); + assert_eq!( proofs_public_vals.len(), - proofs_addrs.len(), - "proof_public_vals.len()={} and proof_addrs.len()={} do not match", - proofs_public_vals.len(), - proofs_addrs.len(), - ); - assert_eq!( - proofs_addrs.len(), func_call.proofs.len(), - "proof_addrs.len()={} and func_call.proofs.len()={} do not match", - proofs_addrs.len(), + "proof_public_vals.len()={} and func_call.proofs.len()={} do not match", + proofs_public_vals.len(), func_call.proofs.len() ); - for (i, (key, (proof, public_vals))) in - zip!(proofs_addrs, &func_call.proofs, proofs_public_vals).enumerate() + for (i, (proof, (key, public_vals))) in + func_call.proofs.iter().zip(proofs_public_vals.iter()).enumerate() { + debug!(target: "demo", "Tranaction::zk_verify i: {}, key: {}", i, key); match zk_bins.lookup(key).unwrap() { ZkContractInfo::Binary(info) => { let verifying_key = &info.verifying_key; @@ -177,10 +163,7 @@ pub struct FuncCall { pub trait CallDataBase { // Public values for verifying the proofs // Needed so we can convert internal types so they can be used in Proof::verify() - fn zk_public_values(&self) -> Vec>; - - // The zk contract ID needed to lookup in the table - fn zk_proof_addrs(&self) -> Vec; + fn zk_public_values(&self) -> Vec<(String, Vec)>; // For upcasting to CallData itself so it can be read in state_transition() fn as_any(&self) -> &dyn Any; diff --git a/bin/daod/src/example_contract/foo/validate.rs b/bin/daod/src/example_contract/foo/validate.rs index a73b1e8f0..dda4dc78b 100644 --- a/bin/daod/src/example_contract/foo/validate.rs +++ b/bin/daod/src/example_contract/foo/validate.rs @@ -35,12 +35,10 @@ pub struct CallData { } impl CallDataBase for CallData { - fn zk_public_values(&self) -> Vec> { - vec![vec![self.header.public_c]] - } - fn zk_proof_addrs(&self) -> Vec { - vec!["example-foo".to_string()] + fn zk_public_values(&self) -> Vec<(String, Vec)> { + vec![("example-foo".to_string(), vec![self.header.public_c])] } + fn as_any(&self) -> &dyn Any { self } diff --git a/bin/daod/src/money_contract/transfer/validate.rs b/bin/daod/src/money_contract/transfer/validate.rs index c68694ecd..aeb57d504 100644 --- a/bin/daod/src/money_contract/transfer/validate.rs +++ b/bin/daod/src/money_contract/transfer/validate.rs @@ -166,28 +166,17 @@ pub struct CallData { } impl CallDataBase for CallData { - fn zk_public_values(&self) -> Vec> { + fn zk_public_values(&self) -> Vec<(String, Vec)> { let mut public_values = Vec::new(); for input in &self.inputs { - public_values.push(input.revealed.make_outputs()); + public_values.push(("money-transfer-burn".to_string(), input.revealed.make_outputs())); } for output in &self.outputs { - public_values.push(output.revealed.make_outputs()); + public_values.push(("money-transfer-mint".to_string(), output.revealed.make_outputs())); } public_values } - fn zk_proof_addrs(&self) -> Vec { - let mut result = Vec::new(); - for _ in &self.inputs { - result.push("money-transfer-burn".to_string()); - } - for _ in &self.outputs { - result.push("money-transfer-mint".to_string()); - } - result - } - fn as_any(&self) -> &dyn Any { self }