Skip to content

Commit

Permalink
fix: ec_mul
Browse files Browse the repository at this point in the history
  • Loading branch information
enitrat committed Sep 23, 2024
1 parent 64bfa7d commit 8a45c9c
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions crates/evm/src/precompiles/ec_mul.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ fn get_bits_little(s: u256) -> Array<felt252> {
s_low = q;
};
let mut s_high = s.high;

if s_high != 0 {
while bits.len() != 128 {
bits.append(0);
}
}
while s_high != 0 {
let (q, r) = core::traits::DivRem::div_rem(s_high, 2);
bits.append(r.into());
Expand All @@ -108,21 +112,32 @@ fn get_bits_little(s: u256) -> Array<felt252> {
// Returns Option::None in case of point at infinity.
// The size of bits array must be at minimum 2 and the point must be on the curve.
fn ec_mul_inner(pt: (u384, u384), mut bits: Array<felt252>) -> Option<(u384, u384)> {
let (x_o, y_o) = pt;
let mut pt = Option::Some(pt);
let (mut temp_x, mut temp_y) = pt;
let mut result: Option<(u384, u384)> = Option::None;
while let Option::Some(bit) = bits.pop_front() {
match pt {
Option::Some((xt, yt)) => pt = Option::Some(double_ec_point_unchecked(xt, yt)),
Option::None => pt = Option::None,
}

if bit != 0 {
match pt {
Option::Some((xt, yt)) => pt = ec_safe_add(x_o, y_o, xt, yt),
Option::None => pt = Option::Some((x_o, y_o)),
match result {
Option::Some((xr, yr)) => result = ec_safe_add(temp_x, temp_y, xr, yr),
Option::None => result = Option::Some((temp_x, temp_y)),
};
};
let (_temp_x, _temp_y) = double_ec_point_unchecked(temp_x, temp_y);
temp_x = _temp_x;
temp_y = _temp_y;
};

pt
return result;
}

#[cfg(test)]
mod tests {
use super::ec_mul;

#[test]
fn test_ec_mul() {
let (x1,y1,s) = (1,2,2);
let (x, y) = ec_mul(x1, y1, s).expect('ec_mul failed');
assert_eq!(x, 0x030644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd3);
assert_eq!(y, 0x15ed738c0e0a7c92e7845f96b2ae9c0a68a6a449e3538fc7ff3ebf7a5a18a2c4);
}
}

0 comments on commit 8a45c9c

Please sign in to comment.