Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sqrt computations when input is *not* a sqrt #142

Merged
merged 2 commits into from
Dec 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions ff/src/fields/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,14 @@ macro_rules! sqrt_impl {
k += 1;
}

let j = v - k - 1;
if k == ($P::TWO_ADICITY as usize) {
// We are in the case where self^(T * 2^k) = x^(P::MODULUS - 1) = 1,
// which means that no square root exists.
return None;
}
let j = v - k;
w = z;
for _ in 0..j {
for _ in 1..j {
w.square_in_place();
}

Expand All @@ -265,17 +270,18 @@ macro_rules! sqrt_impl {
// Is x the square root? If so, return it.
if (x.square() == *$self) {
return Some(x);
}
// Consistency check that if no square root is found,
// it is because none exists.
#[cfg(debug_assertions)]
{
use crate::fields::LegendreSymbol::*;
if ($self.legendre() != QuadraticNonResidue) {
panic!("Input has a square root per its legendre symbol, but it was not found")
} else {
// Consistency check that if no square root is found,
// it is because none exists.
#[cfg(debug_assertions)]
{
use crate::fields::LegendreSymbol::*;
if ($self.legendre() != QuadraticNonResidue) {
panic!("Input has a square root per its legendre symbol, but it was not found")
}
}
None
}
None
}};
}

Expand Down
46 changes: 24 additions & 22 deletions ff/src/fields/models/quadratic_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,29 +289,31 @@ where
.double()
.inverse()
.expect("Two should always have an inverse");
let alpha = alpha
.sqrt()
.expect("We are in the QR case, the norm should have a square root");
let mut delta = (alpha + &self.c0) * &two_inv;
if delta.legendre().is_qnr() {
delta -= α
}
let c0 = delta.sqrt().expect("Delta must have a square root");
let c0_inv = c0.inverse().expect("c0 must have an inverse");
let sqrt_cand = Self::new(c0, self.c1 * &two_inv * &c0_inv);
// Check if sqrt_cand is actually the square root
// if not, there exists no square root.
if sqrt_cand.square() == *self {
return Some(sqrt_cand);
}
#[cfg(debug_assertions)]
{
use crate::fields::LegendreSymbol::*;
if self.legendre() != QuadraticNonResidue {
panic!("Input has a square root per its legendre symbol, but it was not found")
alpha.sqrt().and_then(|alpha| {
let mut delta = (alpha + &self.c0) * &two_inv;
if delta.legendre().is_qnr() {
delta -= α
}
}
None
let c0 = delta.sqrt().expect("Delta must have a square root");
let c0_inv = c0.inverse().expect("c0 must have an inverse");
let sqrt_cand = Self::new(c0, self.c1 * &two_inv * &c0_inv);
// Check if sqrt_cand is actually the square root
// if not, there exists no square root.
if sqrt_cand.square() == *self {
Some(sqrt_cand)
} else {
#[cfg(debug_assertions)]
{
use crate::fields::LegendreSymbol::*;
if self.legendre() != QuadraticNonResidue {
panic!(
"Input has a square root per its legendre symbol, but it was not found"
)
}
}
None
}
})
}

fn sqrt_in_place(&mut self) -> Option<&mut Self> {
Expand Down
5 changes: 4 additions & 1 deletion poly-benches/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ harness = false
[[bench]]
name = "dense_polynomial"
path = "benches/dense_polynomial.rs"
harness = false
harness = false

[lib]
bench = false