aion-context 1.0.0

Cryptographically-signed, versioned business-context file format
Documentation
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
// SPDX-License-Identifier: MIT OR Apache-2.0
//! Cryptographic primitives for AION v2
//!
//! This module provides safe, ergonomic wrappers around battle-tested cryptographic
//! libraries. All operations follow Tiger Style principles: explicit error handling,
//! constant-time where applicable, and automatic zeroization of sensitive data.
//!
//! # Cryptographic Algorithms
//!
//! - **Ed25519**: Digital signatures (RFC 8032, 128-bit security level)
//!   - Used for version signing and author authentication
//!   - Deterministic signatures, no nonce generation issues
//!   - Constant-time operations resistant to timing attacks
//!
//! - **ChaCha20-Poly1305**: Authenticated encryption (RFC 8439, 256-bit key)
//!   - AEAD cipher for rules encryption
//!   - Prevents tampering via authentication tag
//!   - Used in TLS 1.3 as mandatory cipher suite
//!
//! - **BLAKE3**: Cryptographic hashing (256-bit output)
//!   - 5x faster than SHA-256
//!   - Parallelizable for large data
//!   - Used for content hashing and integrity checks
//!
//! - **HKDF**: Key derivation function (RFC 5869, NIST approved)
//!   - HMAC-based key derivation with SHA-256
//!   - Derives multiple keys from master secret
//!   - Context separation via info parameter
//!
//! # Security Properties
//!
//! - **No panics**: All errors explicitly handled
//! - **Constant-time**: Ed25519 operations resistant to timing attacks
//! - **Zeroization**: Signing keys automatically cleared on drop
//! - **Entropy**: OS CSPRNG for key/nonce generation
//! - **Standards**: RFC-compliant implementations
//!
//! # Usage Examples
//!
//! ## Digital Signatures
//!
//! ```
//! use aion_context::crypto::SigningKey;
//!
//! // Generate a new signing key
//! let signing_key = SigningKey::generate();
//! let message = b"Version 1: Updated fraud rules";
//!
//! // Sign a message
//! let signature = signing_key.sign(message);
//!
//! // Verify the signature
//! let verifying_key = signing_key.verifying_key();
//! assert!(verifying_key.verify(message, &signature).is_ok());
//! ```
//!
//! ## Authenticated Encryption
//!
//! ```
//! use aion_context::crypto::{generate_nonce, encrypt, decrypt};
//!
//! let key = [0u8; 32];  // In production, use proper key derivation
//! let nonce = generate_nonce();
//! let plaintext = b"sensitive rules data";
//! let aad = b"version metadata";
//!
//! // Encrypt data
//! let ciphertext = encrypt(&key, &nonce, plaintext, aad).unwrap();
//!
//! // Decrypt data
//! let recovered = decrypt(&key, &nonce, &ciphertext, aad).unwrap();
//! assert_eq!(recovered, plaintext);
//! ```
//!
//! ## Hashing
//!
//! ```
//! use aion_context::crypto::{hash, keyed_hash};
//!
//! // Content hashing
//! let data = b"file content";
//! let content_hash = hash(data);
//!
//! // Keyed hashing (MAC)
//! let key = [0u8; 32];
//! let mac = keyed_hash(&key, data);
//! ```
//!
//! ## Key Derivation
//!
//! ```
//! use aion_context::crypto::derive_key;
//!
//! let master_secret = b"high entropy master key";
//! let salt = b"unique salt value";
//! let info = b"encryption-key-v1";
//!
//! let mut derived_key = [0u8; 32];
//! derive_key(master_secret, salt, info, &mut derived_key).unwrap();
//! // derived_key now contains 32 bytes of derived key material
//! assert_eq!(derived_key.len(), 32);
//! ```

use crate::{AionError, Result};
use ed25519_dalek::{Signature as Ed25519Signature, Signer, Verifier};
use rand::RngCore;
use zeroize::Zeroizing;

// Re-export commonly used types
pub use ed25519_dalek::{SigningKey as Ed25519SigningKey, VerifyingKey as Ed25519VerifyingKey};

/// Signing key for Ed25519 signatures
///
/// Automatically zeroized on drop to protect key material.
///
/// # Security
///
/// - 256-bit private key
/// - Constant-time operations
/// - Automatically zeroized when dropped
/// - **Note**: Implements `Clone` for testing convenience, but cloning key material
///   increases exposure. Use sparingly in production code.
///
/// # Examples
///
/// ```
/// use aion_context::crypto::SigningKey;
///
/// let key = SigningKey::generate();
/// let message = b"test message";
/// let signature = key.sign(message);
/// ```
#[derive(Clone)]
pub struct SigningKey(Zeroizing<[u8; 32]>);

