Skip to main content

Overview

The KeyPair enum represents asymmetric key pairs used for cryptographic operations in TAPLE. It supports multiple cryptographic algorithms through its variants.

KeyPair Enum

pub enum KeyPair {
    Ed25519(Ed25519KeyPair),
    #[cfg(feature = "secp256k1")]
    Secp256k1(Secp256k1KeyPair),
}
The KeyPair enum provides a unified interface for different cryptographic key pair types. Each variant wraps a specific key pair implementation.

Methods

get_key_derivator

pub fn get_key_derivator(&self) -> KeyDerivator
Returns the key derivator type for the current key pair. Returns: The KeyDerivator enum value corresponding to the key pair type.

from_hex

pub fn from_hex(derivator: &KeyDerivator, hex_key: &str) -> Result<KeyPair, Error>
Creates a KeyPair from a hexadecimal-encoded secret key.
derivator
KeyDerivator
required
The key derivator type (Ed25519 or Secp256k1)
hex_key
&str
required
Hexadecimal string representation of the secret key
Returns: A Result containing the KeyPair or an Error.

Example

use taple_core::crypto::{KeyPair, KeyGenerator, Ed25519KeyPair};
use taple_core::identifier::derive::KeyDerivator;

// Create a new Ed25519 key pair
let key_pair = KeyPair::Ed25519(Ed25519KeyPair::new());

// Get the key derivator
let derivator = key_pair.get_key_derivator();

// Create from hex
let hex_key = "48s8j34fuadfeuijakqp93d56829ki21";
let kp = KeyPair::from_hex(&KeyDerivator::Ed25519, hex_key)?;

Ed25519KeyPair

pub type Ed25519KeyPair = BaseKeyPair<PublicKey, SecretKey>;
Ed25519 cryptographic key pair implementation using the Ed25519 signature scheme. This is a type alias for BaseKeyPair specialized with Ed25519 key types.

Creating Key Pairs

new

fn new() -> Self
Generates a new random Ed25519 key pair. Returns: A new Ed25519KeyPair with randomly generated keys.

from_seed

fn from_seed(seed: &[u8]) -> Self
Generates an Ed25519 key pair deterministically from a seed.
seed
&[u8]
required
Byte array seed (maximum 32 bytes). Empty slice generates random keys.
Returns: A new Ed25519KeyPair derived from the seed.

from_public_key

fn from_public_key(public_key: &[u8]) -> Self
Creates an Ed25519 key pair from an existing public key (without secret key).
public_key
&[u8]
required
Raw bytes of the Ed25519 public key
Returns: An Ed25519KeyPair with only the public key set.

from_secret_key

fn from_secret_key(secret_key: &[u8]) -> Self
Creates an Ed25519 key pair from an existing secret key.
secret_key
&[u8]
required
Raw bytes of the Ed25519 secret key (32 bytes)
Returns: A complete Ed25519KeyPair with both public and secret keys.

Key Material Access

public_key_bytes

fn public_key_bytes(&self) -> Vec<u8>
Returns the public key as a byte vector. Returns: Vector of bytes representing the public key.

secret_key_bytes

fn secret_key_bytes(&self) -> Vec<u8>
Returns the secret key as a byte vector. Returns: Vector of bytes representing the secret key, or empty vector if secret key is not available.

to_bytes

fn to_bytes(&self) -> Vec<u8>
Returns the complete key pair as bytes (secret key + public key). Returns: 64-byte vector containing both secret and public keys.

Digital Signatures

sign

fn sign(&self, payload: Payload) -> Result<Vec<u8>, Error>
Signs a payload using the Ed25519 signature algorithm.
payload
Payload
required
The data to sign (must be Payload::Buffer variant)
Returns: A Result containing the 64-byte signature or an error.

verify

fn verify(&self, payload: Payload, signature: &[u8]) -> Result<(), Error>
Verifies a signature against a payload.
payload
Payload
required
The original data that was signed
signature
&[u8]
required
The signature bytes to verify
Returns: Ok(()) if verification succeeds, otherwise an error.

Example

use taple_core::crypto::{Ed25519KeyPair, KeyGenerator, Payload, DSA};

// Generate a new key pair
let key_pair = Ed25519KeyPair::new();

// Sign a message
let message = b"Hello, TAPLE!";
let signature = key_pair.sign(Payload::Buffer(message.to_vec()))?;

