Skip to content

Commit ae4435e

Browse files
committed
improve code
1 parent 7f323aa commit ae4435e

File tree

9 files changed

+47
-86
lines changed

9 files changed

+47
-86
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
pub global MAX_COINS_DATABASE_AMOUNT: u32 = 3;
2+
pub global MAX_MERKLE_TREE_LEVELS: u32 = 3;
3+
4+
pub global SHA256_HASH_SIZE: u32 = 32;
5+
pub global RIPEMD160_HASH_SIZE: u32 = 20;

circuits/app/proof_of_reserve/coins/src/main.nr

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33

44
mod constants;
55

6-
use constants::MAX_COINS_DATABASE_AMOUNT;
6+
use constants::{
7+
MAX_COINS_DATABASE_AMOUNT, MAX_MERKLE_TREE_LEVELS, RIPEMD160_HASH_SIZE, SHA256_HASH_SIZE,
8+
};
79
use crypto::ecdsa::{
810
ecdsa_verify, get_rs_from_signature, get_xy_from_compressed_pubkey,
911
get_xy_from_uncompressed_pubkey,
1012
};
11-
use utils::convert::u64_to_be_bytes;
13+
use utils::merkle_root::merkle_root;
1214

1315
// p2pkh
1416
struct CoinsDatabaseElement {
@@ -22,17 +24,17 @@ struct OwnUtxo {
2224
}
2325

2426
fn main(
25-
const_message_hash: pub [u8; 32],
27+
const_message_hash: pub [u8; SHA256_HASH_SIZE],
2628
coins_database: [CoinsDatabaseElement; MAX_COINS_DATABASE_AMOUNT],
2729
own_utxos: [OwnUtxo; MAX_COINS_DATABASE_AMOUNT],
28-
) -> pub ([[u8; 32]; MAX_COINS_DATABASE_AMOUNT], u64) {
30+
) -> pub ([u8; SHA256_HASH_SIZE], u64) {
2931
let mut owned_amount = 0;
30-
let mut coins_hashes = [[0; 32]; MAX_COINS_DATABASE_AMOUNT];
32+
let mut coins_hashes = [[0; SHA256_HASH_SIZE]; MAX_COINS_DATABASE_AMOUNT];
3133

3234
for i in 0..MAX_COINS_DATABASE_AMOUNT {
3335
if !own_utxos[i].witness.all(|e| e == 0) {
3436
let signature = get_rs_from_signature(own_utxos[i].witness);
35-
let mut key_hash = [0; 20];
37+
let mut key_hash = [0; RIPEMD160_HASH_SIZE];
3638

3739
let (x, y) = if own_utxos[i].pub_key[0] == 4 {
3840
key_hash = ripemd160::ripemd160(sha256::digest(own_utxos[i].pub_key));
@@ -47,8 +49,8 @@ fn main(
4749
get_xy_from_compressed_pubkey(c_key)
4850
};
4951

50-
let mut hash_in_spk = [0; 20];
51-
for j in 0..20 {
52+
let mut hash_in_spk = [0; RIPEMD160_HASH_SIZE];
53+
for j in 0..RIPEMD160_HASH_SIZE {
5254
hash_in_spk[j] = coins_database[i].script_pub_key[j + 3];
5355
}
5456

@@ -66,7 +68,7 @@ fn main(
6668

6769
let mut utx_bytes = [0; 33];
6870

69-
let amount_bytes = u64_to_be_bytes(coins_database[i].amount);
71+
let amount_bytes = Field::to_le_bytes::<8>(coins_database[i].amount as Field);
7072
for j in 0..8 {
7173
utx_bytes[j] = amount_bytes[j];
7274
}
@@ -78,9 +80,9 @@ fn main(
7880
coins_hashes[i] = if coins_database[i].amount != 0 {
7981
sha256::digest(utx_bytes)
8082
} else {
81-
[0; 32]
83+
[0; SHA256_HASH_SIZE]
8284
};
8385
}
8486

85-
(coins_hashes, owned_amount)
87+
(merkle_root::<MAX_COINS_DATABASE_AMOUNT, MAX_MERKLE_TREE_LEVELS>(coins_hashes), owned_amount)
8688
}

circuits/app/proof_of_reserve/nodes_tree/Nargo.toml

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

circuits/app/proof_of_reserve/nodes_tree/src/constants.nr

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

circuits/app/proof_of_reserve/nodes_tree/src/main.nr

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

circuits/app/proof_of_reserve/utxos_tree/Nargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ name = "utxos_tree"
33
type = "bin"
44

55
[dependencies]
6-
utils = { path = "../../../crates/utils" }
6+
utils = { path = "../../../crates/utils" }
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
pub global MAX_COINS_DATABASE_AMOUNT: u32 = 3;
21
pub global MAX_MERKLE_TREE_LEVELS: u32 = 3;
2+
pub global MAX_NODES_AMOUNT: u32 = 3;
33

44
pub global HONK_VK_SIZE: u32 = 128;
55
pub global HONK_PROOF_SIZE: u32 = 456;
66
pub global HONK_IDENTIFIER: u32 = 1;
7-
pub global PUBLIC_INPUTS: u32 = 32 + 32 * MAX_COINS_DATABASE_AMOUNT + 1;
7+
pub global PUBLIC_INPUTS_SIZE: u32 = 65;
8+
9+
pub global SHA256_HASH_SIZE: u32 = 32;
Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,40 @@
11
// Second proof
2-
// Build node from one chunk leafs
2+
// Build root from many nodes
33

44
mod constants;
55

66
use constants::{
7-
HONK_IDENTIFIER, HONK_PROOF_SIZE, HONK_VK_SIZE, MAX_COINS_DATABASE_AMOUNT,
8-
MAX_MERKLE_TREE_LEVELS, PUBLIC_INPUTS,
7+
HONK_IDENTIFIER, HONK_PROOF_SIZE, HONK_VK_SIZE, MAX_MERKLE_TREE_LEVELS, MAX_NODES_AMOUNT,
8+
PUBLIC_INPUTS_SIZE, SHA256_HASH_SIZE,
99
};
1010
use utils::merkle_root::merkle_root;
1111

12+
struct NodeProof {
13+
proof: [Field; HONK_PROOF_SIZE],
14+
public_inputs: [Field; PUBLIC_INPUTS_SIZE],
15+
}
16+
1217
fn main(
1318
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+
node_proofs: [NodeProof; MAX_NODES_AMOUNT],
20+
) -> pub ([u8; SHA256_HASH_SIZE], u64) {
21+
let mut nodes = [[0; SHA256_HASH_SIZE]; MAX_NODES_AMOUNT];
22+
let mut owned_amount = 0;
1923

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;
24+
for i in 0..MAX_NODES_AMOUNT {
25+
std::verify_proof_with_type(
26+
verification_key,
27+
node_proofs[i].proof,
28+
node_proofs[i].public_inputs,
29+
0x0,
30+
HONK_IDENTIFIER,
31+
);
32+
for j in 0..SHA256_HASH_SIZE {
33+
nodes[i][j] = node_proofs[i].public_inputs[j + SHA256_HASH_SIZE] as u8;
2334
}
35+
36+
owned_amount += node_proofs[i].public_inputs[64] as u64;
2437
}
2538

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-
)
39+
(merkle_root::<MAX_NODES_AMOUNT, MAX_MERKLE_TREE_LEVELS>(nodes), owned_amount)
3040
}

circuits/crates/utils/src/convert.nr

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,6 @@ 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-
3122
pub fn sha256_to_le_bytes(value: str<64>) -> [u8; 32] {
3223
let mut res = [0; 32];
3324
let value = value.as_bytes();

0 commit comments

Comments
 (0)