impl SigningKey {
    /// Generate a new random signing key using OS entropy
    ///
    /// Uses the operating system's cryptographically secure random number
    /// generator (`/dev/urandom` on Unix, `CryptGenRandom` on Windows).
    #[must_use]
    pub fn generate() -> Self {
        let key = Ed25519SigningKey::generate(&mut rand::rngs::OsRng);
        Self(Zeroizing::new(key.to_bytes()))
    }

    /// Create a signing key from bytes
    ///
    /// # Errors
    ///
    /// Returns `AionError::InvalidPrivateKey` if the bytes are not a valid Ed25519 private key
    pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
        if bytes.len() != 32 {
            return Err(AionError::InvalidPrivateKey {
                reason: format!("expected 32 bytes, got {}", bytes.len()),
            });
        }

        let mut key_bytes = [0u8; 32];
        key_bytes.copy_from_slice(bytes);

        // Validate the key by trying to create an Ed25519SigningKey
        let _validate = Ed25519SigningKey::from_bytes(&key_bytes);

        Ok(Self(Zeroizing::new(key_bytes)))
    }

    /// Get the bytes of this signing key
    ///
    /// Returns a reference to the key bytes
    #[must_use]
    pub fn to_bytes(&self) -> &[u8; 32] {
        &self.0
    }

    /// Sign a message
    ///
    /// Creates an Ed25519 signature over the message using this key.
    ///
    /// # Examples
    ///
    /// ```
    /// use aion_context::crypto::SigningKey;
    ///
    /// let key = SigningKey::generate();
    /// let signature = key.sign(b"message");
    /// assert_eq!(signature.len(), 64);
    /// ```
    #[must_use]
    pub fn sign(&self, message: &[u8]) -> [u8; 64] {
        let signing_key = Ed25519SigningKey::from_bytes(&self.0);
        signing_key.sign(message).to_bytes()
    }

    /// Get the corresponding verifying key
    #[must_use]
    pub fn verifying_key(&self) -> VerifyingKey {
        let signing_key = Ed25519SigningKey::from_bytes(&self.0);
        VerifyingKey(signing_key.verifying_key())
    }
}

/// Verifying key for Ed25519 signatures
///
/// Used to verify signatures created by the corresponding `SigningKey`.
///
/// # Examples
///
/// ```
/// use aion_context::crypto::{SigningKey, VerifyingKey};
///
/// let signing_key = SigningKey::generate();
/// let verifying_key = signing_key.verifying_key();
///
/// let message = b"test";
/// let signature = signing_key.sign(message);
///
/// assert!(verifying_key.verify(message, &signature).is_ok());
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct VerifyingKey(Ed25519VerifyingKey);

impl VerifyingKey {
    /// Create a verifying key from bytes
    ///
    /// # Errors
    ///
    /// Returns `AionError::InvalidPublicKey` if the bytes are not a valid Ed25519 public key
    pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
        if bytes.len() != 32 {
            return Err(AionError::InvalidPublicKey {
                reason: format!("expected 32 bytes, got {}", bytes.len()),
            });
        }

        let mut key_bytes = [0u8; 32];
        key_bytes.copy_from_slice(bytes);

        let key = Ed25519VerifyingKey::from_bytes(&key_bytes).map_err(|e| {
            AionError::InvalidPublicKey {
                reason: e.to_string(),
            }
        })?;

        Ok(Self(key))
    }

    /// Get the bytes of this verifying key
    #[must_use]
    pub fn to_bytes(&self) -> [u8; 32] {
        self.0.to_bytes()
    }

    /// Verify a signature on a message
    ///
    /// # Errors
    ///
    /// Returns `AionError::InvalidSignature` if the signature is invalid or doesn't match the message
    pub fn verify(&self, message: &[u8], signature: &[u8; 64]) -> Result<()> {
        let sig = Ed25519Signature::from_bytes(signature);

        self.0
            .verify(message, &sig)
            .map_err(|_| AionError::InvalidSignature {
                reason: "signature verification failed".to_string(),
            })
    }
}

/// Hash a message using BLAKE3
///
/// Returns a 32-byte (256-bit) cryptographic hash.
///
/// # Examples
///
/// ```
/// use aion_context::crypto::hash;
///
/// let hash = hash(b"Hello, AION!");
/// assert_eq!(hash.len(), 32);
/// ```
#[must_use]
pub fn hash(data: &[u8]) -> [u8; 32] {
    blake3::hash(data).into()
}

