arcanum-core 0.1.2

Core traits, types, and primitives for the Arcanum cryptographic engine
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//! Core cryptographic traits.
//!
//! These traits define the interfaces for all cryptographic operations in Arcanum.
//! They are designed to be algorithm-agnostic, allowing code to work with any
//! implementation that satisfies the trait bounds.

use crate::error::Result;
use async_trait::async_trait;

// ═══════════════════════════════════════════════════════════════════════════════
// SYMMETRIC ENCRYPTION
// ═══════════════════════════════════════════════════════════════════════════════

/// Trait for symmetric encryption algorithms.
pub trait SymmetricEncrypt {
    /// The size of the key in bytes.
    const KEY_SIZE: usize;
    /// The size of the nonce/IV in bytes.
    const NONCE_SIZE: usize;
    /// The size of the authentication tag in bytes (for AEAD).
    const TAG_SIZE: usize;
    /// Algorithm identifier.
    const ALGORITHM: &'static str;

    /// Encrypt plaintext with the given key and nonce.
    ///
    /// Returns the ciphertext with authentication tag appended.
    fn encrypt(
        key: &[u8],
        nonce: &[u8],
        plaintext: &[u8],
        associated_data: Option<&[u8]>,
    ) -> Result<Vec<u8>>;

    /// Decrypt ciphertext with the given key and nonce.
    ///
    /// Returns the plaintext if authentication succeeds.
    fn decrypt(
        key: &[u8],
        nonce: &[u8],
        ciphertext: &[u8],
        associated_data: Option<&[u8]>,
    ) -> Result<Vec<u8>>;
}

/// Trait for streaming symmetric encryption.
pub trait StreamCipher {
    /// Apply keystream to data (encrypt or decrypt).
    fn apply_keystream(&mut self, data: &mut [u8]);

    /// Seek to a position in the keystream.
    fn seek(&mut self, position: u64);

    /// Get current position in the keystream.
    fn position(&self) -> u64;
}

// ═══════════════════════════════════════════════════════════════════════════════
// ASYMMETRIC ENCRYPTION
// ═══════════════════════════════════════════════════════════════════════════════

/// Trait for asymmetric encryption algorithms.
pub trait AsymmetricEncrypt {
    /// Public key type.
    type PublicKey;
    /// Private key type.
    type PrivateKey;
    /// Algorithm identifier.
    const ALGORITHM: &'static str;

    /// Encrypt data with a public key.
    fn encrypt(public_key: &Self::PublicKey, plaintext: &[u8]) -> Result<Vec<u8>>;

    /// Decrypt data with a private key.
    fn decrypt(private_key: &Self::PrivateKey, ciphertext: &[u8]) -> Result<Vec<u8>>;
}

// ═══════════════════════════════════════════════════════════════════════════════
// KEY EXCHANGE
// ═══════════════════════════════════════════════════════════════════════════════

/// Trait for key exchange algorithms.
pub trait KeyExchange {
    /// Public key type.
    type PublicKey;
    /// Private key type.
    type PrivateKey;
    /// Shared secret type.
    type SharedSecret;
    /// Algorithm identifier.
    const ALGORITHM: &'static str;

    /// Generate a new key pair.
    fn generate_keypair() -> Result<(Self::PrivateKey, Self::PublicKey)>;

    /// Compute shared secret from private key and peer's public key.
    fn compute_shared_secret(
        private_key: &Self::PrivateKey,
        peer_public_key: &Self::PublicKey,
    ) -> Result<Self::SharedSecret>;
}

/// Trait for key encapsulation mechanisms (KEMs).
pub trait KeyEncapsulation {
    /// Public key type.
    type PublicKey;
    /// Private key type.
    type PrivateKey;
    /// Ciphertext type (encapsulated key).
    type Ciphertext;
    /// Shared secret type.
    type SharedSecret;
    /// Algorithm identifier.
    const ALGORITHM: &'static str;

    /// Generate a new key pair.
    fn generate_keypair() -> Result<(Self::PrivateKey, Self::PublicKey)>;

    /// Encapsulate: generate shared secret and ciphertext.
    fn encapsulate(public_key: &Self::PublicKey) -> Result<(Self::Ciphertext, Self::SharedSecret)>;

    /// Decapsulate: recover shared secret from ciphertext.
    fn decapsulate(
        private_key: &Self::PrivateKey,
        ciphertext: &Self::Ciphertext,
    ) -> Result<Self::SharedSecret>;
}

// ═══════════════════════════════════════════════════════════════════════════════
// DIGITAL SIGNATURES
// ═══════════════════════════════════════════════════════════════════════════════

/// Trait for digital signature algorithms.
pub trait Signer {
    /// Public key type.
    type PublicKey;
    /// Private key type.
    type PrivateKey;
    /// Signature type.
    type Signature;
    /// Algorithm identifier.
    const ALGORITHM: &'static str;

    /// Generate a new signing key pair.
    fn generate_keypair() -> Result<(Self::PrivateKey, Self::PublicKey)>;

