From 9a432e0b2ccf4fa5144887af0fb865ae7f963d1e Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Thu, 7 Mar 2024 00:01:57 +0100 Subject: [PATCH] Some small refactorings --- .../curves/bls12_381/field_extension.rs | 41 ++++++++------ .../elliptic_curve/short_weierstrass/point.rs | 56 +++++++++---------- math/src/unsigned_integer/element.rs | 7 +-- 3 files changed, 52 insertions(+), 52 deletions(-) diff --git a/math/src/elliptic_curve/short_weierstrass/curves/bls12_381/field_extension.rs b/math/src/elliptic_curve/short_weierstrass/curves/bls12_381/field_extension.rs index b97f51218..9a280cf28 100644 --- a/math/src/elliptic_curve/short_weierstrass/curves/bls12_381/field_extension.rs +++ b/math/src/elliptic_curve/short_weierstrass/curves/bls12_381/field_extension.rs @@ -108,35 +108,40 @@ impl IsSubFieldOf for BLS12381PrimeField { a: &Self::BaseType, b: &::BaseType, ) -> ::BaseType { - let c0 = FieldElement::from_raw(::mul(a, b[0].value())); - let c1 = FieldElement::from_raw(::mul(a, b[1].value())); - [c0, c1] + [ + FieldElement::from_raw(::mul(a, b[0].value())), + FieldElement::from_raw(::mul(a, b[1].value())), + ] } fn add( a: &Self::BaseType, b: &::BaseType, ) -> ::BaseType { - let c0 = FieldElement::from_raw(::add(a, b[0].value())); - let c1 = FieldElement::from_raw(*b[1].value()); - [c0, c1] + [ + FieldElement::from_raw(::add(a, b[0].value())), + FieldElement::from_raw(*b[1].value()), + ] } fn div( a: &Self::BaseType, b: &::BaseType, ) -> ::BaseType { - let b_inv = Degree2ExtensionField::inv(b).unwrap(); - >::mul(a, &b_inv) + >::mul( + a, + &Degree2ExtensionField::inv(b).unwrap(), + ) } fn sub( a: &Self::BaseType, b: &::BaseType, ) -> ::BaseType { - let c0 = FieldElement::from_raw(::sub(a, b[0].value())); - let c1 = FieldElement::from_raw(::neg(b[1].value())); - [c0, c1] + [ + FieldElement::from_raw(::sub(a, b[0].value())), + FieldElement::from_raw(::neg(b[1].value())), + ] } fn embed(a: Self::BaseType) -> ::BaseType { @@ -171,9 +176,10 @@ impl ByteConversion for FieldElement { Self: core::marker::Sized, { const BYTES_PER_FIELD: usize = 48; - let x0 = FieldElement::from_bytes_be(&bytes[0..BYTES_PER_FIELD])?; - let x1 = FieldElement::from_bytes_be(&bytes[BYTES_PER_FIELD..BYTES_PER_FIELD * 2])?; - Ok(Self::new([x0, x1])) + Ok(Self::new([ + FieldElement::from_bytes_be(&bytes[0..BYTES_PER_FIELD])?, + FieldElement::from_bytes_be(&bytes[BYTES_PER_FIELD..BYTES_PER_FIELD * 2])?, + ])) } fn from_bytes_le(bytes: &[u8]) -> Result @@ -181,9 +187,10 @@ impl ByteConversion for FieldElement { Self: core::marker::Sized, { const BYTES_PER_FIELD: usize = 48; - let x0 = FieldElement::from_bytes_le(&bytes[0..BYTES_PER_FIELD])?; - let x1 = FieldElement::from_bytes_le(&bytes[BYTES_PER_FIELD..BYTES_PER_FIELD * 2])?; - Ok(Self::new([x0, x1])) + Ok(Self::new([ + FieldElement::from_bytes_le(&bytes[0..BYTES_PER_FIELD])?, + FieldElement::from_bytes_le(&bytes[BYTES_PER_FIELD..BYTES_PER_FIELD * 2])?, + ])) } } diff --git a/math/src/elliptic_curve/short_weierstrass/point.rs b/math/src/elliptic_curve/short_weierstrass/point.rs index ec30219b1..bb9c10d2f 100644 --- a/math/src/elliptic_curve/short_weierstrass/point.rs +++ b/math/src/elliptic_curve/short_weierstrass/point.rs @@ -101,14 +101,9 @@ impl ShortWeierstrassProjectivePoint { if u == *py { if v != *px || *py == FieldElement::zero() { - return Self::new([ - FieldElement::zero(), - FieldElement::one(), - FieldElement::zero(), - ]); - } else { - return self.double(); + return Self::neutral_element(); } + return self.double(); } let u = &u - py; @@ -160,8 +155,7 @@ impl IsGroup for ShortWeierstrassProjectivePoint { } fn is_neutral_element(&self) -> bool { - let pz = self.z(); - pz == &FieldElement::zero() + self.z() == &FieldElement::zero() } /// Computes the addition of `self` and `other`. @@ -288,19 +282,18 @@ where } let len = bytes.len() / 3; - let x: FieldElement; - let y: FieldElement; - let z: FieldElement; - - if endianness == Endianness::BigEndian { - x = ByteConversion::from_bytes_be(&bytes[..len])?; - y = ByteConversion::from_bytes_be(&bytes[len..len * 2])?; - z = ByteConversion::from_bytes_be(&bytes[len * 2..])?; - } else { - x = ByteConversion::from_bytes_le(&bytes[..len])?; - y = ByteConversion::from_bytes_le(&bytes[len..len * 2])?; - z = ByteConversion::from_bytes_le(&bytes[len * 2..])?; - } + let (x, y, z) = match endianness { + Endianness::BigEndian => ( + ByteConversion::from_bytes_be(&bytes[..len])?, + ByteConversion::from_bytes_be(&bytes[len..len * 2])?, + ByteConversion::from_bytes_be(&bytes[len * 2..])?, + ), + _ => ( + ByteConversion::from_bytes_le(&bytes[..len])?, + ByteConversion::from_bytes_le(&bytes[len..len * 2])?, + ByteConversion::from_bytes_le(&bytes[len * 2..])?, + ), + }; if z == FieldElement::zero() { let point = Self::new([x, y, z]); @@ -321,16 +314,17 @@ where } let len = bytes.len() / 2; - let x: FieldElement; - let y: FieldElement; - if endianness == Endianness::BigEndian { - x = ByteConversion::from_bytes_be(&bytes[..len])?; - y = ByteConversion::from_bytes_be(&bytes[len..])?; - } else { - x = ByteConversion::from_bytes_le(&bytes[..len])?; - y = ByteConversion::from_bytes_le(&bytes[len..])?; - } + let (x, y) = match endianness { + Endianness::BigEndian => ( + ByteConversion::from_bytes_be(&bytes[..len])?, + ByteConversion::from_bytes_be(&bytes[len..])?, + ), + _ => ( + ByteConversion::from_bytes_le(&bytes[..len])?, + ByteConversion::from_bytes_le(&bytes[len..])?, + ), + }; if E::defining_equation(&x, &y) == FieldElement::zero() { Ok(Self::new([x, y, FieldElement::one()])) diff --git a/math/src/unsigned_integer/element.rs b/math/src/unsigned_integer/element.rs index 409587c35..cf73fc825 100644 --- a/math/src/unsigned_integer/element.rs +++ b/math/src/unsigned_integer/element.rs @@ -530,7 +530,6 @@ impl UnsignedInteger { limbs[i] = self.limbs[a + i]; i += 1; } - Self { limbs } } else { limbs[NUM_LIMBS - 1 - a] = self.limbs[NUM_LIMBS - 1] << b; let mut i = a + 1; @@ -539,8 +538,8 @@ impl UnsignedInteger { | (self.limbs[NUM_LIMBS - i + a] >> (64 - b)); i += 1; } - Self { limbs } } + Self { limbs } } pub const fn const_shr(self, times: usize) -> UnsignedInteger { @@ -558,7 +557,6 @@ impl UnsignedInteger { limbs[a + i] = self.limbs[i]; i += 1; } - Self { limbs } } else { limbs[a] = self.limbs[0] >> b; let mut i = a + 1; @@ -566,8 +564,9 @@ impl UnsignedInteger { limbs[i] = (self.limbs[i - a - 1] << (64 - b)) | (self.limbs[i - a] >> b); i += 1; } - Self { limbs } } + + Self { limbs } } pub const fn add(