/// Keyed hash using BLAKE3
///
/// Provides message authentication (MAC) using a secret key.
///
/// # Examples
///
/// ```
/// use aion_context::crypto::keyed_hash;
///
/// let key = [0u8; 32];
/// let mac = keyed_hash(&key, b"message");
/// assert_eq!(mac.len(), 32);
/// ```
#[must_use]
pub fn keyed_hash(key: &[u8; 32], data: &[u8]) -> [u8; 32] {
    blake3::keyed_hash(key, data).into()
}

/// Derive a key using HKDF-SHA256
///
/// Extracts entropy from input key material and expands it into a derived key.
///
/// # Arguments
///
/// * `ikm` - Input key material
/// * `salt` - Optional salt value (use empty slice if none)
/// * `info` - Context and application-specific information
/// * `output` - Buffer to fill with derived key material
///
/// # Examples
///
/// ```
/// use aion_context::crypto::derive_key;
///
/// let ikm = b"input key material";
/// let salt = b"optional salt";
/// let info = b"application context";
/// let mut output = [0u8; 32];
///
/// derive_key(ikm, salt, info, &mut output).unwrap();
/// ```
///
/// # Errors
///
/// Returns `AionError::InvalidPrivateKey` if key derivation fails (should never happen with valid inputs)
pub fn derive_key(ikm: &[u8], salt: &[u8], info: &[u8], output: &mut [u8]) -> Result<()> {
    use hkdf::Hkdf;
    use sha2::Sha256;

    let hk = Hkdf::<Sha256>::new(Some(salt), ikm);

    hk.expand(info, output)
        .map_err(|_| AionError::InvalidPrivateKey {
            reason: "HKDF expand failed".to_string(),
        })?;

    Ok(())
}

/// Encrypt data using ChaCha20-Poly1305 (AEAD)
///
/// # Arguments
///
/// * `key` - 32-byte encryption key
/// * `nonce` - 12-byte nonce (MUST be unique for each encryption with the same key)
/// * `plaintext` - Data to encrypt
/// * `aad` - Additional authenticated data (not encrypted, but authenticated)
///
/// # Returns
///
/// Ciphertext with authentication tag appended (`plaintext.len()` + 16 bytes)
///
/// # Errors
///
/// Returns `AionError::EncryptionFailed` if encryption fails
///
/// # Security
///
/// **CRITICAL**: Never reuse a nonce with the same key. Use `generate_nonce()` for each encryption.
pub fn encrypt(key: &[u8; 32], nonce: &[u8; 12], plaintext: &[u8], aad: &[u8]) -> Result<Vec<u8>> {
    use chacha20poly1305::{
        aead::{Aead, KeyInit, Payload},
        ChaCha20Poly1305,
    };

    let cipher = ChaCha20Poly1305::new(key.into());
    let payload = Payload {
        msg: plaintext,
        aad,
    };

    cipher
        .encrypt(nonce.into(), payload)
        .map_err(|e| AionError::EncryptionFailed {
            reason: e.to_string(),
        })
}

/// Decrypt data using ChaCha20-Poly1305 (AEAD)
///
/// # Arguments
///
/// * `key` - 32-byte encryption key (same as used for encryption)
/// * `nonce` - 12-byte nonce (same as used for encryption)
/// * `ciphertext` - Encrypted data with authentication tag
/// * `aad` - Additional authenticated data (must match encryption)
///
/// # Returns
///
/// Decrypted plaintext
///
/// # Errors
///
/// Returns `AionError::DecryptionFailed` if:
/// - Authentication tag is invalid (data was tampered with)
/// - Wrong key or nonce used
/// - AAD doesn't match
pub fn decrypt(key: &[u8; 32], nonce: &[u8; 12], ciphertext: &[u8], aad: &[u8]) -> Result<Vec<u8>> {
    use chacha20poly1305::{
        aead::{Aead, KeyInit, Payload},
        ChaCha20Poly1305,
    };

    let cipher = ChaCha20Poly1305::new(key.into());
    let payload = Payload {
        msg: ciphertext,
        aad,
    };

    cipher
        .decrypt(nonce.into(), payload)
        .map_err(|e| AionError::DecryptionFailed {
            reason: e.to_string(),
        })
}

