Skip to content

Commit

Permalink
Merge branch 'master' into quadfield_autoconst
Browse files Browse the repository at this point in the history
  • Loading branch information
jon-chuang authored Feb 7, 2021
2 parents 4f711ab + a3a1ab2 commit c35c023
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
49 changes: 49 additions & 0 deletions ff/src/fields/models/fp2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,31 @@ pub trait Fp2Parameters: 'static + Send + Sync {
fn mul_fp_by_nonresidue(fe: &Self::Fp) -> Self::Fp {
Self::NONRESIDUE * fe
}

/// A specializable method for computing `x + mul_base_field_by_nonresidue(y)`
/// This allows for optimizations when the non-residue is
/// canonically negative in the field.
#[inline(always)]
fn add_and_mul_fp_by_nonresidue(x: &Self::Fp, y: &Self::Fp) -> Self::Fp {
*x + Self::mul_fp_by_nonresidue(y)
}

/// A specializable method for computing `x + y + mul_base_field_by_nonresidue(y)`
/// This allows for optimizations when the non-residue is not `-1`.
#[inline(always)]
fn add_and_mul_fp_by_nonresidue_plus_one(x: &Self::Fp, y: &Self::Fp) -> Self::Fp {
let mut tmp = *x;
tmp += y;
Self::add_and_mul_fp_by_nonresidue(&tmp, &y)
}

/// A specializable method for computing `x - mul_base_field_by_nonresidue(y)`
/// This allows for optimizations when the non-residue is
/// canonically negative in the field.
#[inline(always)]
fn sub_and_mul_fp_by_nonresidue(x: &Self::Fp, y: &Self::Fp) -> Self::Fp {
*x - Self::mul_fp_by_nonresidue(y)
}
}

pub struct Fp2ParamsWrapper<P: Fp2Parameters>(PhantomData<P>);
Expand All @@ -37,6 +62,30 @@ impl<P: Fp2Parameters> QuadExtParameters for Fp2ParamsWrapper<P> {

const FROBENIUS_COEFF_C1: &'static [Self::FrobCoeff] = P::FROBENIUS_COEFF_FP2_C1;

#[inline(always)]
fn add_and_mul_base_field_by_nonresidue(
x: &Self::BaseField,
y: &Self::BaseField,
) -> Self::BaseField {
P::add_and_mul_fp_by_nonresidue(x, y)
}

#[inline(always)]
fn add_and_mul_base_field_by_nonresidue_plus_one(
x: &Self::BaseField,
y: &Self::BaseField,
) -> Self::BaseField {
P::add_and_mul_fp_by_nonresidue_plus_one(x, y)
}

#[inline(always)]
fn sub_and_mul_base_field_by_nonresidue(
x: &Self::BaseField,
y: &Self::BaseField,
) -> Self::BaseField {
P::sub_and_mul_fp_by_nonresidue(x, y)
}

fn mul_base_field_by_frob_coeff(fe: &mut Self::BaseField, power: usize) {
*fe *= &Self::FROBENIUS_COEFF_C1[power % Self::DEGREE_OVER_BASE_PRIME_FIELD];
}
Expand Down
2 changes: 1 addition & 1 deletion ff/src/fields/models/quadratic_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub trait QuadExtParameters: 'static + Send + Sync + Sized {
fn mul_base_field_by_nonresidue(fe: &Self::BaseField) -> Self::BaseField {
Self::NONRESIDUE * fe
}

#[inline(always)]
#[ark_ff_asm::unroll_for_loops]
fn op_and_mul_base_field_by_nonresidue(
Expand Down

0 comments on commit c35c023

Please sign in to comment.