Skip to content

Commit 78060fb

Browse files
committed
Revert "replace u256"
This reverts commit 85f1d59.
1 parent 85f1d59 commit 78060fb

File tree

13 files changed

+237
-108
lines changed

13 files changed

+237
-108
lines changed

ethereum/circuits/lib/Nargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ 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" }
9-
bignum = {tag = "v0.8.3", git = "https://github.com/noir-lang/noir-bignum" }
8+
keccak256 = {tag = "v0.1.1", git = "https://github.com/noir-lang/keccak256" }

ethereum/circuits/lib/src/serde.nr

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ 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 dep::bignum::bignum::BigNum;
19-
use dep::bignum::U256 as u256;
18+
use crate::uint256::U256;
2019

2120
pub trait Serde<let LEN: u32> {
2221
fn serialize(self) -> [Field; LEN];
@@ -35,15 +34,15 @@ impl Serde<U128_SERIALIZED_LEN> for u128 {
3534
}
3635
}
3736

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

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)
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]
4342
}
4443

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

ethereum/circuits/lib/src/serde_test.nr

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

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

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

2422
#[test]
2523
fn simple() {
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);
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);
3029
assert_eq(U256::deserialize(serialized), value);
3130
}
3231
}
Lines changed: 71 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,86 @@
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;
64
use dep::std::ops::Add;
75

86
global uint128_overflow_value: Field = 340282366920938463463374607431768211456; // 2^128
97

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];
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)
1442
}
15-
u256::from_be_bytes(bytes33)
1643
}
1744

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];
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
2356
}
24-
bytes32
2557
}
2658

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];
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 }
3285
}
33-
u256::from_be_bytes(bytes33)
3486
}
Lines changed: 92 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,135 @@
1-
mod from_bytes32 {
2-
use crate::uint256::{from, from_field};
3-
use dep::bignum::bignum::BigNum;
4-
use dep::bignum::U256;
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);
526

27+
let one = U256::one();
28+
assert_eq(one.high, 0);
29+
assert_eq(one.low, 1);
30+
}
31+
32+
mod from_bytes32 {
33+
use crate::uint256::U256;
634
global high: u128 = 0x10000000000000000000000000000000;
735
global low: u128 = 0;
8-
global limit_u256: U256 = U256::zero() - U256::one();
36+
global limit: u128 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;
937

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

1241
#[test]
1342
fn zero() {
1443
let bytes = [0x00; 32];
15-
assert_eq(from(bytes), U256::zero());
44+
assert_eq(U256::from(bytes), U256::zero());
1645
}
1746

1847
#[test]
1948
fn success() {
2049
let mut bytes = [0x00; 32];
2150
bytes[31] = 0x10;
22-
assert_eq(from(bytes), big_number);
51+
assert_eq(U256::from(bytes), big_number);
2352
}
2453

2554
#[test]
2655
fn u256_limit() {
2756
let bytes = [0xff; 32];
28-
assert_eq(from(bytes), limit_u256);
57+
assert_eq(U256::from(bytes), limit_u256);
2958
}
3059
}
3160

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

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

4270
#[test]
4371
fn zero() {
4472
let bytes = [0x00; 32];
45-
assert_eq(into(U256::zero()), bytes);
73+
assert_eq(U256::into(U256::zero()), bytes);
4674
}
4775

4876
#[test]
4977
fn success() {
5078
let mut bytes = [0x00; 32];
5179
bytes[31] = 0x10;
52-
assert_eq(into(big_number), bytes);
80+
assert_eq(U256::into(big_number), bytes);
5381
}
5482

5583
#[test]
5684
fn u256_limit() {
5785
let bytes = [0xff; 32];
58-
assert_eq(into(limit_u256), bytes);
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();
59134
}
60135
}

vlayer/ethereum/circuits/lib/Nargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ 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" }
9-
bignum = {tag = "v0.8.3", git = "https://github.com/noir-lang/noir-bignum" }
8+
keccak256 = {tag = "v0.1.1", git = "https://github.com/noir-lang/keccak256" }

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
use dep::bignum::U256;
21
use dep::ethereum::{
32
account_with_storage::get_account_with_storage,
43
misc::{
54
arrays::subarray_inferred_len,
65
types::{Address, ADDRESS_LENGTH, Bytes32, BYTES32_LENGTH},
76
},
8-
uint256::into,
7+
uint256::U256,
98
};
109

1110
pub struct ERC721Token {
@@ -21,8 +20,12 @@ pub trait ERC721 {
2120
impl ERC721 for ERC721Token {
2221
fn get_owner(self, token_id: Bytes32, block_number: u64) -> Address {
2322
let storage_key = (self.token_id_to_slot)(token_id);
24-
let account =
25-
get_account_with_storage(self.chain_id, block_number, self.address, into(storage_key));
23+
let account = get_account_with_storage(
24+
self.chain_id,
25+
block_number,
26+
self.address,
27+
U256::into(storage_key),
28+
);
2629
subarray_inferred_len(account.values[0], BYTES32_LENGTH - ADDRESS_LENGTH)
2730
}
2831
}

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::{from, from_field};
5+
use dep::ethereum::uint256::U256;
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 = from([
16+
let BORED_APE_YACHT_CLUB_TOKEN_OWNERS_INNER_ENTRIES_SLOT = U256::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 = from_field(10);
46+
let CRYPTO_PUNK_TOKEN_OWNERS_INNER_ENTRIES_SLOT = U256::from_field(10);
4747

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

0 commit comments

Comments
 (0)