tenzro_crypto/lib.rs
1//! Cryptographic primitives for Tenzro Network.
2//!
3//! This crate provides cryptographic operations for the Tenzro Network blockchain,
4//! including:
5//!
6//! - **Key Management**: Ed25519, Secp256k1, and Secp256r1 (P-256) keypairs with address derivation
7//! - **Signatures**: Digital signature creation and verification
8//! - **WebAuthn**: FIDO2 / passkey assertion verification, the device-side primitive for self-custody
9//! - **Hashing**: SHA-256 and Keccak-256 (for EVM compatibility)
10//! - **Encryption**: AES-256-GCM symmetric encryption and X25519 key exchange
11//! - **FROST**: Real threshold Ed25519 signing (RFC 9591) where the secret key is never reconstructed
12//!
13//! # Examples
14//!
15//! ## Generating a keypair and signing
16//!
17//! ```
18//! use tenzro_crypto::{KeyPair, KeyType};
19//! use tenzro_crypto::signatures::{Signer, Ed25519SignerImpl};
20//!
21//! # fn main() -> tenzro_crypto::Result<()> {
22//! // Generate an Ed25519 keypair
23//! let keypair = KeyPair::generate(KeyType::Ed25519)?;
24//! let address = keypair.address();
25//!
26//! // Sign a message
27//! let signer = Ed25519SignerImpl::new(keypair)?;
28//! let message = b"Tenzro Network transaction";
29//! let signature = signer.sign(message)?;
30//! # Ok(())
31//! # }
32//! ```
33//!
34//! ## Hashing
35//!
36//! ```
37//! use tenzro_crypto::hash::{sha256, keccak256};
38//!
39//! let data = b"Tenzro Network";
40//! let sha_hash = sha256(data);
41//! let keccak_hash = keccak256(data);
42//! ```
43//!
44//! ## Encryption
45//!
46//! ```
47//! use tenzro_crypto::encryption::{SymmetricKey, X25519KeyPair, envelope_encrypt, envelope_decrypt};
48//!
49//! # fn main() -> tenzro_crypto::Result<()> {
50//! // Symmetric encryption
51//! let key = SymmetricKey::generate();
52//! let plaintext = b"secret data";
53//! let ciphertext = key.encrypt(plaintext)?;
54//! let decrypted = key.decrypt(&ciphertext)?;
55//!
56//! // Envelope encryption (asymmetric)
57//! let recipient = X25519KeyPair::generate();
58//! let envelope = envelope_encrypt(recipient.public_key(), plaintext)?;
59//! let decrypted = envelope_decrypt(&recipient, &envelope)?;
60//! # Ok(())
61//! # }
62//! ```
63//!
64//! ## FROST-Ed25519 Threshold Signing (RFC 9591)
65//!
66//! Real threshold signing — the secret key is **never** materialized in any
67//! single process. Output is a 64-byte standard Ed25519 signature that
68//! verifies under the group public key with `signatures::verify`.
69//!
70//! ```
71//! use tenzro_crypto::frost::{
72//! keygen_with_trusted_dealer, round1_commit, build_signing_package,
73//! round2_sign, aggregate_signature,
74//! };
75//! use tenzro_crypto::signatures;
76//!
77//! # fn main() -> tenzro_crypto::Result<()> {
78//! // 2-of-3 threshold key (trusted dealer; DKG variant in `frost::dkg_part1`).
79//! let (group_pkg, shares) = keygen_with_trusted_dealer(2, 3)?;
80//!
81//! // Round 1: each signer commits.
82//! let (n1, c1) = round1_commit(&shares[0])?;
83//! let (n2, c2) = round1_commit(&shares[1])?;
84//!
85//! // Coordinator builds the signing package over the message.
86//! let message = b"Tenzro Network FROST transaction";
87//! let signing_pkg = build_signing_package(message, &[c1, c2])?;
88//!
89//! // Round 2: each signer produces its signature share.
90//! let s1 = round2_sign(&signing_pkg, &n1, &shares[0])?;
91//! let s2 = round2_sign(&signing_pkg, &n2, &shares[1])?;
92//!
93//! // Aggregate to a single 64-byte standard Ed25519 signature.
94//! let sig = aggregate_signature(&signing_pkg, &[s1, s2], &group_pkg)?;
95//!
96//! // Verifies under the group public key with the standard Ed25519 verifier.
97//! let group_pk = group_pkg.group_public_key.as_public_key();
98//! signatures::verify(&group_pk, message, &sig)?;
99//! # Ok(())
100//! # }
101//! ```
102
103pub mod bls;
104pub mod composite;
105pub mod encryption;
106pub mod error;
107pub mod frost;
108pub mod hash;
109pub mod keys;
110pub mod p256;
111pub mod pq;
112pub mod rng;
113pub mod signatures;
114pub mod vrf;
115pub mod webauthn;
116
117// Re-export commonly used types
118pub use composite::{
119 CompositePublicKey, CompositeSignature, HybridSigner, HybridVerifier, InMemoryHybridSigner,
120 StandardHybridVerifier,
121};
122pub use error::{CryptoError, Result};
123pub use frost::{
124 DkgRound1, DkgRound1Public, DkgRound1Secret, DkgRound2, DkgRound2Secret, DkgRound2Unicast,
125 GroupPublicKey as FrostGroupPublicKey, PublicKeyPackage as FrostPublicKeyPackage,
126 SecretShare as FrostSecretShare, SignatureShare as FrostSignatureShare,
127 SignerIndex as FrostSignerIndex, SigningCommitments as FrostSigningCommitments,
128 SigningNonces as FrostSigningNonces, SigningPackage as FrostSigningPackage,
129 aggregate_signature as frost_aggregate, build_signing_package as frost_build_signing_package,
130 dkg_part1 as frost_dkg_part1, dkg_part2 as frost_dkg_part2, dkg_part3 as frost_dkg_part3,
131 keygen_with_trusted_dealer as frost_keygen, round1_commit as frost_round1_commit,
132 round2_sign as frost_round2_sign,
133};
134pub use hash::{Hash, Hasher, Keccak256, Sha256, keccak256, sha256};
135pub use keys::{Address, KeyPair, KeyType, PublicKey, SecretKey};
136pub use p256::{
137 P256_PUBLIC_KEY_LEN, P256_PUBLIC_KEY_SEC1_LEN, P256_SIGNATURE_LEN, P256KeyPair, P256Signature,
138 P256Signer, P256Verifier, build_p256verify_input,
139};
140pub use pq::{
141 ML_DSA_65_SIG_LEN, ML_DSA_65_VK_LEN, ML_KEM_768_CT_LEN, ML_KEM_768_EK_LEN, ML_KEM_SS_LEN,
142 MlDsaSigningKey, MlKemDecapsulationKey, ml_dsa_verify, ml_kem_encapsulate,
143};
144pub use signatures::{Signature, Signer, Verifier};
145pub use webauthn::{
146 WebAuthnAssertion, WebAuthnCeremonyType, WebAuthnRelyingParty, unwrap_der_signature,
147 verify_webauthn_assertion, webauthn_signed_hash, webauthn_signed_payload,
148};
149
150#[cfg(test)]
151mod tests {
152 use super::*;
153
154 #[test]
155 fn test_end_to_end_ed25519() {
156 use signatures::{Ed25519SignerImpl, Ed25519VerifierImpl};
157
158 // Generate keypair
159 let keypair = KeyPair::generate(KeyType::Ed25519).unwrap();
160 let address = keypair.address();
161
162 // Create signer
163 let signer = Ed25519SignerImpl::new(keypair).unwrap();
164
165 // Sign message
166 let message = b"Tenzro Network test";
167 let signature = signer.sign(message).unwrap();
168
169 // Verify signature
170 let verifier = Ed25519VerifierImpl::new(signer.public_key().clone()).unwrap();
171 verifier.verify(message, &signature).unwrap();
172
173 println!("Address: {}", address);
174 }
175
176 #[test]
177 fn test_end_to_end_secp256k1() {
178 use signatures::{Secp256k1SignerImpl, Secp256k1VerifierImpl};
179
180 // Generate keypair
181 let keypair = KeyPair::generate(KeyType::Secp256k1).unwrap();
182 let address = keypair.address();
183
184 // Create signer
185 let signer = Secp256k1SignerImpl::new(keypair).unwrap();
186
187 // Sign message
188 let message = b"Tenzro Network EVM transaction";
189 let signature = signer.sign(message).unwrap();
190
191 // Verify signature
192 let verifier = Secp256k1VerifierImpl::new(signer.public_key().clone()).unwrap();
193 verifier.verify(message, &signature).unwrap();
194
195 println!("EVM Address: {}", address);
196 }
197
198 #[test]
199 fn test_hash_and_verify() {
200 use signatures::Ed25519SignerImpl;
201
202 let keypair = KeyPair::generate(KeyType::Ed25519).unwrap();
203 let signer = Ed25519SignerImpl::new(keypair).unwrap();
204
205 // Hash data
206 let data = b"Tenzro Network data";
207 let hash = sha256(data);
208
209 // Sign the hash
210 let signature = signer.sign(hash.as_bytes()).unwrap();
211
212 // Verify
213 use signatures::verify;
214 verify(signer.public_key(), hash.as_bytes(), &signature).unwrap();
215 }
216
217 #[test]
218 fn test_encryption_workflow() {
219 use encryption::{SymmetricKey, X25519KeyPair, envelope_decrypt, envelope_encrypt};
220
221 // Symmetric encryption
222 let sym_key = SymmetricKey::generate();
223 let plaintext = b"Tenzro Network confidential data";
224 let ciphertext = sym_key.encrypt(plaintext).unwrap();
225 let decrypted = sym_key.decrypt(&ciphertext).unwrap();
226 assert_eq!(decrypted, plaintext);
227
228 // Envelope encryption
229 let recipient = X25519KeyPair::generate();
230 let envelope = envelope_encrypt(recipient.public_key(), plaintext).unwrap();
231 let decrypted = envelope_decrypt(&recipient, &envelope).unwrap();
232 assert_eq!(decrypted, plaintext);
233 }
234}