diff --git a/ethereum/circuits/lib/Nargo.toml b/ethereum/circuits/lib/Nargo.toml index 24a85bbf5..f35e14987 100644 --- a/ethereum/circuits/lib/Nargo.toml +++ b/ethereum/circuits/lib/Nargo.toml @@ -5,4 +5,5 @@ authors = ["Arkadiusz Konior, Marek Kirejczyk"] compiler_version = ">=0.30.0" [dependencies] -keccak256 = {tag = "v0.1.1", git = "https://github.com/noir-lang/keccak256" } \ No newline at end of file +keccak256 = {tag = "v0.1.1", git = "https://github.com/noir-lang/keccak256" } +bignum = {tag = "v0.8.3", git = "https://github.com/noir-lang/noir-bignum" } \ No newline at end of file diff --git a/ethereum/circuits/lib/README.md b/ethereum/circuits/lib/README.md index ef420660c..d7e6a0564 100644 --- a/ethereum/circuits/lib/README.md +++ b/ethereum/circuits/lib/README.md @@ -237,22 +237,33 @@ U256 is a structure to use as a type for big numbers. It is used when dealing with numbers up to 2256. They can exceed Field maximum value. In particular it is a word size in ETH and therefore it is a basic type used in both storage and slot values calculations. -[There](.src/uint256.nr) is an unoptimized implementation of this type using two U128 structures. Optimized version will appear in Noir. +This library uses `U256` from the [noir-bignum](https://github.com/noir-lang/noir-bignum) library. The [`uint256.nr`](./src/uint256.nr) module provides conversion utilities between `U256` and `Bytes32`/`Field` types. -Traits implemented for U256: +Traits implemented for U256 (from bignum library): - Add - Eq -- Serde +- Serde (via this library's implementation) ```rust -global u128_number = 0x10000000000000000000000000000000; +use dep::bignum::bignum::BigNum; +use dep::bignum::U256; +use crate::uint256::{from, from_field}; -let big_number = U256::new(u128_number, u128_number); +// Create U256 values +let zero = U256::zero(); +let one = U256::from(1); +let big_number = U256::modulus().udiv(U256::from(16)); -let sum = big_number + U256::one(); -assert_eq(sum, U256 { high: u128_number, low: u128_number + U128::one()}); +// Convert from Bytes32 +let bytes: Bytes32 = [0x10; 32]; +let u256_from_bytes = from(bytes); -let serialized: [Field; 4] = big_number.serialize(); +// Convert from Field +let field_value: Field = 100; +let u256_from_field = from_field(field_value); + +// Serialization (uses 3 limbs) +let serialized: [Field; 3] = big_number.serialize(); assert_eq(U256::deserialize(serialized), big_number); ``` diff --git a/ethereum/circuits/lib/src/serde.nr b/ethereum/circuits/lib/src/serde.nr index b3df4d68c..1eba71813 100644 --- a/ethereum/circuits/lib/src/serde.nr +++ b/ethereum/circuits/lib/src/serde.nr @@ -15,7 +15,8 @@ use crate::receipt::{ MAX_PREFIXED_KEY_NIBBLE_LEN as RECEIPT_MAX_PREFIXED_KEY_NIBBLE_LEN, MAX_VALUE_LEN_M as RECEIPT_MAX_VALUE_LEN_M, }; -use crate::uint256::U256; +use dep::bignum::bignum::BigNum; +use dep::bignum::U256 as u256; pub trait Serde { fn serialize(self) -> [Field; LEN]; @@ -34,15 +35,18 @@ impl Serde for u128 { } } -pub(crate) global U256_SERIALIZED_LEN: u32 = 2; +pub(crate) global u256_SERIALIZED_LEN: u32 = 3; -impl Serde for U256 { - fn serialize(self) -> [Field; U256_SERIALIZED_LEN] { - [self.low as Field, self.high as Field] +impl Serde for u256 { + fn serialize(self) -> [Field; u256_SERIALIZED_LEN] { + self.get_limbs().map(|x: u128| x as Field) } - fn deserialize(data: [Field; U256_SERIALIZED_LEN]) -> Self { - U256 { low: data[0] as u128, high: data[1] as u128 } + fn deserialize(data: [Field; u256_SERIALIZED_LEN]) -> Self { + for i in 0..u256_SERIALIZED_LEN { + data[i].assert_max_bit_size::<120>(); + } + u256::from_limbs(data.map(|x: Field| x as u128)) } } diff --git a/ethereum/circuits/lib/src/serde_test.nr b/ethereum/circuits/lib/src/serde_test.nr index 434de5705..60385d10a 100644 --- a/ethereum/circuits/lib/src/serde_test.nr +++ b/ethereum/circuits/lib/src/serde_test.nr @@ -4,28 +4,32 @@ mod U128_ { #[test] fn simple() { + let value: u128 = 100; let value: u128 = 100; let serialized: [Field; U128_SERIALIZED_LEN] = value.serialize(); assert_eq(serialized[0] as u128, value); assert_eq(u128::deserialize(serialized), value); + assert_eq(serialized[0] as u128, value); + assert_eq(u128::deserialize(serialized), value); } } mod U256 { use crate::serde::Serde; - use crate::serde::U256_SERIALIZED_LEN; - use crate::uint256::U256; + use crate::serde::u256_SERIALIZED_LEN; + use crate::uint256::from_field; + use dep::bignum::bignum::BigNum; + use dep::bignum::U256; - global u128_number: u128 = 0x10000000000000000000000000000000; + global u128_number: Field = 0x10000000000000000000000000000000; #[test] fn simple() { - let value = U256::new(u128_number, u128_number); - let serialized: [Field; U256_SERIALIZED_LEN] = value.serialize(); - - assert_eq(serialized[0] as u128, u128_number); - assert_eq(serialized[1] as u128, u128_number); + let value = from_field(u128_number); + let serialized: [Field; u256_SERIALIZED_LEN] = value.serialize(); + let s = serialized.map(|x: Field| x as u128); + assert_eq(U256::from_limbs(s), value); assert_eq(U256::deserialize(serialized), value); } } diff --git a/ethereum/circuits/lib/src/uint256.nr b/ethereum/circuits/lib/src/uint256.nr index 02a9ed423..21183dacc 100644 --- a/ethereum/circuits/lib/src/uint256.nr +++ b/ethereum/circuits/lib/src/uint256.nr @@ -1,86 +1,24 @@ -use crate::misc::arrays::memcpy_up_to_length; -use crate::misc::bytes32::field_to_bytes32; use crate::misc::types::Bytes32; -use dep::std::ops::Add; +use dep::bignum::bignum::BigNum; +use dep::bignum::U256 as u256; -global uint128_overflow_value: Field = 340282366920938463463374607431768211456; // 2^128 - -pub struct U256 { - pub(crate) high: u128, - pub(crate) low: u128, -} - -impl U256 { - pub fn new(high: u128, low: u128) -> Self { - Self { high, low } - } - - pub fn zero() -> Self { - Self { high: 0, low: 0 } - } - - pub fn one() -> Self { - Self { high: 0, low: 1 } - } - - pub fn from_field(field: Field) -> Self { - U256::from(field_to_bytes32(field)) - } -} - -impl From for U256 { - fn from(bytes: Bytes32) -> Self { - let mut high_bytes = [0; 16]; - memcpy_up_to_length(&mut high_bytes, bytes, 16, 16); - let high = Field::from_le_bytes::<16>(high_bytes) as u128; - - let mut low_bytes = [0; 16]; - memcpy_up_to_length(&mut low_bytes, bytes, 0, 16); - let low = Field::from_le_bytes::<16>(low_bytes) as u128; - - U256::new(high, low) - } -} - -impl Into for U256 { - fn into(self) -> Bytes32 { - let mut bytes = [0; 32]; - memcpy_up_to_length(&mut bytes, (self.low as Field).to_le_bytes::<16>(), 0, 16); - - let high_bytes = (self.high as Field).to_le_bytes::<16>(); - for i in 0..16 { - bytes[i + 16] = high_bytes[i]; - } - - bytes +pub fn from(bytes: Bytes32) -> u256 { + let mut bytes33 = [0; 33]; + for i in 0..32 { + bytes33[i + 1] = bytes[i]; } + u256::from_be_bytes(bytes33) } -impl Eq for U256 { - fn eq(self, other: U256) -> bool { - (self.high == other.high) & (self.low == other.low) +pub fn into(u256: u256) -> Bytes32 { + let bytes = u256.to_be_bytes(); + let mut bytes32 = [0; 32]; + for i in 0..32 { + bytes32[i] = bytes[i + 1]; } + bytes32 } -impl Add for U256 { - fn add(self, other: Self) -> Self { - let lo: Field = self.low as Field + other.low as Field; - - let mut low = 0; - let mut carry = 0; - if (lo.lt(uint128_overflow_value)) { - low = lo; - } else { - low = lo - uint128_overflow_value; - carry = 1; - } - - let hi: Field = self.high as Field + other.high as Field + carry; - assert(hi.lt(uint128_overflow_value), "attempt to add with overflow"); - - let high = hi as u128; - let low = low as u128; - - Self { high, low } - } +pub fn from_field(field: Field) -> u256 { + u256::from(field) } diff --git a/ethereum/circuits/lib/src/uint256_test.nr b/ethereum/circuits/lib/src/uint256_test.nr index f99799f17..1a7f65299 100644 --- a/ethereum/circuits/lib/src/uint256_test.nr +++ b/ethereum/circuits/lib/src/uint256_test.nr @@ -1,135 +1,56 @@ -use crate::uint256::U256; - -global high: u128 = 0x10000000000000000000000000000000; -global low: u128 = 0; - -global big_number: U256 = U256::new(high, low); - -#[test] -fn success() { - assert_eq(big_number.high, high); - assert_eq(big_number.low, low); -} - -#[test] -fn new() { - let big_number = U256::new(high, low); - assert_eq(big_number.high, high); - assert_eq(big_number.low, low); -} - -#[test] -fn zero_and_one() { - let zero = U256::zero(); - assert_eq(zero.high, 0); - assert_eq(zero.low, 0); - - let one = U256::one(); - assert_eq(one.high, 0); - assert_eq(one.low, 1); -} - mod from_bytes32 { - use crate::uint256::U256; - global high: u128 = 0x10000000000000000000000000000000; - global low: u128 = 0; - global limit: u128 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF; + use crate::uint256::from; + use dep::bignum::bignum::BigNum; + use dep::bignum::U256; - global big_number: U256 = U256::new(high, low); - global limit_u256: U256 = U256::new(limit, limit); + global limit_u256: U256 = U256::zero() - U256::one(); + + global big_number: U256 = U256::modulus().udiv(U256::from(16)); #[test] fn zero() { let bytes = [0x00; 32]; - assert_eq(U256::from(bytes), U256::zero()); + assert_eq(from(bytes), U256::zero()); } #[test] fn success() { let mut bytes = [0x00; 32]; - bytes[31] = 0x10; - assert_eq(U256::from(bytes), big_number); + bytes[0] = 0x10; + assert_eq(from(bytes), big_number); } #[test] fn u256_limit() { let bytes = [0xff; 32]; - assert_eq(U256::from(bytes), limit_u256); + assert_eq(from(bytes), limit_u256); } } mod into_bytes32 { - use crate::uint256::U256; - global high: u128 = 0x10000000000000000000000000000000; - global low: u128 = 0; - global limit: u128 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF; + use crate::uint256::into; + use dep::bignum::bignum::BigNum; + use dep::bignum::U256; - global big_number: U256 = U256::new(high, low); - global limit_u256: U256 = U256::new(limit, limit); + global big_number: U256 = U256::modulus().udiv(U256::from(16)); + global limit_u256: U256 = U256::zero() - U256::one(); #[test] fn zero() { let bytes = [0x00; 32]; - assert_eq(U256::into(U256::zero()), bytes); + assert_eq(into(U256::zero()), bytes); } #[test] fn success() { let mut bytes = [0x00; 32]; - bytes[31] = 0x10; - assert_eq(U256::into(big_number), bytes); + bytes[0] = 0x10; + assert_eq(into(big_number), bytes); } #[test] fn u256_limit() { let bytes = [0xff; 32]; - assert_eq(U256::into(limit_u256), bytes); - } -} - -#[test] -fn eq() { - assert_eq(big_number, big_number); - - let big_number2 = U256::new(high, low); - assert_eq(big_number, big_number2); -} - -#[test] -fn not_eq() { - let big_number2 = U256 { high, low: high }; - assert(big_number != big_number2); - - let big_number3 = U256 { high: low, low }; - assert(big_number != big_number3); -} - -mod trait_add { - use crate::uint256::U256; - global high: u128 = 0x10000000000000000000000000000000; - global low: u128 = 0; - global limit: u128 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF; - - global big_number: U256 = U256::new(high, low); - global limit_u256: U256 = U256::new(limit, limit); - - #[test] - fn sum() { - let sum = big_number + big_number; - - assert_eq(sum, U256 { high: 0x20000000000000000000000000000000, low }); - } - - #[test] - fn sum_with_carry() { - let big_number = U256 { high, low: limit }; - let sum = big_number + U256::one(); - - assert_eq(sum, U256 { high: 0x10000000000000000000000000000001, low }); - } - - #[test(should_fail_with = "attempt to add with overflow")] - fn sum_overflow() { - let _ = limit_u256 + U256::one(); + assert_eq(into(limit_u256), bytes); } } diff --git a/vlayer/ethereum/circuits/lib/Nargo.toml b/vlayer/ethereum/circuits/lib/Nargo.toml index 46f7dbcec..19f3ef40d 100644 --- a/vlayer/ethereum/circuits/lib/Nargo.toml +++ b/vlayer/ethereum/circuits/lib/Nargo.toml @@ -5,4 +5,5 @@ compiler_version = ">=0.30.0" [dependencies] ethereum = { path = "../../../../ethereum/circuits/lib" } -keccak256 = {tag = "v0.1.1", git = "https://github.com/noir-lang/keccak256" } \ No newline at end of file +keccak256 = {tag = "v0.1.1", git = "https://github.com/noir-lang/keccak256" } +bignum = {tag = "v0.8.3", git = "https://github.com/noir-lang/noir-bignum" } \ No newline at end of file diff --git a/vlayer/ethereum/circuits/lib/src/nft.nr b/vlayer/ethereum/circuits/lib/src/nft.nr index 3d1f5f5e5..84c8ee650 100644 --- a/vlayer/ethereum/circuits/lib/src/nft.nr +++ b/vlayer/ethereum/circuits/lib/src/nft.nr @@ -1,10 +1,11 @@ +use dep::bignum::U256; use dep::ethereum::{ account_with_storage::get_account_with_storage, misc::{ arrays::subarray_inferred_len, types::{Address, ADDRESS_LENGTH, Bytes32, BYTES32_LENGTH}, }, - uint256::U256, + uint256::into, }; pub struct ERC721Token { @@ -20,12 +21,8 @@ pub trait ERC721 { impl ERC721 for ERC721Token { fn get_owner(self, token_id: Bytes32, block_number: u64) -> Address { let storage_key = (self.token_id_to_slot)(token_id); - let account = get_account_with_storage( - self.chain_id, - block_number, - self.address, - U256::into(storage_key), - ); + let account = + get_account_with_storage(self.chain_id, block_number, self.address, into(storage_key)); subarray_inferred_len(account.values[0], BYTES32_LENGTH - ADDRESS_LENGTH) } } diff --git a/vlayer/ethereum/circuits/lib/src/nft_list.nr b/vlayer/ethereum/circuits/lib/src/nft_list.nr index f8e66935a..4ecbdbc62 100644 --- a/vlayer/ethereum/circuits/lib/src/nft_list.nr +++ b/vlayer/ethereum/circuits/lib/src/nft_list.nr @@ -2,7 +2,7 @@ pub mod mainnet { use crate::chain_id; use crate::nft::ERC721Token; use crate::slot::{dynamic_array_with_precalculated_slot, mapping, struct_slot}; - use dep::ethereum::uint256::U256; + use dep::ethereum::uint256::{from, from_field}; use dep::std::field::bytes32_to_field; pub fn BORED_APE_YACHT_CLUB() -> ERC721Token { @@ -13,7 +13,7 @@ pub mod mainnet { ], token_id_to_slot: |token_id| { let BORED_APE_YACHT_CLUB_MAX_TOKEN_ID: u32 = 9999; - let BORED_APE_YACHT_CLUB_TOKEN_OWNERS_INNER_ENTRIES_SLOT = U256::from([ + let BORED_APE_YACHT_CLUB_TOKEN_OWNERS_INNER_ENTRIES_SLOT = from([ 0x40, 0x57, 0x87, 0xfa, 0x12, 0xa8, 0x23, 0xe0, 0xf2, 0xb7, 0x63, 0x1c, 0xc4, 0x1b, 0x3b, 0xa8, 0x82, 0x8b, 0x33, 0x21, 0xca, 0x81, 0x11, 0x11, 0xfa, 0x75, 0xcd, 0x3a, 0xa3, 0xbb, 0x5a, 0xce, @@ -43,7 +43,7 @@ pub mod mainnet { ], token_id_to_slot: |token_id| { let CRYPTO_PUNK_MAX_TOKEN_ID: u32 = 9999; - let CRYPTO_PUNK_TOKEN_OWNERS_INNER_ENTRIES_SLOT = U256::from_field(10); + let CRYPTO_PUNK_TOKEN_OWNERS_INNER_ENTRIES_SLOT = from_field(10); assert( bytes32_to_field(token_id) as u32 <= CRYPTO_PUNK_MAX_TOKEN_ID, diff --git a/vlayer/ethereum/circuits/lib/src/slot.nr b/vlayer/ethereum/circuits/lib/src/slot.nr index c88a93da5..0235de752 100644 --- a/vlayer/ethereum/circuits/lib/src/slot.nr +++ b/vlayer/ethereum/circuits/lib/src/slot.nr @@ -1,5 +1,5 @@ -use dep::ethereum::misc::types::Bytes32; -use dep::ethereum::uint256::U256; +use dep::bignum::U256; +use dep::ethereum::{misc::types::Bytes32, uint256::{from, from_field, into}}; use dep::keccak256::keccak256; global STORAGE_KEY_HASH_INPUT_LENGTH: u32 = 64; @@ -7,19 +7,24 @@ global STORAGE_KEY_HASH_INPUT_LENGTH: u32 = 64; pub(crate) fn mapping(slot: U256, key: Bytes32) -> U256 { let mut vector: BoundedVec = BoundedVec::new(); vector.extend_from_array(key); - vector.extend_from_array(U256::into(slot)); - U256::from(keccak256(vector.storage(), STORAGE_KEY_HASH_INPUT_LENGTH)) + vector.extend_from_array(into(slot)); + from(keccak256(vector.storage(), STORAGE_KEY_HASH_INPUT_LENGTH)) } pub(crate) fn dynamic_array(slot: U256, size: Field, index: Field) -> U256 { - let start: U256 = U256::from(keccak256(U256::into(slot), 32)); + let start: U256 = from(keccak256::<32>(into(slot), 32)); dynamic_array_with_precalculated_slot(start, size, index) } pub(crate) fn dynamic_array_with_precalculated_slot(slot: U256, size: Field, index: Field) -> U256 { - slot + U256::from_field(size * index) + let product = from_field(size * index); + let sum = slot + product; + assert(sum >= slot, "Attempt to add with overflow"); + sum } pub(crate) fn struct_slot(slot: U256, offset: Field) -> U256 { - slot + U256::from_field(offset) + let sum = slot + from_field(offset); + assert(sum >= slot, "Attempt to add with overflow"); + sum } diff --git a/vlayer/ethereum/circuits/lib/src/slot_test.nr b/vlayer/ethereum/circuits/lib/src/slot_test.nr index ebcc587cf..70fe9bac6 100644 --- a/vlayer/ethereum/circuits/lib/src/slot_test.nr +++ b/vlayer/ethereum/circuits/lib/src/slot_test.nr @@ -1,17 +1,19 @@ mod mapping { use crate::slot::mapping; - use dep::ethereum::misc::bytes32::{address_to_bytes32, field_to_bytes32}; - use dep::ethereum::uint256::U256; + use dep::ethereum::{ + misc::bytes32::{address_to_bytes32, field_to_bytes32}, + uint256::{from, from_field}, + }; #[test] fn adress_mapping() { - let slot = U256::from_field(9); + let slot = from_field(9); let address = [ 0x55, 0xfe, 0x00, 0x2a, 0xef, 0xf0, 0x2f, 0x77, 0x36, 0x4d, 0xe3, 0x39, 0xa1, 0x29, 0x29, 0x23, 0xa1, 0x58, 0x44, 0xb8, ]; assert_eq( - U256::from([ + from([ 0x57, 0xd1, 0x8a, 0xf7, 0x93, 0xd7, 0x30, 0x0c, 0x4b, 0xa4, 0x6d, 0x19, 0x2e, 0xc7, 0xaa, 0x09, 0x50, 0x70, 0xdd, 0xe6, 0xc5, 0x2c, 0x68, 0x7c, 0x6d, 0x0d, 0x92, 0xfb, 0x85, 0x32, 0xb3, 0x05, @@ -22,7 +24,7 @@ mod mapping { #[test] fn mapping_of_mapping() { - let slot = U256::from_field(1); + let slot = from_field(1); let owner = [ 0x5d, 0x82, 0x72, 0x63, 0x05, 0x2a, 0x88, 0x64, 0x23, 0xdc, 0xb1, 0x75, 0xef, 0x9b, 0x74, 0x91, 0xcc, 0x92, 0xed, 0x24, @@ -33,7 +35,7 @@ mod mapping { ]; assert_eq( - U256::from([ + from([ 0x51, 0x5a, 0xd6, 0x04, 0x70, 0x94, 0xbd, 0x71, 0x0c, 0x10, 0x72, 0xcd, 0xa4, 0x7b, 0xc6, 0xcf, 0x43, 0x9c, 0x55, 0xdc, 0xf0, 0xb0, 0xd6, 0x0e, 0x8a, 0x9b, 0xe5, 0x57, 0xf1, 0x86, 0x81, 0x1c, @@ -47,9 +49,9 @@ mod mapping { #[test] fn uint_mapping() { - let slot = U256::from_field(10); + let slot = from_field(10); let uint = 0; - let expected_slot = U256::from([ + let expected_slot = from([ 0x13, 0xda, 0x86, 0x00, 0x8b, 0xa1, 0xc6, 0x92, 0x2d, 0xae, 0xe3, 0xe0, 0x7d, 0xb9, 0x53, 0x05, 0xef, 0x49, 0xeb, 0xce, 0xd9, 0xf5, 0x46, 0x7a, 0x0b, 0x86, 0x13, 0xfc, 0xc6, 0xb3, 0x43, 0xe3, @@ -61,12 +63,12 @@ mod mapping { mod dynamic_array { use crate::slot::dynamic_array; use dep::ethereum::misc::bytes32::MAX_FIELD_VALUE; - use dep::ethereum::uint256::U256; + use dep::ethereum::uint256::{from, from_field}; #[test] fn index_zero() { - let slot = U256::from_field(2); - let expected_slot = U256::from([ + let slot = from_field(2); + let expected_slot = from([ 0x40, 0x57, 0x87, 0xfa, 0x12, 0xa8, 0x23, 0xe0, 0xf2, 0xb7, 0x63, 0x1c, 0xc4, 0x1b, 0x3b, 0xa8, 0x82, 0x8b, 0x33, 0x21, 0xca, 0x81, 0x11, 0x11, 0xfa, 0x75, 0xcd, 0x3a, 0xa3, 0xbb, 0x5a, 0xce, @@ -76,8 +78,8 @@ mod dynamic_array { #[test] fn index_positive() { - let slot = U256::from_field(2); - let expected_slot = U256::from([ + let slot = from_field(2); + let expected_slot = from([ 0x40, 0x57, 0x87, 0xfa, 0x12, 0xa8, 0x23, 0xe0, 0xf2, 0xb7, 0x63, 0x1c, 0xc4, 0x1b, 0x3b, 0xa8, 0x82, 0x8b, 0x33, 0x21, 0xca, 0x81, 0x11, 0x11, 0xfa, 0x75, 0xcd, 0x3a, 0xa3, 0xbb, 0x5a, 0xdc, @@ -85,65 +87,64 @@ mod dynamic_array { assert_eq(expected_slot, dynamic_array(slot, 2, 7)); } - #[test(should_fail_with = "attempt to add with overflow")] + #[test(should_fail_with = "Attempt to add with overflow")] fn fail_overflow() { - let slot = U256::from_field(6); + let slot = from_field(6); let _ = dynamic_array(slot, 2, MAX_FIELD_VALUE); } } mod dynamic_array_with_precalculated_slot { use crate::slot::dynamic_array_with_precalculated_slot; - use dep::ethereum::misc::bytes32::MAX_FIELD_VALUE; - use dep::ethereum::uint256::U256; + use dep::ethereum::{misc::bytes32::MAX_FIELD_VALUE, uint256::{from, from_field}}; #[test] fn index_zero() { - let slot = U256::from_field(2); + let slot = from_field(2); assert_eq(slot, dynamic_array_with_precalculated_slot(slot, 2, 0)); } #[test] fn index_positive() { - let slot = U256::from_field(2); - let expected_slot = U256::from_field(2 + 2 * 7); + let slot = from_field(2); + let expected_slot = from_field(2 + 2 * 7); assert_eq(expected_slot, dynamic_array_with_precalculated_slot(slot, 2, 7)); } #[test] fn big_index() { - let slot = U256::from([0x09; 32]); - let expected_slot = U256::from([ + let slot = from([0x09; 32]); + let expected_slot = from([ 0x9, 0x9, 0x9, 0x9, 0x9, 0x9, 0x9, 0x9, 0x9, 0x9, 0x9, 0x9, 0x9, 0x9, 0x9, 0x9, 0x9, 0x9, 0x9, 0x9, 0x9, 0x9, 0x9, 0x9, 0x9, 0x9, 0x9, 0x0b, 0x5d, 0x14, 0xed, 9, ]); assert_eq(expected_slot, dynamic_array_with_precalculated_slot(slot, 10, 1_000_000_000)); } - #[test(should_fail_with = "attempt to add with overflow")] + #[test(should_fail_with = "Attempt to add with overflow")] fn fail_overflow() { - let slot = U256::from([0xfe; 32]); + let slot = from([0xfe; 32]); let _ = dynamic_array_with_precalculated_slot(slot, 2, MAX_FIELD_VALUE); } } mod struct_slot { use crate::slot::struct_slot; - use dep::ethereum::uint256::U256; + use ethereum::uint256::{from, from_field}; #[test] fn success() { - let slot = U256::from_field(2); - let expected_slot = U256::from([ + let slot = from_field(2); + let expected_slot = from([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, ]); assert_eq(expected_slot, struct_slot(slot, 256)); } - #[test(should_fail_with = "attempt to add with overflow")] + #[test(should_fail_with = "Attempt to add with overflow")] fn overflow() { - let slot = U256::from([0xff; 32]); + let slot = from([0xff; 32]); let _ = struct_slot(slot, 256); } } diff --git a/vlayer/ethereum/circuits/lib/src/token.nr b/vlayer/ethereum/circuits/lib/src/token.nr index 9405e8a36..116b0982b 100644 --- a/vlayer/ethereum/circuits/lib/src/token.nr +++ b/vlayer/ethereum/circuits/lib/src/token.nr @@ -1,10 +1,11 @@ use crate::slot::mapping; +use dep::bignum::U256; use dep::ethereum::{ account_with_storage::get_account_with_storage, account_with_storage_recursive::get_account_with_storage_recursive, misc::{bytes32::address_to_bytes32, types::Address}, + uint256::into, }; -use dep::ethereum::uint256::U256; use dep::std::field::bytes32_to_field; global TOKEN_BALANCE_INDEX: u32 = 0; @@ -34,15 +35,10 @@ impl ERC20 for ERC20Token { self.chain_id, block_number, self.address, - U256::into(storage_key), + into(storage_key), ) } else { - get_account_with_storage( - self.chain_id, - block_number, - self.address, - U256::into(storage_key), - ) + get_account_with_storage(self.chain_id, block_number, self.address, into(storage_key)) }; let balance = account.values[TOKEN_BALANCE_INDEX]; diff --git a/vlayer/ethereum/circuits/lib/src/token_int_test.nr b/vlayer/ethereum/circuits/lib/src/token_int_test.nr index e533e4ef9..d2fdfe8c7 100644 --- a/vlayer/ethereum/circuits/lib/src/token_int_test.nr +++ b/vlayer/ethereum/circuits/lib/src/token_int_test.nr @@ -15,8 +15,8 @@ mod test_ERC20Token { storage_proof::proofs_serialized, }; use dep::ethereum::serde::Serde; - use dep::ethereum::uint256::U256; use dep::std::test::OracleMock; + use ethereum::uint256::from_field; #[test] fn success() { @@ -45,8 +45,8 @@ mod test_ERC20Token { 0xa0, 0xb8, 0x69, 0x91, 0xc6, 0x21, 0x8b, 0x36, 0xc1, 0xd1, 0x9d, 0x4a, 0x2e, 0x9e, 0xb0, 0xce, 0x36, 0x06, 0xeb, 0x48, ], - balances_slot: U256::from_field(9), - allowances_slot: U256::from_field(10), + balances_slot: from_field(9), + allowances_slot: from_field(10), chain_id: MAINNET, }; diff --git a/vlayer/ethereum/circuits/lib/src/token_list.nr b/vlayer/ethereum/circuits/lib/src/token_list.nr index b1bee9417..74b316ca1 100644 --- a/vlayer/ethereum/circuits/lib/src/token_list.nr +++ b/vlayer/ethereum/circuits/lib/src/token_list.nr @@ -1,15 +1,15 @@ pub mod mainnet { use crate::chain_id; use crate::token::ERC20Token; - use dep::ethereum::uint256::U256; + use dep::ethereum::uint256::from_field; pub global USDC: ERC20Token = ERC20Token { address: [ 0xa0, 0xb8, 0x69, 0x91, 0xc6, 0x21, 0x8b, 0x36, 0xc1, 0xd1, 0x9d, 0x4a, 0x2e, 0x9e, 0xb0, 0xce, 0x36, 0x06, 0xeb, 0x48, ], - balances_slot: U256::from_field(9), - allowances_slot: U256::from_field(10), + balances_slot: from_field(9), + allowances_slot: from_field(10), chain_id: chain_id::MAINNET, }; }