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
// SPDX-License-Identifier: MIT OR Apache-2.0
//! Hardware attestation binding — RFC-0026.
//!
//! Ties an aion operational-key epoch to a TEE attestation quote
//! (TPM2, NVIDIA NRAS, Intel TDX, AMD SEV-SNP, AWS Nitro, Arm CCA,
//! Azure Attestation, …) via a record signed by the author's
//! master key.
//!
//! The aion-context crate does **not** verify TEE quotes itself —
//! every platform has a mature library already, and wiring one in
//! as a hard dependency would bloat every consumer. Instead, this
//! module exposes an [`EvidenceVerifier`] trait that callers
//! implement with the platform-specific library of their choice.
//!
//! # Example
//!
//! ```
//! use aion_context::crypto::SigningKey;
//! use aion_context::hw_attestation::{
//!     sign_binding, verify_binding_signature,
//!     AttestationEvidence, AttestationKind,
//! };
//! use aion_context::types::AuthorId;
//!
//! let master = SigningKey::generate();
//! let op = SigningKey::generate();
//! let evidence = AttestationEvidence {
//!     kind: AttestationKind::Tpm2Quote,
//!     nonce: [0u8; 32],
//!     evidence: b"opaque-tpm-quote-bytes".to_vec(),
//! };
//! let binding = sign_binding(
//!     AuthorId::new(1),
//!     0,
//!     op.verifying_key().to_bytes(),
//!     evidence,
//!     &master,
//! );
//! // Full `verify_binding` with a platform verifier requires the
//! // `test-helpers` feature for the built-in test doubles; here we
//! // verify just the master signature, which works without any
//! // platform-specific library.
//! verify_binding_signature(&binding, &master.verifying_key()).unwrap();
//! ```

use crate::crypto::{hash, SigningKey, VerifyingKey};
use crate::types::AuthorId;
use crate::{AionError, Result};

/// Domain separator for hardware-attestation binding messages.
pub const HW_ATTESTATION_DOMAIN: &[u8] = b"AION_V2_KEY_ATTESTATION_V1";

/// Platform whose quote format the evidence bytes carry.
///
/// The crate treats evidence bytes as opaque; this discriminant
/// exists so consumers can dispatch to the right platform
/// verifier.
#[repr(u16)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AttestationKind {
    /// TCG TPM 2.0 `TPMS_ATTEST` quote.
    Tpm2Quote = 1,
    /// NVIDIA Remote Attestation Service token (H100 Confidential
    /// Compute).
    NvidiaNras = 2,
    /// AMD SEV-SNP attestation report.
    AmdSevSnp = 3,
    /// Intel TDX quote.
    IntelTdxReport = 4,
    /// Intel SGX quote.
    IntelSgxReport = 5,
    /// AWS Nitro Enclaves attestation document.
    AwsNitroEnclave = 6,
    /// Arm Confidential Compute Architecture quote.
    ArmCca = 7,
    /// Microsoft Azure Attestation JWT.
    AzureAttestation = 8,
    /// Reserved for consumer-specific evidence formats.
    Custom = 0xFFFF,
}

impl AttestationKind {
    /// Convert a raw `u16` back to a known kind.
    ///
    /// # Errors
    ///
    /// Returns `Err` for discriminants not defined here.
    pub fn from_u16(value: u16) -> Result<Self> {
        match value {
            1 => Ok(Self::Tpm2Quote),
            2 => Ok(Self::NvidiaNras),
            3 => Ok(Self::AmdSevSnp),
            4 => Ok(Self::IntelTdxReport),
            5 => Ok(Self::IntelSgxReport),
            6 => Ok(Self::AwsNitroEnclave),
            7 => Ok(Self::ArmCca),
            8 => Ok(Self::AzureAttestation),
            0xFFFF => Ok(Self::Custom),
            other => Err(AionError::InvalidFormat {
                reason: format!("Unknown attestation kind: {other}"),
            }),
        }
    }
}

