Skip to content

Commit 3947664

Browse files
committed
final touches
1 parent 61f94e6 commit 3947664

File tree

18 files changed

+296
-219
lines changed

18 files changed

+296
-219
lines changed

ethereum/circuits/lib/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,25 @@ If you decide to use our Oracles - you don't need to provide all the data by han
2424
Here is a list of public functions that you should use if you decide to go the **oracles route**:
2525

2626
```rust
27-
pub fn get_header(chain_id: Field, block_number: u32) -> BlockHeaderPartial;
27+
pub fn get_header(chain_id: Field, block_number: u64) -> BlockHeaderPartial;
2828
```
2929

3030
```rust
31-
pub fn get_account(chain_id: Field, block_no: u32, address: Address) -> AccountWithinBlock;
31+
pub fn get_account(chain_id: Field, block_no: u64, address: Address) -> AccountWithinBlock;
3232
```
3333

3434
```rust
35-
pub fn get_account_with_storage(chain_id: Field, block_number: u32, address: Address, storage_key: Bytes32) -> StorageWithinBlock<1>;
35+
pub fn get_account_with_storage(chain_id: Field, block_number: u64, address: Address, storage_key: Bytes32) -> StorageWithinBlock<1>;
3636
```
3737

3838
```rust
39-
pub fn get_receipt<...>(chain_id: Field, block_number: u32, tx_idx: Field, ...) -> TxReceiptWithinBlock;
39+
pub fn get_receipt<...>(chain_id: Field, block_number: u64, tx_idx: Field, ...) -> TxReceiptWithinBlock;
4040
```
4141

4242
```rust
4343
pub fn get_transaction<MAX_DATA_LEN, ...>(
4444
chain_id: Field,
45-
block_number: u32,
45+
block_number: u64,
4646
tx_idx: Field,
4747
...
4848
) -> TransactionWithinBlock<MAX_DATA_LEN>;
@@ -51,7 +51,7 @@ pub fn get_transaction<MAX_DATA_LEN, ...>(
5151
```rust
5252
pub fn get_log<MAX_LOG_DATA_LEN, MAX_LOGS_COUNT>(
5353
chain_id: Field,
54-
block_number: u32,
54+
block_number: u64,
5555
tx_idx: Field,
5656
log_idx: u32
5757
) -> LogWithinBlock<MAX_LOG_DATA_LEN>;

ethereum/circuits/lib/src/account.nr

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use crate::account_with_storage::Account;
22
use crate::header::get_header;
3+
use crate::merkle_patricia_proofs::proof::ProofInput;
34
use crate::misc::types::{Address, Bytes32, BYTES32_LENGTH};
4-
use crate::verifiers::account::verify_account;
55
use crate::serde::Serde;
6-
use crate::merkle_patricia_proofs::proof::ProofInput;
6+
use crate::verifiers::account::verify_account;
77

