Skip to content

Commit

Permalink
feat: remove index_of from Tree trait
Browse files Browse the repository at this point in the history
  • Loading branch information
enricobottazzi committed Nov 13, 2023
1 parent 90e5dd4 commit 8ab0b07
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 43 deletions.
29 changes: 0 additions & 29 deletions zk_prover/src/merkle_sum_tree/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,6 @@ mod test {
// the balance total should be the same
assert_eq!(root.balances, merkle_tree_2.root().balances);

// should retrun the index of an entry that exist in the tree
assert_eq!(
merkle_tree.index_of(
"AtwIxZHo",
[35479.to_biguint().unwrap(), 31699.to_biguint().unwrap()]
),
Some(15)
);

// shouldn't retrun the index of an entry that doesn't exist in the tree
assert_eq!(
merkle_tree.index_of(
"AtwHHHHo",
[35478.to_biguint().unwrap(), 35478.to_biguint().unwrap()]
),
None
);

// should create valid proof for each entry in the tree and verify it
for i in 0..15 {
let proof = merkle_tree.generate_proof(i).unwrap();
Expand Down Expand Up @@ -149,17 +131,6 @@ mod test {
let new_root_balances = sorted_merkle_tree.root().balances;
let new_root_hash = sorted_merkle_tree.root().hash;

// The index of an entry should not be the same for sorted and unsorted MST
assert_ne!(
merkle_tree
.index_of(
"AtwIxZHo",
[35479.to_biguint().unwrap(), 31699.to_biguint().unwrap()]
)
.unwrap(),
sorted_merkle_tree.index_of_username("AtwIxZHo").unwrap()
);

// The root balances should be the same for sorted and unsorted MST
assert!(old_root_balances == new_root_balances);
// The root hash should not be the same for sorted and unsorted MST
Expand Down
14 changes: 0 additions & 14 deletions zk_prover/src/merkle_sum_tree/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,4 @@ pub trait Tree<const N_ASSETS: usize, const N_BYTES: usize> {

proof.root.hash == node.hash && proof.root.balances == node.balances
}

/// Returns the index of the user with the given username and balances in the tree
fn index_of(&self, username: &str, balances: [BigUint; N_ASSETS]) -> Option<usize>
where
[usize; N_ASSETS + 1]: Sized,
{
let entry: Entry<N_ASSETS> = Entry::new(username.to_string(), balances).unwrap();
let leaf = entry.compute_leaf();
let leaf_hash = leaf.hash;

self.nodes()[0]
.iter()
.position(|node| node.hash == leaf_hash)
}
}

0 comments on commit 8ab0b07

Please sign in to comment.