Skip to content

Commit 990edbe

Browse files
committed
check p2pkh utxo spending and acumulate amount
1 parent e45b5a1 commit 990edbe

File tree

12 files changed

+221
-167
lines changed

12 files changed

+221
-167
lines changed

circuits/Nargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ members = [
1717
"app/p2sh_p2wsh",
1818
"app/blocks_recursive/recursive_base",
1919
"app/blocks_recursive/recursive",
20-
"app/proof_of_reserve",
20+
"app/proof_of_reserve/coins",
21+
"app/proof_of_reserve/utxos_tree",
2122
]

circuits/app/proof_of_reserve/Nargo.toml

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

circuits/app/proof_of_reserve/Prover.toml

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "coins"
3+
type = "bin"
4+
5+
[dependencies]
6+
sha256 = { tag = "v0.1.0", git = "https://github.com/noir-lang/sha256" }
7+
ripemd160 = { tag = "v0.0.3", git = "https://github.com/distributed-lab/noir-ripemd160" }
8+
utils = { path = "../../../crates/utils" }
9+
crypto = { path = "../../../crates/crypto" }
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const_message_hash = ["0x2c","0xf2","0x4d","0xba","0x5f","0xb0","0xa3","0x0e","0x26","0xe8","0x3b","0x2a","0xc5","0xb9","0xe2","0x9e","0x1b","0x16","0x1e","0x5c","0x1f","0xa7","0x42","0x5e","0x73","0x04","0x33","0x62","0x93","0x8b","0x98","0x24"]
2+
3+
[[coins_database]]
4+
amount = "6094154"
5+
script_pub_key = ["0x76","0xa9","0x14","0xa7","0x93","0x36","0xda","0x4a","0xda","0x61","0x5f","0x0d","0x20","0x3f","0xc4","0xf0","0x95","0x6b","0x8e","0x6e","0xd7","0xf7","0x8b","0x88","0xac"]
6+
7+
[[coins_database]]
8+
amount = "2238282"
9+
script_pub_key = ["0x76","0xa9","0x14","0xa7","0x93","0x36","0xda","0x4a","0xda","0x61","0x5f","0x0d","0x20","0x3f","0xc4","0xf0","0x95","0x6b","0x8e","0x6e","0xd7","0xf7","0x8b","0x88","0xac"]
10+
11+
[[coins_database]]
12+
amount = "1103741"
13+
script_pub_key = ["0x76","0xa9","0x14","0xa7","0x93","0x36","0xda","0x4a","0xda","0x61","0x5f","0x0d","0x20","0x3f","0xc4","0xf0","0x95","0x6b","0x8e","0x6e","0xd7","0xf7","0x8b","0x88","0xac"]
14+
15+
[[own_utxos]]
16+
pub_key = ["0x03","0xfa","0x81","0x48","0x41","0xe2","0x55","0x0b","0x88","0x24","0xa9","0x82","0xe9","0xfc","0x76","0xb6","0xd6","0x7f","0x7d","0x60","0x6f","0x03","0xd5","0x79","0xfe","0xa3","0x42","0x6b","0x1b","0x33","0x3b","0x07","0xd8","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00"]
17+
witness = [
18+
"0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00",
19+
"0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00",
20+
"0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00",
21+
"0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00",
22+
"0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00",
23+
"0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00",
24+
"0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00",
25+
"0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00",
26+
"0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00"
27+
]
28+
29+
[[own_utxos]]
30+
pub_key = ["0x03","0xfa","0x81","0x48","0x41","0xe2","0x55","0x0b","0x88","0x24","0xa9","0x82","0xe9","0xfc","0x76","0xb6","0xd6","0x7f","0x7d","0x60","0x6f","0x03","0xd5","0x79","0xfe","0xa3","0x42","0x6b","0x1b","0x33","0x3b","0x07","0xd8","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00"]
31+
witness = ["0x30","0x45","0x02","0x21","0x00","0xba","0x29","0x7c","0xea","0x0d","0x9c","0xf8","0xc1","0xcd","0xd3","0xb3","0x4a","0xe1","0xec","0x95","0x9c","0x9b","0x8d","0x46","0x7b","0x41","0xdb","0x0a","0x39","0x54","0x3e","0x32","0x9a","0xe9","0x3a","0x59","0x4e","0x02","0x20","0x0a","0xb1","0xea","0xed","0x72","0x13","0xa9","0x58","0xba","0xe5","0x0e","0x7f","0x4e","0x7a","0xca","0xd5","0x51","0xd6","0x9d","0x7b","0xcb","0x9a","0xec","0xaf","0x36","0xc0","0xa7","0x0f","0xca","0x41","0x7c","0xf9","0x00"]
32+
33+
[[own_utxos]]
34+
pub_key = ["0x03","0xfa","0x81","0x48","0x41","0xe2","0x55","0x0b","0x88","0x24","0xa9","0x82","0xe9","0xfc","0x76","0xb6","0xd6","0x7f","0x7d","0x60","0x6f","0x03","0xd5","0x79","0xfe","0xa3","0x42","0x6b","0x1b","0x33","0x3b","0x07","0xd8","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00"]
35+
witness = ["0x30","0x45","0x02","0x21","0x00","0xba","0x29","0x7c","0xea","0x0d","0x9c","0xf8","0xc1","0xcd","0xd3","0xb3","0x4a","0xe1","0xec","0x95","0x9c","0x9b","0x8d","0x46","0x7b","0x41","0xdb","0x0a","0x39","0x54","0x3e","0x32","0x9a","0xe9","0x3a","0x59","0x4e","0x02","0x20","0x0a","0xb1","0xea","0xed","0x72","0x13","0xa9","0x58","0xba","0xe5","0x0e","0x7f","0x4e","0x7a","0xca","0xd5","0x51","0xd6","0x9d","0x7b","0xcb","0x9a","0xec","0xaf","0x36","0xc0","0xa7","0x0f","0xca","0x41","0x7c","0xf9","0x00"]
36+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub global MAX_COINS_DATABASE_AMOUNT: u32 = 3;
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
mod constants;
2+
3+
use constants::MAX_COINS_DATABASE_AMOUNT;
4+
use crypto::ecdsa::{
5+
ecdsa_verify, get_rs_from_signature, get_xy_from_compressed_pubkey,
6+
get_xy_from_uncompressed_pubkey,
7+
};
8+
use utils::convert::u64_to_be_bytes;
9+
10+
// p2pkh
11+
struct CoinsDatabaseElement {
12+
script_pub_key: [u8; 25],
13+
amount: u64,
14+
}
15+
16+
struct OwnUtxo {
17+
witness: [u8; 72],
18+
pub_key: [u8; 65],
19+
}
20+
21+
fn main(
22+
const_message_hash: pub [u8; 32],
23+
coins_database: [CoinsDatabaseElement; MAX_COINS_DATABASE_AMOUNT],
24+
own_utxos: [OwnUtxo; MAX_COINS_DATABASE_AMOUNT],
25+
) -> pub ([[u8; 32]; MAX_COINS_DATABASE_AMOUNT], u64) {
26+
let mut owned_amount = 0;
27+
let mut coins_hashes = [[0; 32]; MAX_COINS_DATABASE_AMOUNT];
28+
29+
for i in 0..MAX_COINS_DATABASE_AMOUNT {
30+
if !own_utxos[i].witness.all(|e| e == 0) {
31+
let signature = get_rs_from_signature(own_utxos[i].witness);
32+
let mut key_hash = [0; 20];
33+
34+
let (x, y) = if own_utxos[i].pub_key[0] == 4 {
35+
key_hash = ripemd160::ripemd160(sha256::digest(own_utxos[i].pub_key));
36+
get_xy_from_uncompressed_pubkey(own_utxos[i].pub_key)
37+
} else {
38+
let mut c_key = [0; 33];
39+
for j in 0..33 {
40+
c_key[j] = own_utxos[i].pub_key[j]
41+
}
42+
43+
key_hash = ripemd160::ripemd160(sha256::digest(c_key));
44+
get_xy_from_compressed_pubkey(c_key)
45+
};
46+
47+
let mut hash_in_spk = [0; 20];
48+
for j in 0..20 {
49+
hash_in_spk[j] = coins_database[i].script_pub_key[j + 3];
50+
}
51+
52+
assert(
53+
key_hash == hash_in_spk,
54+
"The specified public key does not match the key in the script_pub_key",
55+
);
56+
57+
if ecdsa_verify(x, y, signature, const_message_hash) {
58+
owned_amount += coins_database[i].amount;
59+
} else {
60+
assert(false, f"ECDSA signature check failed on utxo with index {i}");
61+
}
62+
}
63+
64+
let mut utx_bytes = [0; 33];
65+
66+
let amount_bytes = u64_to_be_bytes(coins_database[i].amount);
67+
for j in 0..8 {
68+
utx_bytes[j] = amount_bytes[j];
69+
}
70+
71+
for j in 0..25 {
72+
utx_bytes[j + 8] = coins_database[i].script_pub_key[j];
73+
}
74+
75+
coins_hashes[i] = if coins_database[i].amount != 0 {
76+
sha256::digest(utx_bytes)
77+
} else {
78+
[0; 32]
79+
};
80+
}
81+
82+
(coins_hashes, owned_amount)
83+
}

circuits/app/proof_of_reserve/src/constants.nr

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

circuits/app/proof_of_reserve/src/main.nr

Lines changed: 0 additions & 115 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "utxos_tree"
3+
type = "bin"
4+
5+
[dependencies]
6+
sha256 = { tag = "v0.1.0", git = "https://github.com/noir-lang/sha256" }

0 commit comments

Comments
 (0)