Skip to content

Commit

Permalink
k256/p256: wire up ecdsa::Signer (#101)
Browse files Browse the repository at this point in the history
Adds a type alias for `ecdsa::signer::Signer`, which provides a
high-level ECDSA signing interface.
  • Loading branch information
tarcieri authored Jul 30, 2020
1 parent c6fae25 commit b4b9519
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 68 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

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

9 changes: 5 additions & 4 deletions k256/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ keywords = ["bitcoin", "crypto", "ecc", "ethereum", "secp256k1"]

[dependencies]
cfg-if = "0.1"
ecdsa = { version = "= 0.7.0-pre", optional = true, default-features = false, features = ["hazmat"] }
ecdsa-core = { version = "= 0.7.0-pre", package = "ecdsa", optional = true, default-features = false }
elliptic-curve = { version = "= 0.5.0-pre", default-features = false, features = ["weierstrass"] }
sha2 = { version = "0.9", optional = true, default-features = false }
zeroize = { version = "1", optional = true, default-features = false }

[dev-dependencies]
ecdsa = { version = "= 0.7.0-pre", default-features = false, features = ["dev", "hazmat"] }
ecdsa-core = { version = "= 0.7.0-pre", package = "ecdsa", default-features = false, features = ["dev"] }
hex = "0.4" # TODO: switch to hex-literal
hex-literal = "0.2"
proptest = "0.10"
Expand All @@ -30,14 +29,16 @@ criterion = "0.3"
[features]
default = ["arithmetic", "std"]
arithmetic = []
digest = ["ecdsa/digest"]
digest = ["ecdsa-core/digest"]
ecdsa = ["arithmetic", "ecdsa-core/signer", "sha256", "zeroize"]
endomorphism-mul = []
field-montgomery = []
force-32-bit = []
rand = ["elliptic-curve/rand_core"]
sha256 = ["digest", "sha2"]
test-vectors = []
std = ["elliptic-curve/std"]
zeroize = ["elliptic-curve/zeroize"]

[package.metadata.docs.rs]
all-features = true
Expand Down
10 changes: 2 additions & 8 deletions k256/src/arithmetic/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use elliptic_curve::{
};

#[cfg(feature = "digest")]
use ecdsa::signature::digest::{consts::U32, Digest};
use ecdsa_core::signature::digest::{consts::U32, Digest};

#[cfg(feature = "rand")]
use elliptic_curve::{
Expand All @@ -35,7 +35,7 @@ use elliptic_curve::{
};

#[cfg(feature = "zeroize")]
use zeroize::Zeroize;
use elliptic_curve::zeroize::Zeroize;

#[cfg(test)]
use num_bigint::{BigUint, ToBigUint};
Expand Down Expand Up @@ -259,12 +259,6 @@ impl Scalar {
}
}

impl AsRef<Scalar> for Scalar {
fn as_ref(&self) -> &Scalar {
self
}
}

impl Shr<usize> for Scalar {
type Output = Self;

Expand Down
2 changes: 1 addition & 1 deletion k256/src/arithmetic/scalar/scalar_8x32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::arithmetic::util::{adc32, sbb32};
use core::convert::TryInto;

#[cfg(feature = "zeroize")]
use zeroize::Zeroize;
use elliptic_curve::zeroize::Zeroize;

/// Constant representing the modulus
/// n = FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE BAAEDCE6 AF48A03B BFD25E8C D0364141
Expand Down
34 changes: 20 additions & 14 deletions k256/src/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
pub mod recoverable;

use super::Secp256k1;
use core::borrow::Borrow;

#[cfg(feature = "arithmetic")]
#[cfg(feature = "ecdsa")]
use {
crate::{AffinePoint, ProjectivePoint, Scalar, ScalarBytes},
ecdsa::{
ecdsa_core::{
hazmat::{SignPrimitive, VerifyPrimitive},
Error,
},
Expand All @@ -18,15 +19,20 @@ use {
};

/// ECDSA/secp256k1 signature (fixed-size)
pub type Signature = ::ecdsa::Signature<Secp256k1>;
pub type Signature = ecdsa_core::Signature<Secp256k1>;

/// ECDSA/secp256k1 signer
#[cfg(feature = "ecdsa")]
#[cfg_attr(docsrs, doc(cfg(feature = "ecdsa")))]
pub type Signer = ecdsa_core::Signer<Secp256k1>;

#[cfg(feature = "sha256")]
#[cfg_attr(docsrs, doc(cfg(feature = "sha256")))]
impl ecdsa::hazmat::DigestPrimitive for Secp256k1 {
impl ecdsa_core::hazmat::DigestPrimitive for Secp256k1 {
type Digest = sha2::Sha256;
}

#[cfg(feature = "arithmetic")]
#[cfg(feature = "ecdsa")]
impl SignPrimitive<Secp256k1> for Scalar {
#[allow(clippy::many_single_char_names)]
fn try_sign_prehashed<K>(
Expand All @@ -35,10 +41,10 @@ impl SignPrimitive<Secp256k1> for Scalar {
hashed_msg: &ScalarBytes,
) -> Result<Signature, Error>
where
K: AsRef<Scalar> + Invert<Output = Scalar>,
K: Borrow<Scalar> + Invert<Output = Scalar>,
{
let k_inverse = ephemeral_scalar.invert();
let k = ephemeral_scalar.as_ref();
let k = ephemeral_scalar.borrow();

if k_inverse.is_none().into() || k.is_zero().into() {
return Err(Error::new());
Expand Down Expand Up @@ -68,7 +74,7 @@ impl SignPrimitive<Secp256k1> for Scalar {
}
}

#[cfg(feature = "arithmetic")]
#[cfg(feature = "ecdsa")]
impl VerifyPrimitive<Secp256k1> for AffinePoint {
fn verify_prehashed(
&self,
Expand Down Expand Up @@ -106,8 +112,8 @@ impl VerifyPrimitive<Secp256k1> for AffinePoint {
}
}

#[cfg(feature = "arithmetic")]
#[cfg_attr(docsrs, doc(cfg(feature = "arithmetic")))]
#[cfg(feature = "ecdsa")]
#[cfg_attr(docsrs, doc(cfg(feature = "ecdsa")))]
/// Normalize signature into "low S" form as described in
/// [BIP 0062: Dealing with Malleability][1].
///
Expand All @@ -133,14 +139,14 @@ pub fn normalize_s(signature: &Signature) -> Result<Signature, Error> {
))
}

#[cfg(all(test, feature = "arithmetic"))]
#[cfg(all(test, feature = "ecdsa"))]
mod tests {
use super::*;
use crate::test_vectors::ecdsa::ECDSA_TEST_VECTORS;
use ecdsa::signature::Signature as _;
use ecdsa_core::signature::Signature as _;

ecdsa::new_signing_test!(ECDSA_TEST_VECTORS);
ecdsa::new_verification_test!(ECDSA_TEST_VECTORS);
ecdsa_core::new_signing_test!(ECDSA_TEST_VECTORS);
ecdsa_core::new_verification_test!(ECDSA_TEST_VECTORS);

// Test vectors generated using rust-secp256k1
#[test]
Expand Down
7 changes: 4 additions & 3 deletions k256/src/ecdsa/recoverable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::{
convert::{TryFrom, TryInto},
fmt::{self, Debug},
};
use ecdsa::{signature::Signature as _, Error};
use ecdsa_core::{signature::Signature as _, Error};

#[cfg(feature = "arithmetic")]
use crate::arithmetic::{
Expand Down Expand Up @@ -45,7 +45,8 @@ impl Signature {

/// Recover the [`PublicKey`] used to create the given signature
#[cfg(all(feature = "arithmetic", feature = "sha256"))]
#[cfg_attr(docsrs, doc(cfg(feature = "arithmetic", feature = "sha256")))]
#[cfg_attr(docsrs, doc(cfg(feature = "arithmetic")))]
#[cfg_attr(docsrs, doc(cfg(feature = "sha256")))]
#[allow(non_snake_case, clippy::many_single_char_names)]
pub fn recover_pubkey(&self, msg: &[u8]) -> Result<PublicKey, Error> {
let r = self.r()?;
Expand Down Expand Up @@ -112,7 +113,7 @@ impl Signature {
}
}

impl ecdsa::signature::Signature for Signature {
impl ecdsa_core::signature::Signature for Signature {
fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
bytes.try_into()
}
Expand Down
4 changes: 2 additions & 2 deletions k256/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ mod arithmetic;
#[cfg(feature = "arithmetic")]
mod mul;

#[cfg(feature = "ecdsa")]
#[cfg_attr(docsrs, doc(cfg(feature = "ecdsa")))]
#[cfg(feature = "ecdsa-core")]
#[cfg_attr(docsrs, doc(cfg(feature = "ecdsa-core")))]
pub mod ecdsa;

#[cfg(any(feature = "test-vectors", test))]
Expand Down
2 changes: 1 addition & 1 deletion k256/src/test_vectors/ecdsa.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! ECDSA/secp256k1 test vectors
use ecdsa::dev::TestVector;
use ecdsa_core::dev::TestVector;
use hex_literal::hex;

/// ECDSA/secp256k1 test vectors
Expand Down
9 changes: 5 additions & 4 deletions p256/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,25 @@ categories = ["cryptography", "no-std"]
keywords = ["crypto", "ecc", "nist", "prime256v1", "secp256r1"]

[dependencies]
ecdsa = { version = "= 0.7.0-pre", optional = true, default-features = false }
ecdsa-core = { version = "= 0.7.0-pre", package = "ecdsa", optional = true, default-features = false }
elliptic-curve = { version = "= 0.5.0-pre", default-features = false, features = ["weierstrass"] }
sha2 = { version = "0.9", optional = true, default-features = false }
zeroize = { version = "1", optional = true, default-features = false }

[dev-dependencies]
ecdsa = { version = "= 0.7.0-pre", default-features = false, features = ["dev", "hazmat"] }
ecdsa-core = { version = "= 0.7.0-pre", package = "ecdsa", default-features = false, features = ["dev"] }
hex = "0.4" # TODO: switch to hex-literal
hex-literal = "0.2"
proptest = "0.10"

[features]
default = ["arithmetic", "std"]
arithmetic = []
ecdsa = ["arithmetic", "ecdsa-core/signer", "sha256", "zeroize"]
rand = ["elliptic-curve/rand_core"]
sha256 = ["ecdsa/digest", "ecdsa/hazmat", "sha2"]
sha256 = ["ecdsa-core/digest", "ecdsa-core/hazmat", "sha2"]
test-vectors = []
std = ["elliptic-curve/std"]
zeroize = ["elliptic-curve/zeroize"]

[package.metadata.docs.rs]
all-features = true
Expand Down
8 changes: 1 addition & 7 deletions p256/src/arithmetic/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use elliptic_curve::{
};

#[cfg(feature = "zeroize")]
use zeroize::Zeroize;
use elliptic_curve::zeroize::Zeroize;

use super::util::{adc, mac, sbb};

Expand Down Expand Up @@ -124,12 +124,6 @@ fn shr1(u256: &mut U256) {
}
}

impl AsRef<Scalar> for Scalar {
fn as_ref(&self) -> &Scalar {
self
}
}

impl Ord for Scalar {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
use core::cmp::Ordering::*;
Expand Down
7 changes: 4 additions & 3 deletions p256/src/arithmetic/scalar/blinding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// and extract it into the `elliptic-curve` crate so it can be reused across curves

use super::Scalar;
use core::borrow::Borrow;
use elliptic_curve::{
ops::Invert,
rand_core::{CryptoRng, RngCore},
Expand All @@ -12,7 +13,7 @@ use elliptic_curve::{
};

#[cfg(feature = "zeroize")]
use zeroize::Zeroize;
use elliptic_curve::zeroize::Zeroize;

/// Scalar blinded with a randomly generated masking value.
///
Expand All @@ -38,8 +39,8 @@ impl BlindedScalar {
}
}

impl AsRef<Scalar> for BlindedScalar {
fn as_ref(&self) -> &Scalar {
impl Borrow<Scalar> for BlindedScalar {
fn borrow(&self) -> &Scalar {
&self.scalar
}
}
Expand Down
Loading

0 comments on commit b4b9519

Please sign in to comment.