Skip to content

Commit

Permalink
p384: make core field ops const fn
Browse files Browse the repository at this point in the history
Now that #589 is landed, it's possible to add `const fn` implementations
of all core field operations, including conersions to/from Montgomery
form as well as arithmetic operations: add, double, sub, mul, neg, square
  • Loading branch information
tarcieri committed Jun 3, 2022
1 parent 593ec93 commit b95c036
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 37 additions & 8 deletions p384/src/arithmetic/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ macro_rules! impl_sec1_field_element {
/// Does not perform a check that the field element does not overflow the order.
///
/// Used incorrectly this can lead to invalid results!
fn from_uint_unchecked(w: $uint) -> Self {
Self($to_mont(w.as_ref()).into())
const fn from_uint_unchecked(w: $uint) -> Self {
Self(<$uint>::from_uint_array($to_mont(w.as_uint_array())))
}

/// Returns the big-endian encoding of this [`
Expand All @@ -153,8 +153,8 @@ macro_rules! impl_sec1_field_element {
#[doc = stringify!($uint)]
/// `] in canonical form.
#[inline]
pub fn to_canonical(self) -> $uint {
$from_mont(self.as_ref()).into()
pub const fn to_canonical(self) -> $uint {
<$uint>::from_uint_array($from_mont(self.0.as_uint_array()))
}

/// Determine if this [`
Expand All @@ -179,10 +179,39 @@ macro_rules! impl_sec1_field_element {
self.ct_eq(&Self::ZERO)
}

/// Add elements.
pub const fn add(&self, rhs: &Self) -> Self {
Self(<$uint>::from_uint_array($add(
self.0.as_uint_array(),
rhs.0.as_uint_array(),
)))
}

/// Double element (add it to itself).
#[must_use]
pub fn double(&self) -> Self {
self + self
pub const fn double(&self) -> Self {
self.add(self)
}

/// Subtract elements.
pub const fn sub(&self, rhs: &Self) -> Self {
Self(<$uint>::from_uint_array($sub(
self.0.as_uint_array(),
rhs.0.as_uint_array(),
)))
}

/// Multiply elements.
pub const fn mul(&self, rhs: &Self) -> Self {
Self(<$uint>::from_uint_array($mul(
self.0.as_uint_array(),
rhs.0.as_uint_array(),
)))
}

/// Negate element.
pub const fn neg(&self) -> Self {
Self(<$uint>::from_uint_array($neg(self.0.as_uint_array())))
}

/// Compute [`
Expand Down Expand Up @@ -241,8 +270,8 @@ macro_rules! impl_sec1_field_element {

/// Compute modular square.
#[must_use]
pub fn square(&self) -> Self {
Self($square(self.as_ref()).into())
pub const fn square(&self) -> Self {
Self(<$uint>::from_uint_array($square(self.0.as_uint_array())))
}
}

Expand Down

0 comments on commit b95c036

Please sign in to comment.