Skip to content

Commit

Permalink
ecdsa: use EcdsaCurve in bounds (#792)
Browse files Browse the repository at this point in the history
Replaces previous `PrimeCurve` bounds with the new `EcdsaCurve` trait
(which has a supertrait bound on `PrimeCurve`).
  • Loading branch information
tarcieri authored Jan 17, 2024
1 parent 56b8b7a commit c5206d4
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 146 deletions.
38 changes: 19 additions & 19 deletions ecdsa/src/der.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//!
//! [RFC5912 Section 6]: https://www.rfc-editor.org/rfc/rfc5912#section-6
use crate::{Error, Result};
use crate::{EcdsaCurve, Error, Result};
use core::{
fmt::{self, Debug},
ops::{Add, Range},
Expand All @@ -12,7 +12,7 @@ use der::{asn1::UintRef, Decode, Encode, FixedTag, Length, Reader, Tag, Writer};
use elliptic_curve::{
array::{typenum::Unsigned, Array, ArraySize},
consts::U9,
FieldBytesSize, PrimeCurve,
FieldBytesSize,
};

#[cfg(feature = "alloc")]
Expand Down Expand Up @@ -59,7 +59,7 @@ type SignatureBytes<C> = Array<u8, MaxSize<C>>;
/// [RFC5912 Section 6]: https://www.rfc-editor.org/rfc/rfc5912#section-6
pub struct Signature<C>
where
C: PrimeCurve,
C: EcdsaCurve,
MaxSize<C>: ArraySize,
<FieldBytesSize<C> as Add>::Output: Add<MaxOverhead> + ArraySize,
{
Expand All @@ -76,7 +76,7 @@ where
#[allow(clippy::len_without_is_empty)]
impl<C> Signature<C>
where
C: PrimeCurve,
C: EcdsaCurve,
MaxSize<C>: ArraySize,
<FieldBytesSize<C> as Add>::Output: Add<MaxOverhead> + ArraySize,
{
Expand Down Expand Up @@ -156,7 +156,7 @@ where

impl<C> AsRef<[u8]> for Signature<C>
where
C: PrimeCurve,
C: EcdsaCurve,
MaxSize<C>: ArraySize,
<FieldBytesSize<C> as Add>::Output: Add<MaxOverhead> + ArraySize,
{
Expand All @@ -167,7 +167,7 @@ where

impl<C> Clone for Signature<C>
where
C: PrimeCurve,
C: EcdsaCurve,
MaxSize<C>: ArraySize,
<FieldBytesSize<C> as Add>::Output: Add<MaxOverhead> + ArraySize,
{
Expand All @@ -182,7 +182,7 @@ where

impl<C> Debug for Signature<C>
where
C: PrimeCurve,
C: EcdsaCurve,
MaxSize<C>: ArraySize,
<FieldBytesSize<C> as Add>::Output: Add<MaxOverhead> + ArraySize,
{
Expand All @@ -199,7 +199,7 @@ where

impl<'a, C> Decode<'a> for Signature<C>
where
C: PrimeCurve,
C: EcdsaCurve,
MaxSize<C>: ArraySize,
<FieldBytesSize<C> as Add>::Output: Add<MaxOverhead> + ArraySize,
{
Expand All @@ -220,7 +220,7 @@ where

impl<C> Encode for Signature<C>
where
C: PrimeCurve,
C: EcdsaCurve,
MaxSize<C>: ArraySize,
<FieldBytesSize<C> as Add>::Output: Add<MaxOverhead> + ArraySize,
{
Expand All @@ -235,7 +235,7 @@ where

impl<C> FixedTag for Signature<C>
where
C: PrimeCurve,
C: EcdsaCurve,
MaxSize<C>: ArraySize,
<FieldBytesSize<C> as Add>::Output: Add<MaxOverhead> + ArraySize,
{
Expand All @@ -244,7 +244,7 @@ where

impl<C> From<crate::Signature<C>> for Signature<C>
where
C: PrimeCurve,
C: EcdsaCurve,
MaxSize<C>: ArraySize,
<FieldBytesSize<C> as Add>::Output: Add<MaxOverhead> + ArraySize,
{
Expand All @@ -255,7 +255,7 @@ where

impl<C> TryFrom<&[u8]> for Signature<C>
where
C: PrimeCurve,
C: EcdsaCurve,
MaxSize<C>: ArraySize,
<FieldBytesSize<C> as Add>::Output: Add<MaxOverhead> + ArraySize,
{
Expand All @@ -268,7 +268,7 @@ where

impl<C> TryFrom<Signature<C>> for crate::Signature<C>
where
C: PrimeCurve,
C: EcdsaCurve,
MaxSize<C>: ArraySize,
<FieldBytesSize<C> as Add>::Output: Add<MaxOverhead> + ArraySize,
{
Expand All @@ -287,7 +287,7 @@ where
#[cfg(feature = "alloc")]
impl<C> From<Signature<C>> for Box<[u8]>
where
C: PrimeCurve,
C: EcdsaCurve,
MaxSize<C>: ArraySize,
<FieldBytesSize<C> as Add>::Output: Add<MaxOverhead> + ArraySize,
{
Expand All @@ -299,7 +299,7 @@ where
#[cfg(feature = "alloc")]
impl<C> SignatureEncoding for Signature<C>
where
C: PrimeCurve,
C: EcdsaCurve,
MaxSize<C>: ArraySize,
<FieldBytesSize<C> as Add>::Output: Add<MaxOverhead> + ArraySize,
{
Expand All @@ -313,7 +313,7 @@ where
#[cfg(feature = "alloc")]
impl<C> SignatureBitStringEncoding for Signature<C>
where
C: PrimeCurve,
C: EcdsaCurve,
MaxSize<C>: ArraySize,
<FieldBytesSize<C> as Add>::Output: Add<MaxOverhead> + ArraySize,
{
Expand All @@ -325,7 +325,7 @@ where
#[cfg(feature = "serde")]
impl<C> Serialize for Signature<C>
where
C: PrimeCurve,
C: EcdsaCurve,
MaxSize<C>: ArraySize,
<FieldBytesSize<C> as Add>::Output: Add<MaxOverhead> + ArraySize,
{
Expand All @@ -340,7 +340,7 @@ where
#[cfg(feature = "serde")]
impl<'de, C> Deserialize<'de> for Signature<C>
where
C: PrimeCurve,
C: EcdsaCurve,
MaxSize<C>: ArraySize,
<FieldBytesSize<C> as Add>::Output: Add<MaxOverhead> + ArraySize,
{
Expand Down Expand Up @@ -383,7 +383,7 @@ fn find_scalar_range(outer: &[u8], inner: &[u8]) -> Result<Range<usize>> {
#[cfg(all(feature = "digest", feature = "hazmat"))]
impl<C> signature::PrehashSignature for Signature<C>
where
C: PrimeCurve + crate::hazmat::DigestPrimitive,
C: EcdsaCurve + crate::hazmat::DigestPrimitive,
MaxSize<C>: ArraySize,
<FieldBytesSize<C> as Add>::Output: Add<MaxOverhead> + ArraySize,
{
Expand Down
7 changes: 7 additions & 0 deletions ecdsa/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
// TODO(tarcieri): implement full set of tests from ECDSA2VS
// <https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/documents/dss2/ecdsa2vs.pdf>

use crate::EcdsaCurve;
use elliptic_curve::dev::MockCurve;

impl EcdsaCurve for MockCurve {
const NORMALIZE_S: bool = false;
}

/// ECDSA test vector
pub struct TestVector {
/// Private scalar
Expand Down
18 changes: 9 additions & 9 deletions ecdsa/src/hazmat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
//! Failure to use them correctly can lead to catastrophic failures including
//! FULL PRIVATE KEY RECOVERY!
use crate::{Error, Result};
use crate::{EcdsaCurve, Error, Result};
use core::cmp;
use elliptic_curve::{array::typenum::Unsigned, FieldBytes, PrimeCurve};
use elliptic_curve::{array::typenum::Unsigned, FieldBytes};

#[cfg(feature = "arithmetic")]
use {
Expand Down Expand Up @@ -56,7 +56,7 @@ pub trait SignPrimitive<C>:
+ Reduce<C::Uint, Bytes = FieldBytes<C>>
+ Sized
where
C: PrimeCurve + CurveArithmetic<Scalar = Self>,
C: EcdsaCurve + CurveArithmetic<Scalar = Self>,
SignatureSize<C>: ArraySize,
{
/// Try to sign the prehashed message.
Expand Down Expand Up @@ -128,7 +128,7 @@ where
#[cfg(feature = "arithmetic")]
pub trait VerifyPrimitive<C>: AffineCoordinates<FieldRepr = FieldBytes<C>> + Copy + Sized
where
C: PrimeCurve + CurveArithmetic<AffinePoint = Self>,
C: EcdsaCurve + CurveArithmetic<AffinePoint = Self>,
SignatureSize<C>: ArraySize,
{
/// Verify the prehashed message against the provided ECDSA signature.
Expand Down Expand Up @@ -163,7 +163,7 @@ where
///
/// [1]: https://github.com/RustCrypto/traits/tree/master/signature/derive
#[cfg(feature = "digest")]
pub trait DigestPrimitive: PrimeCurve {
pub trait DigestPrimitive: EcdsaCurve {
/// Preferred digest to use when computing ECDSA signatures for this
/// elliptic curve. This is typically a member of the SHA-2 family.
type Digest: BlockSizeUser + Digest + FixedOutput + FixedOutputReset;
Expand All @@ -187,7 +187,7 @@ where
///
/// [RFC6979 § 2.3.2]: https://datatracker.ietf.org/doc/html/rfc6979#section-2.3.2
/// [SEC1]: https://www.secg.org/sec1-v2.pdf
pub fn bits2field<C: PrimeCurve>(bits: &[u8]) -> Result<FieldBytes<C>> {
pub fn bits2field<C: EcdsaCurve>(bits: &[u8]) -> Result<FieldBytes<C>> {
// Minimum allowed bits size is half the field size
if bits.len() < C::FieldBytesSize::USIZE / 2 {
return Err(Error::new());
Expand Down Expand Up @@ -232,7 +232,7 @@ pub fn sign_prehashed<C, K>(
z: &FieldBytes<C>,
) -> Result<(Signature<C>, RecoveryId)>
where
C: PrimeCurve + CurveArithmetic,
C: EcdsaCurve + CurveArithmetic,
K: AsRef<Scalar<C>> + Invert<Output = CtOption<Scalar<C>>>,
SignatureSize<C>: ArraySize,
{
Expand Down Expand Up @@ -278,7 +278,7 @@ pub fn verify_prehashed<C>(
sig: &Signature<C>,
) -> Result<()>
where
C: PrimeCurve + CurveArithmetic,
C: EcdsaCurve + CurveArithmetic,
SignatureSize<C>: ArraySize,
{
let z = Scalar::<C>::reduce_bytes(z);
Expand All @@ -297,7 +297,7 @@ where
}
}

#[cfg(test)]
#[cfg(all(test, feature = "dev"))]
mod tests {
use super::bits2field;
use elliptic_curve::dev::MockCurve;
Expand Down
Loading

0 comments on commit c5206d4

Please sign in to comment.