    /// Sign a message.
    fn sign(private_key: &Self::PrivateKey, message: &[u8]) -> Result<Self::Signature>;

    /// Verify a signature.
    fn verify(
        public_key: &Self::PublicKey,
        message: &[u8],
        signature: &Self::Signature,
    ) -> Result<bool>;
}

/// Trait for batch signature verification.
pub trait BatchVerifier: Signer {
    /// Verify multiple signatures in batch (more efficient than individual verification).
    fn verify_batch(items: &[(&Self::PublicKey, &[u8], &Self::Signature)]) -> Result<bool>;
}

/// Trait for deterministic signatures (RFC 6979).
pub trait DeterministicSigner: Signer {
    /// Sign with deterministic nonce generation.
    fn sign_deterministic(
        private_key: &Self::PrivateKey,
        message: &[u8],
    ) -> Result<Self::Signature>;
}

// ═══════════════════════════════════════════════════════════════════════════════
// HASH FUNCTIONS
// ═══════════════════════════════════════════════════════════════════════════════

/// Trait for hash functions.
pub trait Hash {
    /// The size of the hash output in bytes.
    const OUTPUT_SIZE: usize;
    /// Algorithm identifier.
    const ALGORITHM: &'static str;

    /// Compute hash of data.
    fn hash(data: &[u8]) -> Vec<u8>;

    /// Create a new hasher for incremental hashing.
    fn new() -> Self;

    /// Update hasher with data.
    fn update(&mut self, data: &[u8]);

    /// Finalize and return hash.
    fn finalize(self) -> Vec<u8>;
}

/// Trait for extendable output functions (XOFs).
pub trait ExtendableOutputFunction {
    /// Algorithm identifier.
    const ALGORITHM: &'static str;

    /// Create a new XOF instance.
    fn new() -> Self;

    /// Update with data.
    fn update(&mut self, data: &[u8]);

    /// Read output bytes.
    fn squeeze(&mut self, output: &mut [u8]);

    /// Finalize and read all output.
    fn finalize_xof(self, output_len: usize) -> Vec<u8>;
}

// ═══════════════════════════════════════════════════════════════════════════════
// MESSAGE AUTHENTICATION CODES
// ═══════════════════════════════════════════════════════════════════════════════

/// Trait for message authentication codes (MACs).
pub trait Mac {
    /// The size of the MAC output in bytes.
    const OUTPUT_SIZE: usize;
    /// The size of the key in bytes.
    const KEY_SIZE: usize;
    /// Algorithm identifier.
    const ALGORITHM: &'static str;

    /// Compute MAC of data.
    fn compute(key: &[u8], data: &[u8]) -> Result<Vec<u8>>;

    /// Verify MAC.
    fn verify(key: &[u8], data: &[u8], tag: &[u8]) -> Result<bool>;
}

// ═══════════════════════════════════════════════════════════════════════════════
// KEY DERIVATION
// ═══════════════════════════════════════════════════════════════════════════════

/// Trait for key derivation functions (KDFs).
pub trait KeyDerivation {
    /// Algorithm identifier.
    const ALGORITHM: &'static str;

    /// Derive key material from input.
    fn derive(
        input_key_material: &[u8],
        salt: Option<&[u8]>,
        info: Option<&[u8]>,
        output_length: usize,
    ) -> Result<Vec<u8>>;
}

/// Trait for password-based key derivation functions.
pub trait PasswordBasedKdf {
    /// Parameters type for this KDF.
    type Params;
    /// Algorithm identifier.
    const ALGORITHM: &'static str;

    /// Derive key from password.
    fn derive(
        password: &[u8],
        salt: &[u8],
        params: &Self::Params,
        output_length: usize,
    ) -> Result<Vec<u8>>;

    /// Hash password for storage.
    fn hash_password(password: &[u8], params: &Self::Params) -> Result<String>;

    /// Verify password against hash.
    fn verify_password(password: &[u8], hash: &str) -> Result<bool>;
}

// ═══════════════════════════════════════════════════════════════════════════════
// ZERO-KNOWLEDGE PROOFS
// ═══════════════════════════════════════════════════════════════════════════════

/// Trait for zero-knowledge proof systems.
pub trait ZkProof {
    /// Proof type.
    type Proof;
    /// Public input type.
    type PublicInput;
    /// Private witness type.
    type Witness;
    /// Verification key type.
    type VerificationKey;
    /// Proving key type.
    type ProvingKey;

    /// Generate a proof.
    fn prove(
        proving_key: &Self::ProvingKey,
        public_input: &Self::PublicInput,
        witness: &Self::Witness,
    ) -> Result<Self::Proof>;

    /// Verify a proof.
    fn verify(
        verification_key: &Self::VerificationKey,
        public_input: &Self::PublicInput,
        proof: &Self::Proof,
    ) -> Result<bool>;
}

// ═══════════════════════════════════════════════════════════════════════════════
// SECRET SHARING
// ═══════════════════════════════════════════════════════════════════════════════

/// Trait for secret sharing schemes.
pub trait SecretSharing {
    /// Share type.
    type Share;

