Skip to content

Commit

Permalink
Remove N_BYTES generic parameter form Tree trait (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
alxkzmn authored Mar 26, 2024
1 parent e8e1e20 commit 57e633e
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 13 deletions.
7 changes: 5 additions & 2 deletions backend/examples/summa_solvency_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -59,11 +60,13 @@ async fn main() -> Result<(), Box<dyn Error>> {
// 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::<N_CURRENCIES, N_BYTES>::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?;
Expand Down
6 changes: 3 additions & 3 deletions backend/src/apis/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl MstInclusionProof {
}

pub struct Snapshot<const LEVELS: usize, const N_CURRENCIES: usize, const N_BYTES: usize> {
pub mst: Box<dyn Tree<N_CURRENCIES, N_BYTES>>,
pub mst: Box<dyn Tree<N_CURRENCIES>>,
trusted_setup: SetupArtifacts,
}

Expand All @@ -57,7 +57,7 @@ where
{
pub fn new<'a>(
signer: &'a SummaSigner,
mst: Box<dyn Tree<N_CURRENCIES, N_BYTES>>,
mst: Box<dyn Tree<N_CURRENCIES>>,
params_path: &str,
timestamp: u64,
) -> Result<Round<'a, LEVELS, N_CURRENCIES, N_BYTES>, Box<dyn Error>>
Expand Down Expand Up @@ -130,7 +130,7 @@ where
[usize; N_CURRENCIES + 2]: Sized,
{
pub fn new(
mst: Box<dyn Tree<N_CURRENCIES, N_BYTES>>,
mst: Box<dyn Tree<N_CURRENCIES>>,
params_path: &str,
) -> Result<Snapshot<LEVELS, N_CURRENCIES, N_BYTES>, Box<dyn std::error::Error>> {
let mst_inclusion_circuit =
Expand Down
4 changes: 2 additions & 2 deletions backend/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion zk_prover/src/circuits/merkle_sum_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<N_CURRENCIES, N_BYTES>) -> Self
pub fn init(merkle_proof: MerkleProof<N_CURRENCIES>) -> Self
where
[usize; N_CURRENCIES + 1]: Sized,
[usize; N_CURRENCIES + 2]: Sized,
Expand Down
2 changes: 1 addition & 1 deletion zk_prover/src/merkle_sum_tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<const N_CURRENCIES: usize, const N_BYTES: usize>
pub struct MerkleProof<const N_CURRENCIES: usize>
where
[usize; N_CURRENCIES + 1]: Sized,
[usize; N_CURRENCIES + 2]: Sized,
Expand Down
2 changes: 1 addition & 1 deletion zk_prover/src/merkle_sum_tree/mst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct MerkleSumTree<const N_CURRENCIES: usize, const N_BYTES: usize> {
is_sorted: bool,
}

impl<const N_CURRENCIES: usize, const N_BYTES: usize> Tree<N_CURRENCIES, N_BYTES>
impl<const N_CURRENCIES: usize, const N_BYTES: usize> Tree<N_CURRENCIES>
for MerkleSumTree<N_CURRENCIES, N_BYTES>
{
fn root(&self) -> &Node<N_CURRENCIES> {
Expand Down
6 changes: 3 additions & 3 deletions zk_prover/src/merkle_sum_tree/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<const N_CURRENCIES: usize, const N_BYTES: usize> {
pub trait Tree<const N_CURRENCIES: usize> {
/// Returns a reference to the root node.
fn root(&self) -> &Node<N_CURRENCIES>;

Expand Down Expand Up @@ -85,7 +85,7 @@ pub trait Tree<const N_CURRENCIES: usize, const N_BYTES: usize> {
fn generate_proof(
&self,
index: usize,
) -> Result<MerkleProof<N_CURRENCIES, N_BYTES>, Box<dyn std::error::Error>>
) -> Result<MerkleProof<N_CURRENCIES>, Box<dyn std::error::Error>>
where
[usize; N_CURRENCIES + 1]: Sized,
[usize; N_CURRENCIES + 2]: Sized,
Expand Down Expand Up @@ -134,7 +134,7 @@ pub trait Tree<const N_CURRENCIES: usize, const N_BYTES: usize> {
}

/// Verifies a MerkleProof.
fn verify_proof(&self, proof: &MerkleProof<N_CURRENCIES, N_BYTES>) -> bool
fn verify_proof(&self, proof: &MerkleProof<N_CURRENCIES>) -> bool
where
[usize; N_CURRENCIES + 1]: Sized,
[usize; N_CURRENCIES + 2]: Sized,
Expand Down

0 comments on commit 57e633e

Please sign in to comment.