Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use scarb macro to generate const powers #865

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -69,6 +74,7 @@ name = "utils"
version = "0.1.0"
dependencies = [
"alexandria_data_structures",
"alexandria_macros",
"evm",
"snforge_std",
]
1 change: 1 addition & 0 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 2 additions & 3 deletions crates/contracts/src/account_contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');

Expand Down Expand Up @@ -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');

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 @@ -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));
Expand Down Expand Up @@ -318,22 +314,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 @@ -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());
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
);
}

Expand Down
1 change: 1 addition & 0 deletions crates/utils/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Loading
Loading