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

ecdsa: bound curve implementations on Order trait #280

Merged
merged 1 commit into from
Apr 21, 2021
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
6 changes: 3 additions & 3 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion ecdsa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ keywords = ["crypto", "ecc", "nist", "secp256k1", "signature"]

[dependencies]
der = { version = "0.3", optional = true, features = ["big-uint"] }
elliptic-curve = { version = "0.9", default-features = false }
elliptic-curve = { version = "0.9.8", default-features = false }
hmac = { version = "0.10", optional = true, default-features = false }
signature = { version = ">= 1.3.0, < 1.4.0", default-features = false, features = ["rand-preview"] }

Expand Down
13 changes: 7 additions & 6 deletions ecdsa/src/der.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use elliptic_curve::{
consts::U9,
generic_array::{typenum::NonZero, ArrayLength, GenericArray},
weierstrass::Curve,
Order,
};

#[cfg(feature = "alloc")]
Expand Down Expand Up @@ -50,7 +51,7 @@ const ENCODING_ERR_MSG: &str = "DER encoding error";
/// Generic over the scalar size of the elliptic curve.
pub struct Signature<C>
where
C: Curve,
C: Curve + Order,
C::FieldSize: Add + ArrayLength<u8> + NonZero,
MaxSize<C>: ArrayLength<u8>,
<C::FieldSize as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
Expand All @@ -67,7 +68,7 @@ where

impl<C> signature::Signature for Signature<C>
where
C: Curve,
C: Curve + Order,
C::FieldSize: Add + ArrayLength<u8> + NonZero,
MaxSize<C>: ArrayLength<u8>,
<C::FieldSize as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
Expand All @@ -81,7 +82,7 @@ where
#[allow(clippy::len_without_is_empty)]
impl<C> Signature<C>
where
C: Curve,
C: Curve + Order,
C::FieldSize: Add + ArrayLength<u8> + NonZero,
MaxSize<C>: ArrayLength<u8>,
<C::FieldSize as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
Expand Down Expand Up @@ -132,7 +133,7 @@ where

impl<C> AsRef<[u8]> for Signature<C>
where
C: Curve,
C: Curve + Order,
C::FieldSize: Add + ArrayLength<u8> + NonZero,
MaxSize<C>: ArrayLength<u8>,
<C::FieldSize as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
Expand All @@ -144,7 +145,7 @@ where

impl<C> fmt::Debug for Signature<C>
where
C: Curve,
C: Curve + Order,
C::FieldSize: Add + ArrayLength<u8> + NonZero,
MaxSize<C>: ArrayLength<u8>,
<C::FieldSize as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
Expand All @@ -159,7 +160,7 @@ where

impl<C> TryFrom<&[u8]> for Signature<C>
where
C: Curve,
C: Curve + Order,
C::FieldSize: Add + ArrayLength<u8> + NonZero,
MaxSize<C>: ArrayLength<u8>,
<C::FieldSize as Add>::Output: Add<MaxOverhead> + ArrayLength<u8>,
Expand Down
14 changes: 8 additions & 6 deletions ecdsa/src/hazmat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
use {
crate::SignatureSize,
core::borrow::Borrow,
elliptic_curve::{ff::PrimeField, ops::Invert, FieldBytes, ProjectiveArithmetic, Scalar},
elliptic_curve::{
ff::PrimeField, ops::Invert, FieldBytes, Order, ProjectiveArithmetic, Scalar,
},
signature::Error,
};

