Enum SigningPrivateKey

Source
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

Source

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);
Source

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);
Source

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);
Source

pub fn new_ssh(key: SSHPrivateKey) -> Self

Creates a new SSH signing private key from an SSHPrivateKey.

§Arguments
  • key - The SSH private key to use
§Returns

A new SSH signing private key

Source

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());
Source

pub fn is_schnorr(&self) -> bool

Checks if this is a Schnorr signing key.

§Returns

true if this is a Schnorr key, false otherwise

Source

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.

Source

pub fn is_ecdsa(&self) -> bool

Checks if this is an ECDSA signing key.

§Returns

true if this is an ECDSA key, false otherwise

Source

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.

Source

pub fn is_ssh(&self) -> bool

Checks if this is an SSH signing key.

§Returns

true if this is an SSH key, false otherwise

Source

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

Source

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 sign
  • rng - 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();
Source

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

Source§

fn as_ref(&self) -> &SigningPrivateKey

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl CBORTagged for SigningPrivateKey

Implementation of the CBORTagged trait for SigningPrivateKey

Source§

fn cbor_tags() -> Vec<Tag>

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

Source§

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,

Creates an instance of this type by decoding it from tagged CBOR. Read more
Source§

fn from_tagged_cbor_data(data: impl AsRef<[u8]>) -> Result<Self, Error>
where Self: Sized,

Creates an instance of this type by decoding it from binary encoded tagged CBOR. Read more
Source§

fn from_untagged_cbor_data(data: impl AsRef<[u8]>) -> Result<Self, Error>
where Self: Sized,

Creates an instance of this type by decoding it from binary encoded untagged CBOR. Read more
Source§

impl CBORTaggedEncodable for SigningPrivateKey

Implementation of the CBORTaggedEncodable trait for SigningPrivateKey

Source§

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

Returns the tagged CBOR encoding of this instance. Read more
Source§

fn tagged_cbor_data(&self) -> Vec<u8>

Returns the tagged value in CBOR binary representation. Read more
Source§

impl Clone for SigningPrivateKey

Source§

fn clone(&self) -> SigningPrivateKey

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SigningPrivateKey

Debug implementation for SigningPrivateKey

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the SigningPrivateKey for display.

For security reasons, the private key data is not displayed.

Source§

impl From<&SigningPrivateKey> for SigningPrivateKey

Implementation of the From trait for reference to SigningPrivateKey

Source§

fn from(key: &SigningPrivateKey) -> Self

Clones a SigningPrivateKey from a reference.

Source§

impl From<SigningPrivateKey> for CBOR

Conversion from SigningPrivateKey to CBOR

Source§

fn from(value: SigningPrivateKey) -> Self

Converts a SigningPrivateKey to a tagged CBOR value.

Source§

impl Hash for SigningPrivateKey

Implementation of hashing for SigningPrivateKey

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Hashes the key’s data.

This is used for collections that require hash support.

1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for SigningPrivateKey

Source§

fn eq(&self, other: &SigningPrivateKey) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

const fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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>

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 sign
  • options - 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§

fn sign(&self, message: &dyn AsRef<[u8]>) -> Result<Signature>

Signs a message using default options. Read more
Source§

impl TryFrom<&SigningPrivateKey> for XID

Implements conversion from SigningPrivateKey reference to XID via the public key.

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(key: &SigningPrivateKey) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<CBOR> for SigningPrivateKey

TryFrom implementation for converting CBOR to SigningPrivateKey

Source§

fn try_from(cbor: CBOR) -> Result<Self>

Tries to convert a CBOR value to a SigningPrivateKey.

This is a convenience method that calls from_tagged_cbor.

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

impl Verifier for SigningPrivateKey

Implementation of the Verifier trait for SigningPrivateKey

Source§

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 verify
  • message - The message that was allegedly signed
§Returns

true if the signature is valid for the message, false otherwise

Source§

impl Eq for SigningPrivateKey

Source§

impl StructuralPartialEq for SigningPrivateKey

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CBORDecodable for T
where T: TryFrom<CBOR, Error = Error>,

Source§

fn try_from_cbor(cbor: &CBOR) -> Result<Self, Error>

Source§

impl<T> CBOREncodable for T
where T: Into<CBOR> + Clone,

Source§

fn to_cbor(&self) -> CBOR

Converts this value to a CBOR object. Read more
Source§

fn to_cbor_data(&self) -> Vec<u8>

Converts this value directly to binary CBOR data. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> URDecodable for T

Source§

fn from_ur(ur: impl AsRef<UR>) -> Result<Self, Error>
where Self: Sized,

Source§

fn from_ur_string(ur_string: impl Into<String>) -> Result<Self, Error>
where Self: Sized,

Source§

impl<T> UREncodable for T

Source§

fn ur(&self) -> UR

Returns the UR representation of the object.
Source§

fn ur_string(&self) -> String

Returns the UR string representation of the object.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> CBORCodable for T

Source§

impl<T> CBORTaggedCodable for T

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> URCodable for T