Skip to content

Commit

Permalink
dev: optimize FromBytes (#907)
Browse files Browse the repository at this point in the history
* dev: optimize FromBytes

* use from_be_bytes_partial if input type isnt known"

* fix: p256 verify output is a 32-bytes array

* fix test p256 verify staticcall
  • Loading branch information
enitrat authored Sep 5, 2024
1 parent 5132b8c commit 37929a8
Show file tree
Hide file tree
Showing 5 changed files with 292 additions and 69 deletions.
47 changes: 42 additions & 5 deletions crates/evm/src/precompiles/p256verify.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,43 @@ use utils::helpers::{U256Trait, ToBytes, FromBytes};

const P256VERIFY_PRECOMPILE_GAS_COST: u128 = 3450;

const ONE_32_BYTES: [
u8
; 32] = [
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x01
];

impl P256Verify of Precompile {
#[inline(always)]
fn address() -> EthAddress {
Expand Down Expand Up @@ -62,7 +99,7 @@ impl P256Verify of Precompile {
return Result::Ok((gas, [].span()));
}

return Result::Ok((gas, [1].span()));
return Result::Ok((gas, ONE_32_BYTES.span()));
}
}

Expand Down Expand Up @@ -105,7 +142,7 @@ mod tests {

let (gas, result) = P256Verify::exec(calldata.span()).unwrap();

let result: u256 = result.from_be_bytes().unwrap();
let result: u256 = result.from_be_bytes().expect('p256verify_precompile_test');
assert_eq!(result, 0x01);
assert_eq!(gas, 3450);
}
Expand Down Expand Up @@ -137,7 +174,7 @@ mod tests {
.memory
.store(0x7618b065f9832de4ca6ca971a7a1adc826d0f7c00181a5fb2ddf79ae00b4e10e, 0x80); // y

vm.stack.push(0x01).unwrap(); // retSize
vm.stack.push(0x20).unwrap(); // retSize
vm.stack.push(0xa0).unwrap(); // retOffset
vm.stack.push(0xa0).unwrap(); // argsSize
vm.stack.push(0x0).unwrap(); // argsOffset
Expand All @@ -148,9 +185,9 @@ mod tests {
vm.exec_staticcall().unwrap();

let mut result = Default::default();
vm.memory.load_n(0x1, ref result, 0xa0);
vm.memory.load_n(0x20, ref result, 0xa0);

assert_eq!(result, array![0x01]);
assert_eq!(result.span(), super::ONE_32_BYTES.span());
}

#[test]
Expand Down
4 changes: 4 additions & 0 deletions crates/utils/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ edition = "2023_10"
evm = { path = "../evm" }
alexandria_data_structures = { path = "../alexandria_data_structures" }

# For profiling
[cairo]
unstable-add-statements-functions-debug-info = true

[tool]
fmt.workspace = true

Expand Down
8 changes: 4 additions & 4 deletions crates/utils/src/crypto/modexp/mpnat.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub impl MPNatTraitImpl of MPNatTrait {
buf.copy_from_bytes_le((WORD_BYTES - r), bytes.slice(0, r)).unwrap();

// safe unwrap, since we know that bytes won't overflow
let word = buf.to_le_bytes().from_be_bytes().unwrap();
let word = buf.to_le_bytes().from_be_bytes().expect('mpnat_from_big_endian_word');
digits.set(i, word);

if i == 0 {
Expand All @@ -88,7 +88,7 @@ pub impl MPNatTraitImpl of MPNatTrait {
buf.copy_from_bytes_le(0, bytes.slice(j, next_j - j)).unwrap();

// safe unwrap, since we know that bytes won't overflow
let word: u64 = buf.to_le_bytes().from_be_bytes().unwrap();
let word: u64 = buf.to_le_bytes().from_be_bytes().expect('mpnat_from_big_endian_word');
digits.set(i, word);

if i == 0 {
Expand Down Expand Up @@ -369,7 +369,7 @@ pub impl MPNatTraitImpl of MPNatTrait {
}

if exp.len() <= (ByteSize::<usize>::byte_size()) {
let exp_as_number: usize = exp.from_le_bytes().unwrap();
let exp_as_number: usize = exp.from_le_bytes_partial().expect('modpow_exp_as_number');

match self.digits.len().checked_mul(exp_as_number) {
Option::Some(max_output_digits) => {
Expand Down Expand Up @@ -714,7 +714,7 @@ mod tests {

i += 1;
};
result.from_le_bytes().unwrap()
result.from_le_bytes_partial().expect('mpnat_to_u128')
}

fn check_modpow_even(base: u128, exp: u128, modulus: u128, expected: u128) {
Expand Down
Loading

0 comments on commit 37929a8

Please sign in to comment.