Optimizations to field and curve arithmetic

This commit is contained in:
Pratyush Mishra
2022-09-12 22:03:00 -07:00
parent 363426c1d4
commit 68f500da01
13 changed files with 82 additions and 39 deletions

View File

@@ -21,7 +21,22 @@ impl Fp2Config for Fq2Config {
];
#[inline(always)]
fn mul_fp_by_nonresidue(fp: &Self::Fp) -> Self::Fp {
-(*fp)
fn mul_fp_by_nonresidue_in_place(fp: &mut Self::Fp) -> &mut Self::Fp {
fp.neg_in_place()
}
#[inline(always)]
fn sub_and_mul_fp_by_nonresidue(y: &mut Self::Fp, x: &Self::Fp) {
*y += x;
}
#[inline(always)]
fn add_and_mul_fp_by_nonresidue_plus_one(y: &mut Self::Fp, x: &Self::Fp) {
*y = *x;
}
fn add_and_mul_fp_by_nonresidue_in_place(y: &mut Self::Fp, x: &Self::Fp) {
y.neg_in_place();
*y += x;
}
}

View File

@@ -82,11 +82,10 @@ impl Fp6Config for Fq6Config {
/// Multiply this element by the quadratic nonresidue 1 + u.
/// Make this generic.
fn mul_fp2_by_nonresidue(fe: &Fq2) -> Fq2 {
let mut copy = *fe;
let t0 = copy.c0;
copy.c0 -= &fe.c1;
copy.c1 += &t0;
copy
fn mul_fp2_by_nonresidue_in_place(fe: &mut Fq2) -> &mut Fq2 {
let t0 = fe.c0;
fe.c0 -= &fe.c1;
fe.c1 += &t0;
fe
}
}

View File

@@ -1718,7 +1718,7 @@ fn test_fq2_legendre() {
// i^2 = -1
let mut m1 = -Fq2::one();
assert_eq!(QuadraticResidue, m1.legendre());
m1 = Fq6Config::mul_fp2_by_nonresidue(&m1);
Fq6Config::mul_fp2_by_nonresidue_in_place(&mut m1);
assert_eq!(QuadraticNonResidue, m1.legendre());
}
@@ -1731,7 +1731,7 @@ fn test_fq2_mul_nonresidue() {
for _ in 0..1000 {
let mut a = Fq2::rand(&mut rng);
let mut b = a;
a = Fq6Config::mul_fp2_by_nonresidue(&a);
Fq6Config::mul_fp2_by_nonresidue_in_place(&mut a);
b.mul_assign(&nqr);
assert_eq!(a, b);
@@ -1747,7 +1747,7 @@ fn test_fq6_mul_nonresidue() {
for _ in 0..1000 {
let mut a = Fq6::rand(&mut rng);
let mut b = a;
a = Fq12Config::mul_fp6_by_nonresidue(&a);
Fq12Config::mul_fp6_by_nonresidue_in_place(&mut a);
b.mul_assign(&nqr);
assert_eq!(a, b);