Expand All @@ -40,7 +42,7 @@ use crate::{
#[cfg_attr(docsrs, doc(cfg(feature = "arithmetic")))]
pub trait SignPrimitive<C>
where
C: Curve + ProjectiveArithmetic,
C: Curve + Order + ProjectiveArithmetic,
Scalar<C>: PrimeField<Repr = FieldBytes<C>>,
SignatureSize<C>: ArrayLength<u8>,
{
Expand All @@ -64,7 +66,7 @@ where
#[cfg_attr(docsrs, doc(cfg(feature = "arithmetic")))]
pub trait RecoverableSignPrimitive<C>
where
C: Curve + ProjectiveArithmetic,
C: Curve + Order + ProjectiveArithmetic,
Scalar<C>: PrimeField<Repr = FieldBytes<C>>,
SignatureSize<C>: ArrayLength<u8>,
{
Expand All @@ -84,7 +86,7 @@ where
#[cfg(feature = "arithmetic")]
impl<C, T> SignPrimitive<C> for T
where
C: Curve + ProjectiveArithmetic,
C: Curve + Order + ProjectiveArithmetic,
T: RecoverableSignPrimitive<C>,
Scalar<C>: PrimeField<Repr = FieldBytes<C>>,
SignatureSize<C>: ArrayLength<u8>,
Expand All @@ -108,7 +110,7 @@ where
#[cfg_attr(docsrs, doc(cfg(feature = "arithmetic")))]
pub trait VerifyPrimitive<C>
where
C: Curve + ProjectiveArithmetic,
C: Curve + Order + ProjectiveArithmetic,
Scalar<C>: PrimeField<Repr = FieldBytes<C>>,
SignatureSize<C>: ArrayLength<u8>,
{
Expand Down Expand Up @@ -137,7 +139,7 @@ where
/// [1]: https://github.com/RustCrypto/traits/tree/master/signature/derive
#[cfg(feature = "digest")]
#[cfg_attr(docsrs, doc(cfg(feature = "digest")))]
pub trait DigestPrimitive: Curve {
pub trait DigestPrimitive: Curve + Order {
/// Preferred digest to use when computing ECDSA signatures for this
/// elliptic curve. This should be a member of the SHA-2 family.
type Digest: Digest;
Expand Down
20 changes: 10 additions & 10 deletions ecdsa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ use core::{
};
use elliptic_curve::{
generic_array::{sequence::Concat, typenum::Unsigned, ArrayLength, GenericArray},
FieldBytes,
FieldBytes, Order,
};

#[cfg(feature = "arithmetic")]
Expand Down Expand Up @@ -126,7 +126,7 @@ pub type SignatureBytes<C> = GenericArray<u8, SignatureSize<C>>;
/// ASN.1 DER-encoded signatures also supported via the
/// [`Signature::from_der`] and [`Signature::to_der`] methods.
#[derive(Clone, Eq, PartialEq)]
pub struct Signature<C: Curve + CheckSignatureBytes>
pub struct Signature<C: Curve + Order + CheckSignatureBytes>
where
SignatureSize<C>: ArrayLength<u8>,
{
Expand All @@ -135,7 +135,7 @@ where

impl<C> Signature<C>
where
C: Curve + CheckSignatureBytes,
C: Curve + Order + CheckSignatureBytes,
SignatureSize<C>: ArrayLength<u8>,
{
/// Create a [`Signature`] from the serialized `r` and `s` scalar values
Expand Down Expand Up @@ -177,7 +177,7 @@ where
#[cfg_attr(docsrs, doc(cfg(feature = "arithmetic")))]
impl<C> Signature<C>
where
C: Curve + ProjectiveArithmetic,
C: Curve + Order + ProjectiveArithmetic,
Scalar<C>: PrimeField<Repr = FieldBytes<C>>,
<Scalar<C> as PrimeField>::Repr: From<Scalar<C>> + for<'a> From<&'a Scalar<C>>,
SignatureSize<C>: ArrayLength<u8>,
Expand Down Expand Up @@ -221,7 +221,7 @@ where

impl<C> signature::Signature for Signature<C>
where
C: Curve + CheckSignatureBytes,
C: Curve + Order + CheckSignatureBytes,
SignatureSize<C>: ArrayLength<u8>,
{
fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
Expand All @@ -231,7 +231,7 @@ where

impl<C> AsRef<[u8]> for Signature<C>
where
C: Curve + CheckSignatureBytes,
C: Curve + Order + CheckSignatureBytes,
SignatureSize<C>: ArrayLength<u8>,
{
fn as_ref(&self) -> &[u8] {
Expand All @@ -241,15 +241,15 @@ where

impl<C> Copy for Signature<C>
where
C: Curve + CheckSignatureBytes,
C: Curve + Order + CheckSignatureBytes,
SignatureSize<C>: ArrayLength<u8>,
<SignatureSize<C> as ArrayLength<u8>>::ArrayType: Copy,
{
}

impl<C> Debug for Signature<C>
where
C: Curve + CheckSignatureBytes,
C: Curve + Order + CheckSignatureBytes,
SignatureSize<C>: ArrayLength<u8>,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand All @@ -264,7 +264,7 @@ where

impl<C> TryFrom<&[u8]> for Signature<C>
where
C: Curve + CheckSignatureBytes,
C: Curve + Order + CheckSignatureBytes,
SignatureSize<C>: ArrayLength<u8>,
{
type Error = Error;
Expand All @@ -285,7 +285,7 @@ where
#[cfg_attr(docsrs, doc(cfg(feature = "der")))]
impl<C> TryFrom<der::Signature<C>> for Signature<C>
where
C: Curve + CheckSignatureBytes,
C: Curve + Order + CheckSignatureBytes,
C::FieldSize: Add + ArrayLength<u8> + NonZero,
der::MaxSize<C>: ArrayLength<u8>,
<C::FieldSize as Add>::Output: Add<der::MaxOverhead> + ArrayLength<u8>,
Expand Down
26 changes: 13 additions & 13 deletions ecdsa/src/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use crate::{
};
use elliptic_curve::{
ff::PrimeField, generic_array::ArrayLength, ops::Invert, subtle::ConstantTimeEq,
weierstrass::Curve, zeroize::Zeroize, FieldBytes, NonZeroScalar, ProjectiveArithmetic, Scalar,
SecretKey,
weierstrass::Curve, zeroize::Zeroize, FieldBytes, NonZeroScalar, Order, ProjectiveArithmetic,
Scalar, SecretKey,
};
use signature::{
digest::{BlockInput, Digest, FixedOutput, Reset, Update},
Expand Down Expand Up @@ -43,7 +43,7 @@ use core::str::FromStr;
#[cfg_attr(docsrs, doc(cfg(feature = "sign")))]
pub struct SigningKey<C>
where
C: Curve + ProjectiveArithmetic,
C: Curve + Order + ProjectiveArithmetic,
Scalar<C>: PrimeField<Repr = FieldBytes<C>>
+ FromDigest<C>
+ Invert<Output = Scalar<C>>
Expand All @@ -56,7 +56,7 @@ where

impl<C> SigningKey<C>
where
C: Curve + ProjectiveArithmetic,
C: Curve + Order + ProjectiveArithmetic,
Scalar<C>: PrimeField<Repr = FieldBytes<C>>
+ FromDigest<C>
+ Invert<Output = Scalar<C>>
Expand Down Expand Up @@ -99,7 +99,7 @@ where

impl<C> From<SecretKey<C>> for SigningKey<C>
where
C: Curve + ProjectiveArithmetic,
C: Curve + Order + ProjectiveArithmetic,
Scalar<C>: PrimeField<Repr = FieldBytes<C>>
+ ConstantTimeEq
+ FromDigest<C>
Expand All @@ -115,7 +115,7 @@ where

impl<C, D> DigestSigner<D, Signature<C>> for SigningKey<C>
where
C: Curve + ProjectiveArithmetic,
C: Curve + Order + ProjectiveArithmetic,
D: FixedOutput<OutputSize = C::FieldSize> + BlockInput + Clone + Default + Reset + Update,
Scalar<C>: PrimeField<Repr = FieldBytes<C>>
+ FromDigest<C>
Expand All @@ -140,7 +140,7 @@ where
impl<C> signature::Signer<Signature<C>> for SigningKey<C>
where
Self: DigestSigner<C::Digest, Signature<C>>,
C: Curve + ProjectiveArithmetic + DigestPrimitive,
C: Curve + Order + ProjectiveArithmetic + DigestPrimitive,
Scalar<C>: PrimeField<Repr = FieldBytes<C>>
+ FromDigest<C>
+ Invert<Output = Scalar<C>>
Expand All @@ -155,7 +155,7 @@ where

impl<C, D> RandomizedDigestSigner<D, Signature<C>> for SigningKey<C>
where
C: Curve + ProjectiveArithmetic,
C: Curve + Order + ProjectiveArithmetic,
D: FixedOutput<OutputSize = C::FieldSize> + BlockInput + Clone + Default + Reset + Update,
Scalar<C>: PrimeField<Repr = FieldBytes<C>>
+ FromDigest<C>
Expand Down Expand Up @@ -187,7 +187,7 @@ where
impl<C> RandomizedSigner<Signature<C>> for SigningKey<C>
where
Self: RandomizedDigestSigner<C::Digest, Signature<C>>,
C: Curve + ProjectiveArithmetic + DigestPrimitive,
C: Curve + Order + ProjectiveArithmetic + DigestPrimitive,
Scalar<C>: PrimeField<Repr = FieldBytes<C>>
+ FromDigest<C>
+ Invert<Output = Scalar<C>>
Expand All @@ -206,7 +206,7 @@ where

impl<C> From<NonZeroScalar<C>> for SigningKey<C>
where
C: Curve + ProjectiveArithmetic,
C: Curve + Order + ProjectiveArithmetic,
Scalar<C>: PrimeField<Repr = FieldBytes<C>>
+ FromDigest<C>
+ Invert<Output = Scalar<C>>
Expand All @@ -224,7 +224,7 @@ where
#[cfg(feature = "verify")]
impl<C> From<&SigningKey<C>> for VerifyingKey<C>
where
C: Curve + ProjectiveArithmetic,
C: Curve + Order + ProjectiveArithmetic,
AffinePoint<C>: Copy + Clone + Debug + Default,
ProjectivePoint<C>: From<AffinePoint<C>>,
Scalar<C>: PrimeField<Repr = FieldBytes<C>>
Expand All @@ -243,7 +243,7 @@ where
#[cfg_attr(docsrs, doc(cfg(feature = "pkcs8")))]
impl<C> FromPrivateKey for SigningKey<C>
where
C: Curve + AlgorithmParameters + ProjectiveArithmetic,
C: Curve + AlgorithmParameters + Order + ProjectiveArithmetic,
AffinePoint<C>: Copy + Clone + Debug + Default + FromEncodedPoint<C> + ToEncodedPoint<C>,
ProjectivePoint<C>: From<AffinePoint<C>>,
Scalar<C>: PrimeField<Repr = FieldBytes<C>>
Expand All @@ -266,7 +266,7 @@ where
#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
impl<C> FromStr for SigningKey<C>
where
C: Curve + AlgorithmParameters + ProjectiveArithmetic,
C: Curve + AlgorithmParameters + Order + ProjectiveArithmetic,
AffinePoint<C>: Copy + Clone + Debug + Default + FromEncodedPoint<C> + ToEncodedPoint<C>,
ProjectivePoint<C>: From<AffinePoint<C>>,
Scalar<C>: PrimeField<Repr = FieldBytes<C>>
Expand Down
Loading