Create enum for LegendreSymbol, fix test.

This commit is contained in:
Michele Orrù 2017-08-09 14:09:54 +02:00
parent 1b6cf85251
commit 57b4e73627
5 changed files with 51 additions and 60 deletions

@ -833,19 +833,14 @@ impl SqrtField for Fq {
}
impl LegendreField for Fq {
fn legendre(&self) -> i32 {
// (q - 1) / 2 =
// 2001204777610833696708894912867952078278441409969503942666029068062015825245418932221343814564507832018947136279893
let x = self.pow([0xcff7fffffffd555, 0xf55ffff58a9ffffd,
0x39869507b587b120, 0x23ba5c279c2895fb,
0x58dd3db21a5d66bb, 0xd0088f51cbff34d2]);
if x == Self::one() {
1
} else if x == Self::zero() {
0
} else {
-1
}
fn legendre(&self) -> ::LegendreSymbol {
use ::LegendreSymbol::*;
let s = self.pow([0xdcff7fffffffd555, 0xf55ffff58a9ffff, 0xb39869507b587b12,
0xb23ba5c279c2895f, 0x258dd3db21a5d66b, 0xd0088f51cbff34d]);
if s == Fq::zero() { Zero }
else if s == Fq::one() { QResidue }
else { QNonResidue }
}
}
@ -1796,29 +1791,21 @@ fn test_fq_ordering() {
fn fq_repr_tests() {
::tests::repr::random_repr_tests::<FqRepr>();
}
#[test]
fn test_fq_legendre() {
assert_eq!(1, Fq::one().legendre());
assert_eq!(0, Fq::zero().legendre());
use ::LegendreSymbol::*;
let e = Fq(FqRepr([0x914577fdcc41112, 0x1a6c20f3392c28e2, 0xd53f75da0c40fd21,
0xb747c10d13caf0d0, 0x0de1adc19c24d8d2, 0x2103f924191033d2]));
assert_eq!(-1, e.legendre());
assert_eq!(QResidue, Fq::one().legendre());
assert_eq!(Zero, Fq::zero().legendre());
assert_eq!(QNonResidue, Fq::from_repr(FqRepr::from(2)).unwrap().legendre());
assert_eq!(QResidue, Fq::from_repr(FqRepr::from(4)).unwrap().legendre());
// f
let e = Fq(FqRepr([0xe51d5292ae8126f, 0x382d60874f48db82, 0xb0dde25abc614254,
0x34f4456bd18813df, 0x2c668010247ee04c, 0x44cb8bbdd7c4f1b0]));
assert_eq!(-1, e.legendre());
// f ** 9
let e = Fq(FqRepr([0x69fc8eb1b590c712, 0xd73f4fb6fd34042e, 0xb5677ef2ed0eede7,
0x367d831c592848c8, 0xb60615cc44e533f5, 0x127da624461b200e]));
assert_eq!(-1, e.legendre());
let e = Fq(FqRepr([0x83c7ad9e29b7facc, 0x97b3c8fbdb50cc39, 0x9e2ccd0eb5db5e72,
0xc74a00d90e1b247d, 0x90e38ef46c8d7eb7, 0x16882d6aa70bb469]));
assert_eq!(1, e.legendre());
let e = FqRepr([0x52a112f249778642, 0xd0bedb989b7991f, 0xdad3b6681aa63c05,
0xf2efc0bb4721b283, 0x6057a98f18c24733, 0x1022c2fd122889e4]);
assert_eq!(QNonResidue, Fq::from_repr(e).unwrap().legendre());
let e = FqRepr([0x6dae594e53a96c74, 0x19b16ca9ba64b37b, 0x5c764661a59bfc68,
0xaa346e9b31c60a, 0x346059f9d87a9fa9, 0x1d61ac6bfd5c88b]);
assert_eq!(QResidue, Fq::from_repr(e).unwrap().legendre());
}

@ -197,7 +197,7 @@ impl SqrtField for Fq2 {
}
impl LegendreField for Fq2 {
fn legendre(&self) -> i32 {
fn legendre(&self) -> ::LegendreSymbol {
Fq2::norm(&self).legendre()
}
}
@ -431,13 +431,14 @@ fn test_fq2_sqrt() {
#[test]
fn test_fq2_legendre() {
use ::LegendreSymbol::*;
// i^2 = -1
let mut m1 = Fq2::one();
m1.negate();
assert_eq!(1, m1.legendre());
assert_eq!(QResidue, m1.legendre());
m1.mul_by_nonresidue();
assert_eq!(-1, m1.legendre());
assert_eq!(QNonResidue, m1.legendre());
}
#[cfg(test)]

@ -1,4 +1,5 @@
use ::{Field, LegendreField, PrimeField, SqrtField, PrimeFieldRepr, PrimeFieldDecodingError};
use ::LegendreSymbol::*;
// r = 52435875175126190479447740508185965837690552500527637822603658699938581184513
const MODULUS: FrRepr = FrRepr([0xffffffff00000001, 0x53bda402fffe5bfe, 0x3339d80809a1d805, 0x73eda753299d7d48]);
@ -559,7 +560,7 @@ impl SqrtField for Fr {
return Some(*self);
}
if self.legendre() != 1 {
if let QNonResidue = self.legendre() {
None
} else {
let mut c = Fr(ROOT_OF_UNITY);
@ -598,16 +599,11 @@ impl SqrtField for Fr {
}
impl LegendreField for Fr {
fn legendre(&self) -> i32 {
// (q - 1) / 2 = 26217937587563095239723870254092982918845276250263818911301829349969290592256
let x = self.pow([0x7fffffff80000000, 0xa9ded2017fff2dff, 0x199cec0404d0ec02, 0x39f6d3a994cebea4]);
if x == Self::one() {
1
} else if x == Self::zero() {
0
} else {
-1
}
fn legendre(&self) -> ::LegendreSymbol {
let s = self.pow([0x7fffffff80000000, 0xa9ded2017fff2dff, 0x199cec0404d0ec02, 0x39f6d3a994cebea4]);
if s == Self::zero() { Zero }
else if s == Self::one() { QResidue }
else { QNonResidue }
}
}
#[cfg(test)]
@ -792,13 +788,13 @@ fn test_fr_repr_sub_noborrow() {
#[test]
fn test_fr_legendre() {
assert_eq!(1, Fr::one().legendre());
assert_eq!(0, Fr::zero().legendre());
assert_eq!(QResidue, Fr::one().legendre());
assert_eq!(Zero, Fr::zero().legendre());
let e = Fr(FrRepr([0x0dbc5349cd5664da, 0x8ac5b6296e3ae29d, 0x127cb819feceaa3b, 0x3a6b21fb03867191]));
assert_eq!(1, e.legendre());
let e = Fr(FrRepr([0x96341aefd047c045, 0x9b5f4254500a4d65, 0x1ee08223b68ac240, 0x31d9cd545c0ec7c6]));
assert_eq!(-1, e.legendre());
let e = FrRepr([0x0dbc5349cd5664da, 0x8ac5b6296e3ae29d, 0x127cb819feceaa3b, 0x3a6b21fb03867191]);
assert_eq!(QResidue, Fr::from_repr(e).unwrap().legendre());
let e = FrRepr([0x96341aefd047c045, 0x9b5f4254500a4d65, 0x1ee08223b68ac240, 0x31d9cd545c0ec7c6]);
assert_eq!(QNonResidue, Fr::from_repr(e).unwrap().legendre());
}
#[test]

@ -336,7 +336,7 @@ pub trait SqrtField: Field
pub trait LegendreField: Field
{
/// Returns the legendre symbol of the field element.
fn legendre(&self) -> i32;
fn legendre(&self) -> LegendreSymbol;
}
@ -417,6 +417,13 @@ pub trait PrimeFieldRepr: Sized +
}
}
#[derive(Debug, PartialEq)]
pub enum LegendreSymbol {
Zero = 0,
QResidue = 1,
QNonResidue = -1
}
/// An error that may occur when trying to interpret a `PrimeFieldRepr` as a
/// `PrimeField` element.
#[derive(Debug)]