/// Opaque evidence blob plus discriminant and freshness nonce.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AttestationEvidence {
    /// Which platform's format `evidence` carries.
    pub kind: AttestationKind,
    /// Freshness nonce — typically the one the TEE embedded in the
    /// quote (so platform verifiers can confirm the quote isn't
    /// replayed).
    pub nonce: [u8; 32],
    /// Platform-specific quote bytes.
    pub evidence: Vec<u8>,
}

/// A [`KeyAttestationBinding`] pairs an aion key epoch with a TEE
/// quote; the master signature commits to `(author_id, epoch,
/// public_key, kind, nonce, BLAKE3(evidence))`.
#[derive(Debug, Clone)]
pub struct KeyAttestationBinding {
    /// Author whose operational key is being attested.
    pub author_id: AuthorId,
    /// Epoch number for that key (matches `KeyEpoch::epoch`).
    pub epoch: u32,
    /// 32-byte operational public key.
    pub public_key: [u8; 32],
    /// TEE evidence.
    pub evidence: AttestationEvidence,
    /// Ed25519 signature by the author's master key over the
    /// canonical binding message.
    pub master_signature: [u8; 64],
}

/// Canonical bytes the master key signs when producing a binding.
#[must_use]
#[allow(clippy::arithmetic_side_effects)] // Fixed-size const arithmetic.
pub fn canonical_binding_message(binding: &KeyAttestationBinding) -> Vec<u8> {
    let evidence_hash = hash(&binding.evidence.evidence);
    let mut msg = Vec::with_capacity(HW_ATTESTATION_DOMAIN.len() + 8 + 4 + 32 + 2 + 32 + 32);
    msg.extend_from_slice(HW_ATTESTATION_DOMAIN);
    msg.extend_from_slice(&binding.author_id.as_u64().to_le_bytes());
    msg.extend_from_slice(&binding.epoch.to_le_bytes());
    msg.extend_from_slice(&binding.public_key);
    msg.extend_from_slice(&(binding.evidence.kind as u16).to_le_bytes());
    msg.extend_from_slice(&binding.evidence.nonce);
    msg.extend_from_slice(&evidence_hash);
    msg
}

/// Produce a signed binding. The caller is responsible for
/// ensuring `public_key` matches the registered `epoch` — this
/// module does not talk to [`crate::key_registry`] directly.
#[must_use]
pub fn sign_binding(
    author: AuthorId,
    epoch: u32,
    public_key: [u8; 32],
    evidence: AttestationEvidence,
    master_key: &SigningKey,
) -> KeyAttestationBinding {
    let mut binding = KeyAttestationBinding {
        author_id: author,
        epoch,
        public_key,
        evidence,
        master_signature: [0u8; 64],
    };
    let message = canonical_binding_message(&binding);
    binding.master_signature = master_key.sign(&message);
    binding
}

/// Verify only the master signature on `binding` — the signed
/// commitment is intact.
///
/// This does **not** run a platform evidence check; callers that
/// need the full story use [`verify_binding`] with an
/// [`EvidenceVerifier`].
///
/// # Errors
///
/// Returns `Err` if the master signature does not verify.
pub fn verify_binding_signature(
    binding: &KeyAttestationBinding,
    master_verifying_key: &VerifyingKey,
) -> Result<()> {
    let message = canonical_binding_message(binding);
    master_verifying_key.verify(&message, &binding.master_signature)
}

/// Trait for platform-specific TEE quote verification.
///
/// Implementations check:
///
/// 1. The TEE quote's internal signatures (vendor root-of-trust).
/// 2. The enclave measurement against an approved policy.
/// 3. That `expected_pubkey` matches the key material baked into
///    the quote.
///
/// aion-context ships three test doubles:
/// [`AcceptAllEvidenceVerifier`], [`RejectAllEvidenceVerifier`],
/// and [`PubkeyPrefixEvidenceVerifier`]. Real platform
/// implementations live in separate crates.
pub trait EvidenceVerifier {
    /// Verify `evidence` and confirm the TEE-attested public key
    /// equals `expected_pubkey`.
    ///
    /// # Errors
    ///
    /// Implementations return `Err` for any failure. Error
    /// content is implementation-defined.
    fn verify(&self, evidence: &AttestationEvidence, expected_pubkey: &[u8; 32]) -> Result<()>;
}

