Skip to content
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

Merged
merged 17 commits into from
Apr 12, 2023
Merged

Add quad hint and integration test #715

merged 17 commits into from
Apr 12, 2023

Conversation

Juan-M-V
Copy link
Contributor

TITLE

Description

Description of the pull request changes and motivation.

Checklist

  • Linked to Github Issue
  • Unit tests added
  • Integration tests added.
  • This change requires new documentation.
    • Documentation has been added/updated.

Comment on lines 180 to 187
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;
}
}
Copy link
Contributor

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.

Comment on lines 179 to 187
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;
}
}
Copy link
Contributor

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:

Suggested change
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;
}
}

@Juan-M-V Juan-M-V marked this pull request as ready for review January 16, 2023 14:23
Copy link
Contributor

@fmoletta fmoletta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great Work!

Comment on lines 1926 to 1944
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());
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
Copy link

codecov bot commented Feb 15, 2023

Codecov Report

Merging #715 (0d91946) into main (2a29461) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##             main     #715   +/-   ##
=======================================
  Coverage   97.74%   97.74%           
=======================================
  Files          74       74           
  Lines       30348    30392   +44     
=======================================
+ Hits        29663    29708   +45     
+ Misses        685      684    -1     
Impacted Files Coverage Δ
...int_processor/builtin_hint_processor_definition.rs 98.66% <100.00%> (+<0.01%) ⬆️
...int_processor/builtin_hint_processor/math_utils.rs 97.81% <100.00%> (+0.06%) ⬆️

... and 1 file with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

fmoletta added a commit that referenced this pull request Mar 10, 2023
exponent >>= 1;
// if adm ≡ -1 (mod CAIRO_PRIME)
if adm == max_felt {
m += FeltBigInt::one() << i;
Copy link
Contributor

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.

juanbono pushed a commit that referenced this pull request Mar 17, 2023
* 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]>
@fmoletta fmoletta added the whitelisted-hint Implementation of hint on whitelist directory label Apr 11, 2023
@fmoletta fmoletta added this pull request to the merge queue Apr 12, 2023
Merged via the queue into main with commit 5fab4cc Apr 12, 2023
@fmoletta fmoletta deleted the is-quad-residue-hint branch April 12, 2023 13:19
kariy pushed a commit to dojoengine/cairo-rs that referenced this pull request Jun 23, 2023
* 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
whitelisted-hint Implementation of hint on whitelist directory
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants