Skip to content

Commit 44ae3b3

Browse files
committed
create trait
1 parent 7653f10 commit 44ae3b3

File tree

9 files changed

+66
-81
lines changed

9 files changed

+66
-81
lines changed

ethereum/circuits/lib/src/account.nr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ type AccountWithStateProofM = (Account, ProofInput<MAX_PREFIXED_KEY_NIBBLE_LEN,
2727

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

30-
#[export]
3130
pub fn get_account(chain_id: u32, block_no: u64, address: Address) -> AccountWithinBlock {
3231
// Safety: verification done separately
3332
let (account, state_proof) =

ethereum/circuits/lib/src/rlp/README.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Whe working with RLP data - it's a common pattern to have a structure (Tx, Log,
5959
```rust
6060
let rlp = [...];
6161
let decoded = decode_list(Fragment::from_array(rlp));
62-
decoded.get(0).assert_eq_u64("nonce", rlp, 0x10);
62+
decoded.get(0).assert_eq_num("nonce", rlp, 0x10);
6363
decoded.get(1).assert_eq_address("to", rlp, [...]);
6464
```
6565

@@ -69,10 +69,7 @@ decoded.get(1).assert_eq_address("to", rlp, [...]);
6969
- `assert_eq_bounded_vec`
7070
- `assert_empty_string`
7171
- `assert_eq_u1`
72-
- `assert_eq_u8`
73-
- `assert_eq_u32`
74-
- `assert_eq_u64`
75-
- `assert_eq_u128`
72+
- `assert_eq_num`
7673
- `assert_eq_address`
7774
- `assert_eq_bytes32`
7875

ethereum/circuits/lib/src/rlp/types.nr

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use crate::misc::bytes::byte_value;
22
use crate::misc::fragment::Fragment;
33
use crate::misc::types::{Address, Bytes32};
4-
use crate::verifiers::tx_helpers::idx::u32_to_u8;
5-
use crate::verifiers::tx_helpers::idx::u64_to_u8;
64

75
// Enum for RLP data type
86
pub(crate) global STRING: u32 = 0;
@@ -71,41 +69,16 @@ impl RlpFragment {
7169
self.assert_eq_bytes(field_name, rlp, Fragment::from_array([value as u8]));
7270
}
7371

74-
pub(crate) fn assert_eq_u8<let FIELD_NAME_LEN: u32, let MAX_RLP_LEN: u32>(
72+
pub fn assert_eq_num<let FIELD_NAME_LEN: u32, let MAX_RLP_LEN: u32, let N: u32, Num>(
7573
self,
7674
field_name: str<FIELD_NAME_LEN>,
7775
rlp: Fragment<MAX_RLP_LEN, u8>,
78-
value: u8,
79-
) {
80-
self.assert_eq_bounded_vec(field_name, rlp, byte_value([value]));
81-
}
82-
83-
pub(crate) fn assert_eq_u32<let FIELD_NAME_LEN: u32, let MAX_RLP_LEN: u32>(
84-
self,
85-
field_name: str<FIELD_NAME_LEN>,
86-
rlp: Fragment<MAX_RLP_LEN, u8>,
87-
value: u32,
88-
) {
89-
self.assert_eq_bounded_vec(field_name, rlp, byte_value(u32_to_u8(value)));
90-
}
91-
92-
pub fn assert_eq_u64<let FIELD_NAME_LEN: u32, let MAX_RLP_LEN: u32>(
93-
self,
94-
field_name: str<FIELD_NAME_LEN>,
95-
rlp: Fragment<MAX_RLP_LEN, u8>,
96-
value: u64,
97-
) {
98-
self.assert_eq_bounded_vec(field_name, rlp, byte_value(u64_to_u8(value)));
99-
}
100-
101-
pub fn assert_eq_u128<let FIELD_NAME_LEN: u32, let MAX_RLP_LEN: u32>(
102-
self,
103-
field_name: str<FIELD_NAME_LEN>,
104-
rlp: Fragment<MAX_RLP_LEN, u8>,
105-
value: u128,
106-
) {
107-
let val: Field = value as Field;
108-
self.assert_eq_bounded_vec(field_name, rlp, byte_value(val.to_be_bytes::<20>()));
76+
value: Num,
77+
)
78+
where
79+
Num: ToBEBytes<N>,
80+
{
81+
self.assert_eq_bounded_vec(field_name, rlp, value.to_bytes());
10982
}
11083

11184
pub(crate) fn assert_eq_address<let FIELD_NAME_LEN: u32, let MAX_RLP_LEN: u32>(
@@ -140,3 +113,31 @@ impl Eq for RlpFragment {
140113
& (self.data_type == other.data_type)
141114
}
142115
}
116+
117+
trait ToBEBytes<let N: u32> {
118+
fn to_bytes(self) -> BoundedVec<u8, N>;
119+
}
120+
121+
impl ToBEBytes<1> for u8 {
122+
fn to_bytes(self) -> BoundedVec<u8, 1> {
123+
byte_value::<1>([self])
124+
}
125+
}
126+
127+
impl ToBEBytes<4> for u32 {
128+
fn to_bytes(self) -> BoundedVec<u8, 4> {
129+
byte_value::<4>((self as Field).to_be_bytes::<4>())
130+
}
131+
}
132+
133+
impl ToBEBytes<8> for u64 {
134+
fn to_bytes(self) -> BoundedVec<u8, 8> {
135+
byte_value::<8>((self as Field).to_be_bytes::<8>())
136+
}
137+
}
138+
139+
impl ToBEBytes<16> for u128 {
140+
fn to_bytes(self) -> BoundedVec<u8, 16> {
141+
byte_value::<16>((self as Field).to_be_bytes::<16>())
142+
}
143+
}

ethereum/circuits/lib/src/rlp/types_test.nr

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -152,20 +152,20 @@ mod rlp_fragment {
152152

153153
#[test]
154154
fn success() {
155-
fragment.assert_eq_u8("Field", encoded_value, value as u8)
155+
fragment.assert_eq_num::<5, 1, 1, u8>("Field", encoded_value, value as u8)
156156
}
157157

158158
#[test(should_fail_with = "Field: Invalid RLP length")]
159159
fn invalid_length() {
160160
let mut invalid_length_fragment = fragment;
161161
invalid_length_fragment.length = 2;
162162

163-
invalid_length_fragment.assert_eq_u8("Field", encoded_value, value)
163+
invalid_length_fragment.assert_eq_num::<5, 1, 1, u8>("Field", encoded_value, value)
164164
}
165165

166166
#[test(should_fail_with = "Field: Invalid RLP value")]
167167
fn invalid_value() {
168-
fragment.assert_eq_u8("Field", encoded_value, value + 1)
168+
fragment.assert_eq_num::<5, 1, 1, u8>("Field", encoded_value, value + 1)
169169
}
170170
}
171171

@@ -179,18 +179,18 @@ mod rlp_fragment {
179179

180180
#[test]
181181
fn success() {
182-
fragment.assert_eq_u32("Field", encoded_value, value)
182+
fragment.assert_eq_num::<5, 4, 4, u32>("Field", encoded_value, value)
183183
}
184184

185185
#[test(should_fail_with = "Field: Invalid RLP length")]
186186
fn invalid_length() {
187187
let short_value = 0x10;
188-
fragment.assert_eq_u32("Field", encoded_value, short_value)
188+
fragment.assert_eq_num::<5, 4, 4, u32>("Field", encoded_value, short_value)
189189
}
190190

191191
#[test(should_fail_with = "Field: Invalid RLP value")]
192192
fn invalid_value() {
193-
fragment.assert_eq_u32("Field", encoded_value, value + 1)
193+
fragment.assert_eq_num::<5, 4, 4, u32>("Field", encoded_value, value + 1)
194194
}
195195
}
196196

@@ -205,18 +205,18 @@ mod rlp_fragment {
205205

206206
#[test]
207207
fn success() {
208-
fragment.assert_eq_u64("Field", encoded_value, value)
208+
fragment.assert_eq_num::<5, 8, 8, u64>("Field", encoded_value, value)
209209
}
210210

211211
#[test(should_fail_with = "Field: Invalid RLP length")]
212212
fn invalid_length() {
213213
let short_value = 0x10;
214-
fragment.assert_eq_u64("Field", encoded_value, short_value)
214+
fragment.assert_eq_num::<5, 8, 8, u64>("Field", encoded_value, short_value)
215215
}
216216

217217
#[test(should_fail_with = "Field: Invalid RLP value")]
218218
fn invalid_value() {
219-
fragment.assert_eq_u64("Field", encoded_value, value + 1)
219+
fragment.assert_eq_num::<5, 8, 8, u64>("Field", encoded_value, value + 1)
220220
}
221221
}
222222

@@ -233,18 +233,18 @@ mod rlp_fragment {
233233

234234
#[test]
235235
fn success() {
236-
fragment.assert_eq_u128("Field", encoded_value, value)
236+
fragment.assert_eq_num::<5, 16, 16, u128>("Field", encoded_value, value)
237237
}
238238

239239
#[test(should_fail_with = "Field: Invalid RLP length")]
240240
fn invalid_length() {
241241
let short_value = 0x10 as u128;
242-
fragment.assert_eq_u128("Field", encoded_value, short_value)
242+
fragment.assert_eq_num::<5, 16, 16, u128>("Field", encoded_value, short_value)
243243
}
244244

245245
#[test(should_fail_with = "Field: Invalid RLP value")]
246246
fn invalid_value() {
247-
fragment.assert_eq_u128("Field", encoded_value, value + 1)
247+
fragment.assert_eq_num::<5, 16, 16, u128>("Field", encoded_value, value + 1)
248248
}
249249
}
250250

ethereum/circuits/lib/src/verifiers/account.nr

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ pub(crate) fn assert_account_equals(
2828
);
2929

3030
let account_rlp = Fragment::from_array(account_rlp_right_padded);
31-
account_rlp_list.get(NONCE_INDEX).assert_eq_u64("Nonce", account_rlp, account.nonce);
32-
account_rlp_list.get(BALANCE_INDEX).assert_eq_u128(
31+
account_rlp_list.get(NONCE_INDEX).assert_eq_num::<5, 110, 8, u64>(
32+
"Nonce",
33+
account_rlp,
34+
account.nonce,
35+
);
36+
account_rlp_list.get(BALANCE_INDEX).assert_eq_num::<7, 110, 16, u128>(
3337
"Balance",
3438
account_rlp,
3539
account.balance as u128,

ethereum/circuits/lib/src/verifiers/header.nr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub fn verify_header(
5050
expected_header_fields_count == header_rlp_list.len(),
5151
"number of header RLP fields does not match",
5252
);
53-
header_rlp_list.get(BLOCK_NUM_INDEX).assert_eq_u64(
53+
header_rlp_list.get(BLOCK_NUM_INDEX).assert_eq_num::<12, 708, 8, u64>(
5454
"Block number",
5555
block_header_rlp,
5656
block_header_partial.number,

ethereum/circuits/lib/src/verifiers/receipt/rlp.nr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub(crate) fn assert_receipt_rlp_equals<let MAX_ENCODED_LEN: u32>(
3333
);
3434
}
3535

36-
receipt_rlp_list.get(CUMULATIVE_GAS_USED_INDEX).assert_eq_u32(
36+
receipt_rlp_list.get(CUMULATIVE_GAS_USED_INDEX).assert_eq_num::<19, MAX_ENCODED_LEN, 4, u32>(
3737
"Cumulative gas used",
3838
receipt_rlp,
3939
receipt.cumulative_gas_used,

ethereum/circuits/lib/src/verifiers/transaction/rlp.nr

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,25 @@ pub(crate) fn assert_tx_rlp_equals<let MAX_DATA_LEN: u32, let MAX_ENCODED_LEN: u
3131
TX_TYPE_TO_FIELD_LAYOUT[tx_type as u32];
3232
assert(rlp_list.len() == field_count, "Invalid number of fields in tx RLP");
3333

34-
rlp_list.get(nonce_idx).assert_eq_u64("Nonce", tx_rlp, tx.nonce);
34+
rlp_list.get(nonce_idx).assert_eq_num::<5, MAX_ENCODED_LEN, 8, u64>("Nonce", tx_rlp, tx.nonce);
3535

36-
rlp_list.get(gas_limit_idx).assert_eq_u64("Gas limit", tx_rlp, tx.gas_limit);
36+
rlp_list.get(gas_limit_idx).assert_eq_num::<9, MAX_ENCODED_LEN, 8, u64>(
37+
"Gas limit",
38+
tx_rlp,
39+
tx.gas_limit,
40+
);
3741

3842
if (tx.to.is_some()) {
3943
rlp_list.get(to_idx).assert_eq_address("To", tx_rlp, tx.to.expect(f"to is missing"));
4044
} else {
4145
rlp_list.get(to_idx).assert_empty_string("To");
4246
}
4347

44-
rlp_list.get(to_idx + 1).assert_eq_u128("Value", tx_rlp, tx.value);
48+
rlp_list.get(to_idx + 1).assert_eq_num::<5, MAX_ENCODED_LEN, 16, u128>("Value", tx_rlp, tx.value);
4549

4650
rlp_list.get(to_idx + 2).assert_eq_bounded_vec("Data", tx_rlp, tx.data);
4751

48-
rlp_list.get(signature_idx).assert_eq_u8("V", tx_rlp, tx.v);
52+
rlp_list.get(signature_idx).assert_eq_num::<1, MAX_ENCODED_LEN, 1, u8>("V", tx_rlp, tx.v);
4953
rlp_list.get(signature_idx + 1).assert_eq_bytes32("R", tx_rlp, tx.r);
5054
rlp_list.get(signature_idx + 2).assert_eq_bytes32("S", tx_rlp, tx.s);
5155
}

ethereum/circuits/lib/src/verifiers/tx_helpers/idx.nr

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,10 @@ pub fn assert_tx_idx_equals<let MAX_KEY_LEN: u32>(
1212
if (length == 0) {
1313
assert(tx_idx == 0, "Key does not match rlp-encoded transaction index");
1414
} else {
15-
let tx_idx_bytes = Fragment::from_vec(right_pad(u32_to_u8(tx_idx as u32)));
15+
let tx_idx_bytes = Fragment::from_vec(right_pad((tx_idx as Field).to_be_bytes::<4>()));
1616
assert(
1717
key_as_rlp.subfragment(offset, length).eq(tx_idx_bytes),
1818
"Key does not match rlp-encoded transaction index",
1919
);
2020
}
2121
}
22-
23-
pub fn u32_to_u8(num: u32) -> [u8; 4] {
24-
let mut out: [u8; 4] = [0; 4];
25-
for i in 0..4 {
26-
let shift: u32 = (24 - (i * 8));
27-
out[i] = (num >> shift) as u8;
28-
}
29-
30-
out
31-
}
32-
33-
pub fn u64_to_u8(num: u64) -> [u8; 8] {
34-
let mut out: [u8; 8] = [0; 8];
35-
for i in 0..8 {
36-
let shift: u32 = 56 - (i * 8);
37-
out[i] = (num >> (shift as u64)) as u8;
38-
}
39-
40-
out
41-
}

0 commit comments

Comments
 (0)