cardano_crypto/
lib.rs

1//! # Cardano Crypto
2//!
3//! Pure Rust implementation of Cardano cryptographic primitives.
4//!
5//! This crate provides a unified interface for all Cardano cryptographic operations:
6//! - **VRF** (Verifiable Random Functions) - IETF Draft-03 and Draft-13
7//! - **KES** (Key Evolving Signatures) - Forward-secure signature schemes
8//! - **DSIGN** (Digital Signatures) - Ed25519 and variants
9//! - **Hash** - Blake2b, SHA-2, and other Cardano hash functions
10//! - **Seed** - Deterministic key derivation
11//! - **CBOR** - Optional serialization support
12//!
13//! # Feature Flags
14//!
15//! This crate uses feature flags to allow selective compilation:
16//!
17//! - `std` (default) - Standard library support
18//! - `alloc` - Allocation support for no_std
19//! - `vrf` - VRF implementations (includes `dsign`, `hash`)
20//! - `kes` - KES implementations (includes `dsign`, `hash`)
21//! - `dsign` - Digital signature algorithms (includes `hash`)
22//! - `hash` - Hash functions
23//! - `cbor` - CBOR serialization
24//! - `serde` - Serde serialization for keys/signatures
25//! - `metrics` - Performance metrics collection
26//! - `logging` - Debug logging support
27//!
28//! # Examples
29//!
30//! ## VRF Proof Generation
31//!
32//! ```rust,ignore
33//! use cardano_crypto::vrf::{VrfDraft03, VrfKeyPair};
34//!
35//! let seed = [0u8; 32];
36//! let keypair = VrfKeyPair::from_seed(&seed);
37//! let proof = keypair.prove(b"message")?;
38//! let output = proof.verify(&keypair.public_key(), b"message")?;
39//! ```
40//!
41//! ## KES Signing
42//!
43//! ```rust,ignore
44//! use cardano_crypto::kes::{Sum6Kes, KesAlgorithm};
45//!
46//! let seed = [0u8; 32];
47//! let signing_key = Sum6Kes::gen_key_from_seed(&seed)?;
48//! let signature = Sum6Kes::sign(&signing_key, 0, b"message")?;
49//! ```
50//!
51//! ## Digital Signatures
52//!
53//! ```rust,ignore
54//! use cardano_crypto::dsign::{Ed25519, DsignAlgorithm};
55//!
56//! let seed = [0u8; 32];
57//! let signing_key = Ed25519::gen_key(&seed);
58//! let signature = Ed25519::sign(&signing_key, b"message");
59//! ```
60
61//! ## Crate metadata
62//!
63//! ```rust
64//! use cardano_crypto::{NAME, VERSION};
65//! assert_eq!(NAME, "cardano-crypto");
66//! // VERSION comes from Cargo.toml at build time and should be present
67//! assert!(VERSION.len() > 0);
68//! ```
69
70#![cfg_attr(not(feature = "std"), no_std)]
71#![cfg_attr(docsrs, feature(doc_cfg))]
72#![deny(missing_docs)]
73#![warn(
74    missing_debug_implementations,
75    rust_2018_idioms,
76    unreachable_pub,
77    clippy::all
78)]
79
80#[cfg(feature = "alloc")]
81extern crate alloc;
82
83#[cfg(feature = "std")]
84extern crate std;
85
86// ============================================================================
87// Common utilities and traits
88// ============================================================================
89
90pub mod common;
91
92// ============================================================================
93// Core cryptographic components
94// ============================================================================
95
96#[cfg(feature = "hash")]
97#[cfg_attr(docsrs, doc(cfg(feature = "hash")))]
98pub mod hash;
99
100#[cfg(feature = "seed")]
101#[cfg_attr(docsrs, doc(cfg(feature = "seed")))]
102pub mod seed;
103
104#[cfg(feature = "dsign")]
105#[cfg_attr(docsrs, doc(cfg(feature = "dsign")))]
106pub mod dsign;
107
108#[cfg(feature = "vrf")]
109#[cfg_attr(docsrs, doc(cfg(feature = "vrf")))]
110pub mod vrf;
111
112#[cfg(feature = "kes")]
113#[cfg_attr(docsrs, doc(cfg(feature = "kes")))]
114pub mod kes;
115
116#[cfg(feature = "cbor")]
117#[cfg_attr(docsrs, doc(cfg(feature = "cbor")))]
118pub mod cbor;
119
120/// Key types, serialization, and utilities matching cardano-api
121pub mod key;
122
123// ============================================================================
124// Re-exports for convenience
125// ============================================================================
126
127#[cfg(feature = "hash")]
128pub use hash::{Blake2b224, Blake2b256, Blake2b512, HashAlgorithm};
129
130#[cfg(feature = "dsign")]
131pub use dsign::{
132    ed25519::{Ed25519Signature, Ed25519SigningKey, Ed25519VerificationKey},
133    DsignAlgorithm, DsignKeyPair, DsignSignature, DsignSigningKey, DsignVerificationKey, Ed25519,
134    SignedDsign,
135};
136
137#[cfg(feature = "vrf")]
138pub use vrf::{
139    CertifiedVrf, OutputVrf, VrfAlgorithm, VrfDraft03, VrfDraft13, VrfKeyPair, VrfProof,
140    VrfSigningKey, VrfVerificationKey,
141};
142
143#[cfg(feature = "kes")]
144pub use kes::{
145    CompactSum0Kes, CompactSum1Kes, CompactSum2Kes, CompactSum3Kes, CompactSum4Kes, CompactSum5Kes,
146    CompactSum6Kes, CompactSum7Kes, KesAlgorithm, KesError, KesKeyPair, KesSignature,
147    KesSigningKey, KesVerificationKey, Period, SignKeyWithPeriodKes, SignedKes, SingleKes, Sum0Kes,
148    Sum1Kes, Sum2Kes, Sum3Kes, Sum4Kes, Sum5Kes, Sum6Kes, Sum7Kes,
149};
150
151#[cfg(feature = "cbor")]
152pub use cbor::{
153    // Core CBOR functions
154    decode_bytes,
155    // Hash CBOR
156    decode_hash,
157    // VRF-specific CBOR
158    decode_output_vrf,
159    decode_proof_vrf,
160    // Generic verification key / signature
161    decode_signature,
162    // DSIGN-specific CBOR
163    decode_signature_dsign,
164    // KES-specific CBOR
165    decode_signature_kes,
166    decode_signing_key_dsign,
167    decode_signing_key_kes,
168    decode_signing_key_vrf,
169    decode_verification_key,
170    decode_verification_key_dsign,
171    decode_verification_key_kes,
172    decode_verification_key_vrf,
173    encode_bytes,
174    encode_hash,
175    encode_output_vrf,
176    encode_proof_vrf,
177    encode_signature,
178    encode_signature_dsign,
179    encode_signature_kes,
180    encode_signing_key_dsign,
181    encode_signing_key_kes,
182    encode_signing_key_vrf,
183    encode_verification_key,
184    encode_verification_key_dsign,
185    encode_verification_key_kes,
186    encode_verification_key_vrf,
187    // Size expressions (Hash)
188    encoded_hash_blake2b224_size,
189    encoded_hash_blake2b256_size,
190    // Size expressions (VRF)
191    encoded_output_vrf_size,
192    encoded_proof_vrf_draft03_size,
193    encoded_proof_vrf_draft13_size,
194    encoded_signature_dsign_size,
195    // Size utilities (generic)
196    encoded_signature_size,
197    // Size expressions (KES)
198    encoded_signature_sum6kes_size,
199    // Size expressions (DSIGN)
200    encoded_signing_key_dsign_size,
201    encoded_signing_key_sum6kes_size,
202    encoded_signing_key_vrf_size,
203    encoded_size_bytes,
204    encoded_verification_key_dsign_size,
205    encoded_verification_key_kes_size,
206    encoded_verification_key_size,
207    encoded_verification_key_vrf_size,
208    // Error type
209    CborError,
210    // Traits
211    FromCbor,
212    ToCbor,
213};
214
215#[cfg(feature = "seed")]
216pub use seed::{derive_seed, expand_seed, SecureSeed, Seed, SeedError, SEED_SIZE};
217
218// Re-export key module types
219pub use key::bech32;
220pub use key::text_envelope;
221
222#[cfg(feature = "hash")]
223pub use key::hash::{
224    hash_payment_verification_key, hash_pool_verification_key, hash_raw,
225    hash_stake_verification_key, hash_verification_key, hash_vrf_verification_key,
226    CommitteeColdKeyHash, CommitteeHotKeyHash, DRepKeyHash, GenesisDelegateKeyHash, GenesisKeyHash,
227    KeyHash, PaymentKeyHash, PoolKeyHash, StakeKeyHash, VrfKeyHash, KEY_HASH_SIZE,
228};
229
230#[cfg(feature = "kes")]
231pub use key::kes_period::{
232    is_kes_expired, is_valid_period, kes_expiry_slot, kes_period_info, period_from_slot,
233    slot_from_period, KESPeriod, KESPeriodInfo, KES_MAX_PERIOD_SUM6, KES_SLOTS_PER_PERIOD_MAINNET,
234    KES_SLOTS_PER_PERIOD_TESTNET,
235};
236
237// ============================================================================
238// Crate metadata
239// ============================================================================
240
241/// Crate version
242pub const VERSION: &str = env!("CARGO_PKG_VERSION");
243
244/// Crate name
245pub const NAME: &str = env!("CARGO_PKG_NAME");