diff --git a/Scarb.lock b/Scarb.lock index 72eb246ac..2dd538f24 100644 --- a/Scarb.lock +++ b/Scarb.lock @@ -8,6 +8,11 @@ dependencies = [ "snforge_std", ] +[[package]] +name = "alexandria_macros" +version = "0.1.0" +source = "git+https://github.com/keep-starknet-strange/alexandria.git#7c19379ab6cf0f8b48be3c5b118ffddd7e26a4d2" + [[package]] name = "contracts" version = "0.1.0" @@ -34,7 +39,7 @@ dependencies = [ [[package]] name = "garaga" version = "0.1.0" -source = "git+https://github.com/keep-starknet-strange/garaga.git#933784eee3811334cf1a5b83d9ffcc9cfda3be8c" +source = "git+https://github.com/keep-starknet-strange/garaga.git#866281e342b0b61f7c2906b9f6d812fe414a100c" [[package]] name = "openzeppelin" @@ -69,6 +74,7 @@ name = "utils" version = "0.1.0" dependencies = [ "alexandria_data_structures", + "alexandria_macros", "evm", "snforge_std", ] diff --git a/Scarb.toml b/Scarb.toml index b0d420965..92213690a 100644 --- a/Scarb.toml +++ b/Scarb.toml @@ -12,6 +12,7 @@ license-file = "LICENSE" [workspace.dependencies] starknet = "2.8.2" +alexandria_macros = { git = "https://github.com/keep-starknet-strange/alexandria.git" } [workspace.tool.fmt] sort-module-level-items = true diff --git a/crates/contracts/src/account_contract.cairo b/crates/contracts/src/account_contract.cairo index 8faa2ea60..a6977bf1f 100644 --- a/crates/contracts/src/account_contract.cairo +++ b/crates/contracts/src/account_contract.cairo @@ -75,7 +75,6 @@ pub mod AccountContract { use core::traits::TryInto; use openzeppelin::token::erc20::interface::{IERC20CamelDispatcher, IERC20CamelDispatcherTrait}; use super::{IAccountLibraryDispatcher, IAccountDispatcherTrait, OutsideExecution}; - use utils::constants::{POW_2_32}; use utils::eth_transaction::EthereumTransactionTrait; use utils::eth_transaction::{EthTransactionTrait, TransactionMetadata}; use utils::helpers::SpanExtTrait; @@ -186,7 +185,7 @@ pub mod AccountContract { "Validate: selector must be eth_send_transaction" ); - let chain_id: u128 = tx_info.chain_id.try_into().unwrap() % POW_2_32; + let chain_id: u128 = tx_info.chain_id.try_into().unwrap() % pow!(2, 32); let signature = deserialize_signature(tx_info.signature, chain_id) .expect('EOA: invalid signature'); @@ -329,7 +328,7 @@ pub mod AccountContract { "selector must be eth_send_transaction" ); - let chain_id: u128 = tx_info.chain_id.try_into().unwrap() % POW_2_32; + let chain_id: u128 = tx_info.chain_id.try_into().unwrap() % pow!(2, 32); let signature = deserialize_signature(signature, chain_id).expect('invalid signature'); diff --git a/crates/evm/src/instructions/comparison_operations.cairo b/crates/evm/src/instructions/comparison_operations.cairo index da6270422..115e482d3 100644 --- a/crates/evm/src/instructions/comparison_operations.cairo +++ b/crates/evm/src/instructions/comparison_operations.cairo @@ -4,7 +4,6 @@ use evm::gas; use evm::model::vm::{VM, VMTrait}; // Internal imports use evm::stack::StackTrait; -use utils::constants::{POW_2_127}; use utils::i256::i256; use utils::math::{Exponentiation, Bitshift, WrappingBitshift}; use utils::traits::BoolIntoNumeric; @@ -175,7 +174,7 @@ impl ComparisonAndBitwiseOperations of ComparisonAndBitwiseOperationsTrait { let value: i256 = self.stack.pop_i256()?; // Checks the MSB bit sign for a 256-bit integer - let positive = value.value.high < POW_2_127; + let positive = value.value.high < pow!(2, 127); let sign = if positive { // If sign is positive, set it to 0. 0 diff --git a/crates/evm/src/memory.cairo b/crates/evm/src/memory.cairo index 230fbd877..758d2e384 100644 --- a/crates/evm/src/memory.cairo +++ b/crates/evm/src/memory.cairo @@ -2,10 +2,6 @@ use core::cmp::{max, min}; use core::integer::{ u32_safe_divmod, u32_as_non_zero, u128_safe_divmod, u128_as_non_zero, u256_as_non_zero }; -use utils::constants::{ - POW_2_0, POW_2_8, POW_2_16, POW_2_24, POW_2_32, POW_2_40, POW_2_48, POW_2_56, POW_2_64, - POW_2_72, POW_2_80, POW_2_88, POW_2_96, POW_2_104, POW_2_112, POW_2_120, POW_256_16 -}; use utils::{ helpers, helpers::SpanExtTrait, helpers::ArrayExtTrait, math::Exponentiation, math::WrappingExponentiation, math::Bitshift @@ -250,7 +246,7 @@ impl InternalMemoryMethods of InternalMemoryTrait { #[inline(always)] fn store_element(ref self: Memory, element: u256, chunk_index: usize, offset_in_chunk: u32) { let mask: u256 = helpers::pow256_rev(offset_in_chunk); - let mask_c: u256 = POW_256_16 / mask; + let mask_c: u256 = pow!(256, 16) / mask; // Split the 2 input bytes16 chunks at offset_in_chunk. let (el_hh, el_hl) = DivRem::div_rem(element.high.into(), u256_as_non_zero(mask_c)); @@ -318,22 +314,22 @@ impl InternalMemoryMethods of InternalMemoryTrait { fn store_aligned_words(ref self: Memory, mut chunk_index: usize, mut elements: Span) { while let Option::Some(words) = elements.multi_pop_front::<16>() { let words = (*words).unbox().span(); - let current: u128 = ((*words[0]).into() * POW_2_120 - + (*words[1]).into() * POW_2_112 - + (*words[2]).into() * POW_2_104 - + (*words[3]).into() * POW_2_96 - + (*words[4]).into() * POW_2_88 - + (*words[5]).into() * POW_2_80 - + (*words[6]).into() * POW_2_72 - + (*words[7]).into() * POW_2_64 - + (*words[8]).into() * POW_2_56 - + (*words[9]).into() * POW_2_48 - + (*words[10]).into() * POW_2_40 - + (*words[11]).into() * POW_2_32 - + (*words[12]).into() * POW_2_24 - + (*words[13]).into() * POW_2_16 - + (*words[14]).into() * POW_2_8 - + (*words[15]).into() * POW_2_0); + let current: u128 = ((*words[0]).into() * pow!(2, 120) + + (*words[1]).into() * pow!(2, 112) + + (*words[2]).into() * pow!(2, 104) + + (*words[3]).into() * pow!(2, 96) + + (*words[4]).into() * pow!(2, 88) + + (*words[5]).into() * pow!(2, 80) + + (*words[6]).into() * pow!(2, 72) + + (*words[7]).into() * pow!(2, 64) + + (*words[8]).into() * pow!(2, 56) + + (*words[9]).into() * pow!(2, 48) + + (*words[10]).into() * pow!(2, 40) + + (*words[11]).into() * pow!(2, 32) + + (*words[12]).into() * pow!(2, 24) + + (*words[13]).into() * pow!(2, 16) + + (*words[14]).into() * pow!(2, 8) + + (*words[15]).into() * pow!(2, 0)); self.items.insert(chunk_index.into(), current); chunk_index += 1; @@ -405,7 +401,7 @@ impl InternalMemoryMethods of InternalMemoryTrait { // Compute mask. let mask: u256 = helpers::pow256_rev(offset_in_chunk); - let mask_c: u256 = POW_256_16 / mask; + let mask_c: u256 = pow!(256, 16) / mask; // Read the words at chunk_index, +1, +2. let w0: u128 = self.items.get(chunk_index.into()); @@ -613,7 +609,6 @@ impl Felt252DictExtensionImpl of Felt252DictExtension { mod tests { use core::num::traits::Bounded; use evm::memory::{MemoryTrait, InternalMemoryTrait}; - use utils::constants::{POW_2_8, POW_2_56, POW_2_64, POW_2_120}; use utils::{math::Exponentiation, math::WrappingExponentiation, helpers, helpers::SpanExtTrait}; mod internal { @@ -841,32 +836,32 @@ mod tests { #[test] fn test_load_should_load_an_element_from_the_memory_with_offset_8() { internal::load_should_load_an_element_from_the_memory_with_offset_stored_with_store_n( - 8, 2 * POW_2_64, POW_2_64 + 8, 2 * pow!(2, 64), pow!(2, 64) ); } #[test] fn test_load_should_load_an_element_from_the_memory_with_offset_7() { internal::load_should_load_an_element_from_the_memory_with_offset_stored_with_store_n( - 7, 2 * POW_2_56, POW_2_56 + 7, 2 * pow!(2, 56), pow!(2, 56) ); } #[test] fn test_load_should_load_an_element_from_the_memory_with_offset_23() { internal::load_should_load_an_element_from_the_memory_with_offset_stored_with_store_n( - 23, 3 * POW_2_56, 2 * POW_2_56 + 23, 3 * pow!(2, 56), 2 * pow!(2, 56) ); } #[test] fn test_load_should_load_an_element_from_the_memory_with_offset_33() { internal::load_should_load_an_element_from_the_memory_with_offset_stored_with_store_n( - 33, 4 * POW_2_8, 3 * POW_2_8 + 33, 4 * pow!(2, 8), 3 * pow!(2, 8) ); } #[test] fn test_load_should_load_an_element_from_the_memory_with_offset_63() { internal::load_should_load_an_element_from_the_memory_with_offset_stored_with_store_n( - 63, 0, 4 * POW_2_120 + 63, 0, 4 * pow!(2, 120) ); } diff --git a/crates/utils/Scarb.toml b/crates/utils/Scarb.toml index ee3ce2097..80f5f6d53 100644 --- a/crates/utils/Scarb.toml +++ b/crates/utils/Scarb.toml @@ -8,6 +8,7 @@ edition = "2023_10" [dependencies] evm = { path = "../evm" } alexandria_data_structures = { path = "../alexandria_data_structures" } +alexandria_macros.workspace = true # For profiling [cairo] diff --git a/crates/utils/src/constants.cairo b/crates/utils/src/constants.cairo index 0c2ecfe12..7cd39a2ff 100644 --- a/crates/utils/src/constants.cairo +++ b/crates/utils/src/constants.cairo @@ -39,195 +39,161 @@ const BURN_ADDRESS: felt252 = 0xdead; // Numeric constants -pub const POW_256_0: u128 = 0x1; -pub const POW_256_1: u128 = 0x100; -pub const POW_256_2: u128 = 0x10000; -pub const POW_256_3: u128 = 0x1000000; -pub const POW_256_4: u128 = 0x100000000; -pub const POW_256_5: u128 = 0x10000000000; -pub const POW_256_6: u128 = 0x1000000000000; -pub const POW_256_7: u128 = 0x100000000000000; -pub const POW_256_8: u128 = 0x10000000000000000; -pub const POW_256_9: u128 = 0x1000000000000000000; -pub const POW_256_10: u128 = 0x100000000000000000000; -pub const POW_256_11: u128 = 0x10000000000000000000000; -pub const POW_256_12: u128 = 0x1000000000000000000000000; -pub const POW_256_13: u128 = 0x100000000000000000000000000; -pub const POW_256_14: u128 = 0x10000000000000000000000000000; -pub const POW_256_15: u128 = 0x1000000000000000000000000000000; -pub const POW_256_16: u256 = 0x100000000000000000000000000000000; pub const POW_256_REV: [ u256 ; 17] = [ - 0x100000000000000000000000000000000, - 0x1000000000000000000000000000000, - 0x10000000000000000000000000000, - 0x100000000000000000000000000, - 0x1000000000000000000000000, - 0x10000000000000000000000, - 0x100000000000000000000, - 0x1000000000000000000, - 0x10000000000000000, - 0x100000000000000, - 0x1000000000000, - 0x10000000000, - 0x100000000, - 0x1000000, - 0x10000, - 0x100, - 0x1 + pow!(256, 16), + pow!(256, 15), + pow!(256, 14), + pow!(256, 13), + pow!(256, 12), + pow!(256, 11), + pow!(256, 10), + pow!(256, 9), + pow!(256, 8), + pow!(256, 7), + pow!(256, 6), + pow!(256, 5), + pow!(256, 4), + pow!(256, 3), + pow!(256, 2), + pow!(256, 1), + pow!(256, 0) ]; pub const POW_2: [ u128 ; 128] = [ - 0x1, - 0x2, - 0x4, - 0x8, - 0x10, - 0x20, - 0x40, - 0x80, - 0x100, - 0x200, - 0x400, - 0x800, - 0x1000, - 0x2000, - 0x4000, - 0x8000, - 0x10000, - 0x20000, - 0x40000, - 0x80000, - 0x100000, - 0x200000, - 0x400000, - 0x800000, - 0x1000000, - 0x2000000, - 0x4000000, - 0x8000000, - 0x10000000, - 0x20000000, - 0x40000000, - 0x80000000, - 0x100000000, - 0x200000000, - 0x400000000, - 0x800000000, - 0x1000000000, - 0x2000000000, - 0x4000000000, - 0x8000000000, - 0x10000000000, - 0x20000000000, - 0x40000000000, - 0x80000000000, - 0x100000000000, - 0x200000000000, - 0x400000000000, - 0x800000000000, - 0x1000000000000, - 0x2000000000000, - 0x4000000000000, - 0x8000000000000, - 0x10000000000000, - 0x20000000000000, - 0x40000000000000, - 0x80000000000000, - 0x100000000000000, - 0x200000000000000, - 0x400000000000000, - 0x800000000000000, - 0x1000000000000000, - 0x2000000000000000, - 0x4000000000000000, - 0x8000000000000000, - 0x10000000000000000, - 0x20000000000000000, - 0x40000000000000000, - 0x80000000000000000, - 0x100000000000000000, - 0x200000000000000000, - 0x400000000000000000, - 0x800000000000000000, - 0x1000000000000000000, - 0x2000000000000000000, - 0x4000000000000000000, - 0x8000000000000000000, - 0x10000000000000000000, - 0x20000000000000000000, - 0x40000000000000000000, - 0x80000000000000000000, - 0x100000000000000000000, - 0x200000000000000000000, - 0x400000000000000000000, - 0x800000000000000000000, - 0x1000000000000000000000, - 0x2000000000000000000000, - 0x4000000000000000000000, - 0x8000000000000000000000, - 0x10000000000000000000000, - 0x20000000000000000000000, - 0x40000000000000000000000, - 0x80000000000000000000000, - 0x100000000000000000000000, - 0x200000000000000000000000, - 0x400000000000000000000000, - 0x800000000000000000000000, - 0x1000000000000000000000000, - 0x2000000000000000000000000, - 0x4000000000000000000000000, - 0x8000000000000000000000000, - 0x10000000000000000000000000, - 0x20000000000000000000000000, - 0x40000000000000000000000000, - 0x80000000000000000000000000, - 0x100000000000000000000000000, - 0x200000000000000000000000000, - 0x400000000000000000000000000, - 0x800000000000000000000000000, - 0x1000000000000000000000000000, - 0x2000000000000000000000000000, - 0x4000000000000000000000000000, - 0x8000000000000000000000000000, - 0x10000000000000000000000000000, - 0x20000000000000000000000000000, - 0x40000000000000000000000000000, - 0x80000000000000000000000000000, - 0x100000000000000000000000000000, - 0x200000000000000000000000000000, - 0x400000000000000000000000000000, - 0x800000000000000000000000000000, - 0x1000000000000000000000000000000, - 0x2000000000000000000000000000000, - 0x4000000000000000000000000000000, - 0x8000000000000000000000000000000, - 0x10000000000000000000000000000000, - 0x20000000000000000000000000000000, - 0x40000000000000000000000000000000, - 0x80000000000000000000000000000000 + pow!(2, 0), + pow!(2, 1), + pow!(2, 2), + pow!(2, 3), + pow!(2, 4), + pow!(2, 5), + pow!(2, 6), + pow!(2, 7), + pow!(2, 8), + pow!(2, 9), + pow!(2, 10), + pow!(2, 11), + pow!(2, 12), + pow!(2, 13), + pow!(2, 14), + pow!(2, 15), + pow!(2, 16), + pow!(2, 17), + pow!(2, 18), + pow!(2, 19), + pow!(2, 20), + pow!(2, 21), + pow!(2, 22), + pow!(2, 23), + pow!(2, 24), + pow!(2, 25), + pow!(2, 26), + pow!(2, 27), + pow!(2, 28), + pow!(2, 29), + pow!(2, 30), + pow!(2, 31), + pow!(2, 32), + pow!(2, 33), + pow!(2, 34), + pow!(2, 35), + pow!(2, 36), + pow!(2, 37), + pow!(2, 38), + pow!(2, 39), + pow!(2, 40), + pow!(2, 41), + pow!(2, 42), + pow!(2, 43), + pow!(2, 44), + pow!(2, 45), + pow!(2, 46), + pow!(2, 47), + pow!(2, 48), + pow!(2, 49), + pow!(2, 50), + pow!(2, 51), + pow!(2, 52), + pow!(2, 53), + pow!(2, 54), + pow!(2, 55), + pow!(2, 56), + pow!(2, 57), + pow!(2, 58), + pow!(2, 59), + pow!(2, 60), + pow!(2, 61), + pow!(2, 62), + pow!(2, 63), + pow!(2, 64), + pow!(2, 65), + pow!(2, 66), + pow!(2, 67), + pow!(2, 68), + pow!(2, 69), + pow!(2, 70), + pow!(2, 71), + pow!(2, 72), + pow!(2, 73), + pow!(2, 74), + pow!(2, 75), + pow!(2, 76), + pow!(2, 77), + pow!(2, 78), + pow!(2, 79), + pow!(2, 80), + pow!(2, 81), + pow!(2, 82), + pow!(2, 83), + pow!(2, 84), + pow!(2, 85), + pow!(2, 86), + pow!(2, 87), + pow!(2, 88), + pow!(2, 89), + pow!(2, 90), + pow!(2, 91), + pow!(2, 92), + pow!(2, 93), + pow!(2, 94), + pow!(2, 95), + pow!(2, 96), + pow!(2, 97), + pow!(2, 98), + pow!(2, 99), + pow!(2, 100), + pow!(2, 101), + pow!(2, 102), + pow!(2, 103), + pow!(2, 104), + pow!(2, 105), + pow!(2, 106), + pow!(2, 107), + pow!(2, 108), + pow!(2, 109), + pow!(2, 110), + pow!(2, 111), + pow!(2, 112), + pow!(2, 113), + pow!(2, 114), + pow!(2, 115), + pow!(2, 116), + pow!(2, 117), + pow!(2, 118), + pow!(2, 119), + pow!(2, 120), + pow!(2, 121), + pow!(2, 122), + pow!(2, 123), + pow!(2, 124), + pow!(2, 125), + pow!(2, 126), + pow!(2, 127) ]; -const POW_2_0: u128 = 0x1; -const POW_2_8: u128 = 0x100; -const POW_2_16: u128 = 0x10000; -const POW_2_24: u128 = 0x1000000; -const POW_2_32: u128 = 0x100000000; -const POW_2_40: u128 = 0x10000000000; -const POW_2_48: u128 = 0x1000000000000; -const POW_2_56: u128 = 0x100000000000000; -const POW_2_64: u128 = 0x10000000000000000; -const POW_2_72: u128 = 0x1000000000000000000; -const POW_2_80: u128 = 0x100000000000000000000; -const POW_2_88: u128 = 0x10000000000000000000000; -const POW_2_96: u128 = 0x1000000000000000000000000; -const POW_2_104: u128 = 0x100000000000000000000000000; -const POW_2_112: u128 = 0x10000000000000000000000000000; -const POW_2_120: u128 = 0x1000000000000000000000000000000; -const POW_2_127: u128 = 0x80000000000000000000000000000000; const MAX_ADDRESS: u256 = 0x7ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff; diff --git a/crates/utils/src/helpers.cairo b/crates/utils/src/helpers.cairo index 8b3b00d1d..85ef6713f 100644 --- a/crates/utils/src/helpers.cairo +++ b/crates/utils/src/helpers.cairo @@ -19,8 +19,8 @@ use core::starknet::{ use core::traits::DivRem; use core::traits::TryInto; use utils::constants::{CONTRACT_ADDRESS_PREFIX, MAX_ADDRESS}; +use utils::constants::{POW_2, POW_256_REV}; -use utils::constants::{POW_2, POW_256_1, POW_256_REV}; use utils::eth_transaction::{TransactionType}; use utils::math::{Bitshift, WrappingBitshift, Exponentiation}; use utils::traits::{U256TryIntoContractAddress, EthAddressIntoU256, TryIntoResult, BoolIntoNumeric}; @@ -689,7 +689,7 @@ pub impl ByteArrayExt of ByteArrayExTrait { let mut word: felt252 = 0; let mut j = 0; while j != 31 { - word = word * POW_256_1.into() + (*bytes.pop_front().unwrap()).into(); + word = word * pow!(256, 1).into() + (*bytes.pop_front().unwrap()).into(); j += 1; }; arr.data.append(word.try_into().unwrap()); @@ -704,7 +704,8 @@ pub impl ByteArrayExt of ByteArrayExTrait { let mut i = 0; while i != pending_word_len { - pending_word = pending_word * POW_256_1.into() + (*bytes.pop_front().unwrap()).into(); + pending_word = pending_word * pow!(256, 1).into() + + (*bytes.pop_front().unwrap()).into(); i += 1; }; arr.pending_word_len = pending_word_len; diff --git a/crates/utils/src/i256.cairo b/crates/utils/src/i256.cairo index 4278ef3dd..90766a5fe 100644 --- a/crates/utils/src/i256.cairo +++ b/crates/utils/src/i256.cairo @@ -1,6 +1,5 @@ use core::integer::{u256_try_as_non_zero}; use core::num::traits::Bounded; -use utils::constants::POW_2_127; use utils::math::{Bitshift, Exponentiation}; #[derive(Copy, Drop, PartialEq)] @@ -35,8 +34,8 @@ pub impl I256PartialOrd of PartialOrd { #[inline(always)] fn lt(lhs: i256, rhs: i256) -> bool { - let lhs_positive = lhs.value.high < POW_2_127; - let rhs_positive = rhs.value.high < POW_2_127; + let lhs_positive = lhs.value.high < pow!(2, 127); + let rhs_positive = rhs.value.high < pow!(2, 127); if (lhs_positive != rhs_positive) { !lhs_positive @@ -47,8 +46,8 @@ pub impl I256PartialOrd of PartialOrd { #[inline(always)] fn gt(lhs: i256, rhs: i256) -> bool { - let lhs_positive = lhs.value.high < POW_2_127; - let rhs_positive = rhs.value.high < POW_2_127; + let lhs_positive = lhs.value.high < pow!(2, 127); + let rhs_positive = rhs.value.high < pow!(2, 127); if (lhs_positive != rhs_positive) { lhs_positive @@ -92,14 +91,14 @@ fn i256_signed_div_rem(a: i256, div: NonZero) -> (i256, i256) { // Take the absolute value of a and div. // Checks the MSB bit sign for a 256-bit integer - let a_positive = a.value.high < POW_2_127; + let a_positive = a.value.high < pow!(2, 127); let a = if a_positive { a } else { i256_neg(a).into() }; - let div_positive = div.value.high < POW_2_127; + let div_positive = div.value.high < pow!(2, 127); div = if div_positive { div } else {