Skip to content

Commit

Permalink
elliptic-curve: source FieldSize from Curve::UInt type
Browse files Browse the repository at this point in the history
The `crypto-bigint` library defines an associated `ArrayLength<u8>` 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<C>` type
alias which can be used anywhere `C::FieldSize` was previously used
which sources the `ArrayLength<u8>` from `C::UInt` instead.
  • Loading branch information
tarcieri committed Jun 2, 2021
1 parent a7bd3d2 commit 1ae4c70
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions elliptic-curve/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use crate::{
bigint::{ArrayEncoding, U256},
consts::U32,
error::{Error, Result},
rand_core::RngCore,
sec1::{FromEncodedPoint, ToEncodedPoint},
Expand Down Expand Up @@ -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 =
Expand Down
16 changes: 6 additions & 10 deletions elliptic-curve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<u8> + Add + Eq + Ord + Unsigned;
}

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

/// Byte representation of a base/scalar field element of a given curve.
pub type FieldBytes<C> = GenericArray<u8, <C as Curve>::FieldSize>;
pub type FieldBytes<C> = GenericArray<u8, FieldSize<C>>;

/// Associate an [`ObjectIdentifier`][`pkcs8::ObjectIdentifier`] (OID) with an
/// elliptic curve algorithm implementation.
Expand Down
6 changes: 3 additions & 3 deletions elliptic-curve/src/scalar/bytes.rs
Original file line number Diff line number Diff line change
@@ -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")]
Expand Down Expand Up @@ -218,7 +218,7 @@ where
type Error = Error;

fn try_from(bytes: &[u8]) -> Result<Self> {
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)
Expand Down
5 changes: 3 additions & 2 deletions elliptic-curve/src/scalar/non_zero.rs
Original file line number Diff line number Diff line change
@@ -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")]
Expand Down Expand Up @@ -134,7 +135,7 @@ where
type Error = Error;

fn try_from(bytes: &[u8]) -> Result<Self> {
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)
Expand Down
24 changes: 10 additions & 14 deletions elliptic-curve/src/sec1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@
//!
//! <https://www.secg.org/sec1-v2.pdf>

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")]
Expand All @@ -39,15 +36,15 @@ 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<C> = <<C as crate::Curve>::FieldSize as Add<U1>>::Output;
pub type CompressedPointSize<C> = <FieldSize<C> as Add<U1>>::Output;

/// Size of an uncompressed point for the given elliptic curve when encoded
/// using the SEC1 `Elliptic-Curve-Point-to-Octet-String` algorithm
/// (including leading `0x04` tag byte).
pub type UncompressedPointSize<C> = <UntaggedPointSize<C> as Add<U1>>::Output;

/// Size of an untagged point for given elliptic curve.
pub type UntaggedPointSize<C> = <<C as crate::Curve>::FieldSize as Add>::Output;
pub type UntaggedPointSize<C> = <FieldSize<C> as Add>::Output;

/// SEC1 encoded curve point.
///
Expand Down Expand Up @@ -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);
Expand All @@ -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<u8, UntaggedPointSize<C>>) -> 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)
}

Expand All @@ -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 }
Expand Down Expand Up @@ -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`].
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions elliptic-curve/src/secret_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -105,7 +104,7 @@ where
pub fn from_bytes(bytes: impl AsRef<[u8]>) -> Result<Self> {
let bytes = bytes.as_ref();

if bytes.len() != C::FieldSize::to_usize() {
if bytes.len() != C::UInt::NUM_BYTES {
return Err(Error);
}

Expand Down

0 comments on commit 1ae4c70

Please sign in to comment.