Skip to content

Commit

Permalink
[WIP] p521: add basic field tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tarcieri committed Nov 1, 2023
1 parent 1f6eb5b commit bda990b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
33 changes: 29 additions & 4 deletions p521/src/arithmetic/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use elliptic_curve::{
Error, FieldBytesEncoding,
};

use super::util::uint_to_le_bytes_unchecked;
use super::util::u576_to_le_bytes;

/// Constant representing the modulus serialized as hex.
/// p = 2^{521} − 1
Expand Down Expand Up @@ -106,7 +106,7 @@ impl FieldElement {
///
/// Used incorrectly this can lead to invalid results!
pub(crate) const fn from_uint_unchecked(w: U576) -> Self {
Self(fiat_p521_from_bytes(&uint_to_le_bytes_unchecked(w)))
Self(fiat_p521_from_bytes(&u576_to_le_bytes(w)))
}

/// Returns the big-endian encoding of this [`FieldElement`].
Expand Down Expand Up @@ -190,7 +190,7 @@ impl FieldElement {
}

/// Multiply elements.
pub const fn mul(&self, rhs: &Self) -> Self {
pub const fn multiply(&self, rhs: &Self) -> Self {
LooseFieldElement::mul(&self.relax(), &rhs.relax())
}

Expand All @@ -217,7 +217,7 @@ impl FieldElement {
res = res.square();

if ((exp[i] >> j) & 1) == 1 {
res = Self::mul(&res, self);
res = Self::multiply(&res, self);
}
}
}
Expand Down Expand Up @@ -534,3 +534,28 @@ impl<'a> Product<&'a FieldElement> for FieldElement {
iter.copied().product()
}
}

#[cfg(test)]
mod tests {
use super::FieldElement;
use elliptic_curve::ff::PrimeField;
use primeorder::{impl_field_identity_tests, impl_primefield_tests};

/// t = (modulus - 1) >> S
const T: [u64; 9] = [
0xffffffff_ffffffff,
0xffffffff_ffffffff,
0xffffffff_ffffffff,
0xffffffff_ffffffff,
0xffffffff_ffffffff,
0xffffffff_ffffffff,
0xffffffff_ffffffff,
0xffffffff_ffffffff,
0x00000000_000000ff,
];

impl_field_identity_tests!(FieldElement);
//impl_field_invert_tests!(FieldElement);
//impl_field_sqrt_tests!(FieldElement);
impl_primefield_tests!(FieldElement, T);
}
2 changes: 1 addition & 1 deletion p521/src/arithmetic/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub(crate) const fn u64x9_to_u32x18(w: &[u64; 9]) -> [u32; 18] {

/// Converts the saturated representation [`U576`] into a 528bit array. Each
/// word is copied in little-endian.
pub const fn uint_to_le_bytes_unchecked(w: U576) -> [u8; 66] {
pub const fn u576_to_le_bytes(w: U576) -> [u8; 66] {
#[cfg(target_pointer_width = "32")]
let words = u32x18_to_u64x9(w.as_words());
#[cfg(target_pointer_width = "64")]
Expand Down

0 comments on commit bda990b

Please sign in to comment.