mirror of
https://github.com/arnaucube/ark-curves-cherry-picked.git
synced 2026-01-08 23:11:29 +01:00
Optimizations to field and curve arithmetic
This commit is contained in:
@@ -21,10 +21,34 @@ impl Fp2Config for Fq2Config {
|
||||
];
|
||||
|
||||
#[inline(always)]
|
||||
fn mul_fp_by_nonresidue(fe: &Self::Fp) -> Self::Fp {
|
||||
let original = fe;
|
||||
let mut fe = -fe.double();
|
||||
fn mul_fp_by_nonresidue_in_place(fe: &mut Self::Fp) -> &mut Self::Fp {
|
||||
let original = *fe;
|
||||
fe.neg_in_place();
|
||||
fe.double_in_place();
|
||||
fe - original
|
||||
fe.double_in_place();
|
||||
*fe -= original;
|
||||
fe
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn sub_and_mul_fp_by_nonresidue(y: &mut Self::Fp, x: &Self::Fp) {
|
||||
let mut original = *y;
|
||||
original += x;
|
||||
y.double_in_place().double_in_place();
|
||||
*y += original;
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn add_and_mul_fp_by_nonresidue_plus_one(y: &mut Self::Fp, x: &Self::Fp) {
|
||||
y.double_in_place().double_in_place().neg_in_place();
|
||||
*y += x;
|
||||
}
|
||||
|
||||
fn add_and_mul_fp_by_nonresidue_in_place(y: &mut Self::Fp, x: &Self::Fp) {
|
||||
let mut original = *y;
|
||||
original.double_in_place().double_in_place();
|
||||
original += &*y;
|
||||
*y = *x;
|
||||
*y -= original;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,10 +68,12 @@ impl Fp6Config for Fq6Config {
|
||||
];
|
||||
|
||||
#[inline(always)]
|
||||
fn mul_fp2_by_nonresidue(fe: &Fq2) -> Fq2 {
|
||||
fn mul_fp2_by_nonresidue_in_place(fe: &mut Fq2) -> &mut Fq2 {
|
||||
// Karatsuba multiplication with constant other = u.
|
||||
let c0 = Fq2Config::mul_fp_by_nonresidue(&fe.c1);
|
||||
let c1 = fe.c0;
|
||||
Fq2::new(c0, c1)
|
||||
let old_c0 = fe.c0;
|
||||
fe.c0 = fe.c1;
|
||||
Fq2Config::mul_fp_by_nonresidue_in_place(&mut fe.c0);
|
||||
fe.c1 = old_c0;
|
||||
fe
|
||||
}
|
||||
}
|
||||
|
||||
@@ -136,7 +136,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());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user