From 6feb0f802f1d8e1e8b092ac1d734b7b4d2294208 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michele=20Orr=C3=B9?= Date: Thu, 10 Aug 2017 18:28:17 +0200 Subject: [PATCH] Merge traits SqrtField and LegendreField into SqrtField. --- src/bls12_381/fq.rs | 24 ++++++++++++------------ src/bls12_381/fq2.rs | 13 ++++++------- src/bls12_381/fr.rs | 19 +++++++++---------- src/lib.rs | 10 +++------- 4 files changed, 30 insertions(+), 36 deletions(-) diff --git a/src/bls12_381/fq.rs b/src/bls12_381/fq.rs index d2efa0d..5f92b1d 100644 --- a/src/bls12_381/fq.rs +++ b/src/bls12_381/fq.rs @@ -1,4 +1,4 @@ -use ::{Field, LegendreField, PrimeField, SqrtField, PrimeFieldRepr, PrimeFieldDecodingError}; +use ::{Field, PrimeField, SqrtField, PrimeFieldRepr, PrimeFieldDecodingError}; use std::cmp::Ordering; use super::fq2::Fq2; @@ -810,6 +810,17 @@ impl 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 { // Shank's algorithm for q mod 4 = 3 // 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] fn test_b_coeff() { diff --git a/src/bls12_381/fq2.rs b/src/bls12_381/fq2.rs index 0c9218c..fb385d7 100644 --- a/src/bls12_381/fq2.rs +++ b/src/bls12_381/fq2.rs @@ -1,5 +1,5 @@ use rand::{Rng, Rand}; -use ::{Field, LegendreField, SqrtField}; +use ::{Field, SqrtField}; use super::fq::{Fq, FROBENIUS_COEFF_FQ2_C1, NEGATIVE_ONE}; use std::cmp::Ordering; @@ -156,6 +156,11 @@ impl Field for Fq2 { } impl SqrtField for Fq2 { + + fn legendre(&self) -> ::LegendreSymbol { + Fq2::norm(&self).legendre() + } + fn sqrt(&self) -> Option { // 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] fn test_fq2_ordering() { let mut a = Fq2 { diff --git a/src/bls12_381/fr.rs b/src/bls12_381/fr.rs index f914942..7d98625 100644 --- a/src/bls12_381/fr.rs +++ b/src/bls12_381/fr.rs @@ -1,4 +1,4 @@ -use ::{Field, LegendreField, PrimeField, SqrtField, PrimeFieldRepr, PrimeFieldDecodingError}; +use ::{Field, PrimeField, SqrtField, PrimeFieldRepr, PrimeFieldDecodingError}; use ::LegendreSymbol::*; // r = 52435875175126190479447740508185965837690552500527637822603658699938581184513 @@ -552,6 +552,14 @@ impl 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 { // Tonelli-Shank's algorithm for q mod 16 = 1 // 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)] use rand::{SeedableRng, XorShiftRng, Rand}; diff --git a/src/lib.rs b/src/lib.rs index 0ba79aa..a0fc2b5 100644 --- a/src/lib.rs +++ b/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. 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 /// quadratic residue. fn sqrt(&self) -> Option; } -/// 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 /// prime field. It is a smart wrapper around a sequence of `u64` limbs, least-significant digit