    /// Split a secret into shares.
    fn split(secret: &[u8], threshold: usize, total_shares: usize) -> Result<Vec<Self::Share>>;

    /// Reconstruct secret from shares.
    fn reconstruct(shares: &[Self::Share]) -> Result<Vec<u8>>;
}

// ═══════════════════════════════════════════════════════════════════════════════
// THRESHOLD SIGNATURES
// ═══════════════════════════════════════════════════════════════════════════════

/// Trait for threshold signature schemes.
#[async_trait]
pub trait ThresholdSigner {
    /// Key share type.
    type KeyShare;
    /// Signature share type.
    type SignatureShare;
    /// Combined signature type.
    type Signature;
    /// Public key type.
    type PublicKey;

    /// Generate key shares via distributed key generation.
    async fn distributed_keygen(
        threshold: usize,
        total_participants: usize,
    ) -> Result<Vec<Self::KeyShare>>;

    /// Generate a signature share.
    fn sign_share(key_share: &Self::KeyShare, message: &[u8]) -> Result<Self::SignatureShare>;

    /// Combine signature shares into final signature.
    fn combine_signatures(
        shares: &[Self::SignatureShare],
        threshold: usize,
    ) -> Result<Self::Signature>;

    /// Verify the final signature.
    fn verify(
        public_key: &Self::PublicKey,
        message: &[u8],
        signature: &Self::Signature,
    ) -> Result<bool>;
}

// ═══════════════════════════════════════════════════════════════════════════════
// KEYSTORE
// ═══════════════════════════════════════════════════════════════════════════════

/// Trait for key storage backends.
#[async_trait]
pub trait KeyStore: Send + Sync {
    /// Store a key.
    async fn store(&self, id: &str, key: &[u8], metadata: Option<&[u8]>) -> Result<()>;

    /// Retrieve a key.
    async fn retrieve(&self, id: &str) -> Result<Option<Vec<u8>>>;

    /// Delete a key.
    async fn delete(&self, id: &str) -> Result<bool>;

    /// List all key IDs.
    async fn list(&self) -> Result<Vec<String>>;

    /// Check if a key exists.
    async fn exists(&self, id: &str) -> Result<bool>;
}

// ═══════════════════════════════════════════════════════════════════════════════
// RANDOM NUMBER GENERATION
// ═══════════════════════════════════════════════════════════════════════════════

/// Trait for cryptographically secure random number generation.
pub trait SecureRng {
    /// Fill buffer with random bytes.
    fn fill_bytes(&mut self, dest: &mut [u8]);

    /// Generate random bytes.
    fn random_bytes(&mut self, len: usize) -> Vec<u8> {
        let mut buf = vec![0u8; len];
        self.fill_bytes(&mut buf);
        buf
    }

    /// Generate a random u64.
    fn random_u64(&mut self) -> u64 {
        let mut buf = [0u8; 8];
        self.fill_bytes(&mut buf);
        u64::from_le_bytes(buf)
    }

    /// Generate a random u32.
    fn random_u32(&mut self) -> u32 {
        let mut buf = [0u8; 4];
        self.fill_bytes(&mut buf);
        u32::from_le_bytes(buf)
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// HYBRID CRYPTOGRAPHY
// ═══════════════════════════════════════════════════════════════════════════════

/// Trait for hybrid encryption (combining symmetric and asymmetric).
pub trait HybridEncrypt {
    /// Public key type.
    type PublicKey;
    /// Private key type.
    type PrivateKey;

    /// Encrypt with hybrid scheme.
    fn encrypt(public_key: &Self::PublicKey, plaintext: &[u8]) -> Result<Vec<u8>>;

    /// Decrypt with hybrid scheme.
    fn decrypt(private_key: &Self::PrivateKey, ciphertext: &[u8]) -> Result<Vec<u8>>;
}

/// Trait for post-quantum hybrid schemes (classical + PQ).
pub trait PostQuantumHybrid {
    /// Classical public key type.
    type ClassicalPublicKey;
    /// Classical private key type.
    type ClassicalPrivateKey;
    /// Post-quantum public key type.
    type PqPublicKey;
    /// Post-quantum private key type.
    type PqPrivateKey;

    /// Generate hybrid key pair.
    #[allow(clippy::type_complexity)]
    fn generate_keypair() -> Result<(
        (Self::ClassicalPrivateKey, Self::PqPrivateKey),
        (Self::ClassicalPublicKey, Self::PqPublicKey),
    )>;

    /// Encapsulate with hybrid KEM.
    fn encapsulate(
        classical_pk: &Self::ClassicalPublicKey,
        pq_pk: &Self::PqPublicKey,
    ) -> Result<(Vec<u8>, Vec<u8>)>;

    /// Decapsulate with hybrid KEM.
    fn decapsulate(
        classical_sk: &Self::ClassicalPrivateKey,
        pq_sk: &Self::PqPrivateKey,
        ciphertext: &[u8],
    ) -> Result<Vec<u8>>;
}