diff --git a/elliptic-curve/src/arithmetic.rs b/elliptic-curve/src/arithmetic.rs index 84cc92555..70c86b4bd 100644 --- a/elliptic-curve/src/arithmetic.rs +++ b/elliptic-curve/src/arithmetic.rs @@ -2,7 +2,7 @@ use crate::{ ops::{LinearCombination, MulByGenerator}, - AffineXCoordinate, AffineYIsOdd, Curve, FieldBytes, IsHigh, PrimeCurve, ScalarCore, + AffineXCoordinate, AffineYIsOdd, Curve, FieldBytes, IsHigh, PrimeCurve, ScalarPrimitive, }; use core::fmt::Debug; use subtle::{ConditionallySelectable, ConstantTimeEq}; @@ -61,7 +61,7 @@ pub trait CurveArithmetic: Curve { /// - [`Send`] /// - [`Sync`] type Scalar: DefaultIsZeroes - + From> + + From> + Into> + Into + IsHigh diff --git a/elliptic-curve/src/dev.rs b/elliptic-curve/src/dev.rs index 11cf4cae5..2951733f8 100644 --- a/elliptic-curve/src/dev.rs +++ b/elliptic-curve/src/dev.rs @@ -47,9 +47,9 @@ pub type PublicKey = crate::PublicKey; /// Secret key. pub type SecretKey = crate::SecretKey; -/// Scalar core. -// TODO(tarcieri): make this the scalar type -pub type ScalarCore = crate::ScalarCore; +/// Scalar primitive type. +// TODO(tarcieri): make this the scalar type when it's more capable +pub type ScalarPrimitive = crate::ScalarPrimitive; /// Scalar bits. #[cfg(feature = "bits")] @@ -90,11 +90,11 @@ impl JwkParameters for MockCurve { /// Example scalar type #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] -pub struct Scalar(ScalarCore); +pub struct Scalar(ScalarPrimitive); impl Field for Scalar { - const ZERO: Self = Self(ScalarCore::ZERO); - const ONE: Self = Self(ScalarCore::ONE); + const ZERO: Self = Self(ScalarPrimitive::ZERO); + const ONE: Self = Self(ScalarPrimitive::ONE); fn random(mut rng: impl RngCore) -> Self { let mut bytes = FieldBytes::default(); @@ -149,7 +149,7 @@ impl PrimeField for Scalar { const DELTA: Self = Self::ZERO; // BOGUS! fn from_repr(bytes: FieldBytes) -> CtOption { - ScalarCore::from_be_bytes(bytes).map(Self) + ScalarPrimitive::from_be_bytes(bytes).map(Self) } fn to_repr(&self) -> FieldBytes { @@ -182,7 +182,7 @@ impl TryFrom for Scalar { type Error = Error; fn try_from(w: U256) -> Result { - Option::from(ScalarCore::new(w)).map(Self).ok_or(Error) + Option::from(ScalarPrimitive::new(w)).map(Self).ok_or(Error) } } @@ -194,7 +194,7 @@ impl From for U256 { impl ConditionallySelectable for Scalar { fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self { - Self(ScalarCore::conditional_select(&a.0, &b.0, choice)) + Self(ScalarPrimitive::conditional_select(&a.0, &b.0, choice)) } } @@ -327,7 +327,7 @@ impl Reduce for Scalar { let (r, underflow) = w.sbb(&MockCurve::ORDER, Limb::ZERO); let underflow = Choice::from((underflow.0 >> (Limb::BITS - 1)) as u8); let reduced = U256::conditional_select(&w, &r, !underflow); - Self(ScalarCore::new(reduced).unwrap()) + Self(ScalarPrimitive::new(reduced).unwrap()) } } @@ -337,8 +337,8 @@ impl From for Scalar { } } -impl From for Scalar { - fn from(scalar: ScalarCore) -> Scalar { +impl From for Scalar { + fn from(scalar: ScalarPrimitive) -> Scalar { Self(scalar) } } diff --git a/elliptic-curve/src/lib.rs b/elliptic-curve/src/lib.rs index cbc96c467..507345596 100644 --- a/elliptic-curve/src/lib.rs +++ b/elliptic-curve/src/lib.rs @@ -49,7 +49,7 @@ //! //! - [`JwkEcKey`] //! - [`PublicKey`] -//! - [`ScalarCore`] +//! - [`ScalarPrimitive`] //! //! Please see type-specific documentation for more information. //! @@ -100,7 +100,7 @@ pub use crate::{ AffineXCoordinate, AffineYIsOdd, DecompactPoint, DecompressPoint, PointCompaction, PointCompression, }, - scalar::{IsHigh, ScalarCore}, + scalar::{IsHigh, ScalarPrimitive}, secret_key::SecretKey, }; pub use crypto_bigint as bigint; diff --git a/elliptic-curve/src/scalar.rs b/elliptic-curve/src/scalar.rs index 0a93b257a..1d0c77677 100644 --- a/elliptic-curve/src/scalar.rs +++ b/elliptic-curve/src/scalar.rs @@ -1,12 +1,12 @@ //! Scalar types. -mod core; #[cfg(feature = "arithmetic")] mod invert; #[cfg(feature = "arithmetic")] mod nonzero; +mod primitive; -pub use self::core::ScalarCore; +pub use self::primitive::ScalarPrimitive; #[cfg(feature = "arithmetic")] pub use self::{invert::invert_vartime, nonzero::NonZeroScalar}; diff --git a/elliptic-curve/src/scalar/nonzero.rs b/elliptic-curve/src/scalar/nonzero.rs index 311d2a465..ecfdb9ede 100644 --- a/elliptic-curve/src/scalar/nonzero.rs +++ b/elliptic-curve/src/scalar/nonzero.rs @@ -3,7 +3,7 @@ use crate::{ ops::{Invert, Reduce, ReduceNonZero}, rand_core::{CryptoRng, RngCore}, - CurveArithmetic, Error, FieldBytes, IsHigh, PrimeCurve, Scalar, ScalarCore, SecretKey, + CurveArithmetic, Error, FieldBytes, IsHigh, PrimeCurve, Scalar, ScalarPrimitive, SecretKey, }; use base16ct::HexDisplay; use core::{ @@ -64,7 +64,7 @@ where /// Create a [`NonZeroScalar`] from a `C::Uint`. pub fn from_uint(uint: C::Uint) -> CtOption { - ScalarCore::new(uint).and_then(|scalar| Self::new(scalar.into())) + ScalarPrimitive::new(uint).and_then(|scalar| Self::new(scalar.into())) } } @@ -128,21 +128,21 @@ where } } -impl From> for ScalarCore +impl From> for ScalarPrimitive where C: CurveArithmetic, { - fn from(scalar: NonZeroScalar) -> ScalarCore { - ScalarCore::from_be_bytes(scalar.to_repr()).unwrap() + fn from(scalar: NonZeroScalar) -> ScalarPrimitive { + ScalarPrimitive::from_be_bytes(scalar.to_repr()).unwrap() } } -impl From<&NonZeroScalar> for ScalarCore +impl From<&NonZeroScalar> for ScalarPrimitive where C: CurveArithmetic, { - fn from(scalar: &NonZeroScalar) -> ScalarCore { - ScalarCore::from_be_bytes(scalar.to_repr()).unwrap() + fn from(scalar: &NonZeroScalar) -> ScalarPrimitive { + ScalarPrimitive::from_be_bytes(scalar.to_repr()).unwrap() } } @@ -339,7 +339,7 @@ where where S: ser::Serializer, { - ScalarCore::from(self).serialize(serializer) + ScalarPrimitive::from(self).serialize(serializer) } } @@ -352,7 +352,8 @@ where where D: de::Deserializer<'de>, { - Option::from(Self::new(ScalarCore::deserialize(deserializer)?.into())) + let scalar = ScalarPrimitive::deserialize(deserializer)?; + Option::from(Self::new(scalar.into())) .ok_or_else(|| de::Error::custom("expected non-zero scalar")) } } diff --git a/elliptic-curve/src/scalar/core.rs b/elliptic-curve/src/scalar/primitive.rs similarity index 74% rename from elliptic-curve/src/scalar/core.rs rename to elliptic-curve/src/scalar/primitive.rs index 472e8376f..31637380e 100644 --- a/elliptic-curve/src/scalar/core.rs +++ b/elliptic-curve/src/scalar/primitive.rs @@ -1,12 +1,7 @@ -//! Generic scalar type with core functionality. +//! Generic scalar type with primitive functionality. use crate::{ bigint::{prelude::*, Limb, NonZero}, - rand_core::{CryptoRng, RngCore}, - subtle::{ - Choice, ConditionallySelectable, ConstantTimeEq, ConstantTimeGreater, ConstantTimeLess, - CtOption, - }, Curve, Error, FieldBytes, IsHigh, Result, }; use base16ct::HexDisplay; @@ -17,6 +12,11 @@ use core::{ str, }; use generic_array::GenericArray; +use rand_core::{CryptoRng, RngCore}; +use subtle::{ + Choice, ConditionallySelectable, ConstantTimeEq, ConstantTimeGreater, ConstantTimeLess, + CtOption, +}; use zeroize::DefaultIsZeroes; #[cfg(feature = "arithmetic")] @@ -28,7 +28,7 @@ use { #[cfg(feature = "serde")] use serdect::serde::{de, ser, Deserialize, Serialize}; -/// Generic scalar type with core functionality. +/// Generic scalar type with primitive functionality. /// /// This type provides a baseline level of scalar arithmetic functionality /// which is always available for all curves, regardless of if they implement @@ -41,14 +41,14 @@ use serdect::serde::{de, ser, Deserialize, Serialize}; /// /// The serialization is a fixed-width big endian encoding. When used with /// textual formats, the binary data is encoded as hexadecimal. -// TODO(tarcieri): make this a fully generic `Scalar` type and use it for `CurveArithmetic` +// TODO(tarcieri): use `crypto-bigint`'s `Residue` type, expose more functionality? #[derive(Copy, Clone, Debug, Default)] -pub struct ScalarCore { +pub struct ScalarPrimitive { /// Inner unsigned integer type. inner: C::Uint, } -impl ScalarCore +impl ScalarPrimitive where C: Curve, { @@ -65,7 +65,7 @@ where /// Scalar modulus. pub const MODULUS: C::Uint = C::ORDER; - /// Generate a random [`ScalarCore`]. + /// Generate a random [`ScalarPrimitive`]. pub fn random(rng: impl CryptoRng + RngCore) -> Self { Self { inner: C::Uint::random_mod(rng, &NonZero::new(Self::MODULUS).unwrap()), @@ -77,12 +77,12 @@ where CtOption::new(Self { inner: uint }, uint.ct_lt(&Self::MODULUS)) } - /// Decode [`ScalarCore`] from big endian bytes. + /// Decode [`ScalarPrimitive`] from big endian bytes. pub fn from_be_bytes(bytes: FieldBytes) -> CtOption { Self::new(C::Uint::from_be_byte_array(bytes)) } - /// Decode [`ScalarCore`] from a big endian byte slice. + /// Decode [`ScalarPrimitive`] from a big endian byte slice. pub fn from_be_slice(slice: &[u8]) -> Result { if slice.len() == C::Uint::BYTES { Option::from(Self::from_be_bytes(GenericArray::clone_from_slice(slice))).ok_or(Error) @@ -91,12 +91,12 @@ where } } - /// Decode [`ScalarCore`] from little endian bytes. + /// Decode [`ScalarPrimitive`] from little endian bytes. pub fn from_le_bytes(bytes: FieldBytes) -> CtOption { Self::new(C::Uint::from_le_byte_array(bytes)) } - /// Decode [`ScalarCore`] from a little endian byte slice. + /// Decode [`ScalarPrimitive`] from a little endian byte slice. pub fn from_le_slice(slice: &[u8]) -> Result { if slice.len() == C::Uint::BYTES { Option::from(Self::from_le_bytes(GenericArray::clone_from_slice(slice))).ok_or(Error) @@ -115,46 +115,46 @@ where self.inner.as_ref() } - /// Is this [`ScalarCore`] value equal to zero? + /// Is this [`ScalarPrimitive`] value equal to zero? pub fn is_zero(&self) -> Choice { self.inner.is_zero() } - /// Is this [`ScalarCore`] value even? + /// Is this [`ScalarPrimitive`] value even? pub fn is_even(&self) -> Choice { self.inner.is_even() } - /// Is this [`ScalarCore`] value odd? + /// Is this [`ScalarPrimitive`] value odd? pub fn is_odd(&self) -> Choice { self.inner.is_odd() } - /// Encode [`ScalarCore`] as big endian bytes. + /// Encode [`ScalarPrimitive`] as big endian bytes. pub fn to_be_bytes(self) -> FieldBytes { self.inner.to_be_byte_array() } - /// Encode [`ScalarCore`] as little endian bytes. + /// Encode [`ScalarPrimitive`] as little endian bytes. pub fn to_le_bytes(self) -> FieldBytes { self.inner.to_le_byte_array() } } #[cfg(feature = "arithmetic")] -impl ScalarCore +impl ScalarPrimitive where C: CurveArithmetic, { - /// Convert [`ScalarCore`] into a given curve's scalar type - // TODO(tarcieri): replace curve-specific scalars with `ScalarCore` + /// Convert [`ScalarPrimitive`] into a given curve's scalar type + // TODO(tarcieri): replace curve-specific scalars with `ScalarPrimitive` pub(super) fn to_scalar(self) -> Scalar { Scalar::::from_repr(self.to_be_bytes()).unwrap() } } // TODO(tarcieri): better encapsulate this? -impl AsRef<[Limb]> for ScalarCore +impl AsRef<[Limb]> for ScalarPrimitive where C: Curve, { @@ -163,7 +163,7 @@ where } } -impl ConditionallySelectable for ScalarCore +impl ConditionallySelectable for ScalarPrimitive where C: Curve, { @@ -174,7 +174,7 @@ where } } -impl ConstantTimeEq for ScalarCore +impl ConstantTimeEq for ScalarPrimitive where C: Curve, { @@ -183,7 +183,7 @@ where } } -impl ConstantTimeLess for ScalarCore +impl ConstantTimeLess for ScalarPrimitive where C: Curve, { @@ -192,7 +192,7 @@ where } } -impl ConstantTimeGreater for ScalarCore +impl ConstantTimeGreater for ScalarPrimitive where C: Curve, { @@ -201,11 +201,11 @@ where } } -impl DefaultIsZeroes for ScalarCore {} +impl DefaultIsZeroes for ScalarPrimitive {} -impl Eq for ScalarCore {} +impl Eq for ScalarPrimitive {} -impl PartialEq for ScalarCore +impl PartialEq for ScalarPrimitive where C: Curve, { @@ -214,7 +214,7 @@ where } } -impl PartialOrd for ScalarCore +impl PartialOrd for ScalarPrimitive where C: Curve, { @@ -223,7 +223,7 @@ where } } -impl Ord for ScalarCore +impl Ord for ScalarPrimitive where C: Curve, { @@ -232,7 +232,7 @@ where } } -impl From for ScalarCore +impl From for ScalarPrimitive where C: Curve, { @@ -243,7 +243,7 @@ where } } -impl Add> for ScalarCore +impl Add> for ScalarPrimitive where C: Curve, { @@ -254,7 +254,7 @@ where } } -impl Add<&ScalarCore> for ScalarCore +impl Add<&ScalarPrimitive> for ScalarPrimitive where C: Curve, { @@ -267,7 +267,7 @@ where } } -impl AddAssign> for ScalarCore +impl AddAssign> for ScalarPrimitive where C: Curve, { @@ -276,7 +276,7 @@ where } } -impl AddAssign<&ScalarCore> for ScalarCore +impl AddAssign<&ScalarPrimitive> for ScalarPrimitive where C: Curve, { @@ -285,7 +285,7 @@ where } } -impl Sub> for ScalarCore +impl Sub> for ScalarPrimitive where C: Curve, { @@ -296,7 +296,7 @@ where } } -impl Sub<&ScalarCore> for ScalarCore +impl Sub<&ScalarPrimitive> for ScalarPrimitive where C: Curve, { @@ -309,7 +309,7 @@ where } } -impl SubAssign> for ScalarCore +impl SubAssign> for ScalarPrimitive where C: Curve, { @@ -318,7 +318,7 @@ where } } -impl SubAssign<&ScalarCore> for ScalarCore +impl SubAssign<&ScalarPrimitive> for ScalarPrimitive where C: Curve, { @@ -327,7 +327,7 @@ where } } -impl Neg for ScalarCore +impl Neg for ScalarPrimitive where C: Curve, { @@ -340,18 +340,18 @@ where } } -impl Neg for &ScalarCore +impl Neg for &ScalarPrimitive where C: Curve, { - type Output = ScalarCore; + type Output = ScalarPrimitive; - fn neg(self) -> ScalarCore { + fn neg(self) -> ScalarPrimitive { -*self } } -impl IsHigh for ScalarCore +impl IsHigh for ScalarPrimitive where C: Curve, { @@ -361,7 +361,7 @@ where } } -impl fmt::Display for ScalarCore +impl fmt::Display for ScalarPrimitive where C: Curve, { @@ -370,7 +370,7 @@ where } } -impl fmt::LowerHex for ScalarCore +impl fmt::LowerHex for ScalarPrimitive where C: Curve, { @@ -379,7 +379,7 @@ where } } -impl fmt::UpperHex for ScalarCore +impl fmt::UpperHex for ScalarPrimitive where C: Curve, { @@ -388,7 +388,7 @@ where } } -impl str::FromStr for ScalarCore +impl str::FromStr for ScalarPrimitive where C: Curve, { @@ -402,7 +402,7 @@ where } #[cfg(feature = "serde")] -impl Serialize for ScalarCore +impl Serialize for ScalarPrimitive where C: Curve, { @@ -415,7 +415,7 @@ where } #[cfg(feature = "serde")] -impl<'de, C> Deserialize<'de> for ScalarCore +impl<'de, C> Deserialize<'de> for ScalarPrimitive where C: Curve, { diff --git a/elliptic-curve/src/secret_key.rs b/elliptic-curve/src/secret_key.rs index 17189852a..73bf8e244 100644 --- a/elliptic-curve/src/secret_key.rs +++ b/elliptic-curve/src/secret_key.rs @@ -8,7 +8,7 @@ #[cfg(all(feature = "pkcs8", feature = "sec1"))] mod pkcs8; -use crate::{Curve, Error, FieldBytes, Result, ScalarCore}; +use crate::{Curve, Error, FieldBytes, Result, ScalarPrimitive}; use core::fmt::{self, Debug}; use crypto_bigint::Integer; use generic_array::GenericArray; @@ -88,7 +88,7 @@ pub(crate) const SEC1_PEM_TYPE_LABEL: &str = "EC PRIVATE KEY"; #[derive(Clone)] pub struct SecretKey { /// Scalar value - inner: ScalarCore, + inner: ScalarPrimitive, } impl SecretKey @@ -107,18 +107,18 @@ where } /// Create a new secret key from a scalar value. - pub fn new(scalar: ScalarCore) -> Self { + pub fn new(scalar: ScalarPrimitive) -> Self { Self { inner: scalar } } - /// Borrow the inner secret [`ScalarCore`] value. + /// Borrow the inner secret [`ScalarPrimitive`] value. /// /// # ⚠️ Warning /// /// This value is key material. /// /// Please treat it with the care it deserves! - pub fn as_scalar_core(&self) -> &ScalarCore { + pub fn as_scalar_core(&self) -> &ScalarPrimitive { &self.inner } @@ -152,7 +152,7 @@ where return Err(Error); } - let inner: ScalarCore = Option::from(ScalarCore::from_be_bytes( + let inner: ScalarPrimitive = Option::from(ScalarPrimitive::from_be_bytes( GenericArray::clone_from_slice(bytes), )) .ok_or(Error)?;