diff --git a/src/bls12_381/fq2.rs b/src/bls12_381/fq2.rs index 3095661..aa6ccde 100644 --- a/src/bls12_381/fq2.rs +++ b/src/bls12_381/fq2.rs @@ -158,7 +158,7 @@ impl Field for Fq2 { impl SqrtField for Fq2 { fn legendre(&self) -> ::LegendreSymbol { - Fq2::norm(&self).legendre() + self.norm().legendre() } fn sqrt(&self) -> Option { @@ -578,7 +578,7 @@ fn bench_fq2_sqrt(b: &mut ::test::Bencher) { #[test] fn fq2_field_tests() { use ::PrimeField; - + ::tests::field::random_field_tests::(); ::tests::field::random_sqrt_tests::(); ::tests::field::random_frobenius_tests::(super::fq::Fq::char(), 13); diff --git a/src/bls12_381/fr.rs b/src/bls12_381/fr.rs index 0f159de..058cb6a 100644 --- a/src/bls12_381/fr.rs +++ b/src/bls12_381/fr.rs @@ -554,6 +554,7 @@ impl Fr { impl SqrtField for Fr { fn legendre(&self) -> ::LegendreSymbol { + // s = self^((r - 1) // 2) let s = self.pow([0x7fffffff80000000, 0xa9ded2017fff2dff, 0x199cec0404d0ec02, 0x39f6d3a994cebea4]); if s == Self::zero() { Zero } else if s == Self::one() { QuadraticResidue } diff --git a/src/tests/field.rs b/src/tests/field.rs index 5f99992..bddb93e 100644 --- a/src/tests/field.rs +++ b/src/tests/field.rs @@ -1,5 +1,5 @@ use rand::{Rng, SeedableRng, XorShiftRng}; -use ::{SqrtField, Field, PrimeField}; +use ::{SqrtField, Field, PrimeField, LegendreSymbol}; pub fn random_frobenius_tests>(characteristic: C, maxpower: usize) { let mut rng = XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); @@ -26,6 +26,7 @@ pub fn random_sqrt_tests() { let a = F::rand(&mut rng); let mut b = a; b.square(); + assert_eq!(b.legendre(), LegendreSymbol::QuadraticResidue); let b = b.sqrt().unwrap(); let mut negb = b; @@ -38,6 +39,8 @@ pub fn random_sqrt_tests() { for _ in 0..10000 { let mut b = c; b.square(); + assert_eq!(b.legendre(), LegendreSymbol::QuadraticResidue); + b = b.sqrt().unwrap(); if b != c {