88
global MAX_KEY_LEN: u32 = 32;
99
global MAX_PREFIXED_KEY_NIBBLE_LEN: u32 = 66; // (MAX_KEY_LEN + 1) * 2
@@ -27,7 +27,7 @@ type AccountWithStateProofM = (Account, ProofInput<MAX_PREFIXED_KEY_NIBBLE_LEN,
2727

2828
type ProofInputSerialized<let LEN: u32> = [Field; LEN];
2929

30-
pub fn get_account(chain_id: Field, block_no: u32, address: Address) -> AccountWithinBlock {
30+
pub fn get_account(chain_id: Field, block_no: u64, address: Address) -> AccountWithinBlock {
3131
let (account, state_proof) = get_account_unconstrained_M(chain_id, block_no, address);
3232
let header = get_header(chain_id, block_no);
3333
verify_account(address, account, state_proof, header.state_root);
@@ -37,11 +37,15 @@ pub fn get_account(chain_id: Field, block_no: u32, address: Address) -> AccountW
3737
#[oracle(get_account)]
3838
unconstrained fn get_account_oracle<let PROOF_INPUT_LEN: u32>(
3939
_chain_id: Field,
40-
_block_no: u32,
41-
_address: [u8; 20]
40+
_block_no: u64,
41+
_address: [u8; 20],
4242
) -> (Account, ProofInputSerialized<PROOF_INPUT_LEN>) {}
4343

44-
unconstrained fn get_account_unconstrained_M(chain_id: Field, block_no: u32, address: Address) -> AccountWithStateProofM {
44+
unconstrained fn get_account_unconstrained_M(
45+
chain_id: Field,
46+
block_no: u64,
47+
address: Address,
48+
) -> AccountWithStateProofM {
4549
let (account, proof_input) = get_account_oracle(chain_id, block_no, address);
4650
let proof_input = Serde::deserialize(proof_input);
4751

ethereum/circuits/lib/src/account_with_storage.nr

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
use crate::header::{get_header, BlockHeaderPartial};
2-
use crate::account::{MAX_ACCOUNT_STATE_LEN, MAX_ACCOUNT_DEPTH_NO_LEAF_M, MAX_ACCOUNT_LEAF_LEN};
3-
use crate::misc::{types::{Address, Bytes32, BYTES32_LENGTH, HASH_LEN}, bytes::right_pad, fragment::Fragment};
1+
use crate::account::{MAX_ACCOUNT_DEPTH_NO_LEAF_M, MAX_ACCOUNT_LEAF_LEN, MAX_ACCOUNT_STATE_LEN};
2+
use crate::header::{BlockHeaderPartial, get_header};
3+
use crate::merkle_patricia_proofs::proof::ProofInput;
4+
use crate::misc::{
5+
bytes::right_pad,
6+
fragment::Fragment,
7+
types::{Address, Bytes32, BYTES32_LENGTH, HASH_LEN},
8+
};
9+
use crate::rlp::decode::decode_string;
410
use crate::serde::Serde;
511
use crate::verifiers::account::verify_account;
612
use crate::verifiers::storage::verify_storage_values;
7-
use crate::merkle_patricia_proofs::proof::ProofInput;
8-
use crate::rlp::decode::decode_string;
913

1014
use dep::std::hash::keccak256;
1115

@@ -17,15 +21,18 @@ global MAX_STORAGE_VALUE_LEN: u32 = 32; // Values taken from storageProofConfig
1721
global MAX_STORAGE_LEAF_LEN: u32 = 69;
1822

1923
struct Account {
20-
nonce: u32,
24+
nonce: u64,
2125
balance: Field,
2226
storage_root: Bytes32,
2327
code_hash: Bytes32,
2428
}
2529

2630
impl Eq for Account {
2731
fn eq(self, other: Self) -> bool {
28-
(self.nonce == other.nonce) & (self.balance == other.balance) & (self.storage_root == other.storage_root) & (self.code_hash == other.code_hash)
32+
(self.nonce == other.nonce)
33+
& (self.balance == other.balance)
34+
& (self.storage_root == other.storage_root)
35+
& (self.code_hash == other.code_hash)
2936
}
3037
}
3138

@@ -34,7 +41,7 @@ impl Eq for Account {
3441
struct StateAndStorageProofInput {
3542
account: Account,
3643
state_proof_input: ProofInput<MAX_PREFIXED_KEY_NIBBLE_LEN, MAX_ACCOUNT_STATE_LEN, MAX_ACCOUNT_DEPTH_NO_LEAF_M, MAX_ACCOUNT_LEAF_LEN>,
37-
storage_proof_input: ProofInput<MAX_PREFIXED_KEY_NIBBLE_LEN, MAX_STORAGE_VALUE_LEN, MAX_STORAGE_DEPTH_NO_LEAF_M, MAX_STORAGE_LEAF_LEN>
44+
storage_proof_input: ProofInput<MAX_PREFIXED_KEY_NIBBLE_LEN, MAX_STORAGE_VALUE_LEN, MAX_STORAGE_DEPTH_NO_LEAF_M, MAX_STORAGE_LEAF_LEN>,
3845
}
3946

4047
struct StorageWithinBlock<let N: u32> {
@@ -47,22 +54,26 @@ type ProofInputSerialized<let LEN: u32> = [Field; LEN];
4754

4855
impl Eq for StorageWithinBlock<1> {
4956
fn eq(self, other: Self) -> bool {
50-
(self.block_hash == other.block_hash) & (self.account == other.account) & (self.values[0] == other.values[0])
57+
(self.block_hash == other.block_hash)
58+
& (self.account == other.account)
59+
& (self.values[0] == other.values[0])
5160
}
5261
}
5362

5463
fn assert_storage_key_equals(
5564
storage_key: Bytes32,
56-
storage_key_hash: [u8; MAX_PREFIXED_KEY_NIBBLE_LEN]
65+
storage_key_hash: [u8; MAX_PREFIXED_KEY_NIBBLE_LEN],
5766
) {
5867
let storage_key_hash_fragment = Fragment::new(
5968
MAX_PREFIXED_KEY_NIBBLE_LEN - HASH_LEN,
6069
HASH_LEN,
61-
storage_key_hash
70+
storage_key_hash,
6271
);
63-
let other_storage_key_hash_fragment = Fragment::from_array(keccak256(storage_key, BYTES32_LENGTH as u32));
72+
let other_storage_key_hash_fragment =
73+
Fragment::from_array(keccak256(storage_key, BYTES32_LENGTH as u32));
6474
assert(
65-
storage_key_hash_fragment.eq(other_storage_key_hash_fragment), "Storage key does not match the argument"
75+
storage_key_hash_fragment.eq(other_storage_key_hash_fragment),
76+
"Storage key does not match the argument",
6677
);
6778
}
6879

@@ -72,7 +83,9 @@ fn get_fragment<let N: u32>(left_padded_value: [u8; N]) -> Fragment<N, u8> {
7283
Fragment::new(value_offset, value_len, left_padded_value)
7384
}
7485

75-
fn get_storage_value(rlp_encoded_value: [u8; MAX_STORAGE_VALUE_LEN]) -> [u8; MAX_STORAGE_VALUE_LEN] {
86+
fn get_storage_value(
87+
rlp_encoded_value: [u8; MAX_STORAGE_VALUE_LEN],
88+
) -> [u8; MAX_STORAGE_VALUE_LEN] {
7689
let mut storage_value = get_fragment(rlp_encoded_value);
7790
let rlp_fragment = decode_string(storage_value);
7891
let rlp_header_len = rlp_fragment.offset;
@@ -89,36 +102,43 @@ fn get_storage_value(rlp_encoded_value: [u8; MAX_STORAGE_VALUE_LEN]) -> [u8; MAX
89102

90103
pub fn get_account_with_storage(
91104
chain_id: Field,
92-
block_number: u32,
105+
block_number: u64,
93106
address: Address,
94-
storage_key: Bytes32
107+
storage_key: Bytes32,
95108
) -> StorageWithinBlock<1> {
96-
let BlockHeaderPartial { number: _, hash, state_root, transactions_root: _, receipts_root: _ } = get_header(chain_id, block_number);
97-
let StateAndStorageProofInput { account, state_proof_input, storage_proof_input } = get_proof_unconstrained(chain_id, block_number, address, storage_key);
109+
let BlockHeaderPartial { number: _, hash, state_root, transactions_root: _, receipts_root: _ } =
110+
get_header(chain_id, block_number);
111+
let StateAndStorageProofInput { account, state_proof_input, storage_proof_input } =
112+
get_proof_unconstrained(chain_id, block_number, address, storage_key);
98113

99114
verify_account(address, account, state_proof_input, state_root);
100115
verify_storage_values([storage_proof_input], account.storage_root);
101116

102117
assert_storage_key_equals(storage_key, storage_proof_input.key);
103118

104-
StorageWithinBlock { block_hash: hash, account, values: [get_storage_value(storage_proof_input.value)] }
119+
StorageWithinBlock {
120+
block_hash: hash,
121+
account,
122+
values: [get_storage_value(storage_proof_input.value)],
123+
}
105124
}
106125

107126
#[oracle(get_proof)]
108127
unconstrained fn get_proof_oracle<let STATE_PROOF_INPUT_LEN: u32, let STORAGE_PROOF_INPUT: u32>(
109128
_chain_id: Field,
110-
_block_no: u32,
129+
_block_no: u64,
111130
_address: Address,
112-
_storage_key: Bytes32
113-
) -> (Account, ProofInputSerialized<STATE_PROOF_INPUT_LEN>, ProofInputSerialized<STORAGE_PROOF_INPUT>) {}
131+
_storage_key: Bytes32,
132+
) -> (Account, ProofInputSerialized<STATE_PROOF_INPUT_LEN>, ProofInputSerialized<STORAGE_PROOF_INPUT>) {}
114133

115134
unconstrained fn get_proof_unconstrained(
116135
chain_id: Field,
117-
block_no: u32,
136+
block_no: u64,
118137
address: Address,
119-
storage_key: Bytes32
138+
storage_key: Bytes32,
120139
) -> StateAndStorageProofInput {
121-
let (account, state_proof_input, storage_proof_input) = get_proof_oracle(chain_id, block_no, address, storage_key);
140+
let (account, state_proof_input, storage_proof_input) =
141+
get_proof_oracle(chain_id, block_no, address, storage_key);
122142
let state_proof_input = Serde::deserialize(state_proof_input);
123143
let storage_proof_input = Serde::deserialize(storage_proof_input);
124144

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::account_with_storage::StorageWithinBlock;
2-
use crate::misc::{fragment::Fragment, types::{Address, Bytes32, ADDRESS_LENGTH, BYTES32_LENGTH}};
2+
use crate::misc::{fragment::Fragment, types::{Address, ADDRESS_LENGTH, Bytes32, BYTES32_LENGTH}};
33
use crate::serde::STORAGE_BLOCK_LEN;
4-
use dep::std::{verify_proof, mem::zeroed};
4+
use dep::std::{mem::zeroed, verify_proof};
55

66
struct RecursiveProof {
77
key_hash: Field,
@@ -13,11 +13,16 @@ global NUM_PUBLIC_INPUTS: u32 = 1 + 1 + ADDRESS_LENGTH + BYTES32_LENGTH + STORAG
1313

1414
pub fn get_account_with_storage_recursive(
1515
chain_id: Field,
16-
block_number: u32,
16+
block_number: u64,
1717
address: Address,
18-
storage_key: Bytes32
18+
storage_key: Bytes32,
1919
) -> StorageWithinBlock<1> {
20-
let (storage_within_block, RecursiveProof {key_hash, verification_key, proof}) = get_account_with_storage_recursive_unconstrained(chain_id, block_number, address, storage_key);
20+
let (storage_within_block, RecursiveProof { key_hash, verification_key, proof }) = get_account_with_storage_recursive_unconstrained(
21+
chain_id,
22+
block_number,
23+
address,
24+
storage_key,
25+
);
2126

2227
let mut public_inputs: Fragment<NUM_PUBLIC_INPUTS, Field> = Fragment::empty();
2328
public_inputs.push_back(chain_id);
@@ -30,7 +35,7 @@ pub fn get_account_with_storage_recursive(
3035
verification_key,
3136
proof,
3237
public_inputs.to_array::<NUM_PUBLIC_INPUTS>(),
33-
key_hash
38+
key_hash,
3439
);
3540

3641
storage_within_block
@@ -39,18 +44,19 @@ pub fn get_account_with_storage_recursive(
3944
#[oracle(get_storage_recursive)]
4045
unconstrained fn get_account_with_storage_recursive_oracle(
4146
chain_id: Field,
42-
block_number: u32,
47+
block_number: u64,
4348
address: Address,
44-
storage_key: Bytes32
49+
storage_key: Bytes32,
4550
) -> ([Field; STORAGE_BLOCK_LEN], RecursiveProof) {}
4651

4752
unconstrained fn get_account_with_storage_recursive_unconstrained(
4853
chain_id: Field,
49-
block_number: u32,
54+
block_number: u64,
5055
address: Address,
51-
storage_key: Bytes32
56+
storage_key: Bytes32,
5257
) -> (StorageWithinBlock<1>, RecursiveProof) {
53-
let (return_value_serialized, proof) = get_account_with_storage_recursive_oracle(chain_id, block_number, address, storage_key);
58+
let (return_value_serialized, proof) =
59+
get_account_with_storage_recursive_oracle(chain_id, block_number, address, storage_key);
5460
let return_value = StorageWithinBlock::deserialize(return_value_serialized);
5561
(return_value, proof)
5662
}

ethereum/circuits/lib/src/header.nr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::verifiers::header::verify_header;
44
global MAX_HEADER_RLP_LEN: u32 = 708;
55

66
struct BlockHeaderPartial {
7-
number: u32,
7+
number: u64,
88
hash: Bytes32,
99
state_root: Bytes32,
1010
transactions_root: Bytes32,
@@ -13,16 +13,16 @@ struct BlockHeaderPartial {
1313

1414
type BlockHeaderRlp = BoundedVec<u8, MAX_HEADER_RLP_LEN>;
1515

16-
pub fn get_header(chain_id: Field, block_number: u32) -> BlockHeaderPartial {
16+
pub fn get_header(chain_id: Field, block_number: u64) -> BlockHeaderPartial {
1717
let (block_header_partial, block_header_rlp) = get_header_unconstrained(chain_id, block_number);
1818
verify_header(chain_id, block_header_partial, block_header_rlp);
1919
assert(block_header_partial.number == block_number, "Block number does not match the argument");
2020
block_header_partial
2121
}
2222

2323
#[oracle(get_header)]
24-
unconstrained fn get_header_oracle(_chain_id: Field, _block_no: u32) -> (BlockHeaderPartial, BlockHeaderRlp) {}
24+
unconstrained fn get_header_oracle(_chain_id: Field, _block_no: u64) -> (BlockHeaderPartial, BlockHeaderRlp) {}
2525

26-
unconstrained fn get_header_unconstrained(chain_id: Field, block_no: u32) -> (BlockHeaderPartial, BlockHeaderRlp) {
26+
unconstrained fn get_header_unconstrained(chain_id: Field, block_no: u64) -> (BlockHeaderPartial, BlockHeaderRlp) {
2727
get_header_oracle(chain_id, block_no)
2828
}

0 commit comments

Comments
 (0)