Browse Source

Polish sqrt in fr.rs: use pattern matching with Legendre enums.

master
Michele Orrù 7 years ago
parent
commit
2d3f498e75
  1. 14
      src/bls12_381/fr.rs

14
src/bls12_381/fr.rs

@ -555,14 +555,10 @@ impl SqrtField for Fr {
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)
match self.legendre() {
if self.is_zero() { Zero => Some(*self),
return Some(*self); QNonResidue => None,
} QResidue => {
if let QNonResidue = self.legendre() {
None
} else {
let mut c = Fr(ROOT_OF_UNITY); let mut c = Fr(ROOT_OF_UNITY);
// r = self^((t + 1) // 2) // r = self^((t + 1) // 2)
let mut r = self.pow([0x7fff2dff80000000, 0x4d0ec02a9ded201, 0x94cebea4199cec04, 0x39f6d3a9]); let mut r = self.pow([0x7fff2dff80000000, 0x4d0ec02a9ded201, 0x94cebea4199cec04, 0x39f6d3a9]);
@ -596,6 +592,7 @@ impl SqrtField for Fr {
Some(r) Some(r)
} }
} }
}
} }
impl LegendreField for Fr { impl LegendreField for Fr {
@ -606,6 +603,7 @@ impl LegendreField for Fr {
else { QNonResidue } else { QNonResidue }
} }
} }
#[cfg(test)] #[cfg(test)]
use rand::{SeedableRng, XorShiftRng, Rand}; use rand::{SeedableRng, XorShiftRng, Rand};

Loading…
Cancel
Save