Skip to content

Commit

Permalink
Update ec runtime methods (#791)
Browse files Browse the repository at this point in the history
* Update rust to 1.81.0 and cairo to 2.8.2

* lock

* clippy

* update

* update lambdaworks math

* make the runtime library abort on panic

* readme

* Update ec runtime methods
  • Loading branch information
edg-l authored Sep 16, 2024
1 parent 2be717c commit a478e89
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,11 @@ pub unsafe extern "C" fn cairo_native__libfunc__ec__ec_point_from_x_nz(
let x3 = x2 * x;
let alpha_x_plus_beta = x + BETA;
let rhs = x3 + alpha_x_plus_beta;
let y = rhs.sqrt().unwrap_or_else(|| Felt::from(3) * rhs);
// https://github.com/starkware-libs/cairo/blob/9b603b88c2e5a98eec1bb8f323260b7765e94911/crates/cairo-lang-runner/src/casm_run/mod.rs#L1825
let y = rhs
.sqrt()
.unwrap_or_else(|| (Felt::THREE * rhs).sqrt().unwrap());
let y = y.min(-y);

match AffinePoint::new(x, y) {
Ok(point) => {
Expand Down Expand Up @@ -358,7 +362,8 @@ pub unsafe extern "C" fn cairo_native__libfunc__ec__ec_state_init(
}
};

let state = AffinePoint::new(random_x, random_y).unwrap();
// We already made sure its a valid point.
let state = AffinePoint::new_unchecked(random_x, random_y);

state_ptr.as_mut()[0].copy_from_slice(&state.x().to_bytes_le());
state_ptr.as_mut()[1].copy_from_slice(&state.y().to_bytes_le());
Expand All @@ -381,16 +386,15 @@ pub unsafe extern "C" fn cairo_native__libfunc__ec__ec_state_add(
mut state_ptr: NonNull<[[u8; 32]; 4]>,
point_ptr: NonNull<[[u8; 32]; 2]>,
) {
let mut state = ProjectivePoint::from_affine(
// We use unchecked methods because the inputs must already be valid points.
let mut state = ProjectivePoint::from_affine_unchecked(
Felt::from_bytes_le(&state_ptr.as_ref()[0]),
Felt::from_bytes_le(&state_ptr.as_ref()[1]),
)
.unwrap();
let point = AffinePoint::new(
);
let point = AffinePoint::new_unchecked(
Felt::from_bytes_le(&point_ptr.as_ref()[0]),
Felt::from_bytes_le(&point_ptr.as_ref()[1]),
)
.unwrap();
);

state += &point;
let state = state.to_affine().unwrap();
Expand Down Expand Up @@ -448,16 +452,15 @@ pub unsafe extern "C" fn cairo_native__libfunc__ec__ec_state_try_finalize_nz(
mut point_ptr: NonNull<[[u8; 32]; 2]>,
state_ptr: NonNull<[[u8; 32]; 4]>,
) -> bool {
let state = ProjectivePoint::from_affine(
// We use unchecked methods because the inputs must already be valid points.
let state = ProjectivePoint::from_affine_unchecked(
Felt::from_bytes_le(&state_ptr.as_ref()[0]),
Felt::from_bytes_le(&state_ptr.as_ref()[1]),
)
.unwrap();
let random = ProjectivePoint::from_affine(
);
let random = ProjectivePoint::from_affine_unchecked(
Felt::from_bytes_le(&state_ptr.as_ref()[2]),
Felt::from_bytes_le(&state_ptr.as_ref()[3]),
)
.unwrap();
);

if state.x() == random.x() && state.y() == random.y() {
false
Expand Down

0 comments on commit a478e89

Please sign in to comment.