Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
trbritt committed Aug 19, 2024
1 parent b119f3c commit a838a46
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 77 deletions.
19 changes: 11 additions & 8 deletions src/fields/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ impl<const D: usize, const N: usize, F: FieldExtensionTrait<D, N>> ConstantTimeE
}
}
impl<'a, 'b, const D: usize, const N: usize, F: FieldExtensionTrait<D, N>>
Add<&'b FieldExtension<D, N, F>> for &'a FieldExtension<D, N, F>
Add<&'b FieldExtension<D, N, F>> for &'a FieldExtension<D, N, F>
{
type Output = FieldExtension<D, N, F>;
fn add(self, other: &'b FieldExtension<D, N, F>) -> Self::Output {

fn add(self, other: &'b FieldExtension<D, N, F>) -> Self::Output {
let mut i = 0;
let mut retval = [F::zero(); N];
while i < N {
Expand All @@ -78,7 +78,8 @@ Add<&'b FieldExtension<D, N, F>> for &'a FieldExtension<D, N, F>
}
}
impl<const D: usize, const N: usize, F: FieldExtensionTrait<D, N>> Add<FieldExtension<D, N, F>>
for FieldExtension<D, N, F> {
for FieldExtension<D, N, F>
{
type Output = Self;
fn add(self, other: FieldExtension<D, N, F>) -> Self::Output {
&self + &other
Expand All @@ -92,8 +93,9 @@ impl<const D: usize, const N: usize, F: FieldExtensionTrait<D, N>> AddAssign
}
}
impl<'a, 'b, const D: usize, const N: usize, F: FieldExtensionTrait<D, N>>
Sub<&'b FieldExtension<D, N, F>> for &'a FieldExtension<D, N, F>{
type Output = FieldExtension<D,N,F>;
Sub<&'b FieldExtension<D, N, F>> for &'a FieldExtension<D, N, F>
{
type Output = FieldExtension<D, N, F>;

fn sub(self, other: &'b FieldExtension<D, N, F>) -> Self::Output {
let mut i = 0;
Expand All @@ -106,10 +108,11 @@ Sub<&'b FieldExtension<D, N, F>> for &'a FieldExtension<D, N, F>{
}
}
impl<const D: usize, const N: usize, F: FieldExtensionTrait<D, N>> Sub<FieldExtension<D, N, F>>
for FieldExtension<D, N, F> {
for FieldExtension<D, N, F>
{
type Output = Self;
fn sub(self, other: FieldExtension<D, N, F>) -> Self::Output {
&self - &other
&self - &other
}
}
impl<const D: usize, const N: usize, F: FieldExtensionTrait<D, N>> SubAssign
Expand Down
15 changes: 4 additions & 11 deletions src/fields/fp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ impl Fp {
// will require the frobenius transformation
match exponent {
1 => self.pow(BN254_FP_MODULUS.value()),
_ => *self
_ => *self,
}
}
pub fn sqrt(&self) -> CtOption<Self> {
Expand All @@ -415,13 +415,9 @@ impl Fp {
// prime that is congruent to 3 mod 4. In this case, the sqrt only has the
// possible solution of $\pm pow(n, \frac{p+1}{4})$, which is where this magic
// number below comes from ;)
let arg =
((Self::new(Self::characteristic()) + Self::one()) / Self::from(4)).value();
let arg = ((Self::new(Self::characteristic()) + Self::one()) / Self::from(4)).value();
let sqrt = self.pow(arg);
CtOption::new(
sqrt,
sqrt.square().ct_eq(self),
)
CtOption::new(sqrt, sqrt.square().ct_eq(self))
}
pub fn square(&self) -> Self {
(*self) * (*self)
Expand Down Expand Up @@ -919,10 +915,7 @@ mod tests {
for _ in 0..100 {
let a = <Fp as FieldExtensionTrait<1, 1>>::rand(&mut OsRng);
let b = a.square();
assert!(
bool::from(b.is_square()),
"Is square failed"
);
assert!(bool::from(b.is_square()), "Is square failed");
}
}
#[test]
Expand Down
11 changes: 5 additions & 6 deletions src/fields/fp12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ impl<'a, 'b> Mul<&'b Fp12> for &'a Fp12 {
])
}
}
impl Mul for Fp12{
impl Mul for Fp12 {
type Output = Self;
fn mul(self, other: Self) -> Self::Output {
(&self).mul(&other)
Expand All @@ -221,9 +221,7 @@ impl MulAssign for Fp12 {
impl Inv for Fp12 {
type Output = Self;
fn inv(self) -> Self::Output {
let tmp = (self.0[0].square()
- (self.0[1].square().residue_mul()))
.inv();
let tmp = (self.0[0].square() - (self.0[1].square().residue_mul())).inv();
Self([self.0[0] * tmp, -(self.0[1] * tmp)])
}
}
Expand Down Expand Up @@ -367,12 +365,13 @@ impl Fp12 {
pub fn frobenius(&self, exponent: usize) -> Self {
Self::new(&[
self.0[0].frobenius(exponent),
self.0[1].frobenius(exponent)
self.0[1]
.frobenius(exponent)
.scale(FROBENIUS_COEFF_FP12_C1[exponent % 12]),
])
}
pub fn square(&self) -> Self {
// For F_{p^{12}} = F_{p^6}(w)/(w^2-\gamma), and A=a_0 + a_1*w \in F_{p^{12}},
// For F_{p^{12}} = F_{p^6}(w)/(w^2-\gamma), and A=a_0 + a_1*w \in F_{p^{12}},
// we determine C=c_0+c_1*w = A^2\in F_{p^{12}}
// Alg 22 from <https://eprint.iacr.org/2010/354.pdf>
let c0 = self.0[0] - self.0[1];
Expand Down
32 changes: 8 additions & 24 deletions src/fields/fp2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,11 @@ impl Fp2 {
if alpha == -Fp2::one() {
let i = Fp2::new(&[Fp::ZERO, Fp::ONE]);
let sqrt = i * a1 * (*self);
CtOption::new(
sqrt,
sqrt.square().ct_eq(self),
)
CtOption::new(sqrt, sqrt.square().ct_eq(self))
} else {
let b = (alpha + Fp2::one()).pow(&P_MINUS_1_OVER_2);
let sqrt = b * a1 * (*self);
CtOption::new(
sqrt,
sqrt.square().ct_eq(self),
)
CtOption::new(sqrt, sqrt.square().ct_eq(self))
}
}
pub fn square(&self) -> Self {
Expand All @@ -131,8 +125,7 @@ impl Fp2 {
}
};
let sum = self.0[0].square()
+ <Fp as FieldExtensionTrait<1, 1>>::quadratic_non_residue()
* (-self.0[0]).square();
+ <Fp as FieldExtensionTrait<1, 1>>::quadratic_non_residue() * (-self.0[0]).square();
Choice::from((legendre(&sum) != -1) as u8)
}
pub fn sgn0(&self) -> Choice {
Expand All @@ -158,8 +151,7 @@ impl FieldExtensionTrait<2, 2> for Fp2 {
FP2_TWIST_CURVE_CONSTANT
}
}
impl<'a, 'b> Mul<&'b Fp2> for &'a Fp2
{
impl<'a, 'b> Mul<&'b Fp2> for &'a Fp2 {
type Output = Fp2;
fn mul(self, other: &'b Fp2) -> Self::Output {
// This requires a bit more consideration. In Fp2,
Expand All @@ -176,11 +168,10 @@ impl<'a, 'b> Mul<&'b Fp2> for &'a Fp2
])
}
}
impl Mul<Fp2> for Fp2
{
impl Mul<Fp2> for Fp2 {
type Output = Self;
fn mul(self, other: Fp2) -> Self::Output {
// TODO linter complains about this being a needless reference if I do &a * &b, so this
// TODO linter complains about this being a needless reference if I do &a * &b, so this
// gets around it
(&self).mul(&other)
}
Expand Down Expand Up @@ -416,11 +407,7 @@ mod tests {
],
);
for i in [a, b, c] {
assert_eq!(
i.square(),
i * i,
"Squaring failed"
);
assert_eq!(i.square(), i * i, "Squaring failed");
}
}
#[test]
Expand Down Expand Up @@ -512,10 +499,7 @@ mod tests {
for _ in 0..100 {
let a = <Fp2 as FieldExtensionTrait<2, 2>>::rand(&mut OsRng);
let b = a.square();
assert!(
bool::from(b.is_square()),
"Is square failed"
);
assert!(bool::from(b.is_square()), "Is square failed");
}
}
}
Expand Down
24 changes: 10 additions & 14 deletions src/fields/fp6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,8 @@ impl Fp6 {
pub fn frobenius(&self, exponent: usize) -> Self {
Self::new(&[
self.0[0].frobenius(exponent),
self.0[1].frobenius(exponent)
* FROBENIUS_COEFF_FP6_C1[exponent % 6],
self.0[2].frobenius(exponent)
* FROBENIUS_COEFF_FP6_C2[exponent % 6],
self.0[1].frobenius(exponent) * FROBENIUS_COEFF_FP6_C1[exponent % 6],
self.0[2].frobenius(exponent) * FROBENIUS_COEFF_FP6_C2[exponent % 6],
])
}

Expand Down Expand Up @@ -260,10 +258,8 @@ impl MulAssign for Fp6 {
impl Inv for Fp6 {
type Output = Self;
fn inv(self) -> Self::Output {
let t0 = self.0[0].square()
- self.0[1] * self.0[2].residue_mul();
let t1 = self.0[2].square().residue_mul()
- self.0[0] * self.0[1];
let t0 = self.0[0].square() - self.0[1] * self.0[2].residue_mul();
let t1 = self.0[2].square().residue_mul() - self.0[0] * self.0[1];
let t2 = self.0[1].square() - self.0[0] * self.0[2];

let inverse = ((self.0[2] * t1 + self.0[1] * t2).residue_mul() + self.0[0] * t0).inv();
Expand Down Expand Up @@ -570,12 +566,12 @@ mod tests {
);
assert_eq!(
a,
a.frobenius(1).
frobenius(1).
frobenius(1).
frobenius(1).
frobenius(1).
frobenius(1),
a.frobenius(1)
.frobenius(1)
.frobenius(1)
.frobenius(1)
.frobenius(1)
.frobenius(1),
"Frobenius failed at cycle order 6"
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/fields/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ pub fn to_larger_uint<const N: usize, const M: usize>(smaller_bytes: &[u8; N]) -
// Specific conversion functions
pub fn u256_to_u512(u256: &U256) -> U512 {
U512::from_be_bytes(to_larger_uint::<32, 64>(&u256.to_be_bytes()))
}
}
2 changes: 1 addition & 1 deletion src/groups/g2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crypto_bigint::U256;
use num_traits::{One, Zero};
use subtle::{Choice, ConstantTimeEq};

/// This is the X coordinate of the generator for the r-torsion of the twist curve, generated
/// This is the X coordinate of the generator for the r-torsion of the twist curve, generated
/// directly from sage
const G2_X: Fp2 = Fp2::new(&[
Fp::new(U256::from_words([
Expand Down
6 changes: 4 additions & 2 deletions src/groups/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,10 @@ mod tests {
fn test_svdw() {
load_g1_reference_data!(g1_points);

if let Ok(d) = SvdW::precompute_constants(Fp::ZERO, <Fp as
FieldExtensionTrait<1,1>>::curve_constant()) {
if let Ok(d) = SvdW::precompute_constants(
Fp::ZERO,
<Fp as FieldExtensionTrait<1, 1>>::curve_constant(),
) {
for s in g1_points.svdw.iter() {
let r = s.i;
let p = s.p;
Expand Down
8 changes: 4 additions & 4 deletions src/pairing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,9 @@ impl MillerLoopResult {
/// time. Again, because of the sparse nature of the returned Fp12 from the doubling and addition
/// steps, we store only the 3 non-zero coefficients in an arr of EllCoeffs
///
/// There's two components to this struct. First, is the original value at which we are
/// computing the line. Second, is an array of the coefficients we determine by the generation
/// of the line, stored as an array. But an array of 87 elements is very specific, no?
/// There's two components to this struct. First, is the original value at which we are
/// computing the line. Second, is an array of the coefficients we determine by the generation
/// of the line, stored as an array. But an array of 87 elements is very specific, no?
/// There's 64 total iterations through the NAF representation, each one incurring a
/// doubling step. Further, there are 9 `1` digits (each with an addition step), and 12 `3`
/// digits, each also with an addition step. After the loop, there are 2 more addition steps, so
Expand Down Expand Up @@ -334,7 +334,7 @@ impl G2Projective {
let g = (b + f).scale(TWO_INV);
let h = (self.y + self.z).square() - (b + c);
let i = e - b;
let j =self.x.square();
let j = self.x.square();
let e_sq = e.square();

self.x = a * (b - f);
Expand Down
13 changes: 7 additions & 6 deletions src/svdw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
//! ----------
//! 1. <https://link.springer.com/chapter/10.1007/11792086_36>

use num_traits::{Inv, Zero};
use crate::fields::fp::Fp;
use num_traits::{Inv, Zero};
use subtle::Choice;

#[derive(Debug)]
Expand All @@ -39,8 +39,7 @@ pub trait SvdWTrait: Sized {

fn find_z_svdw(a: Fp, b: Fp) -> Fp {
let g = |x: &Fp| -> Fp { (*x) * (*x) * (*x) + a * (*x) + b };
let h = |x: &Fp| -> Fp { -(Fp::THREE * (*x) * (*x) + Fp::FOUR * a) / (Fp::FOUR * g(x)
) };
let h = |x: &Fp| -> Fp { -(Fp::THREE * (*x) * (*x) + Fp::FOUR * a) / (Fp::FOUR * g(x)) };
let mut ctr = 1;
loop {
for z_cand in [Fp::from(ctr), -Fp::from(ctr)] {
Expand Down Expand Up @@ -96,8 +95,7 @@ pub trait SvdWTrait: Sized {
}
fn unchecked_map_to_point(&self, u: Fp) -> Result<[Fp; 2], MapError>;
}
impl SvdWTrait for SvdW
{
impl SvdWTrait for SvdW {
fn unchecked_map_to_point(&self, u: Fp) -> Result<[Fp; 2], MapError> {
// Implements the SvdW algorithm for a single scalar point
let cmov = |x: &Fp, y: &Fp, b: &Choice| -> Fp {
Expand Down Expand Up @@ -158,7 +156,10 @@ mod tests {

#[test]
fn test_z_svdw() {
let z = SvdW::find_z_svdw(Fp::ZERO, <Fp as FieldExtensionTrait<1,1>>::curve_constant());
let z = SvdW::find_z_svdw(
Fp::ZERO,
<Fp as FieldExtensionTrait<1, 1>>::curve_constant(),
);
assert_eq!(z, Fp::ONE, "Finding Z failed for BN254");
}
#[test]
Expand Down

0 comments on commit a838a46

Please sign in to comment.