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

Deferred Montgomery reductions #20

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Defer Montgomery reductions during Fp2 multiplication.
  • Loading branch information
ebfull committed Nov 22, 2019
commit cff96bf5ce07b89100f0ca4fb3bff33379df2f31
20 changes: 12 additions & 8 deletions src/fp2.rs
Original file line number Diff line number Diff line change
@@ -209,14 +209,18 @@ impl Fp2 {
// c0 = v0 + v1
// c1 = (a0 + a1) * (b0 + b1) - v0 + v1

let v0 = (&self.c0).mul(&rhs.c0);
let v1 = (&(&self.c1).neg()).mul(&rhs.c1);
let c0 = (&v0).add(&v1);
let c1 = (&(&self.c0).add(&self.c1)).mul(&(&rhs.c0).add(&rhs.c1));
let c1 = (&c1).sub(&v0);
let c1 = (&c1).add(&v1);

Fp2 { c0, c1 }
let v0 = (&self.c0).mul_unreduced(&rhs.c0); // v0 has magnitude 1
let v1 = (&(&self.c1).neg()).mul_unreduced(&rhs.c1); // v1 has magnitude 1
let c0 = v0.add(&v1); // c0 has magnitude 2
let c1 = (&(&self.c0).add(&self.c1)).mul_unreduced(&(&rhs.c0).add(&rhs.c1)); // c1 has magnitude 1
let c1 = c1.add(&v1); // c1 has magnitude 2
let v0 = v0.negate(); // v0 has magnitude 2 (it could have been zero)
let c1 = c1.add(&v0); // c1 has magnitude 4

Fp2 {
c0: Fp::montgomery_reduce(c0),
c1: Fp::montgomery_reduce(c1),
}
}

pub const fn add(&self, rhs: &Fp2) -> Fp2 {