Skip to content

Commit

Permalink
chore: format nr files with rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
sripwoud committed Jun 28, 2024
1 parent 6e9fcf2 commit d91c4e1
Show file tree
Hide file tree
Showing 13 changed files with 213 additions and 148 deletions.
1 change: 1 addition & 0 deletions .dprint.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"**/node_modules",
"yarn.lock",
".yarn",
"packages/merkle-trees/src/globals.nr",
],
"plugins": [
"https://plugins.dprint.dev/json-0.19.3.wasm",
Expand Down
1 change: 1 addition & 0 deletions packages/merkle-trees/src/globals.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global TREE_DEPTH = 256;
82 changes: 43 additions & 39 deletions packages/merkle-trees/src/lib.nr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod globals;
mod merkle;
mod sparse_merkle;

Expand All @@ -10,71 +11,74 @@ trait Creator {
fn default(root: Field, hasher: fn([Field]) -> Field) -> Self;

/**
* Imports an existing Sparse Merkle Tree (SparseMerkleTree) instance.
* @param hasher The hash function that is used to hash the nodes of the tree
* @param root The root of the tree
*/
* Imports an existing Sparse Merkle Tree (SparseMerkleTree) instance.
* @param hasher The hash function that is used to hash the nodes of the tree
* @param root The root of the tree
*/
fn from(root: Field, hasher: fn([Field]) -> Field) -> Self {
Self::default(root, hasher)
}

/**
* Creates a new Sparse Merkle Tree (SparseMerkleTree) instance.
* @param hasher The hash function that is used to hash the nodes of the tree
*/
* Creates a new Sparse Merkle Tree (SparseMerkleTree) instance.
* @param hasher The hash function that is used to hash the nodes of the tree
*/
fn new(hasher: fn([Field]) -> Field) -> Self {
Self::from(0, hasher)
}
}

trait MembershipProver<T, U> {
/**
* Proves that a leaf is a member of the tree.
* @param leaf The leaf to prove
* @param path The hash path and indices
*/
* Proves that a leaf is a member of the tree.
* @param leaf The leaf to prove
* @param path The hash path and indices
*/
fn membership(self, entry: T, path: U);
}

trait NonMembershipProver {
/**
* Verifies a non-membership proof, ie it calculates the tree root
* based on an entry and a matching_entry and all siblings and compares that calculated root
* with the root that is passed to this function.
* @param entry Contains key and value of an entry: (key, value)
* @param matching_entry Contains (key, value) of a matching entry
* @param siblings Contains array of siblings the matching_entry
*/
fn non_membership(self, entry: (Field, Field), matching_entry: (Field, Field), siblings: [Field]);
* Verifies a non-membership proof, ie it calculates the tree root
* based on an entry and a matching_entry and all siblings and compares that calculated root
* with the root that is passed to this function.
* @param entry Contains key and value of an entry: (key, value)
* @param matching_entry Contains (key, value) of a matching entry
* @param siblings Contains array of siblings the matching_entry
*/
fn non_membership(
self,
entry: (Field, Field),
matching_entry: (Field, Field),
siblings: [Field],
);
}

trait Modifier<T, U> {
/**
* Proves the addition of a NEW entry to an existing tree. Based on the siblings first validates the correctness of
* the old root. Then uses the new entry and the siblings to calculate the new tree root.
* NOTE: this function doesn't validate if the key for the new entry already exists in the tree, ie
* if the operation is actually an update. For this operation there is a separate function.
* @param new_entry The new entry to prove addition
* @param siblings The siblings (and indices for MT proofs)
*/
* Proves the addition of a NEW entry to an existing tree. Based on the siblings first validates the correctness of
* the old root. Then uses the new entry and the siblings to calculate the new tree root.
* NOTE: this function doesn't validate if the key for the new entry already exists in the tree, ie
* if the operation is actually an update. For this operation there is a separate function.
* @param new_entry The new entry to prove addition
* @param siblings The siblings (and indices for MT proofs)
*/
fn add(&mut self, new_entry: T, siblings: U);


/**
* Proves the deletion of an existing entry from a tree. Based on the siblings first does a membership proof
* of that existing entry and then calculates the new root (without the entry).
* @param entry Contains key and value of the to-be-deleted entry: (key, value)
* @param siblings The siblings (and indices for MT proofs)
*/
* Proves the deletion of an existing entry from a tree. Based on the siblings first does a membership proof
* of that existing entry and then calculates the new root (without the entry).
* @param entry Contains key and value of the to-be-deleted entry: (key, value)
* @param siblings The siblings (and indices for MT proofs)
*/
fn delete(&mut self, entry: T, siblings: U);


/**
* Proves the update of the value of an existing entry in a tree. Based on the siblings first does a membership proof
* first verifies the membership of the old entry. Then recalculates the new root.
* @param new_value The new value to be added (instead of old_entry[1])
* @param old_entry Contains key and value of the entry to be updated: (key, value)
* @param siblings The siblings (and indices for MT proofs)
*/
* Proves the update of the value of an existing entry in a tree. Based on the siblings first does a membership proof
* first verifies the membership of the old entry. Then recalculates the new root.
* @param new_value The new value to be added (instead of old_entry[1])
* @param old_entry Contains key and value of the entry to be updated: (key, value)
* @param siblings The siblings (and indices for MT proofs)
*/
fn update(&mut self, new_value: Field, old_entry: T, siblings: U);
}
7 changes: 3 additions & 4 deletions packages/merkle-trees/src/merkle.nr
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::{Creator, MembershipProver, NonMembershipProver, Modifier};
use crate::{Creator, MembershipProver, Modifier, NonMembershipProver};

mod tree;
mod tests;
mod tree;

struct MerkleTree {
hasher: fn([Field]) -> Field,
root: Field
root: Field,
}

impl Creator for MerkleTree {
Expand All @@ -19,7 +19,6 @@ impl MembershipProver<Field, HashPath> for MerkleTree {
let root = self.calculate_root(leaf, siblings);
assert(self.root == root);
}

}

impl Modifier<Field, HashPath> for MerkleTree {
Expand Down
37 changes: 26 additions & 11 deletions packages/merkle-trees/src/merkle/tests/pedersen.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use dep::std::hash::pedersen::pedersen_hash_slice;
use crate::merkle::MerkleTree;
use dep::std::hash::pedersen::pedersen_hash_slice;

fn pedersen_hasher(leaves: [Field]) -> Field {
pedersen_hash_slice(leaves)
Expand All @@ -11,7 +11,14 @@ fn test_merkle_tree_membership() {
let mt = MerkleTree::from(root, pedersen_hasher);

let leaf = 0x00;
let paths = (0x00, &[0x01, 0x67635fc829435949ed0ced751d11f3f823087ec463ca6ee53e253fb7e390e74, 0x04]);
let paths = (
0x00,
&[
0x01,
0x67635fc829435949ed0ced751d11f3f823087ec463ca6ee53e253fb7e390e74,
0x04,
],
);
mt.membership(leaf, paths);
}

Expand All @@ -21,7 +28,10 @@ fn test_merkle_tree_add() {
let mut mt = MerkleTree::from(old_root, pedersen_hasher);

let leaf = 0x67635fc829435949ed0ced751d11f3f823087ec463ca6ee53e253fb7e390e74;
let paths = (0x01, &[0x1c59022dba1d97f63021cc5a23e4fe80f019465e0ccb54de9aa91935495354a3]);
let paths = (
0x01,
&[0x1c59022dba1d97f63021cc5a23e4fe80f019465e0ccb54de9aa91935495354a3],
);
mt.add(leaf, paths);
}

Expand All @@ -31,7 +41,10 @@ fn test_merkle_tree_delete() {
let mut mt = MerkleTree::from(old_root, pedersen_hasher);

let leaf = 0x67635fc829435949ed0ced751d11f3f823087ec463ca6ee53e253fb7e390e74;
let paths = (0x00, &[0x1c59022dba1d97f63021cc5a23e4fe80f019465e0ccb54de9aa91935495354a3]);
let paths = (
0x00,
&[0x1c59022dba1d97f63021cc5a23e4fe80f019465e0ccb54de9aa91935495354a3],
);

mt.delete(leaf, paths);

Expand All @@ -45,16 +58,18 @@ fn test_merkle_tree_update() {

let old_leaf = 0x67635fc829435949ed0ced751d11f3f823087ec463ca6ee53e253fb7e390e74;
let leaf = 0xd98561fb02ca04d00801dfdc118b2a24cea0351963587712a28d368041370e1;
let paths = (0x00, &[0x1c59022dba1d97f63021cc5a23e4fe80f019465e0ccb54de9aa91935495354a3]);
let paths = (
0x00,
&[0x1c59022dba1d97f63021cc5a23e4fe80f019465e0ccb54de9aa91935495354a3],
);

mt.update(leaf, old_leaf, paths);

assert(
mt.root == pedersen_hasher(
&[
0xd98561fb02ca04d00801dfdc118b2a24cea0351963587712a28d368041370e1,
0x1c59022dba1d97f63021cc5a23e4fe80f019465e0ccb54de9aa91935495354a3
]
)
mt.root
== pedersen_hasher(&[
0xd98561fb02ca04d00801dfdc118b2a24cea0351963587712a28d368041370e1,
0x1c59022dba1d97f63021cc5a23e4fe80f019465e0ccb54de9aa91935495354a3,
]),
);
}
37 changes: 26 additions & 11 deletions packages/merkle-trees/src/merkle/tests/poseidon.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use dep::std::hash::poseidon::bn254::{hash_2, hash_3};
use crate::merkle::MerkleTree;
use dep::std::hash::poseidon::bn254::{hash_2, hash_3};

fn poseidon_hasher(leaves: [Field]) -> Field {
if leaves.len() == 2 {
Expand All @@ -15,7 +15,14 @@ fn test_merkle_tree_membership() {
let mt = MerkleTree::from(root, poseidon_hasher);

let leaf = 0x00;
let paths = (0x00, &[0x01, 0x26059ac500f935d65bf50b096f757fe1dcb3568822d4e4cb7a8dc95f7bbd24f7, 0x04]);
let paths = (
0x00,
&[
0x01,
0x26059ac500f935d65bf50b096f757fe1dcb3568822d4e4cb7a8dc95f7bbd24f7,
0x04,
],
);

mt.membership(leaf, paths);
}
Expand All @@ -26,7 +33,10 @@ fn test_merkle_tree_add() {
let mut mt = MerkleTree::from(old_root, poseidon_hasher);

let leaf = 0x26059ac500f935d65bf50b096f757fe1dcb3568822d4e4cb7a8dc95f7bbd24f7;
let paths = (0x01, &[0x1910f234d14bea7c640841c9fd0d765e8599a4cd527285590e4159e66b912be1]);
let paths = (
0x01,
&[0x1910f234d14bea7c640841c9fd0d765e8599a4cd527285590e4159e66b912be1],
);
mt.add(leaf, paths);
}

Expand All @@ -36,7 +46,10 @@ fn test_merkle_tree_delete() {
let mut mt = MerkleTree::from(old_root, poseidon_hasher);

let leaf = 0x26059ac500f935d65bf50b096f757fe1dcb3568822d4e4cb7a8dc95f7bbd24f7;
let paths = (0x00, &[0x1910f234d14bea7c640841c9fd0d765e8599a4cd527285590e4159e66b912be1]);
let paths = (
0x00,
&[0x1910f234d14bea7c640841c9fd0d765e8599a4cd527285590e4159e66b912be1],
);
mt.delete(leaf, paths);

assert(mt.root == 0x1910f234d14bea7c640841c9fd0d765e8599a4cd527285590e4159e66b912be1);
Expand All @@ -49,15 +62,17 @@ fn test_merkle_tree_update() {

let old_leaf = 0x26059ac500f935d65bf50b096f757fe1dcb3568822d4e4cb7a8dc95f7bbd24f7;
let leaf = 0xd98561fb02ca04d00801dfdc118b2a24cea0351963587712a28d368041370e1;
let paths = (0x00, &[0x1910f234d14bea7c640841c9fd0d765e8599a4cd527285590e4159e66b912be1]);
let paths = (
0x00,
&[0x1910f234d14bea7c640841c9fd0d765e8599a4cd527285590e4159e66b912be1],
);
mt.update(leaf, old_leaf, paths);

assert(
mt.root == poseidon_hasher(
&[
0xd98561fb02ca04d00801dfdc118b2a24cea0351963587712a28d368041370e1,
0x1910f234d14bea7c640841c9fd0d765e8599a4cd527285590e4159e66b912be1
]
)
mt.root
== poseidon_hasher(&[
0xd98561fb02ca04d00801dfdc118b2a24cea0351963587712a28d368041370e1,
0x1910f234d14bea7c640841c9fd0d765e8599a4cd527285590e4159e66b912be1,
]),
);
}
37 changes: 26 additions & 11 deletions packages/merkle-trees/src/merkle/tests/poseidon2.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use dep::std::hash::poseidon2::Poseidon2::hash;
use crate::merkle::MerkleTree;
use dep::std::hash::poseidon2::Poseidon2::hash;

fn poseidon2_hasher(leaves: [Field]) -> Field {
if leaves.len() == 2 {
Expand All @@ -15,7 +15,14 @@ fn test_merkle_tree_membership() {
let mt = MerkleTree::from(root, poseidon2_hasher);

let leaf = 0x00;
let paths = (0x00, &[0x01, 0x2bc00d90b885b09d12764e764410f7f693f514f7f3ca14d916741ff3968b3079, 0x04]);
let paths = (
0x00,
&[
0x01,
0x2bc00d90b885b09d12764e764410f7f693f514f7f3ca14d916741ff3968b3079,
0x04,
],
);

mt.membership(leaf, paths);
}
Expand All @@ -26,7 +33,10 @@ fn test_merkle_tree_add() {
let mut mt = MerkleTree::from(old_root, poseidon2_hasher);

let leaf = 0x2bc00d90b885b09d12764e764410f7f693f514f7f3ca14d916741ff3968b3079;
let paths = (0x01, &[0x21447efbbddb57d6fc5ad24d906388492e82c44e5160425258dd4ea995e3a06e]);
let paths = (
0x01,
&[0x21447efbbddb57d6fc5ad24d906388492e82c44e5160425258dd4ea995e3a06e],
);
mt.add(leaf, paths);
}

Expand All @@ -36,7 +46,10 @@ fn test_merkle_tree_delete() {
let mut mt = MerkleTree::from(old_root, poseidon2_hasher);

let leaf = 0x2bc00d90b885b09d12764e764410f7f693f514f7f3ca14d916741ff3968b3079;
let paths = (0x00, &[0x21447efbbddb57d6fc5ad24d906388492e82c44e5160425258dd4ea995e3a06e]);
let paths = (
0x00,
&[0x21447efbbddb57d6fc5ad24d906388492e82c44e5160425258dd4ea995e3a06e],
);

mt.delete(leaf, paths);

Expand All @@ -51,15 +64,17 @@ fn test_merkle_tree_update() {
let old_leaf = 0x2bc00d90b885b09d12764e764410f7f693f514f7f3ca14d916741ff3968b3079;
let leaf = 0xd98561fb02ca04d00801dfdc118b2a24cea0351963587712a28d368041370e1;

let paths = (0x00, &[0x21447efbbddb57d6fc5ad24d906388492e82c44e5160425258dd4ea995e3a06e]);
let paths = (
0x00,
&[0x21447efbbddb57d6fc5ad24d906388492e82c44e5160425258dd4ea995e3a06e],
);
mt.update(leaf, old_leaf, paths);

assert(
mt.root == poseidon2_hasher(
&[
0xd98561fb02ca04d00801dfdc118b2a24cea0351963587712a28d368041370e1,
0x21447efbbddb57d6fc5ad24d906388492e82c44e5160425258dd4ea995e3a06e
]
)
mt.root
== poseidon2_hasher(&[
0xd98561fb02ca04d00801dfdc118b2a24cea0351963587712a28d368041370e1,
0x21447efbbddb57d6fc5ad24d906388492e82c44e5160425258dd4ea995e3a06e,
]),
);
}
Loading

0 comments on commit d91c4e1

Please sign in to comment.