pub enum Signature {
Schnorr([u8; 64]),
ECDSA([u8; 64]),
Ed25519([u8; 64]),
SSH(SshSig),
MLDSA(MLDSASignature),
}Expand description
A digital signature created with various signature algorithms.
Signature is an enum representing different types of digital signatures:
Schnorr: A BIP-340 Schnorr signature (64 bytes)ECDSA: An ECDSA signature using the secp256k1 curve (64 bytes)Ed25519: An Ed25519 signature (64 bytes)SSH: An SSH signature in various formatsMLDSA: A post-quantum ML-DSA signature
Signatures can be serialized to and from CBOR with appropriate tags.
§Examples
use bc_components::{SignatureScheme, Signer, Verifier};
// Create a key pair using Schnorr
let (private_key, public_key) = SignatureScheme::Schnorr.keypair();
// Sign a message
let message = b"Hello, world!";
let signature = private_key.sign(&message).unwrap();
// The signature can be verified with the corresponding public key
assert!(public_key.verify(&signature, &message));Converting to and from CBOR:
use bc_components::{SignatureScheme, Signer};
use dcbor::prelude::*;
// Create a signature
let (private_key, _) = SignatureScheme::Schnorr.keypair();
let message = b"Hello, world!";
let signature = private_key.sign(&message).unwrap();
// Convert to CBOR
let cbor: CBOR = signature.clone().into();
let data = cbor.to_cbor_data();
// Convert back from CBOR
let recovered =
bc_components::Signature::from_tagged_cbor_data(&data).unwrap();
// The signatures should be identical
assert_eq!(signature, recovered);Variants§
Schnorr([u8; 64])
A BIP-340 Schnorr signature (64 bytes)
ECDSA([u8; 64])
An ECDSA signature using the secp256k1 curve (64 bytes)
Ed25519([u8; 64])
An Ed25519 signature (64 bytes)
SSH(SshSig)
An SSH signature
MLDSA(MLDSASignature)
A post-quantum ML-DSA signature
Implementations§
Source§impl Signature
impl Signature
Sourcepub fn schnorr_from_data(data: [u8; 64]) -> Self
pub fn schnorr_from_data(data: [u8; 64]) -> Self
Sourcepub fn schnorr_from_data_ref(data: impl AsRef<[u8]>) -> Result<Self>
pub fn schnorr_from_data_ref(data: impl AsRef<[u8]>) -> Result<Self>
Creates a Schnorr signature from a byte slice.
§Arguments
data- A byte slice containing the signature data
§Returns
A Result containing the signature or an error if the data is not
exactly 64 bytes in length.
§Examples
use bc_components::Signature;
let data = vec![0u8; 64]; // In practice, this would be a real signature
let signature = Signature::schnorr_from_data_ref(&data).unwrap();Sourcepub fn ecdsa_from_data(data: [u8; 64]) -> Self
pub fn ecdsa_from_data(data: [u8; 64]) -> Self
Sourcepub fn ecdsa_from_data_ref(data: impl AsRef<[u8]>) -> Result<Self>
pub fn ecdsa_from_data_ref(data: impl AsRef<[u8]>) -> Result<Self>
Creates an ECDSA signature from a byte slice.
§Arguments
data- A byte slice containing the signature data
§Returns
A Result containing the signature or an error if the data is not
exactly 64 bytes in length.
§Examples
use bc_components::Signature;
let data = vec![0u8; 64]; // In practice, this would be a real signature
let signature = Signature::ecdsa_from_data_ref(&data).unwrap();Sourcepub fn ed25519_from_data(data: [u8; 64]) -> Self
pub fn ed25519_from_data(data: [u8; 64]) -> Self
Sourcepub fn ed25519_from_data_ref(data: impl AsRef<[u8]>) -> Result<Self>
pub fn ed25519_from_data_ref(data: impl AsRef<[u8]>) -> Result<Self>
Creates an Ed25519 signature from a byte slice.
§Arguments
data- A byte slice containing the signature data
§Returns
A Result containing the signature or an error if the data is not
exactly 64 bytes in length.
§Examples
use bc_components::Signature;
let data = vec![0u8; 64]; // In practice, this would be a real signature
let signature = Signature::ed25519_from_data_ref(&data).unwrap();Sourcepub fn to_schnorr(&self) -> Option<&[u8; 64]>
pub fn to_schnorr(&self) -> Option<&[u8; 64]>
Returns the Schnorr signature data if this is a Schnorr signature.
§Returns
Some reference to the 64-byte signature data if this is a Schnorr signature, or None if it’s a different signature type.
§Examples
use bc_components::{SignatureScheme, Signer};
// Create a Schnorr signature
let (private_key, _) = SignatureScheme::Schnorr.keypair();
let message = b"Hello, world!";
let signature = private_key.sign(&message).unwrap();
// We can access the Schnorr signature data
assert!(signature.to_schnorr().is_some());
// Create an ECDSA signature
let (ecdsa_key, _) = SignatureScheme::Ecdsa.keypair();
let ecdsa_sig = ecdsa_key.sign(&message).unwrap();
// This will return None since it's not a Schnorr signature
assert!(ecdsa_sig.to_schnorr().is_none());Sourcepub fn to_ecdsa(&self) -> Option<&[u8; 64]>
pub fn to_ecdsa(&self) -> Option<&[u8; 64]>
Returns the ECDSA signature data if this is an ECDSA signature.
§Returns
Some reference to the 64-byte signature data if this is an ECDSA signature, or None if it’s a different signature type.
Sourcepub fn to_ssh(&self) -> Option<&SshSig>
pub fn to_ssh(&self) -> Option<&SshSig>
Returns the SSH signature if this is an SSH signature.
§Returns
Some reference to the SSH signature if this is an SSH signature, or None if it’s a different signature type.
Sourcepub fn scheme(&self) -> Result<SignatureScheme>
pub fn scheme(&self) -> Result<SignatureScheme>
Determines the signature scheme used to create this signature.
§Returns
A Result containing the signature scheme, or an error if the
signature scheme cannot be determined (e.g., for unsupported SSH
algorithms).
§Examples
use bc_components::{SignatureScheme, Signer};
// Create a signature with ECDSA
let (private_key, _) = SignatureScheme::Ecdsa.keypair();
let message = b"Hello, world!";
let signature = private_key.sign(&message).unwrap();
// Get the signature scheme
let scheme = signature.scheme().unwrap();
assert_eq!(scheme, SignatureScheme::Ecdsa);Trait Implementations§
Source§impl CBORTagged for Signature
Implementation of the CBORTagged trait for Signature
impl CBORTagged for Signature
Implementation of the CBORTagged trait for Signature
Returns the CBOR tags used for this type.
For Signature, the tag is 40020.
Source§impl CBORTaggedDecodable for Signature
Implementation of the CBORTaggedDecodable trait for Signature
impl CBORTaggedDecodable for Signature
Implementation of the CBORTaggedDecodable trait for Signature
Source§fn from_untagged_cbor(cbor: CBOR) -> Result<Self>
fn from_untagged_cbor(cbor: CBOR) -> Result<Self>
Creates a Signature from an untagged CBOR value.
§Arguments
cbor- The CBOR value to decode
§Returns
A Result containing the decoded Signature or an error if decoding fails.
§Format
The CBOR value must be one of:
- A byte string (interpreted as a Schnorr signature)
- An array of length 2, where the first element is 1 (ECDSA) or 2 (Ed25519) and the second element is a byte string containing the signature data
- A tagged value with a tag for MLDSA or SSH signatures
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 Signature
Implementation of the CBORTaggedEncodable trait for Signature
impl CBORTaggedEncodable for Signature
Implementation of the CBORTaggedEncodable trait for Signature
Source§fn untagged_cbor(&self) -> CBOR
fn untagged_cbor(&self) -> CBOR
Converts the Signature to an untagged CBOR value.
The CBOR encoding depends on the signature type:
- Schnorr: A byte string containing the 64-byte signature
- ECDSA: An array containing the discriminator 1 and the 64-byte signature
- Ed25519: An array containing the discriminator 2 and the 64-byte signature
- SSH: A tagged text string containing the PEM-encoded signature
- ML-DSA: Delegates to the MLDSASignature implementation
Source§fn tagged_cbor(&self) -> CBOR
fn tagged_cbor(&self) -> CBOR
Source§impl PartialEq for Signature
Implementation of equality comparison for Signature
impl PartialEq for Signature
Implementation of equality comparison for Signature