Merge traits SqrtField and LegendreField into SqrtField.
This commit is contained in:
parent
2d3f498e75
commit
6feb0f802f
|
@ -1,4 +1,4 @@
|
||||||
use ::{Field, LegendreField, PrimeField, SqrtField, PrimeFieldRepr, PrimeFieldDecodingError};
|
use ::{Field, PrimeField, SqrtField, PrimeFieldRepr, PrimeFieldDecodingError};
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
use super::fq2::Fq2;
|
use super::fq2::Fq2;
|
||||||
|
|
||||||
|
@ -810,6 +810,17 @@ impl Fq {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SqrtField for Fq {
|
impl SqrtField for Fq {
|
||||||
|
|
||||||
|
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 }
|
||||||
|
}
|
||||||
|
|
||||||
fn sqrt(&self) -> Option<Self> {
|
fn sqrt(&self) -> Option<Self> {
|
||||||
// Shank's algorithm for q mod 4 = 3
|
// Shank's algorithm for q mod 4 = 3
|
||||||
// https://eprint.iacr.org/2012/685.pdf (page 9, algorithm 2)
|
// https://eprint.iacr.org/2012/685.pdf (page 9, algorithm 2)
|
||||||
|
@ -832,17 +843,6 @@ impl SqrtField for Fq {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LegendreField for Fq {
|
|
||||||
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 }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_b_coeff() {
|
fn test_b_coeff() {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use rand::{Rng, Rand};
|
use rand::{Rng, Rand};
|
||||||
use ::{Field, LegendreField, SqrtField};
|
use ::{Field, SqrtField};
|
||||||
use super::fq::{Fq, FROBENIUS_COEFF_FQ2_C1, NEGATIVE_ONE};
|
use super::fq::{Fq, FROBENIUS_COEFF_FQ2_C1, NEGATIVE_ONE};
|
||||||
|
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
|
@ -156,6 +156,11 @@ impl Field for Fq2 {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SqrtField for Fq2 {
|
impl SqrtField for Fq2 {
|
||||||
|
|
||||||
|
fn legendre(&self) -> ::LegendreSymbol {
|
||||||
|
Fq2::norm(&self).legendre()
|
||||||
|
}
|
||||||
|
|
||||||
fn sqrt(&self) -> Option<Self> {
|
fn sqrt(&self) -> Option<Self> {
|
||||||
// Algorithm 9, https://eprint.iacr.org/2012/685.pdf
|
// Algorithm 9, https://eprint.iacr.org/2012/685.pdf
|
||||||
|
|
||||||
|
@ -196,12 +201,6 @@ impl SqrtField for Fq2 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LegendreField for Fq2 {
|
|
||||||
fn legendre(&self) -> ::LegendreSymbol {
|
|
||||||
Fq2::norm(&self).legendre()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_fq2_ordering() {
|
fn test_fq2_ordering() {
|
||||||
let mut a = Fq2 {
|
let mut a = Fq2 {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use ::{Field, LegendreField, PrimeField, SqrtField, PrimeFieldRepr, PrimeFieldDecodingError};
|
use ::{Field, PrimeField, SqrtField, PrimeFieldRepr, PrimeFieldDecodingError};
|
||||||
use ::LegendreSymbol::*;
|
use ::LegendreSymbol::*;
|
||||||
|
|
||||||
// r = 52435875175126190479447740508185965837690552500527637822603658699938581184513
|
// r = 52435875175126190479447740508185965837690552500527637822603658699938581184513
|
||||||
|
@ -552,6 +552,14 @@ impl Fr {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SqrtField for Fr {
|
impl SqrtField for Fr {
|
||||||
|
|
||||||
|
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 }
|
||||||
|
}
|
||||||
|
|
||||||
fn sqrt(&self) -> Option<Self> {
|
fn sqrt(&self) -> Option<Self> {
|
||||||
// Tonelli-Shank's algorithm for q mod 16 = 1
|
// Tonelli-Shank's algorithm for q mod 16 = 1
|
||||||
// https://eprint.iacr.org/2012/685.pdf (page 12, algorithm 5)
|
// https://eprint.iacr.org/2012/685.pdf (page 12, algorithm 5)
|
||||||
|
@ -595,15 +603,6 @@ impl SqrtField for Fr {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LegendreField for Fr {
|
|
||||||
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)]
|
#[cfg(test)]
|
||||||
use rand::{SeedableRng, XorShiftRng, Rand};
|
use rand::{SeedableRng, XorShiftRng, Rand};
|
||||||
|
|
||||||
|
|
10
src/lib.rs
10
src/lib.rs
|
@ -327,18 +327,14 @@ pub trait Field: Sized +
|
||||||
/// This trait represents an element of a field that has a square root operation described for it.
|
/// This trait represents an element of a field that has a square root operation described for it.
|
||||||
pub trait SqrtField: Field
|
pub trait SqrtField: Field
|
||||||
{
|
{
|
||||||
|
/// Returns the legendre symbol of the field element.
|
||||||
|
fn legendre(&self) -> LegendreSymbol;
|
||||||
|
|
||||||
/// Returns the square root of the field element, if it is
|
/// Returns the square root of the field element, if it is
|
||||||
/// quadratic residue.
|
/// quadratic residue.
|
||||||
fn sqrt(&self) -> Option<Self>;
|
fn sqrt(&self) -> Option<Self>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This trait represents an element of a field that has a Legendre symbol described for it.
|
|
||||||
pub trait LegendreField: Field
|
|
||||||
{
|
|
||||||
/// Returns the legendre symbol of the field element.
|
|
||||||
fn legendre(&self) -> LegendreSymbol;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// This trait represents a wrapper around a biginteger which can encode any element of a particular
|
/// This trait represents a wrapper around a biginteger which can encode any element of a particular
|
||||||
/// prime field. It is a smart wrapper around a sequence of `u64` limbs, least-significant digit
|
/// prime field. It is a smart wrapper around a sequence of `u64` limbs, least-significant digit
|
||||||
|
|
Loading…
Reference in New Issue