From 57e633eda571b7b949a4923fb2ad6bd126f8fd21 Mon Sep 17 00:00:00 2001 From: Alex Kuzmin <6849426+alxkzmn@users.noreply.github.com> Date: Tue, 26 Mar 2024 16:26:38 +0800 Subject: [PATCH] Remove N_BYTES generic parameter form Tree trait (#282) --- backend/examples/summa_solvency_flow.rs | 7 +++++-- backend/src/apis/round.rs | 6 +++--- backend/src/tests.rs | 4 ++-- zk_prover/src/circuits/merkle_sum_tree.rs | 2 +- zk_prover/src/merkle_sum_tree/mod.rs | 2 +- zk_prover/src/merkle_sum_tree/mst.rs | 2 +- zk_prover/src/merkle_sum_tree/tree.rs | 6 +++--- 7 files changed, 16 insertions(+), 13 deletions(-) diff --git a/backend/examples/summa_solvency_flow.rs b/backend/examples/summa_solvency_flow.rs index bff04b84..f11749bb 100644 --- a/backend/examples/summa_solvency_flow.rs +++ b/backend/examples/summa_solvency_flow.rs @@ -16,6 +16,7 @@ use summa_backend::{ use summa_solvency::merkle_sum_tree::MerkleSumTree; const N_CURRENCIES: usize = 2; +const N_BYTES: usize = 8; const USER_INDEX: usize = 0; #[tokio::main] @@ -59,11 +60,13 @@ async fn main() -> Result<(), Box> { // Initialize the `Round` instance to submit the liability commitment. let params_path = "ptau/hermez-raw-11"; let entry_csv = "../csv/entry_16.csv"; - let mst = MerkleSumTree::from_csv(entry_csv).unwrap(); + let mst = MerkleSumTree::::from_csv(entry_csv).unwrap(); // Using the `round` instance, the commitment is dispatched to the Summa contract with the `dispatch_commitment` method. let timestamp = 1u64; - let mut round = Round::<4, 2, 8>::new(&signer, Box::new(mst), params_path, timestamp).unwrap(); + let mut round = + Round::<4, N_CURRENCIES, N_BYTES>::new(&signer, Box::new(mst), params_path, timestamp) + .unwrap(); // Sends the commitment, which should ideally complete without errors. round.dispatch_commitment().await?; diff --git a/backend/src/apis/round.rs b/backend/src/apis/round.rs index 865006ad..f4fe20fe 100644 --- a/backend/src/apis/round.rs +++ b/backend/src/apis/round.rs @@ -39,7 +39,7 @@ impl MstInclusionProof { } pub struct Snapshot { - pub mst: Box>, + pub mst: Box>, trusted_setup: SetupArtifacts, } @@ -57,7 +57,7 @@ where { pub fn new<'a>( signer: &'a SummaSigner, - mst: Box>, + mst: Box>, params_path: &str, timestamp: u64, ) -> Result, Box> @@ -130,7 +130,7 @@ where [usize; N_CURRENCIES + 2]: Sized, { pub fn new( - mst: Box>, + mst: Box>, params_path: &str, ) -> Result, Box> { let mst_inclusion_circuit = diff --git a/backend/src/tests.rs b/backend/src/tests.rs index 8373c7cb..83f6aacf 100644 --- a/backend/src/tests.rs +++ b/backend/src/tests.rs @@ -158,7 +158,7 @@ mod test { let params_path = "ptau/hermez-raw-11"; let entry_csv = "../csv/entry_16.csv"; - let mst = MerkleSumTree::from_csv(entry_csv).unwrap(); + let mst = MerkleSumTree::<2, 8>::from_csv(entry_csv).unwrap(); let mut round_one = Round::<4, 2, 8>::new(&signer, Box::new(mst.clone()), params_path, 1).unwrap(); @@ -238,7 +238,7 @@ mod test { let params_path = "ptau/hermez-raw-11"; let entry_csv = "../csv/entry_16.csv"; - let mst = MerkleSumTree::from_csv(entry_csv).unwrap(); + let mst = MerkleSumTree::<2, 8>::from_csv(entry_csv).unwrap(); let mut round = Round::<4, 2, 8>::new(&signer, Box::new(mst), params_path, 1).unwrap(); let mut liability_commitment_logs = summa_contract diff --git a/zk_prover/src/circuits/merkle_sum_tree.rs b/zk_prover/src/circuits/merkle_sum_tree.rs index 24846e9c..558349cb 100644 --- a/zk_prover/src/circuits/merkle_sum_tree.rs +++ b/zk_prover/src/circuits/merkle_sum_tree.rs @@ -83,7 +83,7 @@ where } /// Initializes the circuit with the merkle proof and the entry of the user of which the inclusion is to be verified. - pub fn init(merkle_proof: MerkleProof) -> Self + pub fn init(merkle_proof: MerkleProof) -> Self where [usize; N_CURRENCIES + 1]: Sized, [usize; N_CURRENCIES + 2]: Sized, diff --git a/zk_prover/src/merkle_sum_tree/mod.rs b/zk_prover/src/merkle_sum_tree/mod.rs index 02bb452a..f9dc074b 100644 --- a/zk_prover/src/merkle_sum_tree/mod.rs +++ b/zk_prover/src/merkle_sum_tree/mod.rs @@ -14,7 +14,7 @@ use halo2_proofs::halo2curves::bn256::Fr as Fp; /// * `sibling_leaf_node_hash_preimage`: The hash preimage of the sibling leaf node. The hash preimage is equal to `[sibling_username, sibling.balance[0], sibling.balance[1], ... sibling.balance[N_CURRENCIES - 1]]` /// * `sibling_middle_node_hash_preimages`: The hash preimages of the sibling middle nodes. The hash preimage is equal to `[sibling_left_child.balance[0] + sibling_right_child.balance[0], sibling_left_child.balance[1] + sibling_right_child.balance[1], ..., sibling_left_child.balance[N_CURRENCIES - 1] + sibling_right_child.balance[N_CURRENCIES - 1], sibling_left_child.hash, sibling_right_child.hash]` #[derive(Clone, Debug)] -pub struct MerkleProof +pub struct MerkleProof where [usize; N_CURRENCIES + 1]: Sized, [usize; N_CURRENCIES + 2]: Sized, diff --git a/zk_prover/src/merkle_sum_tree/mst.rs b/zk_prover/src/merkle_sum_tree/mst.rs index f1413576..50a5b902 100644 --- a/zk_prover/src/merkle_sum_tree/mst.rs +++ b/zk_prover/src/merkle_sum_tree/mst.rs @@ -27,7 +27,7 @@ pub struct MerkleSumTree { is_sorted: bool, } -impl Tree +impl Tree for MerkleSumTree { fn root(&self) -> &Node { diff --git a/zk_prover/src/merkle_sum_tree/tree.rs b/zk_prover/src/merkle_sum_tree/tree.rs index b19019bf..14fb95c1 100644 --- a/zk_prover/src/merkle_sum_tree/tree.rs +++ b/zk_prover/src/merkle_sum_tree/tree.rs @@ -4,7 +4,7 @@ use crate::merkle_sum_tree::{Entry, MerkleProof, Node}; use halo2_proofs::halo2curves::bn256::Fr as Fp; /// A trait representing the basic operations for a Merkle-Sum-like Tree. -pub trait Tree { +pub trait Tree { /// Returns a reference to the root node. fn root(&self) -> &Node; @@ -85,7 +85,7 @@ pub trait Tree { fn generate_proof( &self, index: usize, - ) -> Result, Box> + ) -> Result, Box> where [usize; N_CURRENCIES + 1]: Sized, [usize; N_CURRENCIES + 2]: Sized, @@ -134,7 +134,7 @@ pub trait Tree { } /// Verifies a MerkleProof. - fn verify_proof(&self, proof: &MerkleProof) -> bool + fn verify_proof(&self, proof: &MerkleProof) -> bool where [usize; N_CURRENCIES + 1]: Sized, [usize; N_CURRENCIES + 2]: Sized,