// Verify the signature
key_pair.verify(Payload::Buffer(message.to_vec()), &signature)?;

// Get key material
let public_key = key_pair.public_key_bytes();
let secret_key = key_pair.secret_key_bytes();

Secp256k1KeyPair

pub type Secp256k1KeyPair = BaseKeyPair<PublicKey, SecretKey>;
Secp256k1 cryptographic key pair implementation using the secp256k1 elliptic curve. This curve is also used in Bitcoin and Ethereum. Available when the secp256k1 feature is enabled.

Creating Key Pairs

new

fn new() -> Self
Generates a new random Secp256k1 key pair. Returns: A new Secp256k1KeyPair with randomly generated keys.

from_seed

fn from_seed(seed: &[u8]) -> Self
Generates a Secp256k1 key pair deterministically from a seed.
seed
&[u8]
required
Byte array seed (maximum 32 bytes). Empty slice generates random keys.
Returns: A new Secp256k1KeyPair derived from the seed.

from_public_key

fn from_public_key(public_key: &[u8]) -> Self
Creates a Secp256k1 key pair from an existing public key.
public_key
&[u8]
required
Raw bytes of the Secp256k1 public key (65 bytes for uncompressed format)
Returns: A Secp256k1KeyPair with only the public key set.

from_secret_key

fn from_secret_key(secret_key: &[u8]) -> Self
Creates a Secp256k1 key pair from an existing secret key.
secret_key
&[u8]
required
Raw bytes of the Secp256k1 secret key (32 bytes)
Returns: A complete Secp256k1KeyPair with both public and secret keys.

Key Material Access

The Secp256k1KeyPair implements the same KeyMaterial trait methods as Ed25519KeyPair:
  • public_key_bytes() - Returns the 65-byte public key
  • secret_key_bytes() - Returns the 32-byte secret key
  • to_bytes() - Returns the complete 97-byte key pair

Digital Signatures

Secp256k1KeyPair implements ECDSA (Elliptic Curve Digital Signature Algorithm):

sign

fn sign(&self, payload: Payload) -> Result<Vec<u8>, Error>
Signs a payload using ECDSA with secp256k1. The payload is hashed with SHA-256 before signing.
payload
Payload
required
The data to sign (must be Payload::Buffer variant)
Returns: A Result containing the 64-byte signature or an error.

verify

fn verify(&self, payload: Payload, signature: &[u8]) -> Result<(), Error>
Verifies an ECDSA signature against a payload.
payload
Payload
required
The original data that was signed
signature
&[u8]
required
The signature bytes to verify
Returns: Ok(()) if verification succeeds, otherwise an error.

Example

use taple_core::crypto::{Secp256k1KeyPair, KeyGenerator, Payload, DSA};

// Generate a new key pair
let key_pair = Secp256k1KeyPair::new();

// Sign a message
let message = b"Transaction data";
let signature = key_pair.sign(Payload::Buffer(message.to_vec()))?;

// Verify the signature
key_pair.verify(Payload::Buffer(message.to_vec()), &signature)?;

Traits

KeyGenerator

Provides methods to initialize key pairs in various ways:
pub trait KeyGenerator: KeyMaterial {
    fn new() -> Self;
    fn from_seed(seed: &[u8]) -> Self;
    fn from_public_key(public_key: &[u8]) -> Self;
    fn from_secret_key(private_key: &[u8]) -> Self;
}

KeyMaterial

Provides access to key material bytes:
pub trait KeyMaterial {
    fn public_key_bytes(&self) -> Vec<u8>;
    fn secret_key_bytes(&self) -> Vec<u8>;
    fn to_bytes(&self) -> Vec<u8>;
    fn to_str(&self) -> String;
}

DSA (Digital Signature Algorithm)

Provides signing and verification capabilities:
pub trait DSA {
    fn sign(&self, payload: Payload) -> Result<Vec<u8>, Error>;
    fn verify(&self, payload: Payload, signature: &[u8]) -> Result<(), Error>;
}

Payload

pub enum Payload {
    Buffer(Vec<u8>),
    BufferArray(Vec<Vec<u8>>),
}
Represents the data to be signed or verified.

BaseKeyPair

pub struct BaseKeyPair<P, K> {
    pub public_key: P,
    pub secret_key: Option<K>,
}
The underlying structure for key pairs. The secret key is optional to support public-key-only instances.

Build docs developers (and LLMs) love