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

p384: const fn field arithmetic #589

Merged
merged 1 commit into from
Jun 2, 2022
Merged
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
5 changes: 1 addition & 4 deletions p384/src/arithmetic/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@

#[cfg_attr(target_pointer_width = "32", path = "field/p384_32.rs")]
#[cfg_attr(target_pointer_width = "64", path = "field/p384_64.rs")]
#[allow(dead_code, rustdoc::broken_intra_doc_links)]
#[rustfmt::skip]
mod field_impl;

use self::field_impl::*;
Expand Down Expand Up @@ -118,8 +116,7 @@ mod tests {
/// Test that the precomputed `FieldElement::ONE` constant is correct.
#[test]
fn one() {
let mut one_mont = Default::default();
fiat_p384_to_montgomery(&mut one_mont, U384::ONE.as_ref());
let one_mont = fiat_p384_to_montgomery(U384::ONE.as_ref());
assert_eq!(FieldElement(one_mont.into()), FieldElement::ONE);
}

Expand Down
6,855 changes: 3,344 additions & 3,511 deletions p384/src/arithmetic/field/p384_32.rs

Large diffs are not rendered by default.

2,609 changes: 1,293 additions & 1,316 deletions p384/src/arithmetic/field/p384_64.rs

Large diffs are not rendered by default.

69 changes: 21 additions & 48 deletions p384/src/arithmetic/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,7 @@ macro_rules! impl_sec1_field_element {
///
/// Used incorrectly this can lead to invalid results!
fn from_uint_unchecked(w: $uint) -> Self {
let mut mont = <$uint>::default();
$to_mont(mont.as_mut(), w.as_ref());
Self(mont)
Self($to_mont(w.as_ref()).into())
}

/// Returns the big-endian encoding of this [`
Expand All @@ -156,9 +154,7 @@ macro_rules! impl_sec1_field_element {
/// `] in canonical form.
#[inline]
pub fn to_canonical(self) -> $uint {
let mut ret = <$uint>::default();
$from_mont(ret.as_mut(), self.as_ref());
ret
$from_mont(self.as_ref()).into()
}

/// Determine if this [`
Expand Down Expand Up @@ -203,65 +199,50 @@ macro_rules! impl_sec1_field_element {
type XLimbs = [Word; LIMBS + 1];

let mut d: Word = 1;
let mut f = XLimbs::default();
$msat(&mut f);
let mut f = $msat();

let mut g = XLimbs::default();
$from_mont((&mut g[..LIMBS]).try_into().unwrap(), self.as_ref());
g[..LIMBS].copy_from_slice(&$from_mont(self.as_ref()));

let mut r = <$arr>::from(Self::ONE.0);
let mut v = <$arr>::default();
let mut precomp = <$arr>::default();
$divstep_precomp(&mut precomp);

let mut out1 = Word::default();
let mut out2 = XLimbs::default();
let mut out3 = XLimbs::default();
let mut out4 = <$arr>::default();
let mut out5 = <$arr>::default();
let precomp = $divstep_precomp();

let mut i: usize = 0;

while i < ITERATIONS - ITERATIONS % 2 {
$divstep(
&mut out1, &mut out2, &mut out3, &mut out4, &mut out5, d, &f, &g, &v, &r,
);
$divstep(
&mut d, &mut f, &mut g, &mut v, &mut r, out1, &out2, &out3, &out4, &out5,
);
let (out1, out2, out3, out4, out5) = $divstep(d, &f, &g, &v, &r);
let (out1, out2, out3, out4, out5) = $divstep(out1, &out2, &out3, &out4, &out5);
d = out1;
f = out2;
g = out3;
v = out4;
r = out5;
i += 2;
}

if ITERATIONS % 2 != 0 {
$divstep(
&mut out1, &mut out2, &mut out3, &mut out4, &mut out5, d, &f, &g, &v, &r,
);
let (_out1, out2, _out3, out4, _out5) = $divstep(d, &f, &g, &v, &r);
v = out4;
f = out2;
}

let mut v_opp = <$uint>::default();
$neg(v_opp.as_mut(), &v);

let v_opp = <$uint>::from($neg(&v));
let v = <$uint>::from(v);

let s = ::elliptic_curve::subtle::Choice::from(
((f[f.len() - 1] >> Limb::BIT_SIZE - 1) & 1) as u8,
);

let v = <$uint>::conditional_select(&v, &v_opp, s);

let mut ret = <$uint>::default();
$mul(ret.as_mut(), v.as_ref(), &precomp);
::elliptic_curve::subtle::CtOption::new(Self(ret), !self.is_zero())
let ret = $mul(v.as_ref(), &precomp);
::elliptic_curve::subtle::CtOption::new(Self(ret.into()), !self.is_zero())
}

/// Compute modular square.
#[must_use]
pub fn square(&self) -> Self {
let mut ret = <$uint>::default();
$square(ret.as_mut(), self.as_ref());
Self(ret)
Self($square(self.as_ref()).into())
}
}

Expand Down Expand Up @@ -406,9 +387,7 @@ macro_rules! impl_sec1_field_element {

#[inline]
fn neg(self) -> $fe {
let mut ret = <$uint>::default();
$neg(ret.as_mut(), self.as_ref());
Self(ret)
Self($neg(self.as_ref()).into())
}
}
};
Expand All @@ -423,9 +402,7 @@ macro_rules! impl_field_op {

#[inline]
fn $op_fn(self, rhs: $fe) -> $fe {
let mut out = <$uint>::default();
$func(out.as_mut(), self.as_ref(), rhs.as_ref());
$fe(out)
$fe($func(self.as_ref(), rhs.as_ref()).into())
}
}

Expand All @@ -434,9 +411,7 @@ macro_rules! impl_field_op {

#[inline]
fn $op_fn(self, rhs: &$fe) -> $fe {
let mut out = <$uint>::default();
$func(out.as_mut(), self.as_ref(), rhs.as_ref());
$fe(out)
$fe($func(self.as_ref(), rhs.as_ref()).into())
}
}

Expand All @@ -445,9 +420,7 @@ macro_rules! impl_field_op {

#[inline]
fn $op_fn(self, rhs: &$fe) -> $fe {
let mut out = <$uint>::default();
$func(out.as_mut(), self.as_ref(), rhs.as_ref());
$fe(out)
$fe($func(self.as_ref(), rhs.as_ref()).into())
}
}
};
Expand Down
5 changes: 1 addition & 4 deletions p384/src/arithmetic/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
clippy::too_many_arguments,
clippy::unnecessary_cast
)]
#[allow(dead_code, rustdoc::broken_intra_doc_links)]
#[rustfmt::skip]
mod scalar_impl;

use self::scalar_impl::*;
Expand Down Expand Up @@ -258,8 +256,7 @@ mod tests {
/// Test that the precomputed `Scalar::ONE` constant is correct.
#[test]
fn one() {
let mut one_mont = Default::default();
fiat_p384_scalar_to_montgomery(&mut one_mont, U384::ONE.as_ref());
let one_mont = fiat_p384_scalar_to_montgomery(U384::ONE.as_ref());
assert_eq!(Scalar(one_mont.into()), Scalar::ONE);
}

Expand Down
Loading