Skip to content

Commit 55d893c

Browse files
authored
primefield+p384: upgrade to fiat-crypto v0.1.4 (#1157)
Updates the field element type generating macros in the `primefield` crate to be compatible with the latest `fiat-crypto` code as postprocessed by `fiat-constify`, and regenerates the `p384` field impls using `fiat-crypto` v0.1.4. This update leverages `const_mut_refs` to be much closer to the upstream `fiat-crypto` output, while still providing `const fn` support. This change also starts extracting a `fiat` module within `primeorder` for macros that are specific to `fiat-crypto` output. With this upgrade in place, upgrading the rest of the curve crates should be comparatively straightforward.
1 parent 800e98c commit 55d893c

File tree

14 files changed

+27243
-10309
lines changed

14 files changed

+27243
-10309
lines changed

Cargo.lock

Lines changed: 8 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ ecdsa = { git = "https://github.com/RustCrypto/signatures.git" }
2525
rfc6979 = { git = "https://github.com/RustCrypto/signatures.git" }
2626

2727
# https://github.com/RustCrypto/traits/pull/1777
28+
crypto-bigint = { git = "https://github.com/RustCrypto/crypto-bigint.git" }
2829
elliptic-curve = { git = "https://github.com/RustCrypto/traits.git" }
29-
signature = { git = "https://github.com/RustCrypto/traits.git" }
30+
signature = { git = "https://github.com/RustCrypto/traits.git" }

p384/Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ elliptic-curve = { version = "0.14.0-rc.0", default-features = false, features =
2323
# optional dependencies
2424
ecdsa-core = { version = "=0.17.0-pre.9", package = "ecdsa", optional = true, default-features = false, features = ["der"] }
2525
hex-literal = { version = "1", optional = true }
26+
primefield = { version = "=0.14.0-pre.0", optional = true, path = "../primefield" }
2627
primeorder = { version = "=0.14.0-pre.2", optional = true, path = "../primeorder" }
2728
serdect = { version = "0.3", optional = true, default-features = false }
2829
sha2 = { version = "=0.11.0-pre.5", optional = true, default-features = false }
@@ -41,7 +42,12 @@ default = ["arithmetic", "ecdsa", "pem", "std"]
4142
alloc = ["ecdsa-core?/alloc", "elliptic-curve/alloc", "primeorder?/alloc"]
4243
std = ["alloc", "ecdsa-core?/std", "elliptic-curve/std"]
4344

44-
arithmetic = ["dep:primeorder", "elliptic-curve/arithmetic", "elliptic-curve/digest"]
45+
arithmetic = [
46+
"dep:primefield",
47+
"dep:primeorder",
48+
"elliptic-curve/arithmetic",
49+
"elliptic-curve/digest"
50+
]
4551
bits = ["arithmetic", "elliptic-curve/bits"]
4652
digest = ["ecdsa-core/digest", "ecdsa-core/hazmat"]
4753
ecdh = ["arithmetic", "elliptic-curve/ecdh"]

p384/src/arithmetic/field.rs

Lines changed: 16 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,13 @@ mod field_impl;
2525

2626
use self::field_impl::*;
2727
use crate::{FieldBytes, NistP384};
28-
use core::{
29-
fmt::{self, Debug},
30-
iter::{Product, Sum},
31-
ops::{AddAssign, MulAssign, Neg, SubAssign},
32-
};
33-
use elliptic_curve::ops::Invert;
28+
use core::fmt::{self, Debug};
3429
use elliptic_curve::{
35-
bigint::{Limb, U384},
30+
bigint::U384,
3631
ff::PrimeField,
32+
ops::Invert,
3733
subtle::{Choice, ConstantTimeEq, CtOption},
3834
};
39-
use primeorder::impl_bernstein_yang_invert;
4035

4136
/// Constant representing the modulus
4237
/// p = 2^{384} − 2^{128} − 2^{96} + 2^{32} − 1
@@ -46,50 +41,28 @@ pub(crate) const MODULUS: U384 = U384::from_be_hex(FieldElement::MODULUS);
4641
#[derive(Clone, Copy)]
4742
pub struct FieldElement(pub(super) U384);
4843

49-
primeorder::impl_mont_field_element!(
50-
NistP384,
44+
primefield::field_element_type!(NistP384, FieldElement, FieldBytes, U384, MODULUS);
45+
46+
primefield::fiat_field_arithmetic!(
5147
FieldElement,
5248
FieldBytes,
5349
U384,
54-
MODULUS,
50+
fiat_p384_non_montgomery_domain_field_element,
5551
fiat_p384_montgomery_domain_field_element,
5652
fiat_p384_from_montgomery,
5753
fiat_p384_to_montgomery,
5854
fiat_p384_add,
5955
fiat_p384_sub,
6056
fiat_p384_mul,
6157
fiat_p384_opp,
62-
fiat_p384_square
58+
fiat_p384_square,
59+
fiat_p384_divstep_precomp,
60+
fiat_p384_divstep,
61+
fiat_p384_msat,
62+
fiat_p384_selectznz
6363
);
6464

6565
impl FieldElement {
66-
/// Compute [`FieldElement`] inversion: `1 / self`.
67-
pub fn invert(&self) -> CtOption<Self> {
68-
CtOption::new(self.invert_unchecked(), !self.is_zero())
69-
}
70-
71-
/// Returns the multiplicative inverse of self.
72-
///
73-
/// Does not check that self is non-zero.
74-
const fn invert_unchecked(&self) -> Self {
75-
let words = impl_bernstein_yang_invert!(
76-
self.0.as_words(),
77-
Self::ONE.0.to_words(),
78-
384,
79-
U384::LIMBS,
80-
Limb,
81-
fiat_p384_from_montgomery,
82-
fiat_p384_mul,
83-
fiat_p384_opp,
84-
fiat_p384_divstep_precomp,
85-
fiat_p384_divstep,
86-
fiat_p384_msat,
87-
fiat_p384_selectznz,
88-
);
89-
90-
Self(U384::from_words(words))
91-
}
92-
9366
/// Returns the square root of self mod p, or `None` if no square root
9467
/// exists.
9568
pub fn sqrt(&self) -> CtOption<Self> {
@@ -175,10 +148,6 @@ impl Invert for FieldElement {
175148
mod tests {
176149
use super::FieldElement;
177150
use elliptic_curve::ff::PrimeField;
178-
use primeorder::{
179-
impl_field_identity_tests, impl_field_invert_tests, impl_field_sqrt_tests,
180-
impl_primefield_tests,
181-
};
182151

183152
/// t = (modulus - 1) >> S
184153
const T: [u64; 6] = [
@@ -190,8 +159,8 @@ mod tests {
190159
0x7fffffffffffffff,
191160
];
192161

193-
impl_field_identity_tests!(FieldElement);
194-
impl_field_invert_tests!(FieldElement);
195-
impl_field_sqrt_tests!(FieldElement);
196-
impl_primefield_tests!(FieldElement, T);
162+
primefield::test_field_constants!(FieldElement, T);
163+
primefield::test_field_identity!(FieldElement);
164+
primefield::test_field_invert!(FieldElement);
165+
primefield::test_field_sqrt!(FieldElement);
197166
}

0 commit comments

Comments
 (0)