-
Notifications
You must be signed in to change notification settings - Fork 172
Compute Legendre symbol for hash_to_curve
#77
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
b6b6216
Add `Legendre` trait and macro
davidnevadoc d9052ac
Add legendre macro call for prime fields
davidnevadoc f6ffe4f
Remove unused imports
davidnevadoc 963f357
Remove leftover
davidnevadoc a2e764c
Add `is_quadratic_non_residue` for hash_to_curve
davidnevadoc fc513a4
Add `legendre` function
davidnevadoc 04ad73d
Compute modulus separately
davidnevadoc 6b2777f
Substitute division for shift
davidnevadoc fd4a7c0
Update modulus computation
davidnevadoc 6a22a7e
Add quadratic residue check func
davidnevadoc 8ed6266
Add quadratic residue tests
davidnevadoc f5219a4
Add hash_to_curve bench
davidnevadoc 3832485
Implement Legendre trait for all curves
davidnevadoc ed460ec
Move misplaced comment
davidnevadoc 78a56d5
Add all curves to hash bench
davidnevadoc bdbb2f0
fix: add suggestion for legendre_exp
davidnevadoc 5e7022c
fix: imports after rebase
davidnevadoc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| use criterion::{black_box, criterion_group, criterion_main, Criterion}; | ||
| use pasta_curves::arithmetic::CurveExt; | ||
| use rand_core::{OsRng, RngCore}; | ||
| use std::iter; | ||
|
|
||
| fn hash_to_secp256k1(c: &mut Criterion) { | ||
| hash_to_curve::<halo2curves::secp256k1::Secp256k1>(c, "Secp256k1"); | ||
| } | ||
|
|
||
| fn hash_to_secq256k1(c: &mut Criterion) { | ||
| hash_to_curve::<halo2curves::secq256k1::Secq256k1>(c, "Secq256k1"); | ||
| } | ||
|
|
||
| fn hash_to_secp256r1(c: &mut Criterion) { | ||
| hash_to_curve::<halo2curves::secp256r1::Secp256r1>(c, "Secp256r1"); | ||
| } | ||
|
|
||
| fn hash_to_pallas(c: &mut Criterion) { | ||
| hash_to_curve::<halo2curves::pasta::Ep>(c, "Pallas"); | ||
| } | ||
|
|
||
| fn hash_to_vesta(c: &mut Criterion) { | ||
| hash_to_curve::<halo2curves::pasta::Eq>(c, "Vesta"); | ||
| } | ||
|
|
||
| fn hash_to_bn256(c: &mut Criterion) { | ||
| hash_to_curve::<halo2curves::bn256::G1>(c, "Bn256"); | ||
| } | ||
|
|
||
| fn hash_to_grumpkin(c: &mut Criterion) { | ||
| hash_to_curve::<halo2curves::grumpkin::G1>(c, "Grumpkin"); | ||
| } | ||
|
|
||
| fn hash_to_curve<G: CurveExt>(c: &mut Criterion, name: &'static str) { | ||
| { | ||
| let hasher = G::hash_to_curve("test"); | ||
| let mut rng = OsRng; | ||
| let message = iter::repeat_with(|| rng.next_u32().to_be_bytes()) | ||
| .take(1024) | ||
| .flatten() | ||
| .collect::<Vec<_>>(); | ||
|
|
||
| c.bench_function(&format!("Hash to {}", name), move |b| { | ||
| b.iter(|| hasher(black_box(&message))) | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| criterion_group!( | ||
| benches, | ||
| hash_to_secp256k1, | ||
| hash_to_secq256k1, | ||
| hash_to_secp256r1, | ||
| hash_to_pallas, | ||
| hash_to_vesta, | ||
| hash_to_bn256, | ||
| hash_to_grumpkin, | ||
| ); | ||
| criterion_main!(benches); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| use ff::{Field, PrimeField}; | ||
| use subtle::{Choice, ConstantTimeEq}; | ||
|
|
||
| pub trait Legendre: Field { | ||
| type BasePrimeField: PrimeField; | ||
|
|
||
| // This is (p-1)/2 where p is the modulus of the base prime field | ||
| fn legendre_exp() -> &'static [u64]; | ||
|
|
||
| fn norm(&self) -> Self::BasePrimeField; | ||
|
|
||
| #[inline] | ||
| fn legendre(&self) -> Self::BasePrimeField { | ||
| self.norm().pow(Self::legendre_exp()) | ||
| } | ||
|
|
||
| #[inline] | ||
| fn ct_quadratic_residue(&self) -> Choice { | ||
| self.legendre().ct_eq(&Self::BasePrimeField::ONE) | ||
| } | ||
|
|
||
| #[inline] | ||
| fn ct_quadratic_non_residue(&self) -> Choice { | ||
| self.legendre().ct_eq(&-Self::BasePrimeField::ONE) | ||
| } | ||
| } | ||
|
|
||
| #[macro_export] | ||
| macro_rules! prime_field_legendre { | ||
| ($field:ident ) => { | ||
| impl crate::legendre::Legendre for $field { | ||
| type BasePrimeField = Self; | ||
|
|
||
| #[inline] | ||
| fn legendre_exp() -> &'static [u64] { | ||
| lazy_static::lazy_static! { | ||
| // (p-1) / 2 | ||
| static ref LEGENDRE_EXP: Vec<u64> = | ||
| (num_bigint::BigUint::from_bytes_le((-<$field as ff::Field>::ONE).to_repr().as_ref())/2usize).to_u64_digits(); | ||
| } | ||
| &*LEGENDRE_EXP | ||
| } | ||
|
|
||
| #[inline] | ||
| fn norm(&self) -> Self::BasePrimeField { | ||
| self.clone() | ||
| } | ||
| } | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| mod arithmetic; | ||
| pub mod hash_to_curve; | ||
| #[macro_use] | ||
| pub mod legendre; | ||
| pub mod serde; | ||
|
|
||
| pub mod bn256; | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.