// ---------------------------------------------------------------------------
// Test-double verifiers.
//
// These exist so property tests can exercise the platform-verification branch
// without a real TEE. Gated behind `#[cfg(any(test, feature = "test-helpers"))]`
// so production binaries cannot accidentally depend on `AcceptAllEvidenceVerifier`,
// which would silently accept any quote.
// ---------------------------------------------------------------------------

/// Testing verifier that unconditionally accepts. Useful for
/// exercising the signature path without a real TEE.
#[cfg(any(test, feature = "test-helpers"))]
#[derive(Debug, Clone, Copy, Default)]
pub struct AcceptAllEvidenceVerifier;

#[cfg(any(test, feature = "test-helpers"))]
impl EvidenceVerifier for AcceptAllEvidenceVerifier {
    fn verify(&self, _evidence: &AttestationEvidence, _expected_pubkey: &[u8; 32]) -> Result<()> {
        Ok(())
    }
}

/// Testing verifier that unconditionally rejects.
#[cfg(any(test, feature = "test-helpers"))]
#[derive(Debug, Clone, Copy, Default)]
pub struct RejectAllEvidenceVerifier;

#[cfg(any(test, feature = "test-helpers"))]
impl EvidenceVerifier for RejectAllEvidenceVerifier {
    fn verify(&self, _evidence: &AttestationEvidence, _expected_pubkey: &[u8; 32]) -> Result<()> {
        Err(AionError::InvalidFormat {
            reason: "RejectAllEvidenceVerifier".to_string(),
        })
    }
}

/// Testing verifier that accepts iff `expected_pubkey` appears as
/// a byte-prefix of `evidence.evidence`. A minimal model of
/// "the TEE quote contains the public key" suitable for property
/// tests.
#[cfg(any(test, feature = "test-helpers"))]
#[derive(Debug, Clone, Copy, Default)]
pub struct PubkeyPrefixEvidenceVerifier;

#[cfg(any(test, feature = "test-helpers"))]
impl EvidenceVerifier for PubkeyPrefixEvidenceVerifier {
    fn verify(&self, evidence: &AttestationEvidence, expected_pubkey: &[u8; 32]) -> Result<()> {
        let prefix = evidence
            .evidence
            .get(..32)
            .ok_or_else(|| AionError::InvalidFormat {
                reason: "evidence shorter than 32 bytes".to_string(),
            })?;
        if prefix == expected_pubkey.as_slice() {
            Ok(())
        } else {
            Err(AionError::InvalidFormat {
                reason: "evidence prefix does not match expected pubkey".to_string(),
            })
        }
    }
}

/// Registry-aware binding verification — RFC-0026 / RFC-0034.
///
/// Uses the registered master key for `binding.author_id` to
/// check the master signature, cross-checks `binding.public_key`
/// / `binding.epoch` against the active epoch for the author at
/// `at_version`, then runs the caller-supplied platform evidence
/// verifier.
///
/// # Errors
///
/// Returns `AionError::SignatureVerificationFailed { version: at_version, author }`
/// if the registry has no entry for the author, if there is no
/// active epoch at `at_version`, or if the binding's public key /
/// epoch do not match that active epoch. Returns the underlying
/// error shape if the master signature fails, or if the platform
/// verifier rejects.
pub fn verify_binding<V: EvidenceVerifier>(
    binding: &KeyAttestationBinding,
    registry: &crate::key_registry::KeyRegistry,
    at_version: u64,
    verifier: &V,
) -> Result<()> {
    let signer = binding.author_id;
    let master = registry
        .master_key(signer)
        .ok_or(AionError::SignatureVerificationFailed {
            version: at_version,
            author: signer,
        })?;
    let epoch = registry.active_epoch_at(signer, at_version).ok_or(
        AionError::SignatureVerificationFailed {
            version: at_version,
            author: signer,
        },
    )?;
    if binding.public_key != epoch.public_key || binding.epoch != epoch.epoch {
        return Err(AionError::SignatureVerificationFailed {
            version: at_version,
            author: signer,
        });
    }
    verify_binding_signature(binding, master)?;
    verifier.verify(&binding.evidence, &binding.public_key)
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::indexing_slicing)]
#[allow(deprecated)] // RFC-0034 Phase D: tests exercise the deprecated raw-key verify_binding contract
mod tests {
    use super::*;

