Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename Curve::ElementSize => FieldSize #282

Merged
merged 1 commit into from
Sep 2, 2020
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
15 changes: 8 additions & 7 deletions elliptic-curve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ use subtle::{ConditionallySelectable, ConstantTimeEq, CtOption};
use rand_core::{CryptoRng, RngCore};

/// Byte array containing a serialized scalar value (i.e. an integer)
pub type ElementBytes<C> = GenericArray<u8, <C as Curve>::ElementSize>;
pub type ElementBytes<C> = GenericArray<u8, <C as Curve>::FieldSize>;

/// Elliptic curve.
///
Expand All @@ -82,11 +82,12 @@ pub type ElementBytes<C> = GenericArray<u8, <C as Curve>::ElementSize>;
/// be impl'd by these ZSTs, facilitating types which are generic over elliptic
/// curves (e.g. [`SecretKey`]).
pub trait Curve: Clone + Debug + Default + Eq + Ord + Send + Sync {
/// Number of bytes required to serialize elements of field elements
/// associated with this curve, e.g. elements of the base/scalar fields.
/// 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 for types related to this curve.
type ElementSize: ArrayLength<u8> + Add + Eq + Ord + Unsigned;
/// 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;
}

/// Elliptic curve with curve arithmetic support
Expand All @@ -95,7 +96,7 @@ pub trait Arithmetic: Curve {
type Scalar: ConditionallySelectable
+ ConstantTimeEq
+ Default
+ FromBytes<Size = Self::ElementSize>
+ FromBytes<Size = Self::FieldSize>
+ Into<ElementBytes<Self>>;

/// Affine point type for a given curve
Expand All @@ -121,7 +122,7 @@ pub trait FromDigest<C: Curve> {
/// Instantiate this type from a [`Digest`] instance
fn from_digest<D>(digest: D) -> Self
where
D: Digest<OutputSize = C::ElementSize>;
D: Digest<OutputSize = C::FieldSize>;
}

/// Randomly generate a value.
Expand Down
2 changes: 1 addition & 1 deletion elliptic-curve/src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl<C> FromBytes for NonZeroScalar<C>
where
C: Curve + Arithmetic,
{
type Size = C::ElementSize;
type Size = C::FieldSize;

fn from_bytes(bytes: &ElementBytes<C>) -> CtOption<Self> {
C::Scalar::from_bytes(bytes).and_then(Self::new)
Expand Down
16 changes: 8 additions & 8 deletions elliptic-curve/src/sec1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ use zeroize::Zeroize;
/// 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>::ElementSize as Add<U1>>::Output;
pub type CompressedPointSize<C> = <<C as crate::Curve>::FieldSize 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>::ElementSize as Add>::Output;
pub type UntaggedPointSize<C> = <<C as crate::Curve>::FieldSize as Add>::Output;

/// SEC1 encoded curve point.
///
Expand Down Expand Up @@ -75,7 +75,7 @@ where
let tag = input.first().cloned().ok_or(Error).and_then(Tag::from_u8)?;

// Validate length
let expected_len = tag.message_len(C::ElementSize::to_usize());
let expected_len = tag.message_len(C::FieldSize::to_usize());

if input.len() != expected_len {
return Err(Error);
Expand All @@ -90,7 +90,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::ElementSize::to_usize());
let (x, y) = bytes.split_at(C::FieldSize::to_usize());
Self::from_affine_coords(x.into(), y.into(), false)
}

Expand All @@ -106,7 +106,7 @@ where
let mut bytes = GenericArray::default();
bytes[0] = tag.into();

let element_size = C::ElementSize::to_usize();
let element_size = C::FieldSize::to_usize();
bytes[1..(element_size + 1)].copy_from_slice(x);

if !compress {
Expand Down Expand Up @@ -137,7 +137,7 @@ where

/// Get the length of the encoded point in bytes
pub fn len(&self) -> usize {
self.tag().message_len(C::ElementSize::to_usize())
self.tag().message_len(C::FieldSize::to_usize())
}

/// Get byte slice containing the serialized [`EncodedPoint`].
Expand Down Expand Up @@ -217,7 +217,7 @@ where
/// Get the coordinates for this [`EncodedPoint`] as a pair
#[inline]
fn coordinates(&self) -> (&ElementBytes<C>, Option<&ElementBytes<C>>) {
let (x, y) = self.bytes[1..].split_at(C::ElementSize::to_usize());
let (x, y) = self.bytes[1..].split_at(C::FieldSize::to_usize());

if self.is_compressed() {
(x.into(), None)
Expand Down Expand Up @@ -377,7 +377,7 @@ mod tests {
struct ExampleCurve;

impl Curve for ExampleCurve {
type ElementSize = U32;
type FieldSize = U32;
}

impl weierstrass::Curve for ExampleCurve {
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 @@ -52,7 +52,7 @@ impl<C: Curve> TryFrom<&[u8]> for SecretKey<C> {
type Error = Error;

fn try_from(slice: &[u8]) -> Result<Self, Error> {
if slice.len() == C::ElementSize::to_usize() {
if slice.len() == C::FieldSize::to_usize() {
Ok(SecretKey {
scalar: GenericArray::clone_from_slice(slice),
})
Expand Down