From bda990be7e13391d91bb8829ccf9ece7ad4df3cc Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Tue, 31 Oct 2023 20:19:26 -0600 Subject: [PATCH] [WIP] p521: add basic field tests --- p521/src/arithmetic/field.rs | 33 +++++++++++++++++++++++++++++---- p521/src/arithmetic/util.rs | 2 +- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/p521/src/arithmetic/field.rs b/p521/src/arithmetic/field.rs index 0a57b35a..36583a42 100644 --- a/p521/src/arithmetic/field.rs +++ b/p521/src/arithmetic/field.rs @@ -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 @@ -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`]. @@ -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()) } @@ -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); } } } @@ -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); +} diff --git a/p521/src/arithmetic/util.rs b/p521/src/arithmetic/util.rs index 8b6c6168..b8bfd12b 100644 --- a/p521/src/arithmetic/util.rs +++ b/p521/src/arithmetic/util.rs @@ -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")]