pub enum SigningPrivateKey {
Schnorr(ECPrivateKey),
ECDSA(ECPrivateKey),
Ed25519(Ed25519PrivateKey),
SSH(Box<PrivateKey>),
MLDSA(MLDSAPrivateKey),
}Expand description
A private key used for creating digital signatures.
SigningPrivateKey is an enum representing different types of signing
private keys, including elliptic curve schemes (ECDSA, Schnorr), Edwards
curve schemes (Ed25519), post-quantum schemes (ML-DSA), and SSH keys.
This type implements the Signer trait, allowing it to create signatures of
the appropriate type.
§Examples
Creating a new Schnorr signing key and using it to sign a message:
use bc_components::{ECPrivateKey, Signer, SigningPrivateKey, Verifier};
// Create a new Schnorr signing key
let private_key = SigningPrivateKey::new_schnorr(ECPrivateKey::new());
// Get the corresponding public key
let public_key = private_key.public_key().unwrap();
// Sign a message
let message = b"Hello, world!";
let signature = private_key.sign(&message).unwrap();
// Verify the signature
assert!(public_key.verify(&signature, &message));§CBOR Serialization
SigningPrivateKey can be serialized to and from CBOR with appropriate
tags:
use bc_components::{ECPrivateKey, SigningPrivateKey};
use dcbor::prelude::*;
// Create a key
let private_key = SigningPrivateKey::new_schnorr(ECPrivateKey::new());
// Convert to CBOR
let cbor: CBOR = private_key.clone().into();
let data = cbor.to_cbor_data();
// Convert back from CBOR
let recovered = SigningPrivateKey::from_tagged_cbor_data(&data).unwrap();
// The keys should be equal
assert_eq!(private_key, recovered);Variants§
Schnorr(ECPrivateKey)
A Schnorr private key based on the secp256k1 curve
ECDSA(ECPrivateKey)
An ECDSA private key based on the secp256k1 curve
Ed25519(Ed25519PrivateKey)
An Ed25519 private key
SSH(Box<PrivateKey>)
An SSH private key
MLDSA(MLDSAPrivateKey)
A post-quantum ML-DSA private key
Implementations§
Source§impl SigningPrivateKey
impl SigningPrivateKey
Sourcepub const fn new_schnorr(key: ECPrivateKey) -> Self
pub const fn new_schnorr(key: ECPrivateKey) -> Self
Creates a new Schnorr signing private key from an ECPrivateKey.
§Arguments
key- The elliptic curve private key to use
§Returns
A new Schnorr signing private key
§Examples
use bc_components::{ECPrivateKey, SigningPrivateKey};
// Create a new EC private key
let ec_key = ECPrivateKey::new();
// Create a Schnorr signing key from it
let signing_key = SigningPrivateKey::new_schnorr(ec_key);Sourcepub const fn new_ecdsa(key: ECPrivateKey) -> Self
pub const fn new_ecdsa(key: ECPrivateKey) -> Self
Creates a new ECDSA signing private key from an ECPrivateKey.
§Arguments
key- The elliptic curve private key to use
§Returns
A new ECDSA signing private key
§Examples
use bc_components::{ECPrivateKey, SigningPrivateKey};
// Create a new EC private key
let ec_key = ECPrivateKey::new();
// Create an ECDSA signing key from it
let signing_key = SigningPrivateKey::new_ecdsa(ec_key);Sourcepub const fn new_ed25519(key: Ed25519PrivateKey) -> Self
pub const fn new_ed25519(key: Ed25519PrivateKey) -> Self
Creates a new Ed25519 signing private key from an Ed25519PrivateKey.
§Arguments
key- The Ed25519 private key to use
§Returns
A new Ed25519 signing private key
§Examples
use bc_components::{Ed25519PrivateKey, SigningPrivateKey};
// Create a new Ed25519 private key
let ed_key = Ed25519PrivateKey::new();
// Create an Ed25519 signing key from it
let signing_key = SigningPrivateKey::new_ed25519(ed_key);Sourcepub fn new_ssh(key: SSHPrivateKey) -> Self
pub fn new_ssh(key: SSHPrivateKey) -> Self
Sourcepub fn to_schnorr(&self) -> Option<&ECPrivateKey>
pub fn to_schnorr(&self) -> Option<&ECPrivateKey>
Returns the underlying Schnorr private key if this is a Schnorr key.
§Returns
Some reference to the EC private key if this is a Schnorr key, or None if it’s a different key type.
§Examples
use bc_components::{ECPrivateKey, SigningPrivateKey};
// Create a Schnorr key
let schnorr_key = SigningPrivateKey::new_schnorr(ECPrivateKey::new());
assert!(schnorr_key.to_schnorr().is_some());
// Create an ECDSA key
let ecdsa_key = SigningPrivateKey::new_ecdsa(ECPrivateKey::new());
assert!(ecdsa_key.to_schnorr().is_none());Sourcepub fn is_schnorr(&self) -> bool
pub fn is_schnorr(&self) -> bool
Sourcepub fn to_ecdsa(&self) -> Option<&ECPrivateKey>
pub fn to_ecdsa(&self) -> Option<&ECPrivateKey>
Returns the underlying ECDSA private key if this is an ECDSA key.
§Returns
Some reference to the EC private key if this is an ECDSA key, or None if it’s a different key type.
Sourcepub fn to_ssh(&self) -> Option<&SSHPrivateKey>
pub fn to_ssh(&self) -> Option<&SSHPrivateKey>
Returns the underlying SSH private key if this is an SSH key.
§Returns
Some reference to the SSH private key if this is an SSH key, or None if it’s a different key type.
Sourcepub fn public_key(&self) -> Result<SigningPublicKey>
pub fn public_key(&self) -> Result<SigningPublicKey>
Derives the corresponding public key for this private key.
§Returns
A Result containing the public key, or an error if the public key
cannot be derived (e.g., for MLDSA keys).
§Examples
use bc_components::{ECPrivateKey, SigningPrivateKey};
// Create a Schnorr signing key
let private_key = SigningPrivateKey::new_schnorr(ECPrivateKey::new());
// Derive the public key
let public_key = private_key.public_key().unwrap();Source§impl SigningPrivateKey
impl SigningPrivateKey
Sourcepub fn schnorr_sign(
&self,
message: impl AsRef<[u8]>,
rng: Rc<RefCell<dyn RandomNumberGenerator>>,
) -> Result<Signature>
pub fn schnorr_sign( &self, message: impl AsRef<[u8]>, rng: Rc<RefCell<dyn RandomNumberGenerator>>, ) -> Result<Signature>
Signs a message using Schnorr with the provided random number generator.
This method is only valid for Schnorr keys.
§Arguments
message- The message to signrng- The random number generator to use for signature creation
§Returns
A Result containing the Schnorr signature, or an error if the key is
not a Schnorr key.
§Examples
use std::{cell::RefCell, rc::Rc};
use bc_components::{ECPrivateKey, SigningPrivateKey};
use bc_rand::SecureRandomNumberGenerator;
// Create a Schnorr key
let private_key = SigningPrivateKey::new_schnorr(ECPrivateKey::new());
// Create an RNG
let rng = Rc::new(RefCell::new(SecureRandomNumberGenerator));
// Sign a message
let message = b"Hello, world!";
let signature = private_key.schnorr_sign(&message, rng).unwrap();Sourcepub fn ed25519_sign(&self, message: impl AsRef<[u8]>) -> Result<Signature>
pub fn ed25519_sign(&self, message: impl AsRef<[u8]>) -> Result<Signature>
Signs a message using Ed25519.
This method is only valid for Ed25519 keys.
§Arguments
message- The message to sign
§Returns
A Result containing the Ed25519 signature, or an error if the key is
not an Ed25519 key.
§Examples
use bc_components::{Ed25519PrivateKey, Signer, SigningPrivateKey};
// Create an Ed25519 key
let private_key = SigningPrivateKey::new_ed25519(Ed25519PrivateKey::new());
// Sign a message
let message = b"Hello, world!";
let signature = private_key.sign(&message).unwrap();Trait Implementations§
Source§impl AsRef<SigningPrivateKey> for PrivateKeys
impl AsRef<SigningPrivateKey> for PrivateKeys
Source§fn as_ref(&self) -> &SigningPrivateKey
fn as_ref(&self) -> &SigningPrivateKey
Source§impl CBORTagged for SigningPrivateKey
Implementation of the CBORTagged trait for SigningPrivateKey
impl CBORTagged for SigningPrivateKey
Implementation of the CBORTagged trait for SigningPrivateKey
Returns the CBOR tags used for this type.
For SigningPrivateKey, the tag is 40021.
Source§impl CBORTaggedDecodable for SigningPrivateKey
Implementation of the CBORTaggedDecodable trait for SigningPrivateKey
impl CBORTaggedDecodable for SigningPrivateKey
Implementation of the CBORTaggedDecodable trait for SigningPrivateKey
Source§fn from_untagged_cbor(untagged_cbor: CBOR) -> Result<Self>
fn from_untagged_cbor(untagged_cbor: CBOR) -> Result<Self>
Creates a SigningPrivateKey from an untagged CBOR value.
§Arguments
untagged_cbor- The CBOR value to decode
§Returns
A Result containing the decoded SigningPrivateKey or an error if decoding fails.
§Format
The CBOR value must be one of:
- A byte string (interpreted as a Schnorr private key)
- An array where the first element is a discriminator (1 for ECDSA, 2 for Ed25519) and the second element is a byte string containing the key data
- A tagged value with a tag for ML-DSA or SSH keys
Source§fn from_tagged_cbor(cbor: CBOR) -> Result<Self, Error>where
Self: Sized,
fn from_tagged_cbor(cbor: CBOR) -> Result<Self, Error>where
Self: Sized,
Source§impl CBORTaggedEncodable for SigningPrivateKey
Implementation of the CBORTaggedEncodable trait for SigningPrivateKey
impl CBORTaggedEncodable for SigningPrivateKey
Implementation of the CBORTaggedEncodable trait for SigningPrivateKey
Source§fn untagged_cbor(&self) -> CBOR
fn untagged_cbor(&self) -> CBOR
Converts the SigningPrivateKey to an untagged CBOR value.
The CBOR encoding depends on the key type:
- Schnorr: A byte string containing the 32-byte private key
- ECDSA: An array containing the discriminator 1 and the 32-byte private key
- Ed25519: An array containing the discriminator 2 and the 32-byte private key
- SSH: A tagged text string containing the OpenSSH-encoded private key
- ML-DSA: Delegates to the MLDSAPrivateKey implementation
Source§fn tagged_cbor(&self) -> CBOR
fn tagged_cbor(&self) -> CBOR
Source§impl Clone for SigningPrivateKey
impl Clone for SigningPrivateKey
Source§fn clone(&self) -> SigningPrivateKey
fn clone(&self) -> SigningPrivateKey
1.0.0 · Source§const fn clone_from(&mut self, source: &Self)
const fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for SigningPrivateKey
Debug implementation for SigningPrivateKey
impl Debug for SigningPrivateKey
Debug implementation for SigningPrivateKey
Source§impl From<&SigningPrivateKey> for SigningPrivateKey
Implementation of the From trait for reference to SigningPrivateKey
impl From<&SigningPrivateKey> for SigningPrivateKey
Implementation of the From trait for reference to SigningPrivateKey
Source§fn from(key: &SigningPrivateKey) -> Self
fn from(key: &SigningPrivateKey) -> Self
Clones a SigningPrivateKey from a reference.
Source§impl From<SigningPrivateKey> for CBOR
Conversion from SigningPrivateKey to CBOR
impl From<SigningPrivateKey> for CBOR
Conversion from SigningPrivateKey to CBOR
Source§fn from(value: SigningPrivateKey) -> Self
fn from(value: SigningPrivateKey) -> Self
Converts a SigningPrivateKey to a tagged CBOR value.
Source§impl Hash for SigningPrivateKey
Implementation of hashing for SigningPrivateKey
impl Hash for SigningPrivateKey
Implementation of hashing for SigningPrivateKey
Source§impl PartialEq for SigningPrivateKey
impl PartialEq for SigningPrivateKey
Source§impl Signer for SigningPrivateKey
Implementation of the Signer trait for SigningPrivateKey
impl Signer for SigningPrivateKey
Implementation of the Signer trait for SigningPrivateKey
Source§fn sign_with_options(
&self,
message: &dyn AsRef<[u8]>,
options: Option<SigningOptions>,
) -> Result<Signature>
fn sign_with_options( &self, message: &dyn AsRef<[u8]>, options: Option<SigningOptions>, ) -> Result<Signature>
Signs a message with the appropriate algorithm based on the key type.
This method dispatches to the appropriate signing method based on the key type and provided options.
§Arguments
message- The message to signoptions- Optional signing options (algorithm-specific parameters)
§Returns
A Result containing the signature, or an error if signing fails
§Examples
use std::{cell::RefCell, rc::Rc};
use bc_components::{
ECPrivateKey, Signer, SigningOptions, SigningPrivateKey,
};
use bc_rand::SecureRandomNumberGenerator;
// Create a Schnorr key
let private_key = SigningPrivateKey::new_schnorr(ECPrivateKey::new());
// Create Schnorr signing options
let rng = Rc::new(RefCell::new(SecureRandomNumberGenerator));
let options = SigningOptions::Schnorr { rng };
// Sign a message with options
let message = b"Hello, world!";
let signature = private_key
.sign_with_options(&message, Some(options))
.unwrap();Source§impl TryFrom<&SigningPrivateKey> for XID
Implements conversion from SigningPrivateKey reference to XID via the public
key.
impl TryFrom<&SigningPrivateKey> for XID
Implements conversion from SigningPrivateKey reference to XID via the public key.
Source§impl TryFrom<CBOR> for SigningPrivateKey
TryFrom implementation for converting CBOR to SigningPrivateKey
impl TryFrom<CBOR> for SigningPrivateKey
TryFrom implementation for converting CBOR to SigningPrivateKey
Source§impl Verifier for SigningPrivateKey
Implementation of the Verifier trait for SigningPrivateKey
impl Verifier for SigningPrivateKey
Implementation of the Verifier trait for SigningPrivateKey
Source§fn verify(&self, signature: &Signature, message: &dyn AsRef<[u8]>) -> bool
fn verify(&self, signature: &Signature, message: &dyn AsRef<[u8]>) -> bool
Verifies a signature against a message.
This method is only implemented for Schnorr keys, where it derives the
public key and uses it to verify the signature. For other key types,
this method always returns false.
§Arguments
signature- The signature to verifymessage- The message that was allegedly signed
§Returns
true if the signature is valid for the message, false otherwise