    fn sample_evidence() -> AttestationEvidence {
        AttestationEvidence {
            kind: AttestationKind::Tpm2Quote,
            nonce: [0x42u8; 32],
            evidence: b"opaque-tpm-quote-bytes".to_vec(),
        }
    }

    #[test]
    fn signature_round_trip() {
        let master = SigningKey::generate();
        let binding = sign_binding(
            AuthorId::new(1),
            0,
            [0xAAu8; 32],
            sample_evidence(),
            &master,
        );
        verify_binding_signature(&binding, &master.verifying_key()).unwrap();
    }

    #[test]
    fn wrong_master_rejects() {
        let master = SigningKey::generate();
        let other = SigningKey::generate();
        let binding = sign_binding(
            AuthorId::new(1),
            0,
            [0xAAu8; 32],
            sample_evidence(),
            &master,
        );
        assert!(verify_binding_signature(&binding, &other.verifying_key()).is_err());
    }

    #[test]
    fn tampered_evidence_rejects() {
        let master = SigningKey::generate();
        let mut binding = sign_binding(
            AuthorId::new(1),
            0,
            [0xAAu8; 32],
            sample_evidence(),
            &master,
        );
        binding.evidence.evidence[0] ^= 0x01;
        assert!(verify_binding_signature(&binding, &master.verifying_key()).is_err());
    }

    #[test]
    fn tampered_pubkey_rejects() {
        let master = SigningKey::generate();
        let mut binding = sign_binding(
            AuthorId::new(1),
            0,
            [0xAAu8; 32],
            sample_evidence(),
            &master,
        );
        binding.public_key[0] ^= 0x01;
        assert!(verify_binding_signature(&binding, &master.verifying_key()).is_err());
    }

    use crate::key_registry::KeyRegistry;

    /// Build a registry pinning `author` with `master` + `op.verifying_key()`
    /// as the active epoch-0 operational key.
    fn reg_pinning(author: AuthorId, master: &SigningKey, op: &SigningKey) -> KeyRegistry {
        let mut reg = KeyRegistry::new();
        reg.register_author(author, master.verifying_key(), op.verifying_key(), 0)
            .unwrap_or_else(|_| std::process::abort());
        reg
    }

    #[test]
    fn accept_all_verifier_accepts() {
        let author = AuthorId::new(1);
        let master = SigningKey::generate();
        let op = SigningKey::generate();
        let binding = sign_binding(
            author,
            0,
            op.verifying_key().to_bytes(),
            sample_evidence(),
            &master,
        );
        let reg = reg_pinning(author, &master, &op);
        assert!(verify_binding(&binding, &reg, 1, &AcceptAllEvidenceVerifier).is_ok());
    }

    #[test]
    fn reject_all_verifier_rejects_even_valid_signature() {
        let author = AuthorId::new(1);
        let master = SigningKey::generate();
        let op = SigningKey::generate();
        let binding = sign_binding(
            author,
            0,
            op.verifying_key().to_bytes(),
            sample_evidence(),
            &master,
        );
        let reg = reg_pinning(author, &master, &op);
        assert!(verify_binding(&binding, &reg, 1, &RejectAllEvidenceVerifier).is_err());
    }

