Skip to content

Commit

Permalink
add debug prints to locate long operations
Browse files Browse the repository at this point in the history
  • Loading branch information
enitrat committed Nov 21, 2024
1 parent c2e4ccb commit ad0eb3c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
2 changes: 2 additions & 0 deletions crates/evm/src/precompiles/modexp.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub impl ModExp of Precompile {
}

fn exec(input: Span<u8>) -> Result<(u64, Span<u8>), EVMError> {
println!("Start: Executing Modexp Precompile");
// The format of input is:
// <length_of_BASE> <length_of_EXPONENT> <length_of_MODULUS> <BASE> <EXPONENT> <MODULUS>
// Where every length is a 32-byte left-padded integer representing the number of bytes
Expand Down Expand Up @@ -98,6 +99,7 @@ pub impl ModExp of Precompile {
let output = modexp(base, exponent, modulus);

let return_data = output.pad_left_with_zeroes(mod_len);
println!("End: Executing Modexp Precompile");
Result::Ok((gas.into(), return_data))
}
}
Expand Down
4 changes: 4 additions & 0 deletions crates/utils/src/crypto/modexp/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ use crate::felt_vec::{Felt252VecTrait};
/// Computes `(base ^ exp) % modulus`, where all values are given as big-endian
/// encoded bytes.
pub fn modexp(base: Span<u8>, exp: Span<u8>, modulus: Span<u8>) -> Span<u8> {
println!("Start: MPNAT::from_big_endian operation");
let mut x = MPNatTrait::from_big_endian(base);
let mut m = MPNatTrait::from_big_endian(modulus);
println!("End: MPNAT::from_big_endian operation");

if m.digits.len == 1 && m.digits[0] == 0 {
return [].span();
}

println!("Start: modpow operation");
let mut result = x.modpow(exp, ref m);
println!("End: modpow operation");
result.digits.to_be_bytes()
}
11 changes: 10 additions & 1 deletion crates/utils/src/crypto/modexp/mpnat.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,13 @@ pub impl MPNatTraitImpl of MPNatTrait {
}

if modulus.is_power_of_two() { // return
println!("Start: modpow_with_power_of_two operation");
return self.modpow_with_power_of_two(exp, ref modulus);
println!("End: modpow_with_power_of_two operation");
} else if modulus.is_odd() {
println!("Start: modpow_montgomery operation");
return self.modpow_montgomery(exp, ref modulus);
println!("End: modpow_montgomery operation");
}

// If the modulus is not a power of two and not an odd number then
Expand Down Expand Up @@ -457,13 +461,18 @@ pub impl MPNatTraitImpl of MPNatTrait {
};

let mut base_copy = MPNat { digits: self.digits.duplicate(), };
println!("Start: modpow_montgomery operation");
let mut x1 = base_copy.modpow_montgomery(exp, ref odd);
println!("End: modpow_montgomery operation");
println!("Start: modpow_with_power_of_two operation");
let mut x2 = self.modpow_with_power_of_two(exp, ref power_of_two);
println!("End: modpow_with_power_of_two operation");

println!("Start: koc_2017_inverse operation");
let mut odd_inv = Self::koc_2017_inverse(
ref odd, trailing_zeros * WORD_BITS + additional_zero_bits
);

println!("End: koc_2017_inverse operation");
let s = power_of_two.digits.len();
let mut scratch: Felt252Vec<Word> = Felt252VecImpl::new();
scratch.expand(s).unwrap();
Expand Down

0 comments on commit ad0eb3c

Please sign in to comment.