Skip to content

Commit 85f1d59

Browse files
committed
replace u256
1 parent dc81905 commit 85f1d59

File tree

13 files changed

+108
-237
lines changed

13 files changed

+108
-237
lines changed

ethereum/circuits/lib/Nargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ authors = ["Arkadiusz Konior, Marek Kirejczyk"]
55
compiler_version = ">=0.30.0"
66

77
[dependencies]
8-
keccak256 = {tag = "v0.1.1", git = "https://github.com/noir-lang/keccak256" }
8+
keccak256 = {tag = "v0.1.1", git = "https://github.com/noir-lang/keccak256" }
9+
bignum = {tag = "v0.8.3", git = "https://github.com/noir-lang/noir-bignum" }

ethereum/circuits/lib/src/serde.nr

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ use crate::receipt::{
1515
MAX_PREFIXED_KEY_NIBBLE_LEN as RECEIPT_MAX_PREFIXED_KEY_NIBBLE_LEN,
1616
MAX_VALUE_LEN_M as RECEIPT_MAX_VALUE_LEN_M,
1717
};
18-
use crate::uint256::U256;
18+
use dep::bignum::bignum::BigNum;
19+
use dep::bignum::U256 as u256;
1920

2021
pub trait Serde<let LEN: u32> {
2122
fn serialize(self) -> [Field; LEN];
@@ -34,15 +35,15 @@ impl Serde<U128_SERIALIZED_LEN> for u128 {
3435
}
3536
}
3637

37-
pub(crate) global U256_SERIALIZED_LEN: u32 = 2;
38+
pub(crate) global u256_SERIALIZED_LEN: u32 = 3;
3839