    #[test]
    fn pubkey_prefix_verifier_matches_prefix_only() {
        let author = AuthorId::new(1);
        let master = SigningKey::generate();
        let op = SigningKey::generate();
        let pk = op.verifying_key().to_bytes();
        // Evidence that starts with the pubkey — prefix matches.
        let mut good_evidence = pk.to_vec();
        good_evidence.extend_from_slice(b"tail");
        let good = AttestationEvidence {
            kind: AttestationKind::Tpm2Quote,
            nonce: [0u8; 32],
            evidence: good_evidence,
        };
        let binding_good = sign_binding(author, 0, pk, good, &master);
        let reg = reg_pinning(author, &master, &op);
        assert!(verify_binding(&binding_good, &reg, 1, &PubkeyPrefixEvidenceVerifier).is_ok());

        // Evidence that does not start with the pubkey.
        let bad = AttestationEvidence {
            kind: AttestationKind::Tpm2Quote,
            nonce: [0u8; 32],
            evidence: vec![0u8; 64],
        };
        let binding_bad = sign_binding(author, 0, pk, bad, &master);
        assert!(verify_binding(&binding_bad, &reg, 1, &PubkeyPrefixEvidenceVerifier).is_err());
    }

    #[test]
    fn attestation_kind_round_trips() {
        for kind in [
            AttestationKind::Tpm2Quote,
            AttestationKind::NvidiaNras,
            AttestationKind::AmdSevSnp,
            AttestationKind::IntelTdxReport,
            AttestationKind::IntelSgxReport,
            AttestationKind::AwsNitroEnclave,
            AttestationKind::ArmCca,
            AttestationKind::AzureAttestation,
            AttestationKind::Custom,
        ] {
            let raw = kind as u16;
            assert_eq!(AttestationKind::from_u16(raw).unwrap(), kind);
        }
        assert!(AttestationKind::from_u16(999).is_err());
    }

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

        fn draw_evidence(tc: &hegel::TestCase) -> AttestationEvidence {
            let bytes = tc.draw(gs::binary().max_size(1024));
            let nonce_vec = tc.draw(gs::binary().min_size(32).max_size(32));
            let mut nonce = [0u8; 32];
            nonce.copy_from_slice(&nonce_vec);
            AttestationEvidence {
                kind: AttestationKind::Tpm2Quote,
                nonce,
                evidence: bytes,
            }
        }

        fn draw_pubkey(tc: &hegel::TestCase) -> [u8; 32] {
            let v = tc.draw(gs::binary().min_size(32).max_size(32));
            let mut pk = [0u8; 32];
            pk.copy_from_slice(&v);
            pk
        }

        #[hegel::test]
        fn prop_binding_signature_roundtrip(tc: hegel::TestCase) {
            let master = SigningKey::generate();
            let author = AuthorId::new(tc.draw(gs::integers::<u64>().min_value(1)));
            let epoch = tc.draw(gs::integers::<u32>());
            let pubkey = draw_pubkey(&tc);
            let evidence = draw_evidence(&tc);
            let binding = sign_binding(author, epoch, pubkey, evidence, &master);
            verify_binding_signature(&binding, &master.verifying_key())
                .unwrap_or_else(|_| std::process::abort());
        }

        #[hegel::test]
        fn prop_binding_rejects_wrong_master(tc: hegel::TestCase) {
            let master = SigningKey::generate();
            let other = SigningKey::generate();
            let author = AuthorId::new(tc.draw(gs::integers::<u64>().min_value(1)));
            let epoch = tc.draw(gs::integers::<u32>());
            let pubkey = draw_pubkey(&tc);
            let evidence = draw_evidence(&tc);
            let binding = sign_binding(author, epoch, pubkey, evidence, &master);
            assert!(verify_binding_signature(&binding, &other.verifying_key()).is_err());
        }

        #[hegel::test]
        fn prop_binding_rejects_tampered_evidence(tc: hegel::TestCase) {
            let master = SigningKey::generate();
            let author = AuthorId::new(tc.draw(gs::integers::<u64>().min_value(1)));
            let epoch = tc.draw(gs::integers::<u32>());
            let pubkey = draw_pubkey(&tc);
            let mut evidence = draw_evidence(&tc);
            // Need at least one byte to tamper.
            if evidence.evidence.is_empty() {
                evidence.evidence.push(0);
            }
            let mut binding = sign_binding(author, epoch, pubkey, evidence, &master);
            let idx = tc.draw(
                gs::integers::<usize>()
                    .max_value(binding.evidence.evidence.len().saturating_sub(1)),
            );
            if let Some(b) = binding.evidence.evidence.get_mut(idx) {
                *b ^= 0x01;
            }
            assert!(verify_binding_signature(&binding, &master.verifying_key()).is_err());
        }

