Expand description
Elliptic Curve Digital Signature Algorithm (ECDSA).
This module contains support for computing and verifying ECDSA signatures. To use it, you will need to enable one of the two following Cargo features:
ecdsa-core: provides only theSignaturetype (which represents an ECDSA/secp256k1 signature). Does not require thearithmeticfeature. This is useful for 3rd-party crates which wish to use theSignaturetype for interoperability purposes (particularly in conjunction with thesignature::Signertrait). Example use cases for this include other software implementations of ECDSA/secp256k1 and wrappers for cloud KMS services or hardware devices (HSM or crypto hardware wallet).ecdsa: providesecdsa-corefeatures plus theSigningKeyandVerifyingKeytypes which natively implement ECDSA/secp256k1 signing and verification.
§Signing/Verification Example
// NOTE: requires the `ecdsa` and `getrandom` crate features are enabled
use k256::{
ecdsa::{SigningKey, Signature, signature::Signer},
elliptic_curve::Generate,
SecretKey,
};
// Signing
let signing_key = SigningKey::generate(); // Serialize with `::to_bytes()`
let verifying_key_bytes = signing_key.verifying_key().to_sec1_point(true); // 33-bytes
let message = b"ECDSA proves knowledge of a secret number in the context of a single message";
let signature: Signature = signing_key.sign(message);
// Verification
use k256::{Sec1Point, ecdsa::{VerifyingKey, signature::Verifier}};
let verifying_key = VerifyingKey::from_sec1_bytes(verifying_key_bytes.as_ref())?;
verifying_key.verify(message, &signature)?;§Recovering VerifyingKey from Signature
ECDSA makes it possible to recover the public key used to verify a signature with the assistance of 2-bits of additional information.
This is helpful when there is already a trust relationship for a particular key, and it’s desirable to omit the full public key used to sign a particular message.
One common application of signature recovery with secp256k1 is Ethereum.
§Recovering a VerifyingKey from a signature
use hex_literal::hex;
use k256::ecdsa::{RecoveryId, Signature, VerifyingKey};
use sha3::{Keccak256, Digest};
use elliptic_curve::sec1::ToSec1Point;
let msg = b"example message";
let signature = Signature::try_from(hex!(
"46c05b6368a44b8810d79859441d819b8e7cdc8bfd371e35c53196f4bcacdb51
35c7facce2a97b95eacba8a586d87b7958aaf8368ab29cee481f76e871dbd9cb"
).as_slice())?;
let recid = RecoveryId::try_from(1u8)?;
let recovered_key = VerifyingKey::recover_from_digest(
Keccak256::new_with_prefix(msg),
&signature,
recid
)?;
let expected_key = VerifyingKey::from_sec1_bytes(
&hex!("0200866db99873b09fc2fb1e3ba549b156e96d1a567e3284f5f0e859a83320cb8b")
)?;
assert_eq!(recovered_key, expected_key);Re-exports§
pub use ecdsa_core::signature;
Structs§
- Error
- Signature errors.
- Recovery
Id - Recovery IDs, a.k.a. “recid”.
Traits§
- Ecdsa
Curve - Marker trait for elliptic curves intended for use with ECDSA.
Type Aliases§
- DerSignature
- ECDSA/secp256k1 signature (ASN.1 DER encoded)
- Signature
- ECDSA/secp256k1 signature (fixed-size)
- Signing
Key - ECDSA/secp256k1 signing key
- Verifying
Key - ECDSA/secp256k1 verification key (i.e. public key)