Skip to content

Commit

Permalink
feat: replaced constants powers with macro
Browse files Browse the repository at this point in the history
  • Loading branch information
enitrat committed Aug 23, 2024
1 parent 19a4df4 commit ce294c2
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 222 deletions.
1 change: 1 addition & 0 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ license-file = "LICENSE"
[workspace.dependencies]
starknet = "2.7.1"
cairo_test = "2.7.1"
alexandria_macros = { git = "https://github.com/keep-starknet-strange/alexandria.git" }

[workspace.tool.fmt]
sort-module-level-items = true
Expand Down
3 changes: 1 addition & 2 deletions crates/contracts/src/account_contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ pub mod AccountContract {
use core::traits::TryInto;
use openzeppelin::token::erc20::interface::{IERC20CamelDispatcher, IERC20CamelDispatcherTrait};
use super::{IAccountLibraryDispatcher, IAccountDispatcherTrait};
use utils::constants::{POW_2_32};
use utils::eth_transaction::EthereumTransactionTrait;
use utils::eth_transaction::{EthTransactionTrait, TransactionMetadata};
use utils::helpers::SpanExtTrait;
Expand Down Expand Up @@ -172,7 +171,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');

Expand Down
3 changes: 1 addition & 2 deletions crates/evm/src/instructions/comparison_operations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
51 changes: 23 additions & 28 deletions crates/evm/src/memory.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -256,7 +252,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));
Expand Down Expand Up @@ -324,22 +320,22 @@ impl InternalMemoryMethods of InternalMemoryTrait {
fn store_aligned_words(ref self: Memory, mut chunk_index: usize, mut elements: Span<u8>) {
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;
Expand Down Expand Up @@ -411,7 +407,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());
Expand Down Expand Up @@ -619,7 +615,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 {
Expand Down Expand Up @@ -839,32 +834,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)
);
}

Expand Down
2 changes: 1 addition & 1 deletion crates/utils/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2023_10"
[dependencies]
evm = { path = "../evm" }
alexandria_data_structures = { path = "../alexandria_data_structures" }
alexandria_macros = { git = "https://github.com/keep-starknet-strange/alexandria.git" }
alexandria_macros.workspace = true

[tool]
fmt.workspace = true
Expand Down
Loading

0 comments on commit ce294c2

Please sign in to comment.