mirror of
https://github.com/Qortal/pirate-librustzcash.git
synced 2025-11-01 20:07:02 +00:00
Polish sqrt in fr.rs: use pattern matching with Legendre enums.
This commit is contained in:
@@ -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};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user