        #[hegel::test]
        fn prop_binding_rejects_tampered_pubkey(tc: hegel::TestCase) {
            let master = SigningKey::generate();
            let author = AuthorId::new(tc.draw(gs::integers::<u64>().min_value(1)));
            let epoch = tc.draw(gs::integers::<u32>());
            let pubkey = draw_pubkey(&tc);
            let evidence = draw_evidence(&tc);
            let mut binding = sign_binding(author, epoch, pubkey, evidence, &master);
            binding.public_key[0] ^= 0x01;
            assert!(verify_binding_signature(&binding, &master.verifying_key()).is_err());
        }

        #[hegel::test]
        fn prop_binding_rejects_tampered_nonce(tc: hegel::TestCase) {
            let master = SigningKey::generate();
            let author = AuthorId::new(tc.draw(gs::integers::<u64>().min_value(1)));
            let epoch = tc.draw(gs::integers::<u32>());
            let pubkey = draw_pubkey(&tc);
            let evidence = draw_evidence(&tc);
            let mut binding = sign_binding(author, epoch, pubkey, evidence, &master);
            binding.evidence.nonce[0] ^= 0x01;
            assert!(verify_binding_signature(&binding, &master.verifying_key()).is_err());
        }

        #[hegel::test]
        fn prop_binding_rejects_tampered_author_or_epoch(tc: hegel::TestCase) {
            let master = SigningKey::generate();
            let author_raw = tc.draw(gs::integers::<u64>().min_value(1).max_value(u64::MAX / 2));
            let epoch = tc.draw(gs::integers::<u32>().max_value(u32::MAX - 1));
            let pubkey = draw_pubkey(&tc);
            let evidence = draw_evidence(&tc);
            let author = AuthorId::new(author_raw);
            let binding = sign_binding(author, epoch, pubkey, evidence, &master);

            // Tamper author.
            let mut b1 = binding.clone();
            b1.author_id = AuthorId::new(author_raw.saturating_add(1));
            assert!(verify_binding_signature(&b1, &master.verifying_key()).is_err());

            // Tamper epoch.
            let mut b2 = binding;
            b2.epoch = epoch.saturating_add(1);
            assert!(verify_binding_signature(&b2, &master.verifying_key()).is_err());
        }

        /// Build a registry pinning `author` at epoch 0 with `op.verifying_key()`.
        /// Property tests that draw arbitrary `epoch` values are re-mapped to
        /// epoch 0 for the binding; the registry still pins the right pubkey.
        fn prop_reg(author: AuthorId, master: &SigningKey, op: &SigningKey) -> KeyRegistry {
            let mut reg = KeyRegistry::new();
            reg.register_author(author, master.verifying_key(), op.verifying_key(), 0)
                .unwrap_or_else(|_| std::process::abort());
            reg
        }

        #[hegel::test]
        fn prop_verify_binding_accept_all_ok(tc: hegel::TestCase) {
            let master = SigningKey::generate();
            let op = SigningKey::generate();
            let author = AuthorId::new(tc.draw(gs::integers::<u64>().min_value(1)));
            let evidence = draw_evidence(&tc);
            let binding = sign_binding(author, 0, op.verifying_key().to_bytes(), evidence, &master);
            let reg = prop_reg(author, &master, &op);
            verify_binding(&binding, &reg, 1, &AcceptAllEvidenceVerifier)
                .unwrap_or_else(|_| std::process::abort());
        }

        #[hegel::test]
        fn prop_verify_binding_reject_all_err(tc: hegel::TestCase) {
            let master = SigningKey::generate();
            let op = SigningKey::generate();
            let author = AuthorId::new(tc.draw(gs::integers::<u64>().min_value(1)));
            let evidence = draw_evidence(&tc);
            let binding = sign_binding(author, 0, op.verifying_key().to_bytes(), evidence, &master);
            let reg = prop_reg(author, &master, &op);
            assert!(verify_binding(&binding, &reg, 1, &RejectAllEvidenceVerifier).is_err());
        }

