Skip to content
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
2 changes: 1 addition & 1 deletion elliptic-curve/src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub trait ScalarArithmetic: Curve {
type Scalar: DefaultIsZeroes
+ From<ScalarCore<Self>>
+ Into<FieldBytes<Self>>
+ Into<Self::UInt>
+ Into<Self::Uint>
+ IsHigh
+ ff::Field
+ ff::PrimeField<Repr = FieldBytes<Self>>;
Expand Down
2 changes: 1 addition & 1 deletion elliptic-curve/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub type ScalarBits = crate::ScalarBits<MockCurve>;
pub struct MockCurve;

impl Curve for MockCurve {
type UInt = U256;
type Uint = U256;

const ORDER: U256 =
U256::from_be_hex("ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551");
Expand Down
12 changes: 6 additions & 6 deletions elliptic-curve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,30 +159,30 @@ pub const ALGORITHM_OID: pkcs8::ObjectIdentifier =
/// curves (e.g. [`SecretKey`]).
pub trait Curve: 'static + Copy + Clone + Debug + Default + Eq + Ord + Send + Sync {
/// Integer type used to represent field elements of this elliptic curve.
// TODO(tarcieri): replace this with an e.g. `const Curve::MODULUS: UInt`.
// TODO(tarcieri): replace this with an e.g. `const Curve::MODULUS: Uint`.
// Requires rust-lang/rust#60551, i.e. `const_evaluatable_checked`
type UInt: bigint::AddMod<Output = Self::UInt>
type Uint: bigint::AddMod<Output = Self::Uint>
+ bigint::ArrayEncoding
+ bigint::Encoding
+ bigint::Integer
+ bigint::NegMod<Output = Self::UInt>
+ bigint::NegMod<Output = Self::Uint>
+ bigint::Random
+ bigint::RandomMod
+ bigint::SubMod<Output = Self::UInt>
+ bigint::SubMod<Output = Self::Uint>
+ zeroize::Zeroize;

/// Order constant.
///
/// Subdivided into either 32-bit or 64-bit "limbs" (depending on the
/// target CPU's word size), specified from least to most significant.
const ORDER: Self::UInt;
const ORDER: Self::Uint;
}

/// Marker trait for elliptic curves with prime order.
pub trait PrimeCurve: Curve {}

/// Size of field elements of this elliptic curve.
pub type FieldSize<C> = <<C as Curve>::UInt as bigint::ArrayEncoding>::ByteSize;
pub type FieldSize<C> = <<C as Curve>::Uint as bigint::ArrayEncoding>::ByteSize;

/// Byte representation of a base/scalar field element of a given curve.
pub type FieldBytes<C> = GenericArray<u8, FieldSize<C>>;
Expand Down
20 changes: 10 additions & 10 deletions elliptic-curve/src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,20 @@ pub trait LinearCombination: Group {
}

/// Modular reduction.
pub trait Reduce<UInt: Integer + ArrayEncoding>: Sized {
pub trait Reduce<Uint: Integer + ArrayEncoding>: Sized {
/// Perform a modular reduction, returning a field element.
fn from_uint_reduced(n: UInt) -> Self;
fn from_uint_reduced(n: Uint) -> Self;

/// Interpret the given byte array as a big endian integer and perform
/// a modular reduction.
fn from_be_bytes_reduced(bytes: ByteArray<UInt>) -> Self {
Self::from_uint_reduced(UInt::from_be_byte_array(bytes))
fn from_be_bytes_reduced(bytes: ByteArray<Uint>) -> Self {
Self::from_uint_reduced(Uint::from_be_byte_array(bytes))
}

/// Interpret the given byte array as a little endian integer and perform a
/// modular reduction.
fn from_le_bytes_reduced(bytes: ByteArray<UInt>) -> Self {
Self::from_uint_reduced(UInt::from_le_byte_array(bytes))
fn from_le_bytes_reduced(bytes: ByteArray<Uint>) -> Self {
Self::from_uint_reduced(Uint::from_le_byte_array(bytes))
}

/// Interpret a digest as a big endian integer and perform a modular
Expand All @@ -66,7 +66,7 @@ pub trait Reduce<UInt: Integer + ArrayEncoding>: Sized {
#[cfg_attr(docsrs, doc(cfg(feature = "digest")))]
fn from_be_digest_reduced<D>(digest: D) -> Self
where
D: FixedOutput<OutputSize = UInt::ByteSize>,
D: FixedOutput<OutputSize = Uint::ByteSize>,
{
Self::from_be_bytes_reduced(digest.finalize_fixed())
}
Expand All @@ -77,7 +77,7 @@ pub trait Reduce<UInt: Integer + ArrayEncoding>: Sized {
#[cfg_attr(docsrs, doc(cfg(feature = "digest")))]
fn from_le_digest_reduced<D>(digest: D) -> Self
where
D: FixedOutput<OutputSize = UInt::ByteSize>,
D: FixedOutput<OutputSize = Uint::ByteSize>,
{
Self::from_le_bytes_reduced(digest.finalize_fixed())
}
Expand All @@ -90,7 +90,7 @@ pub trait Reduce<UInt: Integer + ArrayEncoding>: Sized {
///
/// End users should use the [`Reduce`] impl on
/// [`NonZeroScalar`][`crate::NonZeroScalar`] instead.
pub trait ReduceNonZero<UInt: Integer + ArrayEncoding>: Sized {
pub trait ReduceNonZero<Uint: Integer + ArrayEncoding>: Sized {
/// Perform a modular reduction, returning a field element.
fn from_uint_reduced_nonzero(n: UInt) -> Self;
fn from_uint_reduced_nonzero(n: Uint) -> Self;
}
30 changes: 15 additions & 15 deletions elliptic-curve/src/scalar/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use serdect::serde::{de, ser, Deserialize, Serialize};
#[cfg_attr(docsrs, doc(cfg(feature = "arithmetic")))]
pub struct ScalarCore<C: Curve> {
/// Inner unsigned integer type.
inner: C::UInt,
inner: C::Uint,
}

impl<C> ScalarCore<C>
Expand All @@ -55,37 +55,37 @@ where
{
/// Zero scalar.
pub const ZERO: Self = Self {
inner: C::UInt::ZERO,
inner: C::Uint::ZERO,
};

/// Multiplicative identity.
pub const ONE: Self = Self {
inner: C::UInt::ONE,
inner: C::Uint::ONE,
};

/// Scalar modulus.
pub const MODULUS: C::UInt = C::ORDER;
pub const MODULUS: C::Uint = C::ORDER;

/// Generate a random [`ScalarCore`].
pub fn random(rng: impl CryptoRng + RngCore) -> Self {
Self {
inner: C::UInt::random_mod(rng, &NonZero::new(Self::MODULUS).unwrap()),
inner: C::Uint::random_mod(rng, &NonZero::new(Self::MODULUS).unwrap()),
}
}

/// Create a new scalar from [`Curve::UInt`].
pub fn new(uint: C::UInt) -> CtOption<Self> {
/// Create a new scalar from [`Curve::Uint`].
pub fn new(uint: C::Uint) -> CtOption<Self> {
CtOption::new(Self { inner: uint }, uint.ct_lt(&Self::MODULUS))
}

/// Decode [`ScalarCore`] from big endian bytes.
pub fn from_be_bytes(bytes: FieldBytes<C>) -> CtOption<Self> {
Self::new(C::UInt::from_be_byte_array(bytes))
Self::new(C::Uint::from_be_byte_array(bytes))
}

/// Decode [`ScalarCore`] from a big endian byte slice.
pub fn from_be_slice(slice: &[u8]) -> Result<Self> {
if slice.len() == C::UInt::BYTES {
if slice.len() == C::Uint::BYTES {
Option::from(Self::from_be_bytes(GenericArray::clone_from_slice(slice))).ok_or(Error)
} else {
Err(Error)
Expand All @@ -94,20 +94,20 @@ where

/// Decode [`ScalarCore`] from little endian bytes.
pub fn from_le_bytes(bytes: FieldBytes<C>) -> CtOption<Self> {
Self::new(C::UInt::from_le_byte_array(bytes))
Self::new(C::Uint::from_le_byte_array(bytes))
}

/// Decode [`ScalarCore`] from a little endian byte slice.
pub fn from_le_slice(slice: &[u8]) -> Result<Self> {
if slice.len() == C::UInt::BYTES {
if slice.len() == C::Uint::BYTES {
Option::from(Self::from_le_bytes(GenericArray::clone_from_slice(slice))).ok_or(Error)
} else {
Err(Error)
}
}

/// Borrow the inner `C::UInt`.
pub fn as_uint(&self) -> &C::UInt {
/// Borrow the inner `C::Uint`.
pub fn as_uint(&self) -> &C::Uint {
&self.inner
}

Expand Down Expand Up @@ -170,7 +170,7 @@ where
{
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
Self {
inner: C::UInt::conditional_select(&a.inner, &b.inner, choice),
inner: C::Uint::conditional_select(&a.inner, &b.inner, choice),
}
}
}
Expand Down Expand Up @@ -239,7 +239,7 @@ where
{
fn from(n: u64) -> Self {
Self {
inner: C::UInt::from(n),
inner: C::Uint::from(n),
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions elliptic-curve/src/scalar/nonzero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ where
Scalar::<C>::from_repr(repr).and_then(Self::new)
}

/// Create a [`NonZeroScalar`] from a `C::UInt`.
pub fn from_uint(uint: C::UInt) -> CtOption<Self> {
/// Create a [`NonZeroScalar`] from a `C::Uint`.
pub fn from_uint(uint: C::Uint) -> CtOption<Self> {
ScalarCore::new(uint).and_then(|scalar| Self::new(scalar.into()))
}
}
Expand Down Expand Up @@ -262,7 +262,7 @@ where
type Error = Error;

fn try_from(bytes: &[u8]) -> Result<Self, Error> {
if bytes.len() == C::UInt::BYTES {
if bytes.len() == C::Uint::BYTES {
Option::from(NonZeroScalar::from_repr(GenericArray::clone_from_slice(
bytes,
)))
Expand Down
2 changes: 1 addition & 1 deletion elliptic-curve/src/secret_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ where

/// Deserialize raw secret scalar as a big endian integer.
pub fn from_be_bytes(bytes: &[u8]) -> Result<Self> {
if bytes.len() != C::UInt::BYTES {
if bytes.len() != C::Uint::BYTES {
return Err(Error);
}

Expand Down