From 6280a51c318e1abbe3c0e530849f71913369b708 Mon Sep 17 00:00:00 2001 From: Anna Jablonowska Date: Fri, 24 May 2024 12:12:27 +0200 Subject: [PATCH 01/18] Create file --- vlayer/ethereum/circuits/lib/src/README.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 vlayer/ethereum/circuits/lib/src/README.md diff --git a/vlayer/ethereum/circuits/lib/src/README.md b/vlayer/ethereum/circuits/lib/src/README.md new file mode 100644 index 000000000..e69de29bb From 791baa3a21bc3d04fc516d5ad483dd1252ef1692 Mon Sep 17 00:00:00 2001 From: Anna Jablonowska Date: Thu, 23 May 2024 17:06:41 +0200 Subject: [PATCH 02/18] use field_to_bytes32 --- vlayer/ethereum/circuits/lib/src/slot_test.nr | 49 ++++++------------- 1 file changed, 15 insertions(+), 34 deletions(-) diff --git a/vlayer/ethereum/circuits/lib/src/slot_test.nr b/vlayer/ethereum/circuits/lib/src/slot_test.nr index 4e48ede2d..f76409e1d 100644 --- a/vlayer/ethereum/circuits/lib/src/slot_test.nr +++ b/vlayer/ethereum/circuits/lib/src/slot_test.nr @@ -1,13 +1,11 @@ mod mapping { use crate::slot::mapping; - use dep::ethereum::{misc::bytes32::address_to_bytes32}; - use dep::ethereum::misc::{field::field_to_bytes32}; + use dep::ethereum::misc::bytes32::address_to_bytes32; + use dep::ethereum::misc::field::field_to_bytes32; #[test] fn adress_mapping() { - let slot = [ - 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, 0, 9 - ]; + let slot = field_to_bytes32(9); let address = [ 0x55, 0xfe, 0x00, 0x2a, 0xef, 0xf0, 0x2f, 0x77, 0x36, 0x4d, 0xe3, 0x39, 0xa1, 0x29, 0x29, 0x23, 0xa1, 0x58, 0x44, 0xb8 ]; @@ -20,9 +18,7 @@ mod mapping { #[test] fn mapping_of_mapping() { - let slot = [ - 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, 0, 1 - ]; + let slot = field_to_bytes32(1); let owner = [ 0x5d, 0x82, 0x72, 0x63, 0x05, 0x2a, 0x88, 0x64, 0x23, 0xdc, 0xb1, 0x75, 0xef, 0x9b, 0x74, 0x91, 0xcc, 0x92, 0xed, 0x24 ]; @@ -53,13 +49,11 @@ mod mapping { mod dynamic_array { use crate::slot::dynamic_array; - use dep::ethereum::misc::field_test::MAX_FIELD_VALUE; + use dep::ethereum::misc::{field_test::MAX_FIELD_VALUE, field::field_to_bytes32}; #[test] fn index_zero() { - let slot = [ - 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, 0, 2 - ]; + let slot = field_to_bytes32(2); let expected_slot = [ 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 ]; @@ -68,9 +62,7 @@ mod dynamic_array { #[test] fn index_positive() { - let slot = [ - 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, 0, 2 - ]; + let slot = field_to_bytes32(2); let expected_slot = [ 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 ]; @@ -79,33 +71,25 @@ mod dynamic_array { #[test(should_fail_with="Addition overflow")] fn fail_overflow() { - let slot = [ - 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, 0, 6 - ]; + let slot = field_to_bytes32(6); let _ = dynamic_array(slot, MAX_FIELD_VALUE, 2); } } mod dynamic_array_with_precalculated_slot { use crate::slot::dynamic_array_with_precalculated_slot; - use dep::ethereum::misc::field_test::MAX_FIELD_VALUE; + use dep::ethereum::misc::{field_test::MAX_FIELD_VALUE, field::field_to_bytes32}; #[test] fn index_zero() { - let slot = [ - 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, 0, 2 - ]; + let slot = field_to_bytes32(2); assert_eq(slot, dynamic_array_with_precalculated_slot(slot, 0, 2)); } #[test] fn index_positive() { - let slot = [ - 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, 0, 2 - ]; - let expected_slot = [ - 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, 0, 2 + 7 * 2 - ]; + let slot = field_to_bytes32(2); + let expected_slot = field_to_bytes32(2 + 2 * 7); assert_eq(expected_slot, dynamic_array_with_precalculated_slot(slot, 7, 2)); } @@ -127,12 +111,11 @@ mod dynamic_array_with_precalculated_slot { mod struct_slot { use crate::slot::struct_slot; + use dep::ethereum::misc::field::field_to_bytes32; #[test] fn success() { - let slot = [ - 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, 0, 2 - ]; + let slot = field_to_bytes32(2); let expected_slot = [ 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 ]; @@ -142,9 +125,7 @@ mod struct_slot { #[test(should_fail_with="Addition overflow")] fn overflow() { let slot = [0xff; 32]; - let expected_slot = [ - 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, 0, 1 - ]; + let expected_slot = field_to_bytes32(1); assert_eq(expected_slot, struct_slot(slot, 256)); } } From af0e8ce693d47b1281330ed2378710ad36e486b8 Mon Sep 17 00:00:00 2001 From: Anna Jablonowska Date: Thu, 23 May 2024 17:08:23 +0200 Subject: [PATCH 03/18] Fix order of arguments --- vlayer/ethereum/circuits/lib/src/slot_test.nr | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/vlayer/ethereum/circuits/lib/src/slot_test.nr b/vlayer/ethereum/circuits/lib/src/slot_test.nr index f76409e1d..373fa6da9 100644 --- a/vlayer/ethereum/circuits/lib/src/slot_test.nr +++ b/vlayer/ethereum/circuits/lib/src/slot_test.nr @@ -57,7 +57,7 @@ mod dynamic_array { let expected_slot = [ 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 ]; - assert_eq(expected_slot, dynamic_array(slot, 0, 2)); + assert_eq(expected_slot, dynamic_array(slot, 2, 0)); } #[test] @@ -66,13 +66,13 @@ mod dynamic_array { let expected_slot = [ 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 ]; - assert_eq(expected_slot, dynamic_array(slot, 7, 2)); + assert_eq(expected_slot, dynamic_array(slot, 2, 7)); } #[test(should_fail_with="Addition overflow")] fn fail_overflow() { let slot = field_to_bytes32(6); - let _ = dynamic_array(slot, MAX_FIELD_VALUE, 2); + let _ = dynamic_array(slot, 2, MAX_FIELD_VALUE); } } @@ -83,14 +83,14 @@ mod dynamic_array_with_precalculated_slot { #[test] fn index_zero() { let slot = field_to_bytes32(2); - assert_eq(slot, dynamic_array_with_precalculated_slot(slot, 0, 2)); + assert_eq(slot, dynamic_array_with_precalculated_slot(slot, 2, 0)); } #[test] fn index_positive() { let slot = field_to_bytes32(2); let expected_slot = field_to_bytes32(2 + 2 * 7); - assert_eq(expected_slot, dynamic_array_with_precalculated_slot(slot, 7, 2)); + assert_eq(expected_slot, dynamic_array_with_precalculated_slot(slot, 2, 7)); } #[test] @@ -99,13 +99,13 @@ mod dynamic_array_with_precalculated_slot { let expected_slot = [ 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, 1_000_000_000, 10)); + assert_eq(expected_slot, dynamic_array_with_precalculated_slot(slot, 10, 1_000_000_000)); } #[test(should_fail_with="Addition overflow")] fn fail_overflow() { let slot = [0xfe; 32]; - let _ = dynamic_array_with_precalculated_slot(slot, MAX_FIELD_VALUE, 2); + let _ = dynamic_array_with_precalculated_slot(slot, 2, MAX_FIELD_VALUE); } } From f84d1c0cc48444d2a0544180f2a3729803cd472a Mon Sep 17 00:00:00 2001 From: Anna Jablonowska Date: Thu, 23 May 2024 17:10:48 +0200 Subject: [PATCH 04/18] Remove expected from fail test --- vlayer/ethereum/circuits/lib/src/slot_test.nr | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/vlayer/ethereum/circuits/lib/src/slot_test.nr b/vlayer/ethereum/circuits/lib/src/slot_test.nr index 373fa6da9..f5e00e683 100644 --- a/vlayer/ethereum/circuits/lib/src/slot_test.nr +++ b/vlayer/ethereum/circuits/lib/src/slot_test.nr @@ -125,7 +125,6 @@ mod struct_slot { #[test(should_fail_with="Addition overflow")] fn overflow() { let slot = [0xff; 32]; - let expected_slot = field_to_bytes32(1); - assert_eq(expected_slot, struct_slot(slot, 256)); + let _ = struct_slot(slot, 256); } } From 250af5c111cce7ec8ef37a959e8e2efb712536bb Mon Sep 17 00:00:00 2001 From: Anna Jablonowska Date: Thu, 23 May 2024 17:24:03 +0200 Subject: [PATCH 05/18] Move field_to_bytes32 --- ethereum/circuits/lib/src/misc.nr | 3 +-- ethereum/circuits/lib/src/misc/bytes32.nr | 9 +++++++++ .../lib/src/misc/{field_test.nr => bytes32_test.nr} | 6 +++--- ethereum/circuits/lib/src/misc/field.nr | 8 -------- ethereum/circuits/lib/src/misc/fragment_int_test.nr | 2 +- vlayer/ethereum/circuits/lib/src/nft_list.nr | 4 ++-- vlayer/ethereum/circuits/lib/src/slot.nr | 5 ++++- vlayer/ethereum/circuits/lib/src/slot_test.nr | 8 ++++---- vlayer/examples/circuits/is_ape_owner/src/main.nr | 2 +- .../examples/circuits/is_crypto_punk_owner/src/main.nr | 2 +- 10 files changed, 26 insertions(+), 23 deletions(-) rename ethereum/circuits/lib/src/misc/{field_test.nr => bytes32_test.nr} (89%) delete mode 100644 ethereum/circuits/lib/src/misc/field.nr diff --git a/ethereum/circuits/lib/src/misc.nr b/ethereum/circuits/lib/src/misc.nr index 33db4f357..bb5b2bd53 100644 --- a/ethereum/circuits/lib/src/misc.nr +++ b/ethereum/circuits/lib/src/misc.nr @@ -9,8 +9,7 @@ mod fragment_test; mod fragment_int_test; mod types; mod option; -mod field; -mod field_test; mod bytes32; +mod bytes32_test; mod uint256; mod uint256_test; diff --git a/ethereum/circuits/lib/src/misc/bytes32.nr b/ethereum/circuits/lib/src/misc/bytes32.nr index 25bfee16e..4bc74d1bb 100644 --- a/ethereum/circuits/lib/src/misc/bytes32.nr +++ b/ethereum/circuits/lib/src/misc/bytes32.nr @@ -8,3 +8,12 @@ pub fn address_to_bytes32(address: [u8; 20]) -> Bytes32 { } bytes32 } + +pub fn field_to_bytes32(value: Field) -> [u8; 32] { + let byte_array = value.to_be_bytes(32); + let mut bytes = [0; 32]; + for i in 0..32 { + bytes[i] = byte_array[i]; + } + bytes +} diff --git a/ethereum/circuits/lib/src/misc/field_test.nr b/ethereum/circuits/lib/src/misc/bytes32_test.nr similarity index 89% rename from ethereum/circuits/lib/src/misc/field_test.nr rename to ethereum/circuits/lib/src/misc/bytes32_test.nr index 5e0a29fe1..e6bca39f5 100644 --- a/ethereum/circuits/lib/src/misc/field_test.nr +++ b/ethereum/circuits/lib/src/misc/bytes32_test.nr @@ -1,8 +1,8 @@ global MAX_FIELD_VALUE = 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000000; -mod test_field_to_bytes32 { - use crate::misc::field::field_to_bytes32; - use crate::misc::field_test::MAX_FIELD_VALUE; +mod field_to_bytes32 { + use crate::misc::bytes32::field_to_bytes32; + use crate::misc::bytes32_test::MAX_FIELD_VALUE; #[test] fn zero() { diff --git a/ethereum/circuits/lib/src/misc/field.nr b/ethereum/circuits/lib/src/misc/field.nr deleted file mode 100644 index 5493a3f56..000000000 --- a/ethereum/circuits/lib/src/misc/field.nr +++ /dev/null @@ -1,8 +0,0 @@ -pub fn field_to_bytes32(value: Field) -> [u8; 32] { - let byte_array = value.to_be_bytes(32); - let mut bytes = [0; 32]; - for i in 0..32 { - bytes[i] = byte_array[i]; - } - bytes -} diff --git a/ethereum/circuits/lib/src/misc/fragment_int_test.nr b/ethereum/circuits/lib/src/misc/fragment_int_test.nr index b55ebaacb..baffb69bc 100644 --- a/ethereum/circuits/lib/src/misc/fragment_int_test.nr +++ b/ethereum/circuits/lib/src/misc/fragment_int_test.nr @@ -21,7 +21,7 @@ mod u8_fragment { } mod Field_fragment { - use crate::misc::{fragment::Fragment, field_test::MAX_FIELD_VALUE}; + use crate::misc::{fragment::Fragment, bytes32_test::MAX_FIELD_VALUE}; #[test] fn success() { diff --git a/vlayer/ethereum/circuits/lib/src/nft_list.nr b/vlayer/ethereum/circuits/lib/src/nft_list.nr index ff0c26e44..dcee23f57 100644 --- a/vlayer/ethereum/circuits/lib/src/nft_list.nr +++ b/vlayer/ethereum/circuits/lib/src/nft_list.nr @@ -3,7 +3,7 @@ mod mainnet { use crate::chain_id; use crate::slot::{struct_slot, dynamic_array_with_precalculated_slot, mapping}; use dep::std::field::bytes32_to_field; - use dep::ethereum::misc::{types::Bytes32, field::field_to_bytes32}; + use dep::ethereum::misc::{types::Bytes32, bytes32::field_to_bytes32}; pub fn BORED_APE_YACHT_CLUB() -> ERC721Token { ERC721Token { @@ -47,7 +47,7 @@ mod sepolia { use crate::chain_id; use crate::slot::{struct_slot, dynamic_array_with_precalculated_slot}; use dep::std::field::bytes32_to_field; - use dep::ethereum::misc::{types::Bytes32, field::field_to_bytes32}; + use dep::ethereum::misc::{types::Bytes32, bytes32::field_to_bytes32}; // free mint: https://sepolia.etherscan.io/address/0x80d97726548fedae6ad7cf8df4f2b514fd24afba#readContract fn FAKE_BORED_APE_YACHT_CLUB() -> ERC721Token { diff --git a/vlayer/ethereum/circuits/lib/src/slot.nr b/vlayer/ethereum/circuits/lib/src/slot.nr index 134ab25ff..7d5358b97 100644 --- a/vlayer/ethereum/circuits/lib/src/slot.nr +++ b/vlayer/ethereum/circuits/lib/src/slot.nr @@ -1,4 +1,7 @@ -use dep::ethereum::misc::{types::{Address, Bytes32, ADDRESS_LENGTH, BYTES32_LENGTH}, field::field_to_bytes32, bytes::add_bigint}; +use dep::ethereum::misc::{ + types::{Address, Bytes32, ADDRESS_LENGTH, BYTES32_LENGTH}, bytes32::field_to_bytes32, + bytes::add_bigint +}; use dep::std::hash::keccak256; global STORAGE_KEY_HASH_INPUT_LENGTH = 64; diff --git a/vlayer/ethereum/circuits/lib/src/slot_test.nr b/vlayer/ethereum/circuits/lib/src/slot_test.nr index f5e00e683..078cd93fc 100644 --- a/vlayer/ethereum/circuits/lib/src/slot_test.nr +++ b/vlayer/ethereum/circuits/lib/src/slot_test.nr @@ -1,7 +1,7 @@ mod mapping { use crate::slot::mapping; use dep::ethereum::misc::bytes32::address_to_bytes32; - use dep::ethereum::misc::field::field_to_bytes32; + use dep::ethereum::misc::bytes32::field_to_bytes32; #[test] fn adress_mapping() { @@ -49,7 +49,7 @@ mod mapping { mod dynamic_array { use crate::slot::dynamic_array; - use dep::ethereum::misc::{field_test::MAX_FIELD_VALUE, field::field_to_bytes32}; + use dep::ethereum::misc::{bytes32_test::MAX_FIELD_VALUE, bytes32::field_to_bytes32}; #[test] fn index_zero() { @@ -78,7 +78,7 @@ mod dynamic_array { mod dynamic_array_with_precalculated_slot { use crate::slot::dynamic_array_with_precalculated_slot; - use dep::ethereum::misc::{field_test::MAX_FIELD_VALUE, field::field_to_bytes32}; + use dep::ethereum::misc::{bytes32_test::MAX_FIELD_VALUE, bytes32::field_to_bytes32}; #[test] fn index_zero() { @@ -111,7 +111,7 @@ mod dynamic_array_with_precalculated_slot { mod struct_slot { use crate::slot::struct_slot; - use dep::ethereum::misc::field::field_to_bytes32; + use dep::ethereum::misc::bytes32::field_to_bytes32; #[test] fn success() { diff --git a/vlayer/examples/circuits/is_ape_owner/src/main.nr b/vlayer/examples/circuits/is_ape_owner/src/main.nr index 719f5c432..0abae8afc 100644 --- a/vlayer/examples/circuits/is_ape_owner/src/main.nr +++ b/vlayer/examples/circuits/is_ape_owner/src/main.nr @@ -1,7 +1,7 @@ use dep::ethereum::misc::types::Address; use dep::token::nft_list::mainnet::BORED_APE_YACHT_CLUB; use dep::std::field::bytes32_to_field; -use dep::ethereum::misc::field::field_to_bytes32; +use dep::ethereum::misc::bytes32::field_to_bytes32; mod main_test; diff --git a/vlayer/examples/circuits/is_crypto_punk_owner/src/main.nr b/vlayer/examples/circuits/is_crypto_punk_owner/src/main.nr index da492d5b3..7a2e309c5 100644 --- a/vlayer/examples/circuits/is_crypto_punk_owner/src/main.nr +++ b/vlayer/examples/circuits/is_crypto_punk_owner/src/main.nr @@ -1,7 +1,7 @@ use dep::ethereum::misc::types::Address; use dep::token::nft_list::mainnet::CRYPTO_PUNK; use dep::std::field::bytes32_to_field; -use dep::ethereum::misc::field::field_to_bytes32; +use dep::ethereum::misc::bytes32::field_to_bytes32; mod main_test; From c19cda8c8774bb6a07639e1b0e0af0068482852f Mon Sep 17 00:00:00 2001 From: Anna Jablonowska Date: Fri, 24 May 2024 12:36:17 +0200 Subject: [PATCH 06/18] Move uint256 to main library part --- ethereum/circuits/lib/src/lib.nr | 2 ++ ethereum/circuits/lib/src/misc.nr | 2 -- ethereum/circuits/lib/src/serde.nr | 3 ++- ethereum/circuits/lib/src/serde_test.nr | 2 +- ethereum/circuits/lib/src/{misc => }/uint256.nr | 0 ethereum/circuits/lib/src/{misc => }/uint256_test.nr | 2 +- 6 files changed, 6 insertions(+), 5 deletions(-) rename ethereum/circuits/lib/src/{misc => }/uint256.nr (100%) rename ethereum/circuits/lib/src/{misc => }/uint256_test.nr (98%) diff --git a/ethereum/circuits/lib/src/lib.nr b/ethereum/circuits/lib/src/lib.nr index a706f1ddd..09c7cdf54 100644 --- a/ethereum/circuits/lib/src/lib.nr +++ b/ethereum/circuits/lib/src/lib.nr @@ -16,6 +16,8 @@ mod misc; mod chain; mod serde; mod serde_test; +mod uint256; +mod uint256_test; mod fixtures; mod verifiers; diff --git a/ethereum/circuits/lib/src/misc.nr b/ethereum/circuits/lib/src/misc.nr index bb5b2bd53..de3297565 100644 --- a/ethereum/circuits/lib/src/misc.nr +++ b/ethereum/circuits/lib/src/misc.nr @@ -11,5 +11,3 @@ mod types; mod option; mod bytes32; mod bytes32_test; -mod uint256; -mod uint256_test; diff --git a/ethereum/circuits/lib/src/serde.nr b/ethereum/circuits/lib/src/serde.nr index 0f8337671..18b7af8f8 100644 --- a/ethereum/circuits/lib/src/serde.nr +++ b/ethereum/circuits/lib/src/serde.nr @@ -13,7 +13,8 @@ use crate::account_with_storage::{ MAX_PREFIXED_KEY_NIBBLE_LEN as STORAGE_MAX_PREFIXED_KEY_NIBBLE_LEN, MAX_STORAGE_DEPTH_NO_LEAF_M, MAX_STORAGE_VALUE_LEN, MAX_STORAGE_LEAF_LEN }; -use crate::misc::{fragment::Fragment, types::{BYTES32_LENGTH, Bytes32, ADDRESS_LENGTH, Address}, uint256::U256}; +use crate::misc::{fragment::Fragment, types::{BYTES32_LENGTH, Bytes32, ADDRESS_LENGTH, Address}}; +use crate::uint256::U256; use crate::merkle_patricia_proofs::proof::{ProofInput, Proof, Node, MAX_NODE_LEN}; use dep::std::unsafe::zeroed; diff --git a/ethereum/circuits/lib/src/serde_test.nr b/ethereum/circuits/lib/src/serde_test.nr index fd672cf7c..bba60f79c 100644 --- a/ethereum/circuits/lib/src/serde_test.nr +++ b/ethereum/circuits/lib/src/serde_test.nr @@ -14,7 +14,7 @@ mod U128_ { } mod U256 { - use crate::misc::uint256::U256; + use crate::uint256::U256; use crate::serde::U256_SERIALIZED_LEN; global u128_number = 0x10000000000000000000000000000000; diff --git a/ethereum/circuits/lib/src/misc/uint256.nr b/ethereum/circuits/lib/src/uint256.nr similarity index 100% rename from ethereum/circuits/lib/src/misc/uint256.nr rename to ethereum/circuits/lib/src/uint256.nr diff --git a/ethereum/circuits/lib/src/misc/uint256_test.nr b/ethereum/circuits/lib/src/uint256_test.nr similarity index 98% rename from ethereum/circuits/lib/src/misc/uint256_test.nr rename to ethereum/circuits/lib/src/uint256_test.nr index 4d202cd63..175ff768a 100644 --- a/ethereum/circuits/lib/src/misc/uint256_test.nr +++ b/ethereum/circuits/lib/src/uint256_test.nr @@ -1,4 +1,4 @@ -use crate::misc::uint256::U256; +use crate::uint256::U256; global high = U128::from_integer(0x10000000000000000000000000000000); global low = U128::zero(); From 6848ccf6bcaa00d3c5df9bb68969324e5b972fc5 Mon Sep 17 00:00:00 2001 From: Anna Jablonowska Date: Fri, 24 May 2024 12:42:00 +0200 Subject: [PATCH 07/18] Add uint256 in main readme --- ethereum/circuits/lib/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/ethereum/circuits/lib/README.md b/ethereum/circuits/lib/README.md index 4791adef8..65ecb5099 100644 --- a/ethereum/circuits/lib/README.md +++ b/ethereum/circuits/lib/README.md @@ -107,6 +107,7 @@ All the function in this library prove that the objects are contained within som ├── account.nr ├── account_with_storage.nr ├── transaction.nr + ├── uint256.nr └── verifiers    ├── header.nr    ├── receipt.nr From 8c60b71b0b0cc93e4cfb823878bf035049f89ea0 Mon Sep 17 00:00:00 2001 From: Anna Jablonowska Date: Fri, 24 May 2024 12:55:05 +0200 Subject: [PATCH 08/18] Add function headers --- vlayer/ethereum/circuits/lib/src/README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/vlayer/ethereum/circuits/lib/src/README.md b/vlayer/ethereum/circuits/lib/src/README.md index e69de29bb..db0680d95 100644 --- a/vlayer/ethereum/circuits/lib/src/README.md +++ b/vlayer/ethereum/circuits/lib/src/README.md @@ -0,0 +1,7 @@ +```rust +pub(crate) fn mapping(slot: Bytes32, key: Bytes32) -> Bytes32; +``` + +```rust +pub(crate) fn dynamic_array(slot: Bytes32, size: Field, index: Field) -> Bytes32; +``` From de2ebfd420aba53c56cf55a46fab9b6c7bbdc354 Mon Sep 17 00:00:00 2001 From: Anna Jablonowska Date: Mon, 27 May 2024 09:49:44 +0200 Subject: [PATCH 09/18] Add documentation about U256 --- ethereum/circuits/lib/README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/ethereum/circuits/lib/README.md b/ethereum/circuits/lib/README.md index 65ecb5099..767722fe0 100644 --- a/ethereum/circuits/lib/README.md +++ b/ethereum/circuits/lib/README.md @@ -205,3 +205,12 @@ struct TransactionWithinBlock { ## RLP decoding [rlp folder](./src/rlp/README.md) contains tools to work with RLP encoded data + +## U256 + +[uint256.nr](.src/uint256.nr) file includes implementation of U256 type. It is an unoptimized implementation of this type. Optimized version will appear in Noir. +Traits implemented for U256: + +- Add +- Eq +- Serde From becfc5f858831fbbe3b14c41828603b8eecece37 Mon Sep 17 00:00:00 2001 From: Anna Jablonowska Date: Mon, 27 May 2024 10:38:34 +0200 Subject: [PATCH 10/18] Add README about slots library --- vlayer/ethereum/circuits/lib/src/README.md | 50 ++++++++++++++++++++-- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/vlayer/ethereum/circuits/lib/src/README.md b/vlayer/ethereum/circuits/lib/src/README.md index db0680d95..890b99dd2 100644 --- a/vlayer/ethereum/circuits/lib/src/README.md +++ b/vlayer/ethereum/circuits/lib/src/README.md @@ -1,7 +1,49 @@ -```rust -pub(crate) fn mapping(slot: Bytes32, key: Bytes32) -> Bytes32; -``` +# Slots + +This is a [Noir](https://noir-lang.org) library for slots. + +The library consists of functions: + +- ```rust + pub(crate) fn mapping(slot: Bytes32, key: Bytes32) -> Bytes32; + ``` + + Calculates slot number of value corresponding to a mapping `key`. `Slot` argument is the storage location of the mapping calculated after applying storage layout rules. + +- ```rust + pub(crate) fn dynamic_array(slot: Bytes32, size: Field, index: Field) -> Bytes32; + ``` + + Calculates slot number where element at `index` in dynamic array is located. `Slot` argument is the storage location of the dynamic array calculated after applying storage layout rules. `Size` defines the size of elements in the array. + +- There is an analogical function where `slot` argument is a precalculated slot of the first element in the array: + + ```rust + pub(crate) fn dynamic_array_with_precalculated_slot(slot: Bytes32, size: Field, index: Field) -> Bytes32; + ``` + +- ```rust + pub(crate) fn struct_slot(slot: Bytes32, offset: Field) -> Bytes32; + ``` + + Calculates the slot of element at `offset` in the structure that begins at `slot`. + +### Example ```rust -pub(crate) fn dynamic_array(slot: Bytes32, size: Field, index: Field) -> Bytes32; +token_id_to_slot: |token_id| { + let TOKEN_OWNERS_INNER_ENTRIES_SLOT = field_to_bytes32(10); + + mapping(TOKEN_OWNERS_INNER_ENTRIES_SLOT, token_id) +} + +token_id_to_slot: |token_id| { + let TOKEN_OWNERS_INNER_ENTRIES_SLOT = [...]; + + let owner_slot = dynamic_array_with_precalculated_slot(TOKEN_OWNERS_INNER_ENTRIES_SLOT, 2, bytes32_to_field(token_id)); + + struct_slot(owner_slot, 1) +} ``` + +Examples of usage of this library can be seen in [nft_list.nr](./nft_list.nr) file From def0c74aac246106c2d219abd9fd43ae21b7f13f Mon Sep 17 00:00:00 2001 From: Anna Jablonowska Date: Mon, 27 May 2024 10:45:02 +0200 Subject: [PATCH 11/18] Add example for U256 usage --- ethereum/circuits/lib/README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/ethereum/circuits/lib/README.md b/ethereum/circuits/lib/README.md index 767722fe0..d56c566a7 100644 --- a/ethereum/circuits/lib/README.md +++ b/ethereum/circuits/lib/README.md @@ -214,3 +214,15 @@ Traits implemented for U256: - Add - Eq - Serde + +```rust +global u128_number = 0x10000000000000000000000000000000; + +let big_number = U256::new(u128_number, u128_number); + +let sum = big_number + U256::one(); +assert_eq(sum, U256 { high: u128_number, low: u128_number + U128::one()}); + +let serialized: [Field; 4] = big_number.serialize(); +assert_eq(U256::deserialize(serialized), big_number); +``` From ef4ffa7f764ffcda80463c5790d75dcc7402b8ae Mon Sep 17 00:00:00 2001 From: Anna Jablonowska Date: Tue, 28 May 2024 11:23:05 +0200 Subject: [PATCH 12/18] README for slots library after review --- vlayer/ethereum/circuits/lib/src/README.md | 39 ++++++++++++---------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/vlayer/ethereum/circuits/lib/src/README.md b/vlayer/ethereum/circuits/lib/src/README.md index 890b99dd2..c967f7a93 100644 --- a/vlayer/ethereum/circuits/lib/src/README.md +++ b/vlayer/ethereum/circuits/lib/src/README.md @@ -1,6 +1,6 @@ -# Slots +# Solidity storage slots -This is a [Noir](https://noir-lang.org) library for slots. +This is a [Noir](https://noir-lang.org) library for calculating slot positions in contract storage. The library consists of functions: @@ -8,13 +8,13 @@ The library consists of functions: pub(crate) fn mapping(slot: Bytes32, key: Bytes32) -> Bytes32; ``` - Calculates slot number of value corresponding to a mapping `key`. `Slot` argument is the storage location of the mapping calculated after applying storage layout rules. + Calculates the slot of a value corresponding to a mapping `key`. `Slot` argument is the storage location of the mapping calculated after applying storage layout rules. - ```rust pub(crate) fn dynamic_array(slot: Bytes32, size: Field, index: Field) -> Bytes32; ``` - Calculates slot number where element at `index` in dynamic array is located. `Slot` argument is the storage location of the dynamic array calculated after applying storage layout rules. `Size` defines the size of elements in the array. + Calculates the slot where element at `index` in dynamic array is located. `Slot` argument is the storage location of the dynamic array calculated after applying storage layout rules. `Size` defines the size of elements in the array. - There is an analogical function where `slot` argument is a precalculated slot of the first element in the array: @@ -26,24 +26,29 @@ The library consists of functions: pub(crate) fn struct_slot(slot: Bytes32, offset: Field) -> Bytes32; ``` - Calculates the slot of element at `offset` in the structure that begins at `slot`. + Calculates the slot of an element at `offset` in the structure that begins at `slot`. -### Example +### Examples + +The examples below show how slots of particular data can be found using this library. In this case searched data will be the owner of a token of `token_id`. +There are two types of tokens used in these examples. + +#### [Crypto Punk token](https://etherscan.io/address/0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb) + +In case of Crypto Punk tokens, owners of tokens are stored in a map that is stored at 10th slot. It can be found there: https://etherscan.io/address/0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb#code. ```rust -token_id_to_slot: |token_id| { - let TOKEN_OWNERS_INNER_ENTRIES_SLOT = field_to_bytes32(10); + mapping(field_to_bytes32(10), token_id) +``` - mapping(TOKEN_OWNERS_INNER_ENTRIES_SLOT, token_id) -} +#### [Bored ape yacht club token example](https://etherscan.io/token/0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d) -token_id_to_slot: |token_id| { - let TOKEN_OWNERS_INNER_ENTRIES_SLOT = [...]; +In case of Bored Ape Yacht Club tokens, list of owners is stored in a dynamic array of structures of two elements simulating a map. The starting position of this list is 2 as can be checked here: https://evm.storage/eth/19967215/0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d#table. Because of that one can use either `dynamic_array` function with `slot` argument set to 2, or `dynamic_array_with_precalculated_slot` with precalculated slot of this array. After finding position of a structure, `struct_slot` function finds the searched information in this structure. Owner is stored at the second position in this structure, so `offset` argument is set to 1. - let owner_slot = dynamic_array_with_precalculated_slot(TOKEN_OWNERS_INNER_ENTRIES_SLOT, 2, bytes32_to_field(token_id)); +```rust +let BORED_APE_YACHT_CLUB_TOKEN_OWNERS_INNER_ENTRIES_SLOT = keccak256(2, 32); - struct_slot(owner_slot, 1) -} -``` +let owner_slot = dynamic_array_with_precalculated_slot(BORED_APE_YACHT_CLUB_TOKEN_OWNERS_INNER_ENTRIES_SLOT, 2, bytes32_to_field(token_id)); -Examples of usage of this library can be seen in [nft_list.nr](./nft_list.nr) file +struct_slot(owner_slot, 1) +``` From efb730b439e1a423315533c59737ff597e1e272f Mon Sep 17 00:00:00 2001 From: Anna Jablonowska Date: Tue, 28 May 2024 11:33:45 +0200 Subject: [PATCH 13/18] Extract slice_to_array function --- ethereum/circuits/lib/src/misc/arrays.nr | 8 ++++++++ ethereum/circuits/lib/src/misc/bytes32.nr | 10 +++------- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/ethereum/circuits/lib/src/misc/arrays.nr b/ethereum/circuits/lib/src/misc/arrays.nr index 77bfa0f2b..7d021916f 100644 --- a/ethereum/circuits/lib/src/misc/arrays.nr +++ b/ethereum/circuits/lib/src/misc/arrays.nr @@ -72,3 +72,11 @@ pub fn sub_array_equals( } result } + +pub fn slice_to_array(slice: [u8]) -> [u8; LENGTH] { + let mut array = [0; LENGTH]; + for i in 0..LENGTH { + array[i] = slice[i]; + } + array +} diff --git a/ethereum/circuits/lib/src/misc/bytes32.nr b/ethereum/circuits/lib/src/misc/bytes32.nr index 4bc74d1bb..85dc68253 100644 --- a/ethereum/circuits/lib/src/misc/bytes32.nr +++ b/ethereum/circuits/lib/src/misc/bytes32.nr @@ -1,4 +1,4 @@ -use crate::misc::arrays::memcpy; +use crate::misc::arrays::{memcpy, slice_to_array}; use crate::misc::types::Bytes32; pub fn address_to_bytes32(address: [u8; 20]) -> Bytes32 { @@ -10,10 +10,6 @@ pub fn address_to_bytes32(address: [u8; 20]) -> Bytes32 { } pub fn field_to_bytes32(value: Field) -> [u8; 32] { - let byte_array = value.to_be_bytes(32); - let mut bytes = [0; 32]; - for i in 0..32 { - bytes[i] = byte_array[i]; - } - bytes + let byte_slice = value.to_be_bytes(32); + slice_to_array(byte_slice) } From f4803962df90ee468e8135d47eb8e9779e92622f Mon Sep 17 00:00:00 2001 From: Anna Jablonowska Date: Tue, 28 May 2024 11:47:27 +0200 Subject: [PATCH 14/18] Improve U256 docs --- ethereum/circuits/lib/README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ethereum/circuits/lib/README.md b/ethereum/circuits/lib/README.md index d56c566a7..dacf3135f 100644 --- a/ethereum/circuits/lib/README.md +++ b/ethereum/circuits/lib/README.md @@ -208,7 +208,11 @@ struct TransactionWithinBlock { ## U256 -[uint256.nr](.src/uint256.nr) file includes implementation of U256 type. It is an unoptimized implementation of this type. Optimized version will appear in Noir. +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. + +[There](.src/uint256.nr) is an unoptimized implementation of this type using two U128 structures. Optimized version will appear in Noir. + Traits implemented for U256: - Add From 809673ffc75c39dc5d8782a9910314b2794d76e8 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Wed, 29 May 2024 09:40:12 +0200 Subject: [PATCH 15/18] Create top-level vlayer readme and link to STORAGE_SLOTS.md --- vlayer/ethereum/circuits/lib/README.md | 5 +++++ .../circuits/lib/{src/README.md => STORAGE_SLOTS.md} | 0 2 files changed, 5 insertions(+) create mode 100644 vlayer/ethereum/circuits/lib/README.md rename vlayer/ethereum/circuits/lib/{src/README.md => STORAGE_SLOTS.md} (100%) diff --git a/vlayer/ethereum/circuits/lib/README.md b/vlayer/ethereum/circuits/lib/README.md new file mode 100644 index 000000000..1ad9fdbae --- /dev/null +++ b/vlayer/ethereum/circuits/lib/README.md @@ -0,0 +1,5 @@ +# vlayer libraries + +## Storage slots + +[Docs](./STORAGE_SLOTS.md) diff --git a/vlayer/ethereum/circuits/lib/src/README.md b/vlayer/ethereum/circuits/lib/STORAGE_SLOTS.md similarity index 100% rename from vlayer/ethereum/circuits/lib/src/README.md rename to vlayer/ethereum/circuits/lib/STORAGE_SLOTS.md From e41702ea0848510f05b045a0baffffccef824cc5 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Wed, 29 May 2024 10:34:57 +0200 Subject: [PATCH 16/18] Improve storage layout examples --- .vscode/settings.json | 2 +- vlayer/ethereum/circuits/lib/STORAGE_SLOTS.md | 66 +++++++++++++------ 2 files changed, 48 insertions(+), 20 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index fd1176f35..f0c31360d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -17,5 +17,5 @@ "typescript.tsdk": ".yarn/sdks/typescript/lib", "prettier.prettierPath": ".yarn/sdks/prettier/index.cjs", "typescript.enablePromptUseWorkspaceTsdk": true, - "cSpell.words": ["acir", "Barretenberg", "keccaks", "Nargo", "noirc", "viem"] + "cSpell.words": ["acir", "Barretenberg", "keccak", "keccaks", "Nargo", "noirc", "struct", "structs", "viem"] } diff --git a/vlayer/ethereum/circuits/lib/STORAGE_SLOTS.md b/vlayer/ethereum/circuits/lib/STORAGE_SLOTS.md index c967f7a93..b0c4be04e 100644 --- a/vlayer/ethereum/circuits/lib/STORAGE_SLOTS.md +++ b/vlayer/ethereum/circuits/lib/STORAGE_SLOTS.md @@ -1,54 +1,82 @@ # Solidity storage slots -This is a [Noir](https://noir-lang.org) library for calculating slot positions in contract storage. +This is a [Noir](https://noir-lang.org) library for calculating storage locations (later: slots) of Solidity variables. -The library consists of functions: +The library consists of the following functions: - ```rust pub(crate) fn mapping(slot: Bytes32, key: Bytes32) -> Bytes32; ``` - Calculates the slot of a value corresponding to a mapping `key`. `Slot` argument is the storage location of the mapping calculated after applying storage layout rules. + Takes in the mapping `slot`, the `key` and outputs the respective value `slot`. - ```rust pub(crate) fn dynamic_array(slot: Bytes32, size: Field, index: Field) -> Bytes32; ``` - Calculates the slot where element at `index` in dynamic array is located. `Slot` argument is the storage location of the dynamic array calculated after applying storage layout rules. `Size` defines the size of elements in the array. - -- There is an analogical function where `slot` argument is a precalculated slot of the first element in the array: + Takes in the array `slot`, the element `size` and `index` and outputs the respective element `slot`. ```rust pub(crate) fn dynamic_array_with_precalculated_slot(slot: Bytes32, size: Field, index: Field) -> Bytes32; ``` + Similar to a function above, but instead of taking in the array slot it takes in the slot of it's first element which is equal to the `keccak` of the array slot. + + This is useful in cases like EIP 1967 where storage slot is computed manually from variable name instead of numeric storage slot to avoid collisions and achieve storage slots stability throughout code upgrades. + Example: + Storage slot `0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc` (obtained as `bytes32(uint256(keccak256('eip1967.proxy.implementation')) - 1))`. + - ```rust pub(crate) fn struct_slot(slot: Bytes32, offset: Field) -> Bytes32; ``` - Calculates the slot of an element at `offset` in the structure that begins at `slot`. + Takes in the struct `slot`, the element `offset` and outputs the respective struct field `slot`. -### Examples +## Examples -The examples below show how slots of particular data can be found using this library. In this case searched data will be the owner of a token of `token_id`. -There are two types of tokens used in these examples. +Below is the example of how we can use this library to find a storage slot of an NFT owner by `token_id`. -#### [Crypto Punk token](https://etherscan.io/address/0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb) +### [CryptoPunks](https://etherscan.io/address/0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb#code) -In case of Crypto Punk tokens, owners of tokens are stored in a map that is stored at 10th slot. It can be found there: https://etherscan.io/address/0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb#code. +Owners of tokens are stored in a map at position 10. + +```solidity +mapping (uint => address) public punkIndexToAddress; +``` ```rust - mapping(field_to_bytes32(10), token_id) +let owner_slot = mapping(field_to_bytes32(10), token_id) ``` -#### [Bored ape yacht club token example](https://etherscan.io/token/0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d) +### [Bored Ape Yacht Club](https://etherscan.io/token/0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d#code) -In case of Bored Ape Yacht Club tokens, list of owners is stored in a dynamic array of structures of two elements simulating a map. The starting position of this list is 2 as can be checked here: https://evm.storage/eth/19967215/0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d#table. Because of that one can use either `dynamic_array` function with `slot` argument set to 2, or `dynamic_array_with_precalculated_slot` with precalculated slot of this array. After finding position of a structure, `struct_slot` function finds the searched information in this structure. Owner is stored at the second position in this structure, so `offset` argument is set to 1. +This contract is harder to read, but we can use [evm.storage](https://evm.storage/eth/19967215/0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d#table) to find storage layout. -```rust -let BORED_APE_YACHT_CLUB_TOKEN_OWNERS_INNER_ENTRIES_SLOT = keccak256(2, 32); +```solidity +struct MapEntry { + _key bytes32; + _value bytes32; // This contains padded owner address +}; -let owner_slot = dynamic_array_with_precalculated_slot(BORED_APE_YACHT_CLUB_TOKEN_OWNERS_INNER_ENTRIES_SLOT, 2, bytes32_to_field(token_id)); +struct EnumerableMap { + MapEntry[] _entries; + mapping(bytes32 => uint256) _indexes; +}; -struct_slot(owner_slot, 1) +EnumerableMap _tokenOwners; +``` + +`_tokenOwners` is a struct that occupies slots 2 and 3, but we are only interested in it's first field. + +`_entries` is a dynamic array of structs (of size 2) and we are interested in the second field (with offset 1). +`_entries` array is indexed by `token_id`. + +```rust +let array_elem_size = 2; // This element is a struct with two fields. Owner is the second field +let array_elem_idx = bytes32_to_field(token_id); +let struct_owner_field_offset = 1; +let owner_slot = struct_slot( + dynamic_array(2, array_elem_size, array_elem_idx), + struct_owner_field_offset +); ``` From 585003af935bb256670dbaf587ce0db1051e0736 Mon Sep 17 00:00:00 2001 From: Anna Jablonowska Date: Wed, 29 May 2024 09:31:29 +0200 Subject: [PATCH 17/18] Use as_array function for slice --- ethereum/circuits/lib/src/misc/arrays.nr | 8 -------- ethereum/circuits/lib/src/misc/bytes32.nr | 4 ++-- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/ethereum/circuits/lib/src/misc/arrays.nr b/ethereum/circuits/lib/src/misc/arrays.nr index 7d021916f..77bfa0f2b 100644 --- a/ethereum/circuits/lib/src/misc/arrays.nr +++ b/ethereum/circuits/lib/src/misc/arrays.nr @@ -72,11 +72,3 @@ pub fn sub_array_equals( } result } - -pub fn slice_to_array(slice: [u8]) -> [u8; LENGTH] { - let mut array = [0; LENGTH]; - for i in 0..LENGTH { - array[i] = slice[i]; - } - array -} diff --git a/ethereum/circuits/lib/src/misc/bytes32.nr b/ethereum/circuits/lib/src/misc/bytes32.nr index 85dc68253..527fe8377 100644 --- a/ethereum/circuits/lib/src/misc/bytes32.nr +++ b/ethereum/circuits/lib/src/misc/bytes32.nr @@ -1,4 +1,4 @@ -use crate::misc::arrays::{memcpy, slice_to_array}; +use crate::misc::arrays::memcpy; use crate::misc::types::Bytes32; pub fn address_to_bytes32(address: [u8; 20]) -> Bytes32 { @@ -11,5 +11,5 @@ pub fn address_to_bytes32(address: [u8; 20]) -> Bytes32 { pub fn field_to_bytes32(value: Field) -> [u8; 32] { let byte_slice = value.to_be_bytes(32); - slice_to_array(byte_slice) + byte_slice.as_array() } From 049fdf601ef92a81c9b441550082fa6d98097f2a Mon Sep 17 00:00:00 2001 From: Anna Jablonowska Date: Wed, 29 May 2024 12:38:38 +0200 Subject: [PATCH 18/18] Add usefullness of U256 --- ethereum/circuits/lib/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ethereum/circuits/lib/README.md b/ethereum/circuits/lib/README.md index dacf3135f..6c580d5d8 100644 --- a/ethereum/circuits/lib/README.md +++ b/ethereum/circuits/lib/README.md @@ -209,7 +209,8 @@ struct TransactionWithinBlock { ## U256 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. +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.