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 for QuadExtField #358

Merged
merged 6 commits into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
### Bugfixes

- [\#350](https://github.com/arkworks-rs/algebra/pull/350) (ark-serialize) Fix issues with santiation whenever a non-standard `Result` type is in scope.
- [\#358](https://github.com/arkworks-rs/algebra/pull/358) (ark-ff) Fix the bug for `QuadExtField::sqrt` when `c1 = 0 && c0.legendre.is_qnr()`

## v0.3.0

Expand Down
15 changes: 14 additions & 1 deletion ff/src/fields/models/quadratic_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,20 @@ where
// Square root based on the complex method. See
// https://eprint.iacr.org/2012/685.pdf (page 15, algorithm 8)
if self.c1.is_zero() {
return self.c0.sqrt().map(|c0| Self::new(c0, P::BaseField::zero()));
// for c = c0 + c1 * x, we have c1 = 0
// sqrt(c) == sqrt(c0) is an element of Fp2, i.e. sqrt(c0) = a = a0 + a1 * x for some a0, a1 in Fp
// squaring both sides: c0 = a0^2 + a1^2 * x^2 + (2 * a0 * a1 * x) = a0^2 + (a1^2 * P::NONRESIDUE)
// since there are no `x` terms on LHS, a0 * a1 = 0
// so either a0 = sqrt(c0) or a1 = sqrt(c0/P::NONRESIDUE)
if self.c0.legendre().is_qr() {
// either c0 is a valid sqrt in the base field
return self.c0.sqrt().map(|c0| Self::new(c0, P::BaseField::zero()));
} else {
// or we need to compute sqrt(c0/P::NONRESIDUE)
return (self.c0.div(P::NONRESIDUE))
.sqrt()
.map(|res| Self::new(P::BaseField::zero(), res));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if there's a way to precompute P::NONRESIDUE.inverse() here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One simple solution is to add NONRESIDUE_INV into the trait of the field parameters.

}
}
// Try computing the square root
// Check at the end of the algorithm if it was a square root
Expand Down