        #[hegel::test]
        fn prop_pubkey_prefix_verifier_matches_prefix(tc: hegel::TestCase) {
            let master = SigningKey::generate();
            let op = SigningKey::generate();
            let author = AuthorId::new(tc.draw(gs::integers::<u64>().min_value(1)));
            let pubkey = op.verifying_key().to_bytes();
            let tail = tc.draw(gs::binary().max_size(128));
            let mut good_evidence_bytes = pubkey.to_vec();
            good_evidence_bytes.extend_from_slice(&tail);
            let good = AttestationEvidence {
                kind: AttestationKind::Tpm2Quote,
                nonce: [0u8; 32],
                evidence: good_evidence_bytes,
            };
            let binding_good = sign_binding(author, 0, pubkey, good, &master);
            let reg = prop_reg(author, &master, &op);
            verify_binding(&binding_good, &reg, 1, &PubkeyPrefixEvidenceVerifier)
                .unwrap_or_else(|_| std::process::abort());

            // Build a distinct pubkey-shaped prefix and inject it.
            let mut bad_prefix = pubkey;
            bad_prefix[0] ^= 0x01;
            let mut bad_bytes = bad_prefix.to_vec();
            bad_bytes.extend_from_slice(&tail);
            let bad = AttestationEvidence {
                kind: AttestationKind::Tpm2Quote,
                nonce: [0u8; 32],
                evidence: bad_bytes,
            };
            let binding_bad = sign_binding(author, 0, pubkey, bad, &master);
            assert!(verify_binding(&binding_bad, &reg, 1, &PubkeyPrefixEvidenceVerifier).is_err());
        }

        #[hegel::test]
        fn prop_registry_verify_accepts_freshly_bound_key(tc: hegel::TestCase) {
            use crate::key_registry::KeyRegistry;
            let author =
                AuthorId::new(tc.draw(gs::integers::<u64>().min_value(1).max_value(1 << 32)));
            let master = SigningKey::generate();
            let op = SigningKey::generate();
            let mut reg = KeyRegistry::new();
            reg.register_author(author, master.verifying_key(), op.verifying_key(), 0)
                .unwrap_or_else(|_| std::process::abort());
            let evidence = AttestationEvidence {
                kind: AttestationKind::Tpm2Quote,
                nonce: [0x42u8; 32],
                evidence: tc.draw(gs::binary().max_size(128)),
            };
            let binding = sign_binding(author, 0, op.verifying_key().to_bytes(), evidence, &master);
            let at = tc.draw(gs::integers::<u64>().min_value(1).max_value(1 << 20));
            verify_binding(&binding, &reg, at, &AcceptAllEvidenceVerifier)
                .unwrap_or_else(|_| std::process::abort());
        }

        #[hegel::test]
        fn prop_registry_verify_rejects_wrong_master_key(tc: hegel::TestCase) {
            use crate::key_registry::KeyRegistry;
            let author =
                AuthorId::new(tc.draw(gs::integers::<u64>().min_value(1).max_value(1 << 32)));
            let real_master = SigningKey::generate();
            let imposter_master = SigningKey::generate();
            let op = SigningKey::generate();
            // Registry is pinned to the REAL master.
            let mut reg = KeyRegistry::new();
            reg.register_author(author, real_master.verifying_key(), op.verifying_key(), 0)
                .unwrap_or_else(|_| std::process::abort());
            let evidence = AttestationEvidence {
                kind: AttestationKind::Tpm2Quote,
                nonce: [0x99u8; 32],
                evidence: tc.draw(gs::binary().max_size(64)),
            };
            // Binding is signed by the IMPOSTER master — same operational
            // key + epoch, but wrong signer.
            let binding = sign_binding(
                author,
                0,
                op.verifying_key().to_bytes(),
                evidence,
                &imposter_master,
            );
            let at = tc.draw(gs::integers::<u64>().min_value(1).max_value(1 << 20));
            assert!(verify_binding(&binding, &reg, at, &AcceptAllEvidenceVerifier).is_err());
        }
    }
}