|
1 | | -mod constants; |
2 | | - |
3 | | -use constants::MAX_MERKLE_TREE_LEVELS; |
4 | | -use std::cmp::max; |
5 | | - |
6 | | -fn main() { |
7 | | - // let mut utxos = [[0; 32]; MAX_COINS_DATABASE_AMOUNT]; |
8 | | - |
9 | | - // for i in 0..MAX_COINS_DATABASE_AMOUNT { |
10 | | - // // There is a bug with append, so we fill the array manually |
11 | | - // let mut utx_bytes = [0; 33]; |
12 | | - |
13 | | - // let amount_bytes = u64_to_be_bytes(coins_database[i].amount); |
14 | | - // for j in 0..8 { |
15 | | - // utx_bytes[j] = amount_bytes[j]; |
16 | | - // } |
17 | | - |
18 | | - // for j in 0..25 { |
19 | | - // utx_bytes[j + 8] = coins_database[i].script_pub_key[j]; |
20 | | - // } |
21 | | - |
22 | | - // utxos[i] = if coins_database[i].amount != 0 { |
23 | | - // sha256::digest(utx_bytes) |
24 | | - // } else { |
25 | | - // [0; 32] |
26 | | - // }; |
27 | | - // } |
28 | | - |
29 | | - // (merkle_root(utxos), 0) |
30 | | -} |
| 1 | +// Second proof |
| 2 | +// Build node from one chunk leafs |
31 | 3 |
|
32 | | -fn hash(leaf1: [u8; 32], leaf2: [u8; 32]) -> [u8; 32] { |
33 | | - sha256::digest(leaf1.as_slice().append(leaf2).as_array::<64>()) |
34 | | -} |
35 | | - |
36 | | -fn merkle_root<let N: u32>(mut leafs: [[u8; 32]; N]) -> [u8; 32] { |
37 | | - assert((N as Field).lt(2.pow_32(MAX_MERKLE_TREE_LEVELS as Field))); |
38 | | - let mut mtree = [[0; 32]; MAX_MERKLE_TREE_LEVELS]; |
39 | | - let mut last_level = 0; |
40 | | - |
41 | | - for i in 0..N { |
42 | | - let mut leaf = leafs[i]; |
43 | | - for j in 0..MAX_MERKLE_TREE_LEVELS { |
44 | | - if !leaf.all(|e| e == 0) { |
45 | | - if mtree[j].all(|e| e == 0) { |
46 | | - mtree[j] = leaf; |
47 | | - leaf = [0; 32]; |
48 | | - } else { |
49 | | - leaf = hash(mtree[j], leaf); |
50 | | - mtree[j] = [0; 32]; |
51 | | - } |
52 | | - last_level = max(j, last_level); |
53 | | - } |
54 | | - } |
55 | | - } |
56 | | - |
57 | | - let mut tmp_node = [0; 32]; |
58 | | - for i in 0..MAX_MERKLE_TREE_LEVELS { |
59 | | - if ( |
60 | | - (i < last_level & tmp_node.all(|e| e == 0)) |
61 | | - | (i <= last_level & !tmp_node.all(|e| e == 0)) |
62 | | - ) { |
63 | | - if !mtree[i].all(|e| e == 0) { |
64 | | - let right = if tmp_node.all(|e| e == 0) { |
65 | | - mtree[i] |
66 | | - } else { |
67 | | - tmp_node |
68 | | - }; |
| 4 | +mod constants; |
69 | 5 |
|
70 | | - tmp_node = hash(mtree[i], right); |
71 | | - mtree[i] = [0; 32]; |
72 | | - } else if !tmp_node.all(|e| e == 0) { |
73 | | - tmp_node = hash(tmp_node, tmp_node); |
74 | | - } |
| 6 | +use constants::{ |
| 7 | + HONK_IDENTIFIER, HONK_PROOF_SIZE, HONK_VK_SIZE, MAX_COINS_DATABASE_AMOUNT, |
| 8 | + MAX_MERKLE_TREE_LEVELS, PUBLIC_INPUTS, |
| 9 | +}; |
| 10 | +use utils::merkle_root::merkle_root; |
| 11 | + |
| 12 | +fn main( |
| 13 | + verification_key: [Field; HONK_VK_SIZE], |
| 14 | + proof: [Field; HONK_PROOF_SIZE], |
| 15 | + public_inputs: [Field; PUBLIC_INPUTS], |
| 16 | +) -> pub ([u8; 32], u64) { |
| 17 | + std::verify_proof_with_type(verification_key, proof, public_inputs, 0x0, HONK_IDENTIFIER); |
| 18 | + let mut leafs = [[0; 32]; MAX_COINS_DATABASE_AMOUNT]; |
| 19 | + |
| 20 | + for i in 0..MAX_COINS_DATABASE_AMOUNT { |
| 21 | + for j in 0..32 { |
| 22 | + leafs[i][j] = public_inputs[i * 32 + 32 + j] as u8; |
75 | 23 | } |
76 | 24 | } |
77 | 25 |
|
78 | | - if tmp_node.all(|e| e == 0) { |
79 | | - mtree[last_level] |
80 | | - } else { |
81 | | - tmp_node |
82 | | - } |
| 26 | + ( |
| 27 | + merkle_root::<MAX_COINS_DATABASE_AMOUNT, MAX_MERKLE_TREE_LEVELS>(leafs), |
| 28 | + public_inputs[MAX_COINS_DATABASE_AMOUNT * 32 + 32] as u64, |
| 29 | + ) |
83 | 30 | } |
0 commit comments