/// Generate a random nonce for ChaCha20-Poly1305
///
/// Uses OS entropy to generate a cryptographically secure 12-byte nonce.
///
/// # Examples
///
/// ```
/// use aion_context::crypto::generate_nonce;
///
/// let nonce = generate_nonce();
/// assert_eq!(nonce.len(), 12);
/// ```
#[must_use]
pub fn generate_nonce() -> [u8; 12] {
    let mut nonce = [0u8; 12];
    rand::rngs::OsRng.fill_bytes(&mut nonce);
    nonce
}

#[cfg(test)]
#[allow(clippy::unwrap_used)] // Tests are allowed to panic
mod tests {
    use super::*;

    mod signing {
        use super::*;

        #[test]
        fn should_generate_signing_key() {
            let key = SigningKey::generate();
            let bytes = key.to_bytes();
            assert_eq!(bytes.len(), 32);
        }

        #[test]
        fn should_create_signing_key_from_bytes() {
            let original = SigningKey::generate();
            let bytes = *original.to_bytes();

            let restored = SigningKey::from_bytes(&bytes).unwrap();
            assert_eq!(*original.to_bytes(), *restored.to_bytes());
        }

        #[test]
        fn should_reject_invalid_key_length() {
            let result = SigningKey::from_bytes(&[0u8; 16]);
            assert!(result.is_err());
        }

        #[test]
        fn should_sign_message() {
            let key = SigningKey::generate();
            let signature = key.sign(b"test message");
            assert_eq!(signature.len(), 64);
        }

        #[test]
        fn should_verify_valid_signature() {
            let key = SigningKey::generate();
            let message = b"test message";
            let signature = key.sign(message);

            let verifying_key = key.verifying_key();
            assert!(verifying_key.verify(message, &signature).is_ok());
        }

        #[test]
        fn should_reject_invalid_signature() {
            let key = SigningKey::generate();
            let message = b"test message";
            let mut signature = key.sign(message);

            // Tamper with signature
            signature[0] ^= 1;

            let verifying_key = key.verifying_key();
            assert!(verifying_key.verify(message, &signature).is_err());
        }

        #[test]
        fn should_reject_wrong_message() {
            let key = SigningKey::generate();
            let signature = key.sign(b"original message");

            let verifying_key = key.verifying_key();
            assert!(verifying_key
                .verify(b"different message", &signature)
                .is_err());
        }

        #[test]
        fn should_serialize_verifying_key() {
            let key = SigningKey::generate();
            let verifying_key = key.verifying_key();
            let bytes = verifying_key.to_bytes();
            assert_eq!(bytes.len(), 32);

            let restored = VerifyingKey::from_bytes(&bytes).unwrap();
            assert_eq!(verifying_key.to_bytes(), restored.to_bytes());
        }
    }

    mod hashing {
        use super::*;

        #[test]
        fn should_hash_data() {
            let hash1 = hash(b"test data");
            assert_eq!(hash1.len(), 32);

            // Same input produces same hash
            let hash2 = hash(b"test data");
            assert_eq!(hash1, hash2);
        }

        #[test]
        fn should_produce_different_hashes_for_different_data() {
            let hash1 = hash(b"data1");
            let hash2 = hash(b"data2");
            assert_ne!(hash1, hash2);
        }

        #[test]
        fn should_create_keyed_hash() {
            let key = [0u8; 32];
            let mac = keyed_hash(&key, b"message");
            assert_eq!(mac.len(), 32);
        }

        #[test]
        fn should_produce_different_macs_with_different_keys() {
            let key1 = [0u8; 32];
            let key2 = [1u8; 32];

            let mac1 = keyed_hash(&key1, b"message");
            let mac2 = keyed_hash(&key2, b"message");
            assert_ne!(mac1, mac2);
        }
    }

    mod key_derivation {
        use super::*;

        #[test]
        fn should_derive_key() {
            let ikm = b"input key material";
            let salt = b"salt";
            let info = b"context";
            let mut output = [0u8; 32];

            derive_key(ikm, salt, info, &mut output).unwrap();

            // Output should not be all zeros
            assert_ne!(output, [0u8; 32]);
        }

        #[test]
        fn should_produce_deterministic_output() {
            let ikm = b"input key material";
            let salt = b"salt";
            let info = b"context";

            let mut output1 = [0u8; 32];
            derive_key(ikm, salt, info, &mut output1).unwrap();

            let mut output2 = [0u8; 32];
            derive_key(ikm, salt, info, &mut output2).unwrap();

            assert_eq!(output1, output2);
        }

