From 41910dbdca1d3afdb3d3bbcbf490a223dc103c69 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Fri, 12 Sep 2025 18:11:17 -0600 Subject: [PATCH] elliptic-curve: rename `ScalarPrimitive` => `ScalarValue` Denotes the type reperents an integer value in range of the scalar modulus. Ideally we could get rid of this type if there were a suitable replacement type somewhere else, e.g. RustCrypto/elliptic-curves#1192 --- elliptic-curve/src/arithmetic.rs | 6 +- elliptic-curve/src/dev.rs | 29 +++--- elliptic-curve/src/lib.rs | 4 +- elliptic-curve/src/macros.rs | 20 ++-- elliptic-curve/src/scalar.rs | 4 +- elliptic-curve/src/scalar/nonzero.rs | 20 ++-- .../src/scalar/{primitive.rs => value.rs} | 95 +++++++++---------- elliptic-curve/src/secret_key.rs | 12 +-- 8 files changed, 94 insertions(+), 96 deletions(-) rename elliptic-curve/src/scalar/{primitive.rs => value.rs} (74%) diff --git a/elliptic-curve/src/arithmetic.rs b/elliptic-curve/src/arithmetic.rs index e61ac45c8..ef0d66986 100644 --- a/elliptic-curve/src/arithmetic.rs +++ b/elliptic-curve/src/arithmetic.rs @@ -1,7 +1,7 @@ //! Elliptic curve arithmetic traits. use crate::{ - Curve, CurveGroup, Error, FieldBytes, Group, NonZeroScalar, PrimeCurve, ScalarPrimitive, + Curve, CurveGroup, Error, FieldBytes, Group, NonZeroScalar, PrimeCurve, ScalarValue, ops::{Invert, LinearCombination, Mul, Reduce}, point::{AffineCoordinates, NonIdentity}, scalar::{FromUintUnchecked, IsHigh}, @@ -68,10 +68,10 @@ pub trait CurveArithmetic: Curve { type Scalar: AsRef + DefaultIsZeroes + From> - + From> + + From> + FromUintUnchecked + Into> - + Into> + + Into> + Into + Invert> + IsHigh diff --git a/elliptic-curve/src/dev.rs b/elliptic-curve/src/dev.rs index f5c871a77..3039cf037 100644 --- a/elliptic-curve/src/dev.rs +++ b/elliptic-curve/src/dev.rs @@ -50,9 +50,8 @@ pub type PublicKey = crate::PublicKey; /// Secret key. pub type SecretKey = crate::SecretKey; -/// Scalar primitive type. -// TODO(tarcieri): make this the scalar type when it's more capable -pub type ScalarPrimitive = crate::ScalarPrimitive; +/// Scalar value type. +pub type ScalarValue = crate::ScalarValue; /// Scalar bits. #[cfg(feature = "bits")] @@ -90,11 +89,11 @@ impl AssociatedOid for MockCurve { /// Example scalar type #[derive(Clone, Copy, Debug, Default, Eq, PartialEq, PartialOrd, Ord)] -pub struct Scalar(ScalarPrimitive); +pub struct Scalar(ScalarValue); impl Field for Scalar { - const ZERO: Self = Self(ScalarPrimitive::ZERO); - const ONE: Self = Self(ScalarPrimitive::ONE); + const ZERO: Self = Self(ScalarValue::ZERO); + const ONE: Self = Self(ScalarValue::ONE); fn try_from_rng(rng: &mut R) -> core::result::Result { let mut bytes = FieldBytes::default(); @@ -147,7 +146,7 @@ impl PrimeField for Scalar { const DELTA: Self = Self::ZERO; // BOGUS! fn from_repr(bytes: FieldBytes) -> CtOption { - ScalarPrimitive::from_bytes(&bytes).map(Self) + ScalarValue::from_bytes(&bytes).map(Self) } fn to_repr(&self) -> FieldBytes { @@ -184,7 +183,7 @@ impl AsRef for Scalar { impl ConditionallySelectable for Scalar { fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self { - Self(ScalarPrimitive::conditional_select(&a.0, &b.0, choice)) + Self(ScalarValue::conditional_select(&a.0, &b.0, choice)) } } @@ -363,7 +362,7 @@ impl Reduce for Scalar { let (r, underflow) = w.borrowing_sub(&MockCurve::ORDER, Limb::ZERO); let underflow = Choice::from((underflow.0 >> (Limb::BITS - 1)) as u8); let reduced = U256::conditional_select(w, &r, !underflow); - Self(ScalarPrimitive::new(reduced).unwrap()) + Self(ScalarValue::new(reduced).unwrap()) } } @@ -387,14 +386,14 @@ impl From for Scalar { } } -impl From for Scalar { - fn from(scalar: ScalarPrimitive) -> Scalar { +impl From for Scalar { + fn from(scalar: ScalarValue) -> Scalar { Self(scalar) } } -impl From for ScalarPrimitive { - fn from(scalar: Scalar) -> ScalarPrimitive { +impl From for ScalarValue { + fn from(scalar: Scalar) -> ScalarValue { scalar.0 } } @@ -417,7 +416,7 @@ impl TryFrom for Scalar { type Error = Error; fn try_from(w: U256) -> Result { - Option::from(ScalarPrimitive::new(w)).map(Self).ok_or(Error) + ScalarValue::new(w).into_option().map(Self).ok_or(Error) } } @@ -425,7 +424,7 @@ impl FromUintUnchecked for Scalar { type Uint = U256; fn from_uint_unchecked(uint: U256) -> Self { - Self(ScalarPrimitive::from_uint_unchecked(uint)) + Self(ScalarValue::from_uint_unchecked(uint)) } } diff --git a/elliptic-curve/src/lib.rs b/elliptic-curve/src/lib.rs index d4b723069..f8fc396a1 100644 --- a/elliptic-curve/src/lib.rs +++ b/elliptic-curve/src/lib.rs @@ -62,7 +62,7 @@ //! `Deserialize` impls are provided for the following types: //! //! - [`PublicKey`] -//! - [`ScalarPrimitive`] +//! - [`ScalarValue`] //! //! Please see type-specific documentation for more information. //! @@ -108,7 +108,7 @@ mod public_key; pub use crate::{ error::{Error, Result}, field::{FieldBytes, FieldBytesEncoding, FieldBytesSize}, - scalar::ScalarPrimitive, + scalar::ScalarValue, secret_key::SecretKey, }; pub use crypto_bigint as bigint; diff --git a/elliptic-curve/src/macros.rs b/elliptic-curve/src/macros.rs index 9e69df31b..871ac5ffc 100644 --- a/elliptic-curve/src/macros.rs +++ b/elliptic-curve/src/macros.rs @@ -25,27 +25,27 @@ macro_rules! scalar_from_impls { } } - impl From<$crate::ScalarPrimitive<$curve>> for $scalar { - fn from(w: $crate::ScalarPrimitive<$curve>) -> Self { + impl From<$crate::ScalarValue<$curve>> for $scalar { + fn from(w: $crate::ScalarValue<$curve>) -> Self { <$scalar>::from(&w) } } - impl From<&$crate::ScalarPrimitive<$curve>> for $scalar { - fn from(w: &$crate::ScalarPrimitive<$curve>) -> $scalar { + impl From<&$crate::ScalarValue<$curve>> for $scalar { + fn from(w: &$crate::ScalarValue<$curve>) -> $scalar { <$scalar>::from_uint_unchecked(*w.as_uint()) } } - impl From<$scalar> for $crate::ScalarPrimitive<$curve> { - fn from(scalar: $scalar) -> $crate::ScalarPrimitive<$curve> { - $crate::ScalarPrimitive::from(&scalar) + impl From<$scalar> for $crate::ScalarValue<$curve> { + fn from(scalar: $scalar) -> $crate::ScalarValue<$curve> { + $crate::ScalarValue::from(&scalar) } } - impl From<&$scalar> for $crate::ScalarPrimitive<$curve> { - fn from(scalar: &$scalar) -> $crate::ScalarPrimitive<$curve> { - $crate::ScalarPrimitive::new(scalar.into()).unwrap() + impl From<&$scalar> for $crate::ScalarValue<$curve> { + fn from(scalar: &$scalar) -> $crate::ScalarValue<$curve> { + $crate::ScalarValue::new(scalar.into()).unwrap() } } diff --git a/elliptic-curve/src/scalar.rs b/elliptic-curve/src/scalar.rs index 9f3abdad7..8b4aa17d6 100644 --- a/elliptic-curve/src/scalar.rs +++ b/elliptic-curve/src/scalar.rs @@ -4,9 +4,9 @@ mod blinded; #[cfg(feature = "arithmetic")] mod nonzero; -mod primitive; +mod value; -pub use self::primitive::ScalarPrimitive; +pub use self::value::ScalarValue; #[cfg(feature = "arithmetic")] pub use self::{blinded::BlindedScalar, nonzero::NonZeroScalar}; diff --git a/elliptic-curve/src/scalar/nonzero.rs b/elliptic-curve/src/scalar/nonzero.rs index fd9e5315c..cc2a75daf 100644 --- a/elliptic-curve/src/scalar/nonzero.rs +++ b/elliptic-curve/src/scalar/nonzero.rs @@ -1,7 +1,7 @@ //! Non-zero scalar type. use crate::{ - CurveArithmetic, Error, FieldBytes, PrimeCurve, Scalar, ScalarPrimitive, SecretKey, + CurveArithmetic, Error, FieldBytes, PrimeCurve, Scalar, ScalarValue, SecretKey, ops::{self, BatchInvert, Invert, Reduce, ReduceNonZero}, point::NonIdentity, scalar::IsHigh, @@ -86,7 +86,7 @@ where /// Create a [`NonZeroScalar`] from a `C::Uint`. pub fn from_uint(uint: C::Uint) -> CtOption { - ScalarPrimitive::new(uint).and_then(|scalar| Self::new(scalar.into())) + ScalarValue::new(uint).and_then(|scalar| Self::new(scalar.into())) } /// Transform array reference containing [`NonZeroScalar`]s to an array reference to the inner @@ -214,22 +214,22 @@ where } } -impl From> for ScalarPrimitive +impl From> for ScalarValue where C: CurveArithmetic, { #[inline] - fn from(scalar: NonZeroScalar) -> ScalarPrimitive { + fn from(scalar: NonZeroScalar) -> ScalarValue { Self::from(&scalar) } } -impl From<&NonZeroScalar> for ScalarPrimitive +impl From<&NonZeroScalar> for ScalarValue where C: CurveArithmetic, { - fn from(scalar: &NonZeroScalar) -> ScalarPrimitive { - ScalarPrimitive::from_bytes(&scalar.to_repr()).unwrap() + fn from(scalar: &NonZeroScalar) -> ScalarValue { + ScalarValue::from_bytes(&scalar.to_repr()).unwrap() } } @@ -247,7 +247,7 @@ where C: CurveArithmetic, { fn from(sk: &SecretKey) -> NonZeroScalar { - let scalar = sk.as_scalar_primitive().to_scalar(); + let scalar = sk.as_scalar_value().to_scalar(); debug_assert!(!bool::from(scalar.is_zero())); Self { scalar } } @@ -492,7 +492,7 @@ where where S: ser::Serializer, { - ScalarPrimitive::from(self).serialize(serializer) + ScalarValue::from(self).serialize(serializer) } } @@ -505,7 +505,7 @@ where where D: de::Deserializer<'de>, { - let scalar = ScalarPrimitive::deserialize(deserializer)?; + let scalar = ScalarValue::deserialize(deserializer)?; Self::new(scalar.into()) .into_option() .ok_or_else(|| de::Error::custom("expected non-zero scalar")) diff --git a/elliptic-curve/src/scalar/primitive.rs b/elliptic-curve/src/scalar/value.rs similarity index 74% rename from elliptic-curve/src/scalar/primitive.rs rename to elliptic-curve/src/scalar/value.rs index 92d00bd87..6cdf5f97b 100644 --- a/elliptic-curve/src/scalar/primitive.rs +++ b/elliptic-curve/src/scalar/value.rs @@ -1,4 +1,4 @@ -//! Generic scalar type with primitive functionality. +//! Integer values within the range of a given [`Curve`]'s scalar modulus. use crate::{ Curve, Error, FieldBytes, FieldBytesEncoding, Result, @@ -27,11 +27,10 @@ use super::{CurveArithmetic, Scalar}; #[cfg(feature = "serde")] use serdect::serde::{Deserialize, Serialize, de, ser}; -/// Generic scalar type with primitive functionality. +/// Integer values within the range of a given [`Curve`]'s scalar modulus. /// /// This type provides a baseline level of scalar arithmetic functionality -/// which is always available for all curves, regardless of if they implement -/// any arithmetic traits. +/// which is always available for all curves. /// /// # `serde` support /// @@ -40,14 +39,14 @@ use serdect::serde::{Deserialize, Serialize, de, ser}; /// /// The serialization is a fixed-width big endian encoding. When used with /// textual formats, the binary data is encoded as hexadecimal. -// TODO(tarcieri): use `crypto-bigint`'s `Residue` type, expose more functionality? +// TODO(tarcieri): replace with `primefield`? RustCrypto/elliptic-curves#1192 #[derive(Copy, Clone, Debug, Default)] -pub struct ScalarPrimitive { +pub struct ScalarValue { /// Inner unsigned integer type. inner: C::Uint, } -impl ScalarPrimitive +impl ScalarValue where C: Curve, { @@ -64,7 +63,7 @@ where /// Scalar modulus. pub const MODULUS: Odd = C::ORDER; - /// Generate a random [`ScalarPrimitive`]. + /// Generate a random [`ScalarValue`]. pub fn random(rng: &mut R) -> Self { Self { inner: C::Uint::random_mod(rng, Self::MODULUS.as_nz_ref()), @@ -76,12 +75,12 @@ where CtOption::new(Self { inner: uint }, uint.ct_lt(&Self::MODULUS)) } - /// Decode [`ScalarPrimitive`] from a serialized field element + /// Decode [`ScalarValue`] from a serialized field element pub fn from_bytes(bytes: &FieldBytes) -> CtOption { Self::new(C::Uint::decode_field_bytes(bytes)) } - /// Decode [`ScalarPrimitive`] from a big endian byte slice. + /// Decode [`ScalarValue`] from a big endian byte slice. pub fn from_slice(slice: &[u8]) -> Result { let bytes = Array::try_from(slice).map_err(|_| Error)?; Self::from_bytes(&bytes).into_option().ok_or(Error) @@ -97,22 +96,22 @@ where self.inner.as_ref() } - /// Is this [`ScalarPrimitive`] value equal to zero? + /// Is this [`ScalarValue`] value equal to zero? pub fn is_zero(&self) -> Choice { self.inner.is_zero() } - /// Is this [`ScalarPrimitive`] value even? + /// Is this [`ScalarValue`] value even? pub fn is_even(&self) -> Choice { self.inner.is_even() } - /// Is this [`ScalarPrimitive`] value odd? + /// Is this [`ScalarValue`] value odd? pub fn is_odd(&self) -> Choice { self.inner.is_odd() } - /// Encode [`ScalarPrimitive`] as a serialized field element. + /// Encode [`ScalarValue`] as a serialized field element. pub fn to_bytes(&self) -> FieldBytes { self.inner.encode_field_bytes() } @@ -123,7 +122,7 @@ where } } -impl FromUintUnchecked for ScalarPrimitive +impl FromUintUnchecked for ScalarValue where C: Curve, { @@ -135,18 +134,18 @@ where } #[cfg(feature = "arithmetic")] -impl ScalarPrimitive +impl ScalarValue where C: CurveArithmetic, { - /// Convert [`ScalarPrimitive`] into a given curve's scalar type. + /// Convert [`ScalarValue`] into a given curve's scalar type. pub(super) fn to_scalar(self) -> Scalar { Scalar::::from_uint_unchecked(self.inner) } } // TODO(tarcieri): better encapsulate this? -impl AsRef<[Limb]> for ScalarPrimitive +impl AsRef<[Limb]> for ScalarValue where C: Curve, { @@ -155,7 +154,7 @@ where } } -impl ConditionallySelectable for ScalarPrimitive +impl ConditionallySelectable for ScalarValue where C: Curve, { @@ -166,7 +165,7 @@ where } } -impl ConstantTimeEq for ScalarPrimitive +impl ConstantTimeEq for ScalarValue where C: Curve, { @@ -175,7 +174,7 @@ where } } -impl ConstantTimeLess for ScalarPrimitive +impl ConstantTimeLess for ScalarValue where C: Curve, { @@ -184,7 +183,7 @@ where } } -impl ConstantTimeGreater for ScalarPrimitive +impl ConstantTimeGreater for ScalarValue where C: Curve, { @@ -193,11 +192,11 @@ where } } -impl DefaultIsZeroes for ScalarPrimitive {} +impl DefaultIsZeroes for ScalarValue {} -impl Eq for ScalarPrimitive {} +impl Eq for ScalarValue {} -impl PartialEq for ScalarPrimitive +impl PartialEq for ScalarValue where C: Curve, { @@ -206,7 +205,7 @@ where } } -impl PartialOrd for ScalarPrimitive +impl PartialOrd for ScalarValue where C: Curve, { @@ -215,7 +214,7 @@ where } } -impl Ord for ScalarPrimitive +impl Ord for ScalarValue where C: Curve, { @@ -224,7 +223,7 @@ where } } -impl From for ScalarPrimitive +impl From for ScalarValue where C: Curve, { @@ -235,7 +234,7 @@ where } } -impl Add> for ScalarPrimitive +impl Add> for ScalarValue where C: Curve, { @@ -246,7 +245,7 @@ where } } -impl Add<&ScalarPrimitive> for ScalarPrimitive +impl Add<&ScalarValue> for ScalarValue where C: Curve, { @@ -259,7 +258,7 @@ where } } -impl AddAssign> for ScalarPrimitive +impl AddAssign> for ScalarValue where C: Curve, { @@ -268,7 +267,7 @@ where } } -impl AddAssign<&ScalarPrimitive> for ScalarPrimitive +impl AddAssign<&ScalarValue> for ScalarValue where C: Curve, { @@ -277,7 +276,7 @@ where } } -impl Sub> for ScalarPrimitive +impl Sub> for ScalarValue where C: Curve, { @@ -288,7 +287,7 @@ where } } -impl Sub<&ScalarPrimitive> for ScalarPrimitive +impl Sub<&ScalarValue> for ScalarValue where C: Curve, { @@ -301,7 +300,7 @@ where } } -impl SubAssign> for ScalarPrimitive +impl SubAssign> for ScalarValue where C: Curve, { @@ -310,7 +309,7 @@ where } } -impl SubAssign<&ScalarPrimitive> for ScalarPrimitive +impl SubAssign<&ScalarValue> for ScalarValue where C: Curve, { @@ -319,7 +318,7 @@ where } } -impl Neg for ScalarPrimitive +impl Neg for ScalarValue where C: Curve, { @@ -332,18 +331,18 @@ where } } -impl Neg for &ScalarPrimitive +impl Neg for &ScalarValue where C: Curve, { - type Output = ScalarPrimitive; + type Output = ScalarValue; - fn neg(self) -> ScalarPrimitive { + fn neg(self) -> ScalarValue { -*self } } -impl ShrAssign for ScalarPrimitive +impl ShrAssign for ScalarValue where C: Curve, { @@ -352,7 +351,7 @@ where } } -impl IsHigh for ScalarPrimitive +impl IsHigh for ScalarValue where C: Curve, { @@ -362,7 +361,7 @@ where } } -impl fmt::Display for ScalarPrimitive +impl fmt::Display for ScalarValue where C: Curve, { @@ -371,7 +370,7 @@ where } } -impl fmt::LowerHex for ScalarPrimitive +impl fmt::LowerHex for ScalarValue where C: Curve, { @@ -380,7 +379,7 @@ where } } -impl fmt::UpperHex for ScalarPrimitive +impl fmt::UpperHex for ScalarValue where C: Curve, { @@ -389,7 +388,7 @@ where } } -impl str::FromStr for ScalarPrimitive +impl str::FromStr for ScalarValue where C: Curve, { @@ -403,7 +402,7 @@ where } #[cfg(feature = "serde")] -impl Serialize for ScalarPrimitive +impl Serialize for ScalarValue where C: Curve, { @@ -416,7 +415,7 @@ where } #[cfg(feature = "serde")] -impl<'de, C> Deserialize<'de> for ScalarPrimitive +impl<'de, C> Deserialize<'de> for ScalarValue where C: Curve, { diff --git a/elliptic-curve/src/secret_key.rs b/elliptic-curve/src/secret_key.rs index dbc435605..bc2a10a6f 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, ScalarPrimitive}; +use crate::{Curve, Error, FieldBytes, Result, ScalarValue}; use core::fmt::{self, Debug}; use hybrid_array::typenum::Unsigned; use subtle::{Choice, ConstantTimeEq, CtOption}; @@ -74,7 +74,7 @@ use {crate::pkcs8::DecodePrivateKey, core::str::FromStr}; #[derive(Clone)] pub struct SecretKey { /// Scalar value - inner: ScalarPrimitive, + inner: ScalarValue, } impl SecretKey @@ -115,19 +115,19 @@ where /// # Returns /// /// This will return a none if the scalar is all-zero. - pub fn from_scalar(scalar: impl Into>) -> CtOption { + pub fn from_scalar(scalar: impl Into>) -> CtOption { let inner = scalar.into(); CtOption::new(Self { inner }, !inner.is_zero()) } - /// Borrow the inner secret [`ScalarPrimitive`] value. + /// Borrow the inner secret [`ScalarValue`] value. /// /// # ⚠️ Warning /// /// This value is key material. /// /// Please treat it with the care it deserves! - pub fn as_scalar_primitive(&self) -> &ScalarPrimitive { + pub fn as_scalar_value(&self) -> &ScalarValue { &self.inner } @@ -157,7 +157,7 @@ where /// Deserialize secret key from an encoded secret scalar. pub fn from_bytes(bytes: &FieldBytes) -> Result { - let inner = ScalarPrimitive::::from_bytes(bytes) + let inner = ScalarValue::::from_bytes(bytes) .into_option() .ok_or(Error)?;