Skip to content

Commit

Permalink
dev: refactor constants to be base 2 instead of base 256 (#365)
Browse files Browse the repository at this point in the history
* dev: refactor constants to be base 2 instead of base 256

* change constants type to u128

* remove type suffix from POW_2_XXX
  • Loading branch information
lambda-0x authored Oct 3, 2023
1 parent 9c220dd commit cdc61dc
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 94 deletions.
42 changes: 20 additions & 22 deletions crates/evm/src/memory.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ use integer::{
u32_safe_divmod, u32_as_non_zero, u128_safe_divmod, u128_as_non_zero, u256_as_non_zero
};
use utils::constants::{
POW_256_0_U128, POW_256_1_U128, POW_256_2_U128, POW_256_3_U128, POW_256_4_U128, POW_256_5_U128,
POW_256_6_U128, POW_256_7_U128, POW_256_8_U128, POW_256_9_U128, POW_256_10_U128,
POW_256_11_U128, POW_256_12_U128, POW_256_13_U128, POW_256_14_U128, POW_256_15_U128,
POW_256_16_U256
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 cmp::{max};
use utils::{
Expand Down Expand Up @@ -292,7 +290,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_U256 / 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 @@ -361,22 +359,22 @@ impl InternalMemoryMethods of InternalMemoryTrait {
break;
}

let current: u128 = ((*elements[0]).into() * POW_256_15_U128
+ (*elements[1]).into() * POW_256_14_U128
+ (*elements[2]).into() * POW_256_13_U128
+ (*elements[3]).into() * POW_256_12_U128
+ (*elements[4]).into() * POW_256_11_U128
+ (*elements[5]).into() * POW_256_10_U128
+ (*elements[6]).into() * POW_256_9_U128
+ (*elements[7]).into() * POW_256_8_U128
+ (*elements[8]).into() * POW_256_7_U128
+ (*elements[9]).into() * POW_256_6_U128
+ (*elements[10]).into() * POW_256_5_U128
+ (*elements[11]).into() * POW_256_4_U128
+ (*elements[12]).into() * POW_256_3_U128
+ (*elements[13]).into() * POW_256_2_U128
+ (*elements[14]).into() * POW_256_1_U128
+ (*elements[15]).into() * POW_256_0_U128);
let current: u128 = ((*elements[0]).into() * POW_2_120
+ (*elements[1]).into() * POW_2_112
+ (*elements[2]).into() * POW_2_104
+ (*elements[3]).into() * POW_2_96
+ (*elements[4]).into() * POW_2_88
+ (*elements[5]).into() * POW_2_80
+ (*elements[6]).into() * POW_2_72
+ (*elements[7]).into() * POW_2_64
+ (*elements[8]).into() * POW_2_56
+ (*elements[9]).into() * POW_2_48
+ (*elements[10]).into() * POW_2_40
+ (*elements[11]).into() * POW_2_32
+ (*elements[12]).into() * POW_2_24
+ (*elements[13]).into() * POW_2_16
+ (*elements[14]).into() * POW_2_8
+ (*elements[15]).into() * POW_2_0);

self.items.insert(chunk_index.into(), current);
chunk_index += 1;
Expand Down Expand Up @@ -451,7 +449,7 @@ impl InternalMemoryMethods of InternalMemoryTrait {
// Compute mask.

let mask: u256 = helpers::pow256_rev(offset_in_chunk);
let mask_c: u256 = POW_256_16_U256 / 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
32 changes: 16 additions & 16 deletions crates/evm/src/tests/test_memory.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use evm::memory::{MemoryTrait, InternalMemoryTrait, MemoryPrintTrait};
use utils::{
math::Exponentiation, math::WrappingExponentiation, helpers, helpers::SpanExtensionTrait
};
use utils::constants::{POW_256_1_U128, POW_256_7_U128, POW_256_8_U128, POW_256_15_U128};
use utils::constants::{POW_2_8, POW_2_56, POW_2_64, POW_2_120};
use integer::BoundedInt;

mod internal {
Expand Down Expand Up @@ -239,36 +239,36 @@ fn test_load_should_load_an_element_from_the_memory() {
#[available_gas(200000000)]
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_256_8_U128, POW_256_8_U128, 0
8, 2 * POW_2_64, POW_2_64, 0
);
}
#[test]
#[available_gas(200000000)]
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_256_7_U128, POW_256_7_U128, 0
7, 2 * POW_2_56, POW_2_56, 0
);
}
#[test]
#[available_gas(200000000)]
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_256_7_U128, 2 * POW_256_7_U128, 0
23, 3 * POW_2_56, 2 * POW_2_56, 0
);
}

#[test]
#[available_gas(200000000)]
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_256_1_U128, 3 * POW_256_1_U128, 0
33, 4 * POW_2_8, 3 * POW_2_8, 0
);
}
#[test]
#[available_gas(200000000)]
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_256_15_U128, 0
63, 0, 4 * POW_2_120, 0
);
}

