Signature

Enum Signature 

Source
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 formats
  • MLDSA: 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

Source

pub fn schnorr_from_data(data: [u8; 64]) -> Self

Creates a Schnorr signature from a 64-byte array.

§Arguments
  • data - The 64-byte signature data
§Returns

A new Schnorr signature

§Examples
use bc_components::Signature;

let data = [0u8; 64]; // In practice, this would be a real signature
let signature = Signature::schnorr_from_data(data);
Source

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

pub fn ecdsa_from_data(data: [u8; 64]) -> Self

Creates an ECDSA signature from a 64-byte array.

§Arguments
  • data - The 64-byte signature data
§Returns

A new ECDSA signature

§Examples
use bc_components::Signature;

let data = [0u8; 64]; // In practice, this would be a real signature
let signature = Signature::ecdsa_from_data(data);
Source

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

pub fn ed25519_from_data(data: [u8; 64]) -> Self

Creates an Ed25519 signature from a 64-byte array.

§Arguments
  • data - The 64-byte signature data
§Returns

A new Ed25519 signature

§Examples
use bc_components::Signature;

let data = [0u8; 64]; // In practice, this would be a real signature
let signature = Signature::ed25519_from_data(data);
Source

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

pub fn from_ssh(sig: SshSig) -> Self

Creates an SSH signature from an SshSig object.

§Arguments
  • sig - The SSH signature object
§Returns

A new SSH signature

Source

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

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.

Source

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.

Source

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 AsRef<Signature> for Signature

Implementation of AsRef for Signature

Source§

fn as_ref(&self) -> &Signature

Returns a reference to self.

Source§

impl CBORTagged for Signature

Implementation of the CBORTagged trait for Signature

Source§

fn cbor_tags() -> Vec<Tag>

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

Source§

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,

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 Signature

Implementation of the CBORTaggedEncodable trait for Signature

Source§

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

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 Signature

Source§

fn clone(&self) -> Signature

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

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Signature

Debug implementation for Signature

Source§

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

Formats the signature for display.

For binary signatures (Schnorr, ECDSA, Ed25519), displays the hex-encoded signature data. For SSH and ML-DSA signatures, displays the signature object.

Source§

impl From<Signature> for CBOR

Conversion from Signature to CBOR

Source§

fn from(value: Signature) -> Self

Converts a Signature to a tagged CBOR value.

Source§

impl PartialEq for Signature

Implementation of equality comparison for Signature

Source§

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

Compares two signatures for equality.

Signatures are equal if they have the same type and the same signature data. Signatures of different types (e.g., Schnorr vs ECDSA) are never equal.

1.0.0 · Source§

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 TryFrom<CBOR> for Signature

TryFrom implementation for converting CBOR to Signature

Source§

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

Tries to convert a CBOR value to a Signature.

This is a convenience method that calls from_tagged_cbor.

Source§

type Error = Error

The type returned in the event of a conversion error.

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