-
Notifications
You must be signed in to change notification settings - Fork 168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add quad hint and integration test #715
Conversation
felt/src/bigint_felt.rs
Outdated
let mut m = FeltBigInt::zero(); | ||
for i in 0..48 { | ||
let mut adm = &a * &(&d).pow(&m); | ||
adm = adm.pow(1_u64 << (47 - i)); | ||
if &adm == &max_felt { | ||
m += 1 << i; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I strongly suspect this loop can be changed to perform much fewer calls to pow
, which is likely the culprit. I'll need some time to think of a way to do that.
felt/src/bigint_felt.rs
Outdated
let d = (&FeltBigInt::new(3_i32)).pow(&trailing_prime); | ||
let mut m = FeltBigInt::zero(); | ||
for i in 0..48 { | ||
let mut adm = &a * &(&d).pow(&m); | ||
adm = adm.pow(1_u64 << (47 - i)); | ||
if &adm == &max_felt { | ||
m += 1 << i; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this might help:
let d = (&FeltBigInt::new(3_i32)).pow(&trailing_prime); | |
let mut m = FeltBigInt::zero(); | |
for i in 0..48 { | |
let mut adm = &a * &(&d).pow(&m); | |
adm = adm.pow(1_u64 << (47 - i)); | |
if &adm == &max_felt { | |
m += 1 << i; | |
} | |
} | |
let d = (&FeltBigInt::new(3_i32)).pow(&trailing_prime); | |
let d_pow = d.clone(); | |
let mut adm = a.clone(); | |
for i in 0..48 { | |
adm = adm.pow(1_u64 << (47 - i)); | |
if &adm == &max_felt { | |
d_pow *= d_pow * d; | |
} | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great Work!
fn run_is_quad_residue(ref x in "([1-9][0-9]*)") { | ||
let mut vm = vm!(); | ||
vm.run_context.fp = 2; | ||
vm.memory = memory![((1, 1), (&x[..], 10))]; | ||
let ids_data = ids_data!["y", "x"]; | ||
|
||
assert_eq!(run_hint!(vm, ids_data, hint_code::IS_QUAD_RESIDUE), Ok(())); | ||
|
||
let x = &Felt::parse_bytes(x.as_bytes(), 10).unwrap(); | ||
|
||
if x.is_zero() || x.is_one() { | ||
assert_eq!(vm.get_integer(&Relocatable::from((1, 0))).unwrap().as_ref(), x); | ||
} else if x.pow(Felt::max_value() >> 1).is_one() { | ||
assert_eq!(vm.get_integer(&Relocatable::from((1, 0))).unwrap().into_owned(), x.sqrt()); | ||
} else { | ||
assert_eq!(vm.get_integer(&Relocatable::from((1, 0))).unwrap().into_owned(), (x / Felt::new(3)).sqrt()); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fn run_is_quad_residue(ref x in "([1-9][0-9]*)") { | |
let mut vm = vm!(); | |
vm.run_context.fp = 2; | |
vm.memory = memory![((1, 1), (&x[..], 10))]; | |
let ids_data = ids_data!["y", "x"]; | |
assert_eq!(run_hint!(vm, ids_data, hint_code::IS_QUAD_RESIDUE), Ok(())); | |
let x = &Felt::parse_bytes(x.as_bytes(), 10).unwrap(); | |
if x.is_zero() || x.is_one() { | |
assert_eq!(vm.get_integer(&Relocatable::from((1, 0))).unwrap().as_ref(), x); | |
} else if x.pow(Felt::max_value() >> 1).is_one() { | |
assert_eq!(vm.get_integer(&Relocatable::from((1, 0))).unwrap().into_owned(), x.sqrt()); | |
} else { | |
assert_eq!(vm.get_integer(&Relocatable::from((1, 0))).unwrap().into_owned(), (x / Felt::new(3)).sqrt()); | |
} | |
} | |
} | |
fn run_is_quad_residue_for_quad_residue(ref x in "([1-9][0-9]*)") { | |
let x = &Felt::parse_bytes(x.as_bytes(), 10).unwrap(); | |
let quad = x * x; | |
let mut vm = vm!(); | |
vm.run_context.fp = 2; | |
vm.memory = memory![((1, 1), &quad)]; | |
let ids_data = ids_data!["y", "x"]; | |
assert_eq!(run_hint!(vm, ids_data, hint_code::IS_QUAD_RESIDUE), Ok(())); | |
assert_eq!(vm.get_integer(&Relocatable::from((1, 0))).unwrap().into_owned(), x); | |
} | |
} | |
fn run_is_quad_residue_for_non_quad_residue(ref x in "([1-9][0-9]*)") { | |
let x = &Felt::parse_bytes(x.as_bytes(), 10).unwrap(); | |
prop_assume!(x.pow(Felt::max_value() >> 1).is_one()); | |
let mut vm = vm!(); | |
vm.run_context.fp = 2; | |
vm.memory = memory![((1, 1), &x)]; | |
let ids_data = ids_data!["y", "x"]; | |
assert_eq!(run_hint!(vm, ids_data, hint_code::IS_QUAD_RESIDUE), Ok(())); | |
assert_eq!(vm.get_integer(&Relocatable::from((1, 0))).unwrap().into_owned(), (x / Felt::new(3)).sqrt()); | |
} | |
} |
For 0
and 1
just use unit tests.
Codecov Report
@@ Coverage Diff @@
## main #715 +/- ##
=======================================
Coverage 97.74% 97.74%
=======================================
Files 74 74
Lines 30348 30392 +44
=======================================
+ Hits 29663 29708 +45
+ Misses 685 684 -1
... and 1 file with indirect coverage changes 📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
felt/src/bigint_felt.rs
Outdated
exponent >>= 1; | ||
// if adm ≡ -1 (mod CAIRO_PRIME) | ||
if adm == max_felt { | ||
m += FeltBigInt::one() << i; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can make a lookup table with the 192
possible powers of 2
for this.
* Initial progress * Implement random_ec_point hint * Add integration test * Fix alpha & beta values * Remove commented code * fix test program * Pad seed bytes * Pad left * Fix i padding * Fix x & y_coef * Copy sqrt implementation from PR #715 * Move sqrt to math_utils + cleanup * Add proptests * use constants in proptests * Add some tests * Add test * Add test * Add implementation for hint on chained_ec_op * Fix string format * Fix tests * Add test program * Add test program * Add impl for recover_y hint * Add test for hint * Add integration tests * Clippy * Clippy * Add newline at EOf * Remove unused trait impl * Update src/hint_processor/builtin_hint_processor/ec_utils.rs Co-authored-by: Mario Rugiero <[email protected]> * Use constant for Felt::max / 2 * Add missing import * Fix proptest dependency * Try to fix wasm tests --------- Co-authored-by: Mario Rugiero <[email protected]>
* Add quad hint and integration test * Fix out of memory bug * Implement hint and fix bugs * Add prop test * Add comment to sqrt * Take smaller root * Fix clippy warnings * Add sqrt prop test * Remove zero from test * Fix qrong sqrt being used * Fix qrong sqrt being used * Reduce integration test lenght * Fix wasm test * Clippy --------- Co-authored-by: Juanma <[email protected]> Co-authored-by: Federica <[email protected]>
TITLE
Description
Description of the pull request changes and motivation.
Checklist