39-
impl Serde<U256_SERIALIZED_LEN> for U256 {
40-
fn serialize(self) -> [Field; U256_SERIALIZED_LEN] {
41-
[self.low as Field, self.high as Field]
40+
impl Serde<u256_SERIALIZED_LEN> for u256 {
41+
fn serialize(self) -> [Field; u256_SERIALIZED_LEN] {
42+
self.get_limbs().map(|x: u128| x as Field)
4243
}
4344

44-
fn deserialize(data: [Field; U256_SERIALIZED_LEN]) -> Self {
45-
U256 { low: data[0] as u128, high: data[1] as u128 }
45+
fn deserialize(data: [Field; u256_SERIALIZED_LEN]) -> Self {
46+
u256::from_limbs(data.map(|x: Field| x as u128))
4647
}
4748
}
4849

ethereum/circuits/lib/src/serde_test.nr

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,19 @@ mod U128_ {
1414

1515
mod U256 {
1616
use crate::serde::Serde;
17-
use crate::serde::U256_SERIALIZED_LEN;
18-
use crate::uint256::U256;
17+
use crate::serde::u256_SERIALIZED_LEN;
18+
use crate::uint256::from_field;
19+
use dep::bignum::bignum::BigNum;
20+
use dep::bignum::U256;
1921

20-
global u128_number: u128 = 0x10000000000000000000000000000000;
22+
global u128_number: Field = 0x10000000000000000000000000000000;
2123

2224
#[test]
2325
fn simple() {
24-
let value = U256::new(u128_number, u128_number);
25-
let serialized: [Field; U256_SERIALIZED_LEN] = value.serialize();
26-
27-
assert_eq(serialized[0] as u128, u128_number);
28-
assert_eq(serialized[1] as u128, u128_number);
26+
let value = from_field(u128_number);
27+
let serialized: [Field; u256_SERIALIZED_LEN] = value.serialize();
28+
let s = serialized.map(|x: Field| x as u128);
29+
assert_eq(U256::from_limbs(s), value);
2930
assert_eq(U256::deserialize(serialized), value);
3031
}
3132
}
Lines changed: 19 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,34 @@
11
use crate::misc::arrays::memcpy_up_to_length;
22
use crate::misc::bytes32::field_to_bytes32;
33
use crate::misc::types::Bytes32;
4+
use dep::bignum::bignum::BigNum;
5+
use dep::bignum::U256 as u256;
46
use dep::std::ops::Add;
57

68
global uint128_overflow_value: Field = 340282366920938463463374607431768211456; // 2^128
79

8-
pub struct U256 {
9-
pub(crate) high: u128,
10-
pub(crate) low: u128,
11-
}
12-
13-
impl U256 {
14-
pub fn new(high: u128, low: u128) -> Self {
15-
Self { high, low }
16-
}
17-
18-
pub fn zero() -> Self {
19-
Self { high: 0, low: 0 }
20-
}
21-
22-
pub fn one() -> Self {
23-
Self { high: 0, low: 1 }
24-
}
25-
26-
pub fn from_field(field: Field) -> Self {
27-
U256::from(field_to_bytes32(field))
28-
}
29-
}
30-
31-
impl From<Bytes32> for U256 {
32-
fn from(bytes: Bytes32) -> Self {
33-
let mut high_bytes = [0; 16];
34-
memcpy_up_to_length(&mut high_bytes, bytes, 16, 16);
35-
let high = Field::from_le_bytes::<16>(high_bytes) as u128;
36-
37-
let mut low_bytes = [0; 16];
38-
memcpy_up_to_length(&mut low_bytes, bytes, 0, 16);
39-
let low = Field::from_le_bytes::<16>(low_bytes) as u128;
40-
41-
U256::new(high, low)
10+
pub fn from(bytes: Bytes32) -> u256 {
11+
let mut bytes33 = [0; 33];
12+
for i in 0..32 {
13+
bytes33[i + 1] = bytes[i];
4214
}
15+
u256::from_be_bytes(bytes33)
4316
}
4417

45-
impl Into<Bytes32> for U256 {
46-
fn into(self) -> Bytes32 {
47-
let mut bytes = [0; 32];
48-
memcpy_up_to_length(&mut bytes, (self.low as Field).to_le_bytes::<16>(), 0, 16);
49-
50-
let high_bytes = (self.high as Field).to_le_bytes::<16>();
51-
for i in 0..16 {
52-
bytes[i + 16] = high_bytes[i];
53-
}
54-
55-
bytes
18+
pub fn into(u256: u256) -> Bytes32 {
19+
let bytes = u256.to_be_bytes();
20+
let mut bytes32 = [0; 32];
21+
for i in 0..32 {
22+
bytes32[i] = bytes[i + 1];
5623
}
24+
bytes32
5725
}
5826

59-
impl Eq for U256 {
60-
fn eq(self, other: U256) -> bool {
61-
(self.high == other.high) & (self.low == other.low)
62-
}
63-
}
64-
65-
impl Add for U256 {
66-
fn add(self, other: Self) -> Self {
67-
let lo: Field = self.low as Field + other.low as Field;
68-
69-
let mut low = 0;
70-
let mut carry = 0;
71-
if (lo.lt(uint128_overflow_value)) {
72-
low = lo;
73-
} else {
74-
low = lo - uint128_overflow_value;
75-
carry = 1;
76-
}
77-
78-
let hi: Field = self.high as Field + other.high as Field + carry;
79-
assert(hi.lt(uint128_overflow_value), "attempt to add with overflow");
80-
81-
let high = hi as u128;
82-
let low = low as u128;
83-
84-
Self { high, low }
27+
pub fn from_field(field: Field) -> u256 {
28+
let bytes = field.to_be_bytes::<32>();
29+
let mut bytes33 = [0; 33];
30+
for i in 0..32 {
31+
bytes33[i + 1] = bytes[i];
8532
}
33+
u256::from_be_bytes(bytes33)
8634
}
Lines changed: 17 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,135 +1,60 @@
1-
use crate::uint256::U256;
2-
3-
global high: u128 = 0x10000000000000000000000000000000;
4-
global low: u128 = 0;
5-
6-
global big_number: U256 = U256::new(high, low);
7-
8-
#[test]
9-
fn success() {
10-
assert_eq(big_number.high, high);
11-
assert_eq(big_number.low, low);
12-
}
13-
14-
#[test]
15-
fn new() {
16-
let big_number = U256::new(high, low);
17-
assert_eq(big_number.high, high);
18-
assert_eq(big_number.low, low);
19-
}
20-
21-
#[test]
22-
fn zero_and_one() {
23-
let zero = U256::zero();
24-
assert_eq(zero.high, 0);
25-
assert_eq(zero.low, 0);
26-
27-
let one = U256::one();
28-
assert_eq(one.high, 0);
29-
assert_eq(one.low, 1);
30-
}
31-
321
mod from_bytes32 {
33-
use crate::uint256::U256;
2+
use crate::uint256::{from, from_field};
3+
use dep::bignum::bignum::BigNum;
4+
use dep::bignum::U256;
5+
346
global high: u128 = 0x10000000000000000000000000000000;
357
global low: u128 = 0;
36-
global limit: u128 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;
8+
global limit_u256: U256 = U256::zero() - U256::one();
379

38-
global big_number: U256 = U256::new(high, low);
39-
global limit_u256: U256 = U256::new(limit, limit);
10+
global big_number: U256 = from_field(0x10000000000000000000000000000000);
4011

4112
#[test]
4213
fn zero() {
4314
let bytes = [0x00; 32];
44-
assert_eq(U256::from(bytes), U256::zero());
15+
assert_eq(from(bytes), U256::zero());
4516
}
4617

4718
#[test]
4819
fn success() {
4920
let mut bytes = [0x00; 32];
5021
bytes[31] = 0x10;
51-
assert_eq(U256::from(bytes), big_number);
22+
assert_eq(from(bytes), big_number);
5223
}
5324

5425
#[test]
5526
fn u256_limit() {
5627
let bytes = [0xff; 32];
57-
assert_eq(U256::from(bytes), limit_u256);
28+
assert_eq(from(bytes), limit_u256);
5829
}
5930
}
6031

6132
mod into_bytes32 {
62-
use crate::uint256::U256;
33+
use crate::uint256::{from_field, into};
34+
use dep::bignum::bignum::BigNum;
35+
use dep::bignum::U256;
6336
global high: u128 = 0x10000000000000000000000000000000;
6437
global low: u128 = 0;
65-
global limit: u128 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;
6638

67-
global big_number: U256 = U256::new(high, low);
68-
global limit_u256: U256 = U256::new(limit, limit);
39+
global big_number: U256 = from_field(0x10000000000000000000000000000000);
40+
global limit_u256: U256 = U256::zero() - U256::one();
6941

7042
#[test]
7143
fn zero() {
7244
let bytes = [0x00; 32];
73-
assert_eq(U256::into(U256::zero()), bytes);
45+
assert_eq(into(U256::zero()), bytes);
7446
}
7547

7648
#[test]
7749
fn success() {
7850
let mut bytes = [0x00; 32];
7951
bytes[31] = 0x10;
80-
assert_eq(U256::into(big_number), bytes);
52+
assert_eq(into(big_number), bytes);
8153
}
8254

8355
#[test]
8456
fn u256_limit() {
8557
let bytes = [0xff; 32];
86-
assert_eq(U256::into(limit_u256), bytes);
87-
}
88-
}
89-
90-
#[test]
91-
fn eq() {
92-
assert_eq(big_number, big_number);
93-
94-
let big_number2 = U256::new(high, low);
95-
assert_eq(big_number, big_number2);
96-
}
97-
98-
#[test]
99-
fn not_eq() {
100-
let big_number2 = U256 { high, low: high };
101-
assert(big_number != big_number2);
102-
103-
let big_number3 = U256 { high: low, low };
104-
assert(big_number != big_number3);
105-
}
106-
107-
mod trait_add {
108-
use crate::uint256::U256;
109-
global high: u128 = 0x10000000000000000000000000000000;
110-
global low: u128 = 0;
111-
global limit: u128 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;
112-
113-
global big_number: U256 = U256::new(high, low);
114-
global limit_u256: U256 = U256::new(limit, limit);
115-
116-
#[test]
117-
fn sum() {
118-
let sum = big_number + big_number;
119-
120-
assert_eq(sum, U256 { high: 0x20000000000000000000000000000000, low });
121-
}
122-
123-
#[test]
124-
fn sum_with_carry() {
125-
let big_number = U256 { high, low: limit };
126-
let sum = big_number + U256::one();
127-
128-
assert_eq(sum, U256 { high: 0x10000000000000000000000000000001, low });
129-
}
130-
131-
#[test(should_fail_with = "attempt to add with overflow")]
132-
fn sum_overflow() {
133-
let _ = limit_u256 + U256::one();
58+
assert_eq(into(limit_u256), bytes);
13459
}
13560
}

vlayer/ethereum/circuits/lib/Nargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ compiler_version = ">=0.30.0"
55

66
[dependencies]
77
ethereum = { path = "../../../../ethereum/circuits/lib" }
8-
keccak256 = {tag = "v0.1.1", git = "https://github.com/noir-lang/keccak256" }
8+
keccak256 = {tag = "v0.1.1", git = "https://github.com/noir-lang/keccak256" }
9+
bignum = {tag = "v0.8.3", git = "https://github.com/noir-lang/noir-bignum" }

vlayer/ethereum/circuits/lib/src/nft.nr

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
use dep::bignum::U256;
12
use dep::ethereum::{
23
account_with_storage::get_account_with_storage,
34
misc::{
45
arrays::subarray_inferred_len,
56
types::{Address, ADDRESS_LENGTH, Bytes32, BYTES32_LENGTH},
67
},
7-
uint256::U256,
8+
uint256::into,
89
};
910

1011
pub struct ERC721Token {
@@ -20,12 +21,8 @@ pub trait ERC721 {
2021
impl ERC721 for ERC721Token {
2122
fn get_owner(self, token_id: Bytes32, block_number: u64) -> Address {
2223
let storage_key = (self.token_id_to_slot)(token_id);
23-
let account = get_account_with_storage(
24-
self.chain_id,
25-
block_number,
26-
self.address,
27-
U256::into(storage_key),
28-
);
24+
let account =
25+
get_account_with_storage(self.chain_id, block_number, self.address, into(storage_key));
2926
subarray_inferred_len(account.values[0], BYTES32_LENGTH - ADDRESS_LENGTH)
3027
}
3128
}

vlayer/ethereum/circuits/lib/src/nft_list.nr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ pub mod mainnet {
22
use crate::chain_id;
33
use crate::nft::ERC721Token;
44
use crate::slot::{dynamic_array_with_precalculated_slot, mapping, struct_slot};
5-
use dep::ethereum::uint256::U256;
5+
use dep::ethereum::uint256::{from, from_field};
66
use dep::std::field::bytes32_to_field;
77

88
pub fn BORED_APE_YACHT_CLUB() -> ERC721Token {
@@ -13,7 +13,7 @@ pub mod mainnet {
1313
],
1414
token_id_to_slot: |token_id| {
1515
let BORED_APE_YACHT_CLUB_MAX_TOKEN_ID: u32 = 9999;
16-
let BORED_APE_YACHT_CLUB_TOKEN_OWNERS_INNER_ENTRIES_SLOT = U256::from([
16+
let BORED_APE_YACHT_CLUB_TOKEN_OWNERS_INNER_ENTRIES_SLOT = from([
1717
0x40, 0x57, 0x87, 0xfa, 0x12, 0xa8, 0x23, 0xe0, 0xf2, 0xb7, 0x63, 0x1c, 0xc4,
1818
0x1b, 0x3b, 0xa8, 0x82, 0x8b, 0x33, 0x21, 0xca, 0x81, 0x11, 0x11, 0xfa, 0x75,
1919
0xcd, 0x3a, 0xa3, 0xbb, 0x5a, 0xce,
@@ -43,7 +43,7 @@ pub mod mainnet {
4343
],
4444
token_id_to_slot: |token_id| {
4545
let CRYPTO_PUNK_MAX_TOKEN_ID: u32 = 9999;
46-
let CRYPTO_PUNK_TOKEN_OWNERS_INNER_ENTRIES_SLOT = U256::from_field(10);
46+
let CRYPTO_PUNK_TOKEN_OWNERS_INNER_ENTRIES_SLOT = from_field(10);
4747

4848
assert(
4949
bytes32_to_field(token_id) as u32 <= CRYPTO_PUNK_MAX_TOKEN_ID,

0 commit comments

Comments
 (0)