-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.nr
80 lines (63 loc) · 1.85 KB
/
main.nr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use dep::std;
global DEPTH: Field = 4;
/// Hashing function
fn hash_1(input: Field) -> Field {
std::hash::poseidon::bn254::hash_1([input])
}
fn hash_2(left: Field, right: Field) -> Field {
std::hash::poseidon::bn254::hash_2([left, right])
}
/// Compute the merkle tree root
fn compute_merkle_root(key: [u1; DEPTH], leaf: Field, nodes: [Field; DEPTH]) -> Field {
// Start with the `leaf` node.
let mut node: Field = leaf;
for i in 0..DEPTH {
// Hash current node `node` with provided node `nodes[i]`
// to left or right with `nodes[i]` depending on `key`s i-th bit.
node = if (key[i] == 0) {
hash_2(node, nodes[i])
} else {
hash_2(nodes[i], node)
};
}
node
}
/// Main circuit.
fn main(
receiver: pub Field,
key: [u1; DEPTH],
secret: Field,
nullifier: pub Field,
nodes: [Field; DEPTH],
root: pub Field
) {
// Compute `leaf` using `secret`.
let leaf: Field = hash_1(secret + 1);
// Assert given `nullifier` is derived from `secret`.
assert(nullifier == hash_1(secret + 2));
// Ensure `leaf` is included in the merkle tree.
assert(compute_merkle_root(key, leaf, nodes) == root);
// Tie `receiver` into proof.
assert(receiver + leaf + nullifier != 0);
}
/// Tests
fn helper_get_zero_nodes() -> [Field; DEPTH] {
let mut nodes = [0; DEPTH];
let mut z = 0;
for d in 0..DEPTH {
z = hash_2(z, z);
nodes[d] = z;
}
nodes
}
#[test]
fn test_compute_merkle_root() {
// This should produce the same root
// for any `key` value on an empty tree.
let key = [0; DEPTH];
let nodes = helper_get_zero_nodes();
let leaf = nodes[0];
let root = hash_2(nodes[DEPTH - 1], nodes[DEPTH - 1]);
let c_root = compute_merkle_root(key, leaf, nodes);
assert(root == c_root);
}