Expand All @@ -285,37 +285,37 @@ fn test_load_should_load_an_element_from_the_memory_with_offset_500() {
#[available_gas(200000000)]
fn test_load_should_load_an_element_from_the_memory_with_offset_8_and_active_segment_1() {
internal::load_should_load_an_element_from_the_memory_with_offset_stored_with_store_n(
8, 2 * POW_256_8_U128, POW_256_8_U128, 1
8, 2 * POW_2_64, POW_2_64, 1
);
}
#[test]
#[available_gas(200000000)]
fn test_load_should_load_an_element_from_the_memory_with_offset_7_and_active_segment_2() {
internal::load_should_load_an_element_from_the_memory_with_offset_stored_with_store_n(
7, 2 * POW_256_7_U128, POW_256_7_U128, 2
7, 2 * POW_2_56, POW_2_56, 2
);
}
#[test]
#[available_gas(200000000)]
fn test_load_should_load_an_element_from_the_memory_with_offset_23_and_active_segment_3() {
internal::load_should_load_an_element_from_the_memory_with_offset_stored_with_store_n(
23, 3 * POW_256_7_U128, 2 * POW_256_7_U128, 3
23, 3 * POW_2_56, 2 * POW_2_56, 3
);
}

#[test]
#[available_gas(200000000)]
fn test_load_should_load_an_element_from_the_memory_with_offset_33_and_active_segment_4() {
internal::load_should_load_an_element_from_the_memory_with_offset_stored_with_store_n(
33, 4 * POW_256_1_U128, 3 * POW_256_1_U128, 4
33, 4 * POW_2_8, 3 * POW_2_8, 2
);
}
#[test]
#[should_panic(expected: ('u32_mul Overflow',))]
#[available_gas(200000000)]
fn test_load_should_load_an_element_from_the_memory_with_offset_63_and_active_segment_usize_max() {
internal::load_should_load_an_element_from_the_memory_with_offset_stored_with_store_n(
63, 0, 4 * POW_256_15_U128, BoundedInt::<usize>::max()
63, 0, 4 * POW_2_120, BoundedInt::<usize>::max()
);
}

Expand All @@ -332,37 +332,37 @@ fn test_load_should_load_an_element_from_the_memory_with_offset_500_and_active_s
#[available_gas(200000000)]
fn test_load_should_load_an_element_from_the_memory_with_offset_8_and_active_segment_1_with_store() {
internal::load_should_load_an_element_from_the_memory_with_offset_stored_with_store(
8, 2 * POW_256_8_U128, POW_256_8_U128, 1
8, 2 * POW_2_64, POW_2_64, 1
);
}
#[test]
#[available_gas(200000000)]
fn test_load_should_load_an_element_from_the_memory_with_offset_7_and_active_segment_2_with_store() {
internal::load_should_load_an_element_from_the_memory_with_offset_stored_with_store(
7, 2 * POW_256_7_U128, POW_256_7_U128, 2
7, 2 * POW_2_56, POW_2_56, 2
);
}
#[test]
#[available_gas(200000000)]
fn test_load_should_load_an_element_from_the_memory_with_offset_23_and_active_segment_3_with_store() {
internal::load_should_load_an_element_from_the_memory_with_offset_stored_with_store(
23, 3 * POW_256_7_U128, 2 * POW_256_7_U128, 3
23, 3 * POW_2_56, 2 * POW_2_56, 3
);
}

#[test]
#[available_gas(200000000)]
fn test_load_should_load_an_element_from_the_memory_with_offset_33_and_active_segment_4_with_store() {
internal::load_should_load_an_element_from_the_memory_with_offset_stored_with_store(
33, 4 * POW_256_1_U128, 3 * POW_256_1_U128, 4
33, 4 * POW_2_8, 3 * POW_2_8, 4
);
}
#[test]
#[should_panic(expected: ('u32_mul Overflow',))]
#[available_gas(200000000)]
fn test_load_should_load_an_element_from_the_memory_with_offset_63_and_active_segment_usize_max_with_store() {
internal::load_should_load_an_element_from_the_memory_with_offset_stored_with_store(
63, 0, 4 * POW_256_15_U128, BoundedInt::<usize>::max()
63, 0, 4 * POW_2_120, BoundedInt::<usize>::max()
);
}

Expand Down
68 changes: 33 additions & 35 deletions crates/utils/src/constants.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,38 @@ const CHAIN_ID: u256 = 1263227476;
const STACK_MAX_DEPTH: usize = 1024;

// Numeric constants
const POW_256_0_U256: u256 = 0x1;
const POW_256_1_U256: u256 = 0x100;
const POW_256_2_U256: u256 = 0x10000;
const POW_256_3_U256: u256 = 0x1000000;
const POW_256_4_U256: u256 = 0x100000000;
const POW_256_5_U256: u256 = 0x10000000000;
const POW_256_6_U256: u256 = 0x1000000000000;
const POW_256_7_U256: u256 = 0x100000000000000;
const POW_256_8_U256: u256 = 0x10000000000000000;
const POW_256_9_U256: u256 = 0x1000000000000000000;
const POW_256_10_U256: u256 = 0x100000000000000000000;
const POW_256_11_U256: u256 = 0x10000000000000000000000;
const POW_256_12_U256: u256 = 0x1000000000000000000000000;
const POW_256_13_U256: u256 = 0x100000000000000000000000000;
const POW_256_14_U256: u256 = 0x10000000000000000000000000000;
const POW_256_15_U256: u256 = 0x1000000000000000000000000000000;
const POW_256_16_U256: u256 = 0x100000000000000000000000000000000;

const POW_256_0_U128: u128 = 0x1;
const POW_256_1_U128: u128 = 0x100;
const POW_256_2_U128: u128 = 0x10000;
const POW_256_3_U128: u128 = 0x1000000;
const POW_256_4_U128: u128 = 0x100000000;
const POW_256_5_U128: u128 = 0x10000000000;
const POW_256_6_U128: u128 = 0x1000000000000;
const POW_256_7_U128: u128 = 0x100000000000000;
const POW_256_8_U128: u128 = 0x10000000000000000;
const POW_256_9_U128: u128 = 0x1000000000000000000;
const POW_256_10_U128: u128 = 0x100000000000000000000;
const POW_256_11_U128: u128 = 0x10000000000000000000000;
const POW_256_12_U128: u128 = 0x1000000000000000000000000;
const POW_256_13_U128: u128 = 0x100000000000000000000000000;
const POW_256_14_U128: u128 = 0x10000000000000000000000000000;
const POW_256_15_U128: u128 = 0x1000000000000000000000000000000;
// POW_256_16 doesn't fit into u128
const POW_256_0: u128 = 0x1;
const POW_256_1: u128 = 0x100;
const POW_256_2: u128 = 0x10000;
const POW_256_3: u128 = 0x1000000;
const POW_256_4: u128 = 0x100000000;
const POW_256_5: u128 = 0x10000000000;
const POW_256_6: u128 = 0x1000000000000;
const POW_256_7: u128 = 0x100000000000000;
const POW_256_8: u128 = 0x10000000000000000;
const POW_256_9: u128 = 0x1000000000000000000;
const POW_256_10: u128 = 0x100000000000000000000;
const POW_256_11: u128 = 0x10000000000000000000000;
const POW_256_12: u128 = 0x1000000000000000000000000;
const POW_256_13: u128 = 0x100000000000000000000000000;
const POW_256_14: u128 = 0x10000000000000000000000000000;
const POW_256_15: u128 = 0x1000000000000000000000000000000;
const POW_256_16: u256 = 0x100000000000000000000000000000000;

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;
41 changes: 20 additions & 21 deletions crates/utils/src/helpers.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ use debug::PrintTrait;
use starknet::{EthAddress, EthAddressIntoFelt252};
use cmp::min;
use utils::constants::{
POW_256_0_U256, POW_256_1_U256, POW_256_2_U256, POW_256_3_U256, POW_256_4_U256, POW_256_5_U256,
POW_256_6_U256, POW_256_7_U256, POW_256_8_U256, POW_256_9_U256, POW_256_10_U256,
POW_256_11_U256, POW_256_12_U256, POW_256_13_U256, POW_256_14_U256, POW_256_15_U256,
POW_256_16_U256,
POW_256_0, POW_256_1, POW_256_2, POW_256_3, POW_256_4, POW_256_5, POW_256_6, POW_256_7,
POW_256_8, POW_256_9, POW_256_10, POW_256_11, POW_256_12, POW_256_13, POW_256_14, POW_256_15,
POW_256_16,
};
use keccak::u128_split;

Expand Down Expand Up @@ -33,39 +32,39 @@ fn pow256_rev(i: usize) -> u256 {
}

if i == 0 {
return POW_256_16_U256;
return POW_256_16;
} else if i == 1 {
return POW_256_15_U256;
return POW_256_15.into();
} else if i == 2 {
return POW_256_14_U256;
return POW_256_14.into();
} else if i == 3 {
return POW_256_13_U256;
return POW_256_13.into();
} else if i == 4 {
return POW_256_12_U256;
return POW_256_12.into();
} else if i == 5 {
return POW_256_11_U256;
return POW_256_11.into();
} else if i == 6 {
return POW_256_10_U256;
return POW_256_10.into();
} else if i == 7 {
return POW_256_9_U256;
return POW_256_9.into();
} else if i == 8 {
return POW_256_8_U256;
return POW_256_8.into();
} else if i == 9 {
return POW_256_7_U256;
return POW_256_7.into();
} else if i == 10 {
return POW_256_6_U256;
return POW_256_6.into();
} else if i == 11 {
return POW_256_5_U256;
return POW_256_5.into();
} else if i == 12 {
return POW_256_4_U256;
return POW_256_4.into();
} else if i == 13 {
return POW_256_3_U256;
return POW_256_3.into();
} else if i == 14 {
return POW_256_2_U256;
return POW_256_2.into();
} else if i == 15 {
return POW_256_1_U256;
return POW_256_1.into();
} else {
return POW_256_0_U256;
return POW_256_0.into();
}
}

Expand Down

0 comments on commit cdc61dc

Please sign in to comment.