Skip to content

Commit

Permalink
updating to match Noir 0.30.0
Browse files Browse the repository at this point in the history
  • Loading branch information
signorecello committed Jul 10, 2024
1 parent 4f30d84 commit d546dd4
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 96 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ jobs:
- uses: actions/checkout@v4
- uses: noir-lang/[email protected]
with:
toolchain: 0.26.0
toolchain: 0.30.0
- run: nargo test
2 changes: 1 addition & 1 deletion packages/merkle-trees/Nargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
name = "merkle_trees"
type = "lib"
authors = ["fabianschu", "signorecello"]
compiler_version = "0.26.0"
compiler_version = "0.30.0"
37 changes: 33 additions & 4 deletions packages/merkle-trees/src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,52 @@ trait Calculator<T> {
fn calculate_two_roots(self, entry: T, siblings: (Field, [Field])) -> (Field, Field);
}

trait Creator {
fn default(root: Field, hasher: fn([Field]) -> Field) -> Self;
trait SMT_Creator {
fn default(
root: Field,
leaf_hasher: fn([Field; 3]) -> Field,
hasher: fn([Field; 2]) -> 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
*/
fn from(root: Field, hasher: fn([Field]) -> Field) -> Self {
fn from(
root: Field,
leaf_hasher: fn([Field; 3]) -> Field,
hasher: fn([Field; 2]) -> Field,
) -> Self {
Self::default(root, leaf_hasher, hasher)
}

/**
* 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(leaf_hasher: fn([Field; 3]) -> Field, hasher: fn([Field; 2]) -> Field) -> Self {
Self::from(0, leaf_hasher, hasher)
}
}

trait MT_Creator {
fn default(root: Field, hasher: fn([Field; 2]) -> 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
*/
fn from(root: Field, hasher: fn([Field; 2]) -> 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
*/
fn new(hasher: fn([Field]) -> Field) -> Self {
fn new(hasher: fn([Field; 2]) -> Field) -> Self {
Self::from(0, hasher)
}
}
Expand Down
16 changes: 8 additions & 8 deletions packages/merkle-trees/src/merkle.nr
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use crate::{Creator, MembershipProver, Modifier, NonMembershipProver};
use crate::{MT_Creator, MembershipProver, Modifier, NonMembershipProver};

mod tests;
mod tree;

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

impl Creator for MerkleTree {
fn default(root: Field, hasher: fn([Field]) -> Field) -> Self {
impl MT_Creator for MerkleTree {
fn default(root: Field, hasher: fn([Field; 2]) -> Field) -> Self {
Self { root, hasher }
}
}
Expand Down Expand Up @@ -45,11 +45,11 @@ impl Modifier<Field, HashPath> for MerkleTree {
let sibling = siblings.1[i];
if sibling != 0 {
if index_bits[i] == 0 {
new_parent = (self.hasher)(&[new_parent, sibling]);
old_parent = (self.hasher)(&[old_parent, sibling]);
new_parent = (self.hasher)([new_parent, sibling]);
old_parent = (self.hasher)([old_parent, sibling]);
} else {
new_parent = (self.hasher)(&[sibling, new_parent]);
old_parent = (self.hasher)(&[sibling, old_parent]);
new_parent = (self.hasher)([sibling, new_parent]);
old_parent = (self.hasher)([sibling, old_parent]);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions packages/merkle-trees/src/merkle/tests/pedersen.nr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::merkle::MerkleTree;
use dep::std::hash::pedersen::pedersen_hash_slice;
use dep::std::hash::pedersen_hash;

fn pedersen_hasher(leaves: [Field]) -> Field {
pedersen_hash_slice(leaves)
fn pedersen_hasher(leaves: [Field; 2]) -> Field {
pedersen_hash(leaves)
}

#[test]
Expand Down Expand Up @@ -67,7 +67,7 @@ fn test_merkle_tree_update() {

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

fn poseidon_hasher(leaves: [Field]) -> Field {
if leaves.len() == 2 {
hash_2([leaves[0], leaves[1]])
} else {
hash_3([leaves[0], leaves[1], leaves[2]])
}
fn poseidon_hasher(leaves: [Field; 2]) -> Field {
hash_2([leaves[0], leaves[1]])
}

#[test]
Expand Down Expand Up @@ -70,7 +66,7 @@ fn test_merkle_tree_update() {

assert(
mt.root
== poseidon_hasher(&[
== poseidon_hasher([
0xd98561fb02ca04d00801dfdc118b2a24cea0351963587712a28d368041370e1,
0x1910f234d14bea7c640841c9fd0d765e8599a4cd527285590e4159e66b912be1,
]),
Expand Down
10 changes: 3 additions & 7 deletions packages/merkle-trees/src/merkle/tests/poseidon2.nr
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
use crate::merkle::MerkleTree;
use dep::std::hash::poseidon2::Poseidon2::hash;

fn poseidon2_hasher(leaves: [Field]) -> Field {
if leaves.len() == 2 {
hash([leaves[0], leaves[1]], 2)
} else {
hash([leaves[0], leaves[1], leaves[2]], 3)
}
fn poseidon2_hasher(leaves: [Field; 2]) -> Field {
hash([leaves[0], leaves[1]], 2)
}

#[test]
Expand Down Expand Up @@ -72,7 +68,7 @@ fn test_merkle_tree_update() {

assert(
mt.root
== poseidon2_hasher(&[
== poseidon2_hasher([
0xd98561fb02ca04d00801dfdc118b2a24cea0351963587712a28d368041370e1,
0x21447efbbddb57d6fc5ad24d906388492e82c44e5160425258dd4ea995e3a06e,
]),
Expand Down
10 changes: 5 additions & 5 deletions packages/merkle-trees/src/merkle/tree.nr
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl Calculator<Field> for MerkleTree {
left = node;
right = sibling;
}
node = (self.hasher)(&[left, right]);
node = (self.hasher)([left, right]);
}
}
node
Expand Down Expand Up @@ -54,15 +54,15 @@ impl Calculator<Field> for MerkleTree {
}

if (index_bits[i] == 0) {
root_with_leaf = (self.hasher)(&[root_with_leaf, sibling]);
root_with_leaf = (self.hasher)([root_with_leaf, sibling]);

if (root_without_leaf != sibling) {
root_without_leaf = (self.hasher)(&[root_without_leaf, sibling]);
root_without_leaf = (self.hasher)([root_without_leaf, sibling]);
}
} else {
root_with_leaf = (self.hasher)(&[sibling, root_with_leaf]);
root_with_leaf = (self.hasher)([sibling, root_with_leaf]);
if (root_without_leaf != sibling) {
root_without_leaf = (self.hasher)(&[sibling, root_without_leaf]);
root_without_leaf = (self.hasher)([sibling, root_without_leaf]);
}
};
}
Expand Down
33 changes: 21 additions & 12 deletions packages/merkle-trees/src/sparse_merkle.nr
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
use crate::{Creator, MembershipProver, Modifier, NonMembershipProver};
use crate::{MembershipProver, Modifier, NonMembershipProver, SMT_Creator};

mod tests;
mod tree;

struct SparseMerkleTree {
root: Field,
hasher: fn([Field]) -> Field,
leaf_hasher: fn([Field; 3]) -> Field,
hasher: fn([Field; 2]) -> Field,
}

impl Creator for SparseMerkleTree {
fn default(root: Field, hasher: fn([Field]) -> Field) -> Self {
Self { root, hasher }
impl SMT_Creator for SparseMerkleTree {
fn default(
root: Field,
leaf_hasher: fn([Field; 3]) -> Field,
hasher: fn([Field; 2]) -> Field,
) -> Self {
Self {
root,
leaf_hasher,
hasher,
}
}
}

Expand Down Expand Up @@ -50,7 +59,7 @@ impl Modifier<(Field, Field), [Field]> for SparseMerkleTree {
// otherwise the correctness of the old root is validated based on the siblings after which
// the new root is calculated and returned
if (self.root == 0) {
self.root = (self.hasher)(&[new_entry.0, new_entry.1, 1]);
self.root = (self.leaf_hasher)([new_entry.0, new_entry.1, 1]);
} else {
let (old, new) = self.calculate_two_roots(new_entry, (new_entry.0, siblings));
assert(old == self.root);
Expand All @@ -72,21 +81,21 @@ impl Modifier<(Field, Field), [Field]> for SparseMerkleTree {
let path = key.to_be_bits(siblings.len() as u32);

// old_parent is a container to temporarily store the nodes that ultimately lead to the OLD root
let mut old_parent: Field = (self.hasher)(&[key, old_value, 1]);
let mut old_parent: Field = (self.leaf_hasher)([key, old_value, 1]);
// new_parent is a container to temporarily store the nodes that ultimately lead to the NEW root
let mut new_parent: Field = (self.hasher)(&[key, new_value, 1]);
let mut new_parent: Field = (self.leaf_hasher)([key, new_value, 1]);
// starting from the bottom of the tree, for each level it checks whether there is a sibling and if
// that is the case, it hashes the two containers with the sibling and updates the containers with the
// resulting hashes until the uppermost level is reached aka the root node
for i in 0..siblings.len() {
let sibling = siblings[i];
if sibling != 0 {
if path[i] == 0 {
new_parent = (self.hasher)(&[new_parent, sibling]);
old_parent = (self.hasher)(&[old_parent, sibling]);
new_parent = (self.hasher)([new_parent, sibling]);
old_parent = (self.hasher)([old_parent, sibling]);
} else {
new_parent = (self.hasher)(&[sibling, new_parent]);
old_parent = (self.hasher)(&[sibling, old_parent]);
new_parent = (self.hasher)([sibling, new_parent]);
old_parent = (self.hasher)([sibling, old_parent]);
}
}
}
Expand Down
24 changes: 14 additions & 10 deletions packages/merkle-trees/src/sparse_merkle/tests/pedersen.nr
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
use crate::globals::TREE_DEPTH;
use crate::sparse_merkle::SparseMerkleTree;
use dep::std::hash::pedersen_hash_slice;
use dep::std::hash::pedersen_hash;

fn pedersen_hasher(leaves: [Field]) -> Field {
pedersen_hash_slice(leaves)
fn pedersen_hasher(leaves: [Field; 2]) -> Field {
pedersen_hash(leaves)
}

fn leaf_pedersen_hasher(leaves: [Field; 3]) -> Field {
pedersen_hash(leaves)
}

#[test]
fn test_verify_membership_proof() {
let root = 10163036226218365628416274178218539053881692695713984759452839539868301499377;
let smt = SparseMerkleTree::from(root, pedersen_hasher);
let smt = SparseMerkleTree::from(root, leaf_pedersen_hasher, pedersen_hasher);

let key = 20438969296305830531522370305156029982566273432331621236661483041446048135547;
let value = 17150136040889237739751319962368206600863150289695545292530539263327413090784;
Expand All @@ -25,7 +29,7 @@ fn test_verify_membership_proof() {
#[test]
fn test_verify_non_membership_proof() {
let root = 10163036226218365628416274178218539053881692695713984759452839539868301499377;
let smt = SparseMerkleTree::from(root, pedersen_hasher);
let smt = SparseMerkleTree::from(root, leaf_pedersen_hasher, pedersen_hasher);

let key = 20438969296305830531522380305156029982566273432331621236661483041446048135547;
let value = 17150136040889237739751319962368206600863250289695545292530539263327413090784;
Expand All @@ -44,7 +48,7 @@ fn test_verify_non_membership_proof() {

#[test]
fn test_add_first_element() {
let mut smt = SparseMerkleTree::new(pedersen_hasher);
let mut smt = SparseMerkleTree::new(leaf_pedersen_hasher, pedersen_hasher);

let key = 20438969296305830531522370305156029982566273432331621236661483041446048135547;
let value = 17150136040889237739751319962368206600863150289695545292530539263327413090784;
Expand All @@ -60,7 +64,7 @@ fn test_add_first_element() {
#[test]
fn test_add_element_to_one_element_tree() {
let root = 2422708535743783816563452741494007579003622904961533867614614610167375232032;
let mut smt = SparseMerkleTree::from(root, pedersen_hasher);
let mut smt = SparseMerkleTree::from(root, leaf_pedersen_hasher, pedersen_hasher);

let key = 7374494214024125590767526270082639043919066776944047470878693145844636921798;
let value = 3315292394704439116482935361251007857050519763420264982454883186141315324846;
Expand All @@ -77,7 +81,7 @@ fn test_add_element_to_one_element_tree() {
#[test]
fn test_add_element_to_existing_tree() {
let root = 13995819305603022633355680906127521476353407789113491617487780281225566393218;
let mut smt = SparseMerkleTree::from(root, pedersen_hasher);
let mut smt = SparseMerkleTree::from(root, leaf_pedersen_hasher, pedersen_hasher);

let key = 12467743582502009806452203915647380852106587605639139696405928234368558796420;
let value = 7661601117049907361128926075270530269257730340678343102988736234309528818274;
Expand All @@ -95,7 +99,7 @@ fn test_add_element_to_existing_tree() {
#[test]
fn test_delete() {
let root = 8307334591379324778417663235463648615723981385559143500721691536202573318888;
let mut smt = SparseMerkleTree::from(root, pedersen_hasher);
let mut smt = SparseMerkleTree::from(root, leaf_pedersen_hasher, pedersen_hasher);

let key = 12467743582502009806452203915647380852106587605639139696405928234368558796420;
let value = 7661601117049907361128926075270530269257730340678343102988736234309528818274;
Expand All @@ -113,7 +117,7 @@ fn test_delete() {
#[test]
fn test_update() {
let root = 8307334591379324778417663235463648615723981385559143500721691536202573318888;
let mut smt = SparseMerkleTree::from(root, pedersen_hasher);
let mut smt = SparseMerkleTree::from(root, leaf_pedersen_hasher, pedersen_hasher);

let key = 12467743582502009806452203915647380852106587605639139696405928234368558796420;
let old_value = 7661601117049907361128926075270530269257730340678343102988736234309528818274;
Expand Down
Loading

0 comments on commit d546dd4

Please sign in to comment.