Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some small refactorings #832

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,35 +108,40 @@ impl IsSubFieldOf<Degree2ExtensionField> for BLS12381PrimeField {
a: &Self::BaseType,
b: &<Degree2ExtensionField as IsField>::BaseType,
) -> <Degree2ExtensionField as IsField>::BaseType {
let c0 = FieldElement::from_raw(<Self as IsField>::mul(a, b[0].value()));
let c1 = FieldElement::from_raw(<Self as IsField>::mul(a, b[1].value()));
[c0, c1]
[
FieldElement::from_raw(<Self as IsField>::mul(a, b[0].value())),
FieldElement::from_raw(<Self as IsField>::mul(a, b[1].value())),
]
}

fn add(
a: &Self::BaseType,
b: &<Degree2ExtensionField as IsField>::BaseType,
) -> <Degree2ExtensionField as IsField>::BaseType {
let c0 = FieldElement::from_raw(<Self as IsField>::add(a, b[0].value()));
let c1 = FieldElement::from_raw(*b[1].value());
[c0, c1]
[
FieldElement::from_raw(<Self as IsField>::add(a, b[0].value())),
FieldElement::from_raw(*b[1].value()),
]
}

fn div(
a: &Self::BaseType,
b: &<Degree2ExtensionField as IsField>::BaseType,
) -> <Degree2ExtensionField as IsField>::BaseType {
let b_inv = Degree2ExtensionField::inv(b).unwrap();
<Self as IsSubFieldOf<Degree2ExtensionField>>::mul(a, &b_inv)
<Self as IsSubFieldOf<Degree2ExtensionField>>::mul(
a,
&Degree2ExtensionField::inv(b).unwrap(),
)
}

fn sub(
a: &Self::BaseType,
b: &<Degree2ExtensionField as IsField>::BaseType,
) -> <Degree2ExtensionField as IsField>::BaseType {
let c0 = FieldElement::from_raw(<Self as IsField>::sub(a, b[0].value()));
let c1 = FieldElement::from_raw(<Self as IsField>::neg(b[1].value()));
[c0, c1]
[
FieldElement::from_raw(<Self as IsField>::sub(a, b[0].value())),
FieldElement::from_raw(<Self as IsField>::neg(b[1].value())),
]
}

fn embed(a: Self::BaseType) -> <Degree2ExtensionField as IsField>::BaseType {
Expand Down Expand Up @@ -171,19 +176,21 @@ impl ByteConversion for FieldElement<Degree2ExtensionField> {
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<Self, crate::errors::ByteConversionError>
where
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])?,
]))
}
}

Expand Down
56 changes: 25 additions & 31 deletions math/src/elliptic_curve/short_weierstrass/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,9 @@ impl<E: IsShortWeierstrass> ShortWeierstrassProjectivePoint<E> {

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;
Expand Down Expand Up @@ -159,8 +154,7 @@ impl<E: IsShortWeierstrass> IsGroup for ShortWeierstrassProjectivePoint<E> {
}

fn is_neutral_element(&self) -> bool {
let pz = self.z();
pz == &FieldElement::zero()
self.z() == &FieldElement::zero()
}

/// Computes the addition of `self` and `other`.
Expand Down Expand Up @@ -287,19 +281,18 @@ where
}

let len = bytes.len() / 3;
let x: FieldElement<E::BaseField>;
let y: FieldElement<E::BaseField>;
let z: FieldElement<E::BaseField>;

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]);
Expand All @@ -320,16 +313,17 @@ where
}

let len = bytes.len() / 2;
let x: FieldElement<E::BaseField>;
let y: FieldElement<E::BaseField>;

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()]))
Expand Down
7 changes: 3 additions & 4 deletions math/src/unsigned_integer/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,6 @@ impl<const NUM_LIMBS: usize> UnsignedInteger<NUM_LIMBS> {
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;
Expand All @@ -557,8 +556,8 @@ impl<const NUM_LIMBS: usize> UnsignedInteger<NUM_LIMBS> {
| (self.limbs[NUM_LIMBS - i + a] >> (64 - b));
i += 1;
}
Self { limbs }
}
Self { limbs }
}

pub const fn const_shr(self, times: usize) -> UnsignedInteger<NUM_LIMBS> {
Expand All @@ -576,16 +575,16 @@ impl<const NUM_LIMBS: usize> UnsignedInteger<NUM_LIMBS> {
limbs[a + i] = self.limbs[i];
i += 1;
}
Self { limbs }
} else {
limbs[a] = self.limbs[0] >> b;
let mut i = a + 1;
while i < NUM_LIMBS {
limbs[i] = (self.limbs[i - a - 1] << (64 - b)) | (self.limbs[i - a] >> b);
i += 1;
}
Self { limbs }
}

Self { limbs }
}

pub const fn add(
Expand Down
Loading