Skip to main content

Overview

The xmtp_cryptography crate provides low-level cryptographic primitives and utilities for the XMTP protocol. It includes signature verification, key generation, hashing utilities, and OpenMLS integration.

Installation

Key Exports

tls_codec::SecretVLBytes
Type alias for secret byte arrays with automatic zeroization on drop. Used for securely handling private keys and sensitive cryptographic material.
module
Re-export of the OpenMLS crate for MLS protocol operations.

Core Modules

signature

Signature creation and verification:
  • ECDSA signature recovery
  • Ethereum address validation
  • EIP-191 message signing
  • Signature format conversion

basic_credential

OpenMLS basic credential implementation:
  • Identity credential management
  • Credential serialization/deserialization
  • Integration with MLS key packages

hash

Cryptographic hashing utilities:
  • SHA-256 hashing
  • Message digest computation
  • Hash-based operations

rand

Secure random number generation:
  • Cryptographically secure RNG
  • Random key generation
  • Nonce generation

ethereum

Ethereum-specific cryptography:
  • Wallet signature verification
  • Address derivation
  • Chain-specific operations

configuration

Cryptographic configuration constants:
  • Key lengths
  • Algorithm parameters
  • Protocol constants

utils

Utility functions for cryptographic operations.

Main Types and Traits

enum
Signature types that support public key recovery.Variants:
  • Eip191Signature(Vec<u8>) - Ethereum EIP-191 signature with recovery ID
Methods:
  • recover_address(message: &str) -> Result<String> - Recover signer address
enum
Errors that can occur during signature operations.Variants:
  • BadAddressFormat - Invalid address format
  • BadSignatureFormat - Invalid signature format
  • BadSignature - Signature verification failed
  • Signer - Signer error
  • Unknown - Unknown error
enum
Errors related to identifier validation.Common cases:
  • Invalid Ethereum address format
  • Invalid address length
  • Unsupported identifier type

Signature Functions

fn(Address) -> String
Convert an Ethereum H160 address to a hex string with 0x prefix.
fn(&str) -> bool
Check if a string is a valid Ethereum address (valid hex and length 20).
fn(&Secret) -> Result<[u8; 32]>
Derive a public key from a private key for Ed25519.

Usage Examples

Signature Verification

Address Conversion

Working with Secret Keys

Random Key Generation

Hashing Operations

OpenMLS Integration

Configuration Constants

From the configuration module:
usize
Length of Ed25519 keys in bytes (32).
usize
Standard signature length for various algorithms.

Features

feature
Expose private keys in addition to public keys (use with caution).
feature
Testing utilities and mock implementations.

Platform Support

  • Native (all platforms)
  • WebAssembly (with getrandom support)
    • Requires wasm_js feature for browser environments

Security Considerations

Secret Zeroization

The Secret type automatically zeroizes memory on drop:

Random Number Generation

Always use the provided RNG functions for cryptographic operations:

Signature Validation

Always validate addresses after signature recovery:

Error Handling

Dependencies

Key cryptographic dependencies:
  • libcrux-ed25519 - Ed25519 operations
  • ed25519-dalek - EdDSA signatures
  • openmls - MLS protocol
  • alloy - Ethereum cryptography
  • sha2 - SHA-256 hashing
  • rand - Secure random number generation
  • zeroize - Memory zeroization