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
23 changes: 23 additions & 0 deletions elliptic-curve/src/encoding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//! Traits for decoding/encoding elliptic curve elements (i.e. base and scalar
//! field elements) as bytes.

use generic_array::{ArrayLength, GenericArray};
use subtle::{ConditionallySelectable, CtOption};

/// Try to decode the given bytes into a curve element
pub trait FromBytes: ConditionallySelectable + Sized {
/// Size of the serialized byte array
type Size: ArrayLength<u8>;

/// Try to decode this object from bytes
fn from_bytes(bytes: &GenericArray<u8, Self::Size>) -> CtOption<Self>;
}

/// Encode this curve element as bytes
pub trait ToBytes {
/// Size of the serialized byte array
type Size: ArrayLength<u8>;

/// Encode this object to bytes
fn to_bytes(&self) -> GenericArray<u8, Self::Size>;
}
3 changes: 2 additions & 1 deletion elliptic-curve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#[cfg(feature = "std")]
extern crate std;

pub mod encoding;
pub mod error;
pub mod ops;
pub mod point;
Expand Down Expand Up @@ -77,7 +78,7 @@ pub trait Arithmetic: Curve {
type Scalar: ConditionallySelectable
+ ConstantTimeEq
+ Default
+ secret_key::FromSecretKey<Self>;
+ encoding::FromBytes<Size = Self::ElementSize>;

/// Affine point type for a given curve
type AffinePoint: ConditionallySelectable + Mul<scalar::NonZeroScalar<Self>> + point::Generator;
Expand Down
9 changes: 0 additions & 9 deletions elliptic-curve/src/secret_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use core::{
fmt::{self, Debug},
};
use generic_array::{typenum::Unsigned, GenericArray};
use subtle::CtOption;

#[cfg(feature = "rand_core")]
use {
Expand Down Expand Up @@ -91,11 +90,3 @@ impl<C: Curve> Drop for SecretKey<C> {
self.scalar.zeroize();
}
}

/// Trait for deserializing a value from a secret key.
///
/// This is intended for use with the `Scalar` type for a given elliptic curve.
pub trait FromSecretKey<C: Curve>: Sized {
/// Deserialize this value from a [`SecretKey`]
fn from_secret_key(secret_key: &SecretKey<C>) -> CtOption<Self>;
}
2 changes: 1 addition & 1 deletion elliptic-curve/src/weierstrass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub mod public_key;

pub use self::{
point::{CompressedPoint, CompressedPointSize, UncompressedPoint, UncompressedPointSize},
public_key::{FromPublicKey, PublicKey},
public_key::PublicKey,
};

/// Marker trait for elliptic curves in short Weierstrass form
Expand Down
24 changes: 2 additions & 22 deletions elliptic-curve/src/weierstrass/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use super::{
Curve,
};
use crate::{
point::Generator, scalar::NonZeroScalar, secret_key::FromSecretKey, Arithmetic, Error,
SecretKey,
encoding::FromBytes, point::Generator, scalar::NonZeroScalar, Arithmetic, Error, SecretKey,
};
use core::{
fmt::{self, Debug},
Expand All @@ -17,7 +16,6 @@ use generic_array::{
typenum::{Unsigned, U1},
ArrayLength, GenericArray,
};
use subtle::CtOption;

/// Size of an untagged point for given elliptic curve.
pub type UntaggedPointSize<C> = <<C as crate::Curve>::ElementSize as Add>::Output;
Expand Down Expand Up @@ -133,7 +131,7 @@ where
///
/// The `compress` flag requests point compression.
pub fn from_secret_key(secret_key: &SecretKey<C>, compress: bool) -> Result<Self, Error> {
let ct_option = C::Scalar::from_secret_key(&secret_key).and_then(NonZeroScalar::new);
let ct_option = C::Scalar::from_bytes(secret_key.as_bytes()).and_then(NonZeroScalar::new);

if ct_option.is_none().into() {
return Err(Error);
Expand Down Expand Up @@ -208,21 +206,3 @@ where
PublicKey::Uncompressed(point)
}
}

/// Trait for deserializing a value from a public key.
///
/// This is intended for use with the `AffinePoint` type for a given elliptic curve.
pub trait FromPublicKey<C: Curve>: Sized
where
C::ElementSize: Add<U1>,
<C::ElementSize as Add>::Output: Add<U1>,
CompressedPointSize<C>: ArrayLength<u8>,
UncompressedPointSize<C>: ArrayLength<u8>,
{
/// Deserialize this value from a [`PublicKey`]
///
/// # Returns
///
/// `None` if the public key is not on the curve.
fn from_public_key(public_key: &PublicKey<C>) -> CtOption<Self>;
}