4 Commits

Author SHA1 Message Date
Dev Ojha
53dd4de35b Merge branch 'master' into fq2_neg_nonresidue 2021-02-06 00:39:52 -06:00
ValarDragon
cd60d33bcb new add + mul by residue + 1 2021-02-05 20:13:02 -06:00
ValarDragon
64ece6414f Add optimization for bls12_381 and bn254 2021-02-05 12:48:57 -06:00
ValarDragon
bb033e9949 Use negative non-residue optimization 2021-02-05 11:53:30 -06:00
3 changed files with 54 additions and 0 deletions

View File

@@ -25,6 +25,7 @@ impl Fp2Parameters for Fq2Parameters {
field_new!(Fq, "-1"),
];
// Mul by -5
#[inline(always)]
fn mul_fp_by_nonresidue(fe: &Self::Fp) -> Self::Fp {
let original = fe;
@@ -32,6 +33,35 @@ impl Fp2Parameters for Fq2Parameters {
fe.double_in_place();
fe - original
}
// x + -5 * y, computed as x - 5*y
#[inline(always)]
fn add_and_mul_fp_by_nonresidue(x: &Self::Fp, y: &Self::Fp) -> Self::Fp {
// c becomes 5 * y
let mut c = y.double();
c.double_in_place();
c += y;
*x - c
}
// x + y + (-5 * y), computed as x - 4*y
#[inline(always)]
fn add_and_mul_fp_by_nonresidue_plus_one(x: &Self::Fp, y: &Self::Fp) -> Self::Fp {
// c becomes 4 * y
let mut c = y.double();
c.double_in_place();
*x - c
}
// x - (-5 * y), computed as x + 5*y
#[inline(always)]
fn sub_and_mul_fp_by_nonresidue(x: &Self::Fp, y: &Self::Fp) -> Self::Fp {
// c becomes 5 * y
let mut c = y.double();
c.double_in_place();
c += y;
*x + c
}
}
pub const FQ2_ZERO: Fq2 = field_new!(Fq2, FQ_ZERO, FQ_ZERO);

View File

@@ -29,6 +29,18 @@ impl Fp2Parameters for Fq2Parameters {
fn mul_fp_by_nonresidue(fp: &Self::Fp) -> Self::Fp {
-(*fp)
}
// x + -1 * y, computed as x - y
#[inline(always)]
fn add_and_mul_fp_by_nonresidue(x: &Self::Fp, y: &Self::Fp) -> Self::Fp {
*x - y
}
// x - (-1 * y), computed as x + y
#[inline(always)]
fn sub_and_mul_fp_by_nonresidue(x: &Self::Fp, y: &Self::Fp) -> Self::Fp {
*x + y
}
}
pub const FQ2_ZERO: Fq2 = field_new!(Fq2, FQ_ZERO, FQ_ZERO);

View File

@@ -32,6 +32,18 @@ impl Fp2Parameters for Fq2Parameters {
fn mul_fp_by_nonresidue(fe: &Self::Fp) -> Self::Fp {
-(*fe)
}
// x + -1 * y, computed as x - y
#[inline(always)]
fn add_and_mul_fp_by_nonresidue(x: &Self::Fp, y: &Self::Fp) -> Self::Fp {
*x - y
}
// x - (-1 * y), computed as x + y
#[inline(always)]
fn sub_and_mul_fp_by_nonresidue(x: &Self::Fp, y: &Self::Fp) -> Self::Fp {
*x + y
}
}
pub const FQ2_ZERO: Fq2 = field_new!(Fq2, FQ_ZERO, FQ_ZERO);