        #[test]
        fn should_produce_different_keys_for_different_info() {
            let ikm = b"input key material";
            let salt = b"salt";

            let mut output1 = [0u8; 32];
            derive_key(ikm, salt, b"context1", &mut output1).unwrap();

            let mut output2 = [0u8; 32];
            derive_key(ikm, salt, b"context2", &mut output2).unwrap();

            assert_ne!(output1, output2);
        }
    }

    mod encryption {
        use super::*;

        #[test]
        fn should_encrypt_and_decrypt() {
            let key = [0u8; 32];
            let nonce = generate_nonce();
            let plaintext = b"secret message";
            let aad = b"additional data";

            let ciphertext = encrypt(&key, &nonce, plaintext, aad).unwrap();
            assert_eq!(ciphertext.len(), plaintext.len() + 16); // +16 for auth tag

            let decrypted = decrypt(&key, &nonce, &ciphertext, aad).unwrap();
            assert_eq!(decrypted, plaintext);
        }

        #[test]
        fn should_reject_tampered_ciphertext() {
            let key = [0u8; 32];
            let nonce = generate_nonce();
            let plaintext = b"secret message";
            let aad = b"additional data";

            let mut ciphertext = encrypt(&key, &nonce, plaintext, aad).unwrap();

            // Tamper with ciphertext
            if let Some(byte) = ciphertext.get_mut(0) {
                *byte ^= 1;
            }

            let result = decrypt(&key, &nonce, &ciphertext, aad);
            assert!(result.is_err());
        }

        #[test]
        fn should_reject_wrong_aad() {
            let key = [0u8; 32];
            let nonce = generate_nonce();
            let plaintext = b"secret message";

            let ciphertext = encrypt(&key, &nonce, plaintext, b"aad1").unwrap();

            let result = decrypt(&key, &nonce, &ciphertext, b"aad2");
            assert!(result.is_err());
        }

        #[test]
        fn should_reject_wrong_key() {
            let key1 = [0u8; 32];
            let key2 = [1u8; 32];
            let nonce = generate_nonce();
            let plaintext = b"secret message";
            let aad = b"additional data";

            let ciphertext = encrypt(&key1, &nonce, plaintext, aad).unwrap();

            let result = decrypt(&key2, &nonce, &ciphertext, aad);
            assert!(result.is_err());
        }

        #[test]
        fn should_generate_unique_nonces() {
            let nonce1 = generate_nonce();
            let nonce2 = generate_nonce();
            assert_ne!(nonce1, nonce2);
        }
    }

    mod properties {
        use super::*;
        use hegel::generators as gs;

        #[hegel::test]
        fn prop_sign_verify_roundtrip(tc: hegel::TestCase) {
            let message = tc.draw(gs::binary().max_size(4096));
            let key = SigningKey::generate();
            let signature = key.sign(&message);
            let verifying_key = key.verifying_key();
            assert!(verifying_key.verify(&message, &signature).is_ok());
        }

        #[hegel::test]
        fn prop_verify_rejects_wrong_key(tc: hegel::TestCase) {
            let message = tc.draw(gs::binary().max_size(4096));
            let signer = SigningKey::generate();
            let other = SigningKey::generate();
            let signature = signer.sign(&message);
            assert!(other.verifying_key().verify(&message, &signature).is_err());
        }

        #[hegel::test]
        fn prop_verify_rejects_tampered_message(tc: hegel::TestCase) {
            let mut message = tc.draw(gs::binary().min_size(1).max_size(4096));
            let key = SigningKey::generate();
            let signature = key.sign(&message);
            let max = message
                .len()
                .checked_sub(1)
                .unwrap_or_else(|| std::process::abort());
            let flip_index = tc.draw(gs::integers::<usize>().max_value(max));
            if let Some(byte) = message.get_mut(flip_index) {
                *byte ^= 0x01;
            }
            assert!(key.verifying_key().verify(&message, &signature).is_err());
        }

        #[hegel::test]
        fn prop_hash_is_deterministic(tc: hegel::TestCase) {
            let data = tc.draw(gs::binary().max_size(8192));
            assert_eq!(hash(&data), hash(&data));
        }

        #[hegel::test]
        fn prop_verifying_key_roundtrip_verifies(tc: hegel::TestCase) {
            let message = tc.draw(gs::binary().max_size(4096));
            let signer = SigningKey::generate();
            let original = signer.verifying_key();
            let restored = VerifyingKey::from_bytes(&original.to_bytes())
                .unwrap_or_else(|_| std::process::abort());
            let signature = signer.sign(&message);
            assert!(restored.verify(&message, &signature).is_ok());
        }
    }
}