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
9 changes: 6 additions & 3 deletions elliptic-curve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ pub mod secret_key;
#[cfg_attr(docsrs, doc(cfg(feature = "weierstrass")))]
pub mod weierstrass;

pub use self::{error::Error, secret_key::SecretKey};
pub use self::{
error::Error,
secret_key::{FromSecretKey, SecretKey},
};
pub use generic_array::{self, typenum::consts};
pub use subtle;

Expand Down Expand Up @@ -62,13 +65,13 @@ pub trait Curve: Clone + Debug + Default + Eq + Ord + Send + Sync {
/// Elliptic curve with curve arithmetic support
pub trait Arithmetic: Curve {
/// Scalar type for a given curve
type Scalar;
type Scalar: FromSecretKey<Self>;

/// Affine point type for a given curve
type AffinePoint;
}

/// Trait for randomly generating a value.
/// Randomly generate a value.
///
/// Primarily intended for use with scalar types for a particular curve.
#[cfg(feature = "rand_core")]
Expand Down
9 changes: 9 additions & 0 deletions elliptic-curve/src/secret_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use core::{
fmt::{self, Debug},
};
use generic_array::{typenum::Unsigned, GenericArray};
use subtle::CtOption;

#[cfg(feature = "rand_core")]
use {
Expand Down Expand Up @@ -89,3 +90,11 @@ 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::PublicKey,
public_key::{FromPublicKey, PublicKey},
};

use crate::{Arithmetic, ScalarBytes};
Expand Down
19 changes: 19 additions & 0 deletions elliptic-curve/src/weierstrass/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ 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 @@ -192,3 +193,21 @@ 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>;
}