Skip to content

Commit e45b5a1

Browse files
committed
calc merkle root
1 parent 1c5e211 commit e45b5a1

File tree

8 files changed

+169
-105
lines changed

8 files changed

+169
-105
lines changed

app/proof_of_reserve/Prover.toml

Whitespace-only changes.

app/proof_of_reserve/src/constants.nr

Lines changed: 0 additions & 5 deletions
This file was deleted.

app/proof_of_reserve/src/main.nr

Lines changed: 0 additions & 99 deletions
This file was deleted.

app/proof_of_reserve/Nargo.toml renamed to circuits/app/proof_of_reserve/Nargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ type = "bin"
44

55
[dependencies]
66
trees = { git = "https://github.com/privacy-scaling-explorations/zk-kit.noir", tag = "merkle-trees-v0.0.1", directory = "packages/merkle-trees" }
7-
sha256 = { tag = "v0.1.0", git = "https://github.com/noir-lang/sha256" }
7+
sha256 = { tag = "v0.1.0", git = "https://github.com/noir-lang/sha256" }
8+
utils = { path = "../../crates/utils" }
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
[[coins_database]]
2+
amount = "124123"
3+
script_pub_key = ["0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"]
4+
5+
[[coins_database]]
6+
amount = "124123"
7+
script_pub_key = ["0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"]
8+
9+
[[coins_database]]
10+
amount = "124123"
11+
script_pub_key = ["0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"]
12+
13+
[[coins_database]]
14+
amount = "124123"
15+
script_pub_key = ["0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"]
16+
17+
[[coins_database]]
18+
amount = "124123"
19+
script_pub_key = ["0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"]
20+
21+
[[coins_database]]
22+
amount = "124123"
23+
script_pub_key = ["0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"]
24+
25+
[[coins_database]]
26+
amount = "124123"
27+
script_pub_key = ["0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"]
28+
29+
[[coins_database]]
30+
amount = "124123"
31+
script_pub_key = ["0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"]
32+
33+
[[coins_database]]
34+
amount = "124123"
35+
script_pub_key = ["0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"]
36+
37+
[[coins_database]]
38+
amount = "124123"
39+
script_pub_key = ["0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pub global MAX_COINS_DATABASE_AMOUNT: u32 = 10;
2+
pub global MAX_USER_UTXOS_AMOUNT: u32 = 2;
3+
4+
pub global MAX_MERKLE_TREE_LEVELS: u32 = 6;
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
mod constants;
2+
3+
use constants::{MAX_COINS_DATABASE_AMOUNT, MAX_MERKLE_TREE_LEVELS, MAX_USER_UTXOS_AMOUNT};
4+
use std::cmp::max;
5+
use trees::merkle::{MerkleTree, Modifier, MT_Creator};
6+
use utils::convert::u64_to_be_bytes;
7+
8+
// p2pkh
9+
struct COINS_DATABASE_ELEMENT {
10+
script_pub_key: [u8; 25],
11+
amount: u64,
12+
}
13+
14+
struct UTXO {
15+
index: u64,
16+
script_sig: [u8; 140],
17+
}
18+
19+
fn main(
20+
//const_message: pub [u8; 32],
21+
coins_database: [COINS_DATABASE_ELEMENT; MAX_COINS_DATABASE_AMOUNT],
22+
//own_utxo: [UTXO; MAX_USER_UTXOS_AMOUNT],
23+
) -> pub ([u8; 32], Field) {
24+
let mut utxos = [[0; 32]; MAX_COINS_DATABASE_AMOUNT];
25+
26+
for i in 0..MAX_COINS_DATABASE_AMOUNT {
27+
// There is a bug with append, so we fill the array manually
28+
let mut utx_bytes = [0; 33];
29+
30+
let amount_bytes = u64_to_be_bytes(coins_database[i].amount);
31+
for j in 0..8 {
32+
utx_bytes[j] = amount_bytes[j];
33+
}
34+
35+
for j in 0..25 {
36+
utx_bytes[j + 8] = coins_database[i].script_pub_key[j];
37+
}
38+
39+
utxos[i] = if coins_database[i].amount != 0 {
40+
sha256::digest(utx_bytes)
41+
} else {
42+
[0; 32]
43+
};
44+
}
45+
46+
(merkle_root(utxos), 0)
47+
}
48+
49+
// fn hasher(leaves: [[u8; 32]; 2]) -> [u8; 32] {
50+
// hash(leaves[0], leaves[1])
51+
// }
52+
53+
// fn merkle_root_lib<let N: u32>(mut leafs: [[u8; 32]; N]) -> [u8; 32] {
54+
// let mut mt = MerkleTree::new(hasher);
55+
// mt.add([1; 32], 0, []);
56+
// mt.add([2; 32], 1, [[1; 32]]);
57+
// let root = mt.root;
58+
// mt.add([3; 32], 1, [root]);
59+
// mt.add([4; 32], 3, [[3; 32], [248, 24, 175, 211, 122, 109, 195, 188, 146, 251, 68, 115, 16, 17, 39, 112, 6, 219, 78, 250, 110, 144, 35, 205, 116, 104, 192, 35, 53, 210, 42, 77]]);
60+
61+
// [0; 32]
62+
// }
63+
64+
fn hash(leaf1: [u8; 32], leaf2: [u8; 32]) -> [u8; 32] {
65+
sha256::digest(leaf1.as_slice().append(leaf2).as_array::<64>())
66+
}
67+
68+
fn merkle_root<let N: u32>(mut leafs: [[u8; 32]; N]) -> [u8; 32] {
69+
assert((N as Field).lt(2.pow_32(MAX_MERKLE_TREE_LEVELS as Field)));
70+
let mut mtree = [[0; 32]; MAX_MERKLE_TREE_LEVELS];
71+
let mut last_level = 0;
72+
73+
for i in 0..N {
74+
let mut leaf = leafs[i];
75+
for j in 0..MAX_MERKLE_TREE_LEVELS {
76+
if !leaf.all(|e| e == 0) {
77+
if mtree[j].all(|e| e == 0) {
78+
mtree[j] = leaf;
79+
leaf = [0; 32];
80+
} else {
81+
leaf = hash(mtree[j], leaf);
82+
mtree[j] = [0; 32];
83+
}
84+
last_level = max(j, last_level);
85+
}
86+
}
87+
}
88+
89+
let mut tmp_node = [0; 32];
90+
for i in 0..MAX_MERKLE_TREE_LEVELS {
91+
if (
92+
(i < last_level & tmp_node.all(|e| e == 0))
93+
| (i <= last_level & !tmp_node.all(|e| e == 0))
94+
) {
95+
if !mtree[i].all(|e| e == 0) {
96+
let right = if tmp_node.all(|e| e == 0) {
97+
mtree[i]
98+
} else {
99+
tmp_node
100+
};
101+
102+
tmp_node = hash(mtree[i], right);
103+
mtree[i] = [0; 32];
104+
} else if !tmp_node.all(|e| e == 0) {
105+
tmp_node = hash(tmp_node, tmp_node);
106+
}
107+
}
108+
}
109+
110+
if tmp_node.all(|e| e == 0) {
111+
mtree[last_level]
112+
} else {
113+
tmp_node
114+
}
115+
}

circuits/crates/utils/src/convert.nr

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ pub fn u32_to_be_bytes(value: u32) -> [u8; 4] {
1919
res
2020
}
2121

22+
pub fn u64_to_be_bytes(value: u64) -> [u8; 8] {
23+
let mut res = [0; 8];
24+
for i in 0..8 {
25+
res[i] = (value >> ((7 - i) << 3)) as u8;
26+
}
27+
28+
res
29+
}
30+
2231
pub fn sha256_to_le_bytes(value: str<64>) -> [u8; 32] {
2332
let mut res = [0; 32];
2433
let value = value.as_bytes();

0 commit comments

Comments
 (0)