Skip to content

Commit 6937f5d

Browse files
authored
elliptic-curve: add impl_field_element! macro (#1021)
Extracts the macro of the same name from the `p384` crate so it can be used with other elliptic curve crates. Closes #1017.
1 parent 2bdca99 commit 6937f5d

File tree

3 files changed

+515
-0
lines changed

3 files changed

+515
-0
lines changed

elliptic-curve/src/dev.rs

+72
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,78 @@ impl Neg for ProjectivePoint {
707707
}
708708
}
709709

710+
/// Constant representing the base field modulus
711+
/// p = 2^{224}(2^{32} − 1) + 2^{192} + 2^{96} − 1
712+
pub const MODULUS: U256 =
713+
U256::from_be_hex("ffffffff00000001000000000000000000000000ffffffffffffffffffffffff");
714+
715+
/// Example base field element.
716+
#[derive(Clone, Copy, Debug)]
717+
pub struct FieldElement(pub(crate) U256);
718+
719+
/// Internal field element representation.
720+
#[cfg(target_pointer_width = "32")]
721+
type FeWords = [u32; 8];
722+
723+
/// Internal field element representation.
724+
#[cfg(target_pointer_width = "64")]
725+
type FeWords = [u64; 4];
726+
727+
impl_field_element!(
728+
FieldElement,
729+
FieldBytes,
730+
U256,
731+
MODULUS,
732+
FeWords,
733+
p256_from_montgomery,
734+
p256_to_montgomery,
735+
p256_add,
736+
p256_sub,
737+
p256_mul,
738+
p256_opp,
739+
p256_square
740+
);
741+
742+
impl FieldElement {
743+
/// Returns the multiplicative inverse of self, if self is non-zero.
744+
pub fn invert(&self) -> CtOption<Self> {
745+
unimplemented!()
746+
}
747+
748+
/// Returns the square root of self mod p, or `None` if no square root exists.
749+
pub fn sqrt(&self) -> CtOption<Self> {
750+
unimplemented!()
751+
}
752+
}
753+
754+
const fn p256_from_montgomery(_: &FeWords) -> FeWords {
755+
unimplemented!()
756+
}
757+
758+
const fn p256_to_montgomery(w: &FeWords) -> FeWords {
759+
*w
760+
}
761+
762+
const fn p256_add(_: &FeWords, _: &FeWords) -> FeWords {
763+
unimplemented!()
764+
}
765+
766+
const fn p256_sub(_: &FeWords, _: &FeWords) -> FeWords {
767+
unimplemented!()
768+
}
769+
770+
const fn p256_mul(_: &FeWords, _: &FeWords) -> FeWords {
771+
unimplemented!()
772+
}
773+
774+
const fn p256_opp(_: &FeWords) -> FeWords {
775+
unimplemented!()
776+
}
777+
778+
const fn p256_square(_: &FeWords) -> FeWords {
779+
unimplemented!()
780+
}
781+
710782
#[cfg(test)]
711783
mod tests {
712784
use super::Scalar;

elliptic-curve/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ pub mod ops;
7171
#[cfg(feature = "sec1")]
7272
pub mod sec1;
7373

74+
#[macro_use]
75+
mod macros;
76+
7477
mod error;
7578
mod point;
7679
mod scalar;

0 commit comments

Comments
 (0)