From 1ae4c706a865f6f02264c58099287c6ee3381b61 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Wed, 2 Jun 2021 13:24:24 -0700 Subject: [PATCH] elliptic-curve: source FieldSize from Curve::UInt type The `crypto-bigint` library defines an associated `ArrayLength` for every `UInt` type as part of the `ArrayEncoding` trait. This means we don't need to define both: we can now source what was previously `C::FieldSize` via `C::UInt::ByteSize`. This commit performs that replacement, adding a `FieldSize` type alias which can be used anywhere `C::FieldSize` was previously used which sources the `ArrayLength` from `C::UInt` instead. --- Cargo.lock | 2 +- elliptic-curve/src/dev.rs | 2 -- elliptic-curve/src/lib.rs | 16 ++++++---------- elliptic-curve/src/scalar/bytes.rs | 6 +++--- elliptic-curve/src/scalar/non_zero.rs | 5 +++-- elliptic-curve/src/sec1.rs | 24 ++++++++++-------------- elliptic-curve/src/secret_key.rs | 5 ++--- 7 files changed, 25 insertions(+), 35 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 13d8a9e37..337f4c40c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -181,7 +181,7 @@ dependencies = [ [[package]] name = "crypto-bigint" version = "0.1.0" -source = "git+https://github.com/rustcrypto/utils.git#01c945e05d86cd36b7222f0a8628cf51410eebc7" +source = "git+https://github.com/rustcrypto/utils.git#4ff54f79d5c478720d5e6297e53208365aabbf07" dependencies = [ "generic-array", "subtle", diff --git a/elliptic-curve/src/dev.rs b/elliptic-curve/src/dev.rs index 3605c7cd9..313c3f69c 100644 --- a/elliptic-curve/src/dev.rs +++ b/elliptic-curve/src/dev.rs @@ -3,7 +3,6 @@ use crate::{ bigint::{ArrayEncoding, U256}, - consts::U32, error::{Error, Result}, rand_core::RngCore, sec1::{FromEncodedPoint, ToEncodedPoint}, @@ -39,7 +38,6 @@ pub const PSEUDO_COORDINATE_FIXED_BASE_MUL: [u8; 32] = pub struct MockCurve; impl Curve for MockCurve { - type FieldSize = U32; type UInt = U256; const ORDER: U256 = diff --git a/elliptic-curve/src/lib.rs b/elliptic-curve/src/lib.rs index 407905f7f..1717b4704 100644 --- a/elliptic-curve/src/lib.rs +++ b/elliptic-curve/src/lib.rs @@ -89,8 +89,8 @@ pub use secret_key::SecretKey; #[cfg(feature = "zeroize")] pub use zeroize; -use core::{fmt::Debug, ops::Add}; -use generic_array::{typenum::Unsigned, ArrayLength, GenericArray}; +use core::fmt::Debug; +use generic_array::GenericArray; /// Algorithm [`ObjectIdentifier`][`pkcs8::ObjectIdentifier`] for elliptic /// curve public key cryptography. @@ -118,17 +118,13 @@ pub trait Curve: Clone + Debug + Default + Eq + Ord + Send + Sync { /// 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; - - /// Size of this curve's field in *bytes*, i.e. the number of bytes needed - /// to serialize a field element. - /// - /// This is used for computing the sizes of field element types related to - /// this curve and other types composed from them (e.g. signatures). - type FieldSize: ArrayLength + Add + Eq + Ord + Unsigned; } +/// Size of field elements of this elliptic curve. +pub type FieldSize = <::UInt as ArrayEncoding>::ByteSize; + /// Byte representation of a base/scalar field element of a given curve. -pub type FieldBytes = GenericArray::FieldSize>; +pub type FieldBytes = GenericArray>; /// Associate an [`ObjectIdentifier`][`pkcs8::ObjectIdentifier`] (OID) with an /// elliptic curve algorithm implementation. diff --git a/elliptic-curve/src/scalar/bytes.rs b/elliptic-curve/src/scalar/bytes.rs index 7544a4936..87d23b699 100644 --- a/elliptic-curve/src/scalar/bytes.rs +++ b/elliptic-curve/src/scalar/bytes.rs @@ -1,11 +1,11 @@ //! Scalar bytes. -use crate::{Curve, Error, FieldBytes, Result}; +use crate::{bigint::NumBytes, Curve, Error, FieldBytes, Result}; use core::{ convert::{TryFrom, TryInto}, mem, }; -use generic_array::{typenum::Unsigned, GenericArray}; +use generic_array::GenericArray; use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption}; #[cfg(feature = "arithmetic")] @@ -218,7 +218,7 @@ where type Error = Error; fn try_from(bytes: &[u8]) -> Result { - if bytes.len() == C::FieldSize::to_usize() { + if bytes.len() == C::UInt::NUM_BYTES { Option::from(ScalarBytes::new(GenericArray::clone_from_slice(bytes))).ok_or(Error) } else { Err(Error) diff --git a/elliptic-curve/src/scalar/non_zero.rs b/elliptic-curve/src/scalar/non_zero.rs index df0327ef2..ad76e86b8 100644 --- a/elliptic-curve/src/scalar/non_zero.rs +++ b/elliptic-curve/src/scalar/non_zero.rs @@ -1,13 +1,14 @@ //! Non-zero scalar type. use crate::{ + bigint::NumBytes, ops::Invert, rand_core::{CryptoRng, RngCore}, Curve, Error, FieldBytes, ProjectiveArithmetic, Result, Scalar, }; use core::{convert::TryFrom, ops::Deref}; use ff::{Field, PrimeField}; -use generic_array::{typenum::Unsigned, GenericArray}; +use generic_array::GenericArray; use subtle::{Choice, ConditionallySelectable, CtOption}; #[cfg(feature = "zeroize")] @@ -134,7 +135,7 @@ where type Error = Error; fn try_from(bytes: &[u8]) -> Result { - if bytes.len() == C::FieldSize::to_usize() { + if bytes.len() == C::UInt::NUM_BYTES { NonZeroScalar::from_repr(GenericArray::clone_from_slice(bytes)).ok_or(Error) } else { Err(Error) diff --git a/elliptic-curve/src/sec1.rs b/elliptic-curve/src/sec1.rs index c71bb5da8..3e67a5b3c 100644 --- a/elliptic-curve/src/sec1.rs +++ b/elliptic-curve/src/sec1.rs @@ -5,15 +5,12 @@ //! //! -use crate::{weierstrass::Curve, Error, FieldBytes, Result}; +use crate::{bigint::NumBytes, weierstrass::Curve, Error, FieldBytes, FieldSize, Result}; use core::{ fmt::{self, Debug}, ops::Add, }; -use generic_array::{ - typenum::{Unsigned, U1}, - ArrayLength, GenericArray, -}; +use generic_array::{typenum::U1, ArrayLength, GenericArray}; use subtle::{Choice, ConditionallySelectable}; #[cfg(feature = "alloc")] @@ -39,7 +36,7 @@ use crate::{ /// Size of a compressed point for the given elliptic curve when encoded /// using the SEC1 `Elliptic-Curve-Point-to-Octet-String` algorithm /// (including leading `0x02` or `0x03` tag byte). -pub type CompressedPointSize = <::FieldSize as Add>::Output; +pub type CompressedPointSize = as Add>::Output; /// Size of an uncompressed point for the given elliptic curve when encoded /// using the SEC1 `Elliptic-Curve-Point-to-Octet-String` algorithm @@ -47,7 +44,7 @@ pub type CompressedPointSize = <::FieldSize as Add>::O pub type UncompressedPointSize = as Add>::Output; /// Size of an untagged point for given elliptic curve. -pub type UntaggedPointSize = <::FieldSize as Add>::Output; +pub type UntaggedPointSize = as Add>::Output; /// SEC1 encoded curve point. /// @@ -84,7 +81,7 @@ where let tag = input.first().cloned().ok_or(Error).and_then(Tag::from_u8)?; // Validate length - let expected_len = tag.message_len(C::FieldSize::to_usize()); + let expected_len = tag.message_len(C::UInt::NUM_BYTES); if input.len() != expected_len { return Err(Error); @@ -99,7 +96,7 @@ where /// encoded as the concatenated `x || y` coordinates with no leading SEC1 /// tag byte (which would otherwise be `0x04` for an uncompressed point). pub fn from_untagged_bytes(bytes: &GenericArray>) -> Self { - let (x, y) = bytes.split_at(C::FieldSize::to_usize()); + let (x, y) = bytes.split_at(C::UInt::NUM_BYTES); Self::from_affine_coordinates(x.into(), y.into(), false) } @@ -115,11 +112,10 @@ where let mut bytes = GenericArray::default(); bytes[0] = tag.into(); - let element_size = C::FieldSize::to_usize(); - bytes[1..(element_size + 1)].copy_from_slice(x); + bytes[1..(C::UInt::NUM_BYTES + 1)].copy_from_slice(x); if !compress { - bytes[(element_size + 1)..].copy_from_slice(y); + bytes[(C::UInt::NUM_BYTES + 1)..].copy_from_slice(y); } Self { bytes } @@ -151,7 +147,7 @@ where /// Get the length of the encoded point in bytes pub fn len(&self) -> usize { - self.tag().message_len(C::FieldSize::to_usize()) + self.tag().message_len(C::UInt::NUM_BYTES) } /// Get byte slice containing the serialized [`EncodedPoint`]. @@ -250,7 +246,7 @@ where return Coordinates::Identity; } - let (x, y) = self.bytes[1..].split_at(C::FieldSize::to_usize()); + let (x, y) = self.bytes[1..].split_at(C::UInt::NUM_BYTES); if self.is_compressed() { Coordinates::Compressed { diff --git a/elliptic-curve/src/secret_key.rs b/elliptic-curve/src/secret_key.rs index 604fda9de..cad09836b 100644 --- a/elliptic-curve/src/secret_key.rs +++ b/elliptic-curve/src/secret_key.rs @@ -10,13 +10,12 @@ #[cfg(feature = "pkcs8")] mod pkcs8; -use crate::{Curve, Error, FieldBytes, Result}; +use crate::{bigint::NumBytes, Curve, Error, FieldBytes, Result}; use core::{ convert::TryFrom, fmt::{self, Debug}, ops::Deref, }; -use generic_array::typenum::Unsigned; use zeroize::Zeroize; #[cfg(feature = "arithmetic")] @@ -105,7 +104,7 @@ where pub fn from_bytes(bytes: impl AsRef<[u8]>) -> Result { let bytes = bytes.as_ref(); - if bytes.len() != C::FieldSize::to_usize() { + if bytes.len() != C::UInt::NUM_BYTES { return Err(Error); }