Skip to content

Commit

Permalink
Fix bug in ec_point_from_x (#386)
Browse files Browse the repository at this point in the history
* Add NonZero case to check_next_type

* stash

* Minimal solution

* Improve solution

* Try much easier solution

* Clean code

* Clippy + Fmt

* Unleash the tests
  • Loading branch information
fmoletta authored Dec 15, 2023
1 parent cbc1d52 commit 3f6f705
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ cairo-lang-runner = "=2.3"
libc = "0.2"
starknet-crypto = "0.6"
starknet-curve = "0.4"
lazy_static = "1.4"
13 changes: 12 additions & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
#![allow(non_snake_case)]

use lazy_static::lazy_static;
use starknet_crypto::FieldElement;
use starknet_curve::AffinePoint;
use starknet_types_core::felt::{felt_to_bigint, Felt};
use std::{collections::HashMap, fs::File, io::Write, os::fd::FromRawFd, ptr::NonNull, slice};
lazy_static! {
pub static ref HALF_PRIME: FieldElement = FieldElement::from_dec_str(
"1809251394333065606848661391547535052811553607665798349986546028067936010240"
)
.unwrap();
}

pub(crate) fn as_cairo_short_string(value: &Felt) -> Option<String> {
let mut as_string = String::default();
Expand Down Expand Up @@ -235,7 +242,11 @@ pub unsafe extern "C" fn cairo_native__libfunc__ec__ec_point_from_x_nz(
.unwrap();

match AffinePoint::from_x(x) {
Some(point) => {
Some(mut point) => {
// If y > PRIME/ 2 use PRIME - y
if point.y >= *HALF_PRIME {
point.y = -point.y
}
point_ptr.as_mut()[1].copy_from_slice(&point.y.to_bytes_be());
point_ptr.as_mut()[1].reverse();

Expand Down
10 changes: 9 additions & 1 deletion tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,15 @@ pub fn compare_outputs(
prop_assert_eq!(vm_value, native_value)
}
CoreTypeConcrete::Uint128MulGuarantee(_) => todo!(),
CoreTypeConcrete::NonZero(_) => todo!(),
CoreTypeConcrete::NonZero(info) => {
prop_assert!(native_rets.peek().is_some());
check_next_type(
reg.get_type(&info.ty).expect("type should exist"),
&mut [native_rets.next().unwrap()].into_iter(),
vm_rets,
reg,
)?;
}
CoreTypeConcrete::Nullable(_) => todo!(),
CoreTypeConcrete::RangeCheck(_) => {
// runner: ignore
Expand Down
3 changes: 0 additions & 3 deletions tests/ec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ fn ec_point_zero() {
.unwrap();
}

#[ignore = "TODO: possible bug in ec_point_from_x_nz"]
#[test]
fn ec_point_from_x_big() {
let x = DeprecatedFelt::new(
Expand All @@ -82,7 +81,6 @@ fn ec_point_from_x_big() {
.unwrap();
}

#[ignore = "TODO: Still have to implement NonZero type comparisons"]
#[test]
fn ec_point_from_x_small() {
let x = DeprecatedFelt::new(BigUint::from_str("1234").unwrap());
Expand Down Expand Up @@ -125,7 +123,6 @@ proptest! {
)?;
}

#[ignore = "TODO: possible bug in ec_point_from_x_nz"]
#[test]
fn ec_point_from_x_proptest(a in any_felt()) {
let program = &EC_POINT_FROM_X;
Expand Down

0 comments on commit 3f6f705

Please sign in to comment.