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
//! Three-layer proof verification (ADR-135).
//!
//! - **P1**: Capability existence + rights check (< 1 us, bitmap AND).
//! - **P2**: Structural invariant validation (< 100 us, constant-time).
//! - **P3**: Deep proof — derivation chain integrity (root reachability, epoch monotonicity).
use crate::derivation::DerivationTree;
use crate::error::ProofError;
use crate::table::CapabilityTable;
use rvm_types::CapRights;
/// Nonce ring buffer size for replay prevention.
///
/// Increased from 64 to 4096 to prevent replay attacks that exploit
/// the small ring buffer window (security finding: nonce ring too small).
const NONCE_RING_SIZE: usize = 4096;
/// Policy context for P2 validation.
#[derive(Debug, Clone, Copy)]
pub struct PolicyContext {
/// The expected owner partition ID.
pub expected_owner: u32,
/// Region lower bound (used for bounds checking).
pub region_base: u64,
/// Region upper bound.
pub region_limit: u64,
/// Lease expiry timestamp in nanoseconds.
pub lease_expiry_ns: u64,
/// Current timestamp in nanoseconds.
pub current_time_ns: u64,
/// Maximum delegation depth (typically 8).
pub max_delegation_depth: u8,
/// Nonce for replay prevention.
pub nonce: u64,
}
/// Three-layer proof verifier.
///
/// Encapsulates the epoch and nonce tracker needed for P1/P2/P3 verification.
pub struct ProofVerifier<const N: usize> {
/// Reference epoch for stale-handle detection.
current_epoch: u32,
/// Nonce ring buffer for replay prevention.
nonce_ring: [u64; NONCE_RING_SIZE],
/// Hash-indexed nonce lookup: `nonce_hash[nonce % SIZE]` stores the
/// nonce value for O(1) replay detection instead of O(N) linear scan.
nonce_hash: [u64; NONCE_RING_SIZE],
/// Write position in the nonce ring.
nonce_write_pos: usize,
/// Monotonic watermark: any nonce below this value is rejected
/// outright, even if it has fallen off the ring buffer. This
/// prevents replaying very old nonces after ring eviction.
nonce_watermark: u64,
/// Whether nonce == 0 is allowed to bypass replay checks.
///
/// Default is `false` (zero nonce is rejected). Set to `true` only
/// for boot-time or backwards-compatible contexts where a sentinel
/// nonce is acceptable.
allow_zero_nonce: bool,
}
impl<const N: usize> ProofVerifier<N> {
/// Creates a new proof verifier with the given epoch.
///
/// By default, nonce == 0 is **rejected** (no zero-nonce bypass).
/// Use [`set_allow_zero_nonce`](Self::set_allow_zero_nonce) to enable
/// the sentinel behaviour for boot-time contexts.
#[must_use]
#[allow(clippy::large_stack_arrays)]
pub const fn new(epoch: u32) -> Self {
Self {
current_epoch: epoch,
nonce_ring: [0u64; NONCE_RING_SIZE],
nonce_hash: [0u64; NONCE_RING_SIZE],
nonce_write_pos: 0,
nonce_watermark: 0,
allow_zero_nonce: false,
}
}
/// Set whether nonce == 0 is allowed to bypass replay checks.
pub fn set_allow_zero_nonce(&mut self, allow: bool) {
self.allow_zero_nonce = allow;
}
/// Updates the current epoch.
pub fn set_epoch(&mut self, epoch: u32) {
self.current_epoch = epoch;
}
/// P1: Capability existence + rights check.
///
/// Budget: < 1 us. No allocation. All checks execute regardless of
/// intermediate failures to prevent timing side-channel leakage.
/// The final error returned is deliberately the most generic
/// (`InvalidHandle`) to avoid leaking which check failed.
///
/// # Errors
///
/// Returns [`ProofError::InvalidHandle`] if the handle is invalid.
/// Returns [`ProofError::StaleCapability`] if the epoch does not match.
/// Returns [`ProofError::InsufficientRights`] if the rights are insufficient.
#[inline]
pub fn verify_p1(
&self,
table: &CapabilityTable<N>,
cap_index: u32,
cap_generation: u32,
required_rights: CapRights,
) -> Result<(), ProofError> {
// Run ALL checks unconditionally to prevent timing side channels.
// We accumulate a bitmask of failures rather than early-returning.
let mut fail_mask: u8 = 0;
let lookup_result = table.lookup(cap_index, cap_generation);
// Check 1: Handle validity.
let (epoch_match, rights_match) = if let Ok(slot) = &lookup_result {
// Check 2: Epoch match.
let e = slot.token.epoch() == self.current_epoch;
// Check 3: Rights subset.
let r = slot.token.has_rights(required_rights);
(e, r)
} else {
fail_mask |= 1;
// Still "compute" epoch and rights checks against dummy values
// to keep timing constant. The compiler should not elide these
// because fail_mask is read below.
(false, false)
};
if !epoch_match {
fail_mask |= 2;
}
if !rights_match {
fail_mask |= 4;
}
if fail_mask == 0 {
Ok(())
} else if fail_mask & 1 != 0 {
Err(ProofError::InvalidHandle)
} else if fail_mask & 2 != 0 {
Err(ProofError::StaleCapability)
} else {
Err(ProofError::InsufficientRights)
}
}
/// P2: Structural invariant validation (constant-time).
///
/// Budget: < 100 us. All checks execute regardless of intermediate
/// failures to prevent timing side-channel leakage (ADR-135).
///
/// Checks: ownership chain, region bounds, lease expiry,
/// delegation depth, nonce replay.
///
/// # Errors
///
/// Returns [`ProofError::PolicyViolation`] if any structural check fails.
pub fn verify_p2(
&mut self,
table: &CapabilityTable<N>,
tree: &DerivationTree<N>,
cap_index: u32,
cap_generation: u32,
ctx: &PolicyContext,
) -> Result<(), ProofError> {
let mut valid = true;
// 1. Ownership chain valid.
let owner_ok = table
.lookup(cap_index, cap_generation)
.is_ok_and(|slot| slot.owner.as_u32() == ctx.expected_owner);
valid &= owner_ok;
// 2. Region bounds legal.
valid &= ctx.region_base < ctx.region_limit;
// 3. Lease not expired.
valid &= ctx.current_time_ns <= ctx.lease_expiry_ns;
// 4. Delegation depth within limit.
let depth_ok = tree
.depth(cap_index)
.is_ok_and(|d| d <= ctx.max_delegation_depth);
valid &= depth_ok;
// 5. Nonce not replayed.
let nonce_ok = self.check_nonce(ctx.nonce);
valid &= nonce_ok;
if valid {
self.mark_nonce(ctx.nonce);
Ok(())
} else {
Err(ProofError::PolicyViolation)
}
}
/// P3: Deep proof — derivation chain integrity verification.
///
/// Walks the derivation tree from the given capability back to its
/// root and verifies:
/// 1. Every ancestor is valid (not revoked).
/// 2. Depth decreases monotonically toward the root.
/// 3. Epoch values are non-decreasing from root to leaf.
/// 4. The chain terminates at a root node (depth 0).
/// 5. The chain length does not exceed `max_depth`.
///
/// Budget: < 10 us for depth <= 8 (typical). Worst-case O(depth).
///
/// # Errors
///
/// Returns [`ProofError::DerivationChainBroken`] if the chain is
/// invalid, tampered, or does not reach a root.
pub fn verify_p3(
&self,
table: &CapabilityTable<N>,
tree: &DerivationTree<N>,
cap_index: u32,
cap_generation: u32,
max_depth: u8,
) -> Result<(), ProofError> {
// Verify the capability itself is valid.
let _slot = table
.lookup(cap_index, cap_generation)
.map_err(|_| ProofError::DerivationChainBroken)?;
// Verify the derivation node exists and is valid.
let node = tree
.get(cap_index)
.ok_or(ProofError::DerivationChainBroken)?;
if !node.is_valid {
return Err(ProofError::DerivationChainBroken);
}
// If this IS a root, chain is trivially valid.
if node.depth == 0 {
return Ok(());
}
// Walk the derivation tree up to the root.
let mut current_depth = node.depth;
let mut current_epoch = node.epoch;
let mut steps = 0u8;
// Walk ancestors. The derivation tree uses first-child/next-sibling,
// so we need to find the parent. We do this by scanning for a node
// that has `cap_index` in its children chain.
let mut current_idx = cap_index;
loop {
steps += 1;
if steps > max_depth {
return Err(ProofError::DerivationChainBroken);
}
// Find the parent of current_idx.
let parent_idx = tree.find_parent(current_idx);
match parent_idx {
Some(pidx) => {
let Some(parent) = tree.get(pidx) else {
return Err(ProofError::DerivationChainBroken);
};
// Ancestor must be valid.
if !parent.is_valid {
return Err(ProofError::DerivationChainBroken);
}
// Depth must decrease.
if parent.depth >= current_depth {
return Err(ProofError::DerivationChainBroken);
}
// Epoch must be non-decreasing from root to leaf
// (parent.epoch <= child.epoch).
if parent.epoch > current_epoch {
return Err(ProofError::DerivationChainBroken);
}
if parent.depth == 0 {
// Reached the root — chain is valid.
return Ok(());
}
current_depth = parent.depth;
current_epoch = parent.epoch;
current_idx = pidx;
}
None => {
// No parent found but we're not at root — broken chain.
return Err(ProofError::DerivationChainBroken);
}
}
}
}
/// Checks if a nonce has been used recently.
///
/// Rejects nonces that are below the monotonic watermark (very old
/// nonces that have already fallen off the ring) as well as nonces
/// still present in the ring buffer.
///
/// Nonce == 0 is rejected unless `allow_zero_nonce` is set. This
/// prevents callers from silently skipping replay protection by
/// passing a default/uninitialized nonce value.
fn check_nonce(&self, nonce: u64) -> bool {
if nonce == 0 {
return self.allow_zero_nonce;
}
// Watermark check: reject any nonce below the low-water mark.
if nonce <= self.nonce_watermark {
return false;
}
// O(1) hash-indexed lookup instead of linear scan.
let hash_slot = usize::try_from(nonce % NONCE_RING_SIZE as u64).unwrap_or(0);
if self.nonce_hash[hash_slot] == nonce {
return false;
}
true
}
/// Records a nonce as used and advances the watermark.
fn mark_nonce(&mut self, nonce: u64) {
if nonce == 0 {
return;
}
self.nonce_ring[self.nonce_write_pos] = nonce;
// Populate hash index for O(1) lookup.
let hash_slot = usize::try_from(nonce % NONCE_RING_SIZE as u64).unwrap_or(0);
self.nonce_hash[hash_slot] = nonce;
self.nonce_write_pos = (self.nonce_write_pos + 1) % NONCE_RING_SIZE;
// Advance watermark: the watermark tracks the minimum nonce
// that was evicted from the ring. When we wrap, the oldest
// entry is being overwritten, so we bump the watermark.
if self.nonce_write_pos == 0 {
// We just wrapped. Find the minimum value in the ring
// to set as the new watermark.
let mut min_val = u64::MAX;
for entry in &self.nonce_ring {
if *entry != 0 && *entry < min_val {
min_val = *entry;
}
}
if min_val != u64::MAX && min_val > self.nonce_watermark {
self.nonce_watermark = min_val;
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use rvm_types::{CapToken, CapType, PartitionId};
fn setup() -> (CapabilityTable<64>, DerivationTree<64>, ProofVerifier<64>) {
let table = CapabilityTable::<64>::new();
let tree = DerivationTree::<64>::new();
let verifier = ProofVerifier::<64>::new(0);
(table, tree, verifier)
}
fn all_rights() -> CapRights {
CapRights::READ
.union(CapRights::WRITE)
.union(CapRights::EXECUTE)
.union(CapRights::GRANT)
.union(CapRights::REVOKE)
}
#[test]
fn test_p1_valid() {
let (mut table, _, verifier) = setup();
let owner = PartitionId::new(1);
let token = CapToken::new(100, CapType::Region, all_rights(), 0);
let (idx, gen) = table.insert_root(token, owner, 0).unwrap();
assert!(verifier
.verify_p1(&table, idx, gen, CapRights::READ)
.is_ok());
}
#[test]
fn test_p1_invalid_handle() {
let (table, _, verifier) = setup();
assert_eq!(
verifier.verify_p1(&table, 99, 0, CapRights::READ),
Err(ProofError::InvalidHandle)
);
}
#[test]
fn test_p1_stale_epoch() {
let (mut table, _, verifier) = setup();
let token = CapToken::new(100, CapType::Region, all_rights(), 5);
let (idx, gen) = table.insert_root(token, PartitionId::new(1), 0).unwrap();
assert_eq!(
verifier.verify_p1(&table, idx, gen, CapRights::READ),
Err(ProofError::StaleCapability)
);
}
#[test]
fn test_p1_insufficient_rights() {
let (mut table, _, verifier) = setup();
let token = CapToken::new(100, CapType::Region, CapRights::READ, 0);
let (idx, gen) = table.insert_root(token, PartitionId::new(1), 0).unwrap();
assert_eq!(
verifier.verify_p1(&table, idx, gen, CapRights::WRITE),
Err(ProofError::InsufficientRights)
);
}
#[test]
fn test_p2_all_pass() {
let (mut table, mut tree, mut verifier) = setup();
let token = CapToken::new(100, CapType::Region, all_rights(), 0);
let (idx, gen) = table.insert_root(token, PartitionId::new(1), 0).unwrap();
tree.add_root(idx, 0).unwrap();
let ctx = PolicyContext {
expected_owner: 1,
region_base: 0x1000,
region_limit: 0x2000,
lease_expiry_ns: 1_000_000_000,
current_time_ns: 500_000_000,
max_delegation_depth: 8,
nonce: 42,
};
assert!(verifier.verify_p2(&table, &tree, idx, gen, &ctx).is_ok());
}
#[test]
fn test_p2_nonce_replay() {
let (mut table, mut tree, mut verifier) = setup();
let token = CapToken::new(100, CapType::Region, all_rights(), 0);
let (idx, gen) = table.insert_root(token, PartitionId::new(1), 0).unwrap();
tree.add_root(idx, 0).unwrap();
let ctx = PolicyContext {
expected_owner: 1,
region_base: 0x1000,
region_limit: 0x2000,
lease_expiry_ns: 1_000_000_000,
current_time_ns: 500_000_000,
max_delegation_depth: 8,
nonce: 55,
};
assert!(verifier.verify_p2(&table, &tree, idx, gen, &ctx).is_ok());
assert_eq!(
verifier.verify_p2(&table, &tree, idx, gen, &ctx),
Err(ProofError::PolicyViolation)
);
}
#[test]
fn test_p3_root_passes() {
let (mut table, mut tree, verifier) = setup();
let token = CapToken::new(100, CapType::Region, all_rights(), 0);
let (idx, gen) = table.insert_root(token, PartitionId::new(1), 0).unwrap();
tree.add_root(idx, 0).unwrap();
assert!(verifier.verify_p3(&table, &tree, idx, gen, 8).is_ok());
}
#[test]
fn test_p3_one_level_derivation() {
let (mut table, mut tree, verifier) = setup();
let owner = PartitionId::new(1);
// Create root.
let root_token = CapToken::new(100, CapType::Region, all_rights(), 0);
let (root_idx, _root_gen) = table.insert_root(root_token, owner, 0).unwrap();
tree.add_root(root_idx, 0).unwrap();
// Derive a child.
let child_token = CapToken::new(200, CapType::Region, CapRights::READ, 0);
let (child_idx, child_gen) = table.insert_root(child_token, owner, 0).unwrap();
tree.add_child(root_idx, child_idx, 1, 1).unwrap();
// P3 should follow child → root and succeed.
assert!(verifier
.verify_p3(&table, &tree, child_idx, child_gen, 8)
.is_ok());
}
#[test]
fn test_p3_nonexistent_fails() {
let (table, tree, verifier) = setup();
assert_eq!(
verifier.verify_p3(&table, &tree, 99, 0, 8),
Err(ProofError::DerivationChainBroken),
);
}
#[test]
fn test_p3_revoked_ancestor_fails() {
let (mut table, mut tree, verifier) = setup();
let owner = PartitionId::new(1);
let root_token = CapToken::new(100, CapType::Region, all_rights(), 0);
let (root_idx, _) = table.insert_root(root_token, owner, 0).unwrap();
tree.add_root(root_idx, 0).unwrap();
let child_token = CapToken::new(200, CapType::Region, CapRights::READ, 0);
let (child_idx, child_gen) = table.insert_root(child_token, owner, 0).unwrap();
tree.add_child(root_idx, child_idx, 1, 1).unwrap();
// Revoke the root.
tree.revoke(root_idx).unwrap();
// P3 should fail because root is revoked.
assert_eq!(
verifier.verify_p3(&table, &tree, child_idx, child_gen, 8),
Err(ProofError::DerivationChainBroken),
);
}
#[test]
fn test_nonce_ring_4096_churn() {
// Verify that after filling the 4096-entry ring, old nonces are
// rejected by the monotonic watermark even after eviction.
let (mut table, mut tree, mut verifier) = setup();
let token = CapToken::new(100, CapType::Region, all_rights(), 0);
let (idx, gen) = table.insert_root(token, PartitionId::new(1), 0).unwrap();
tree.add_root(idx, 0).unwrap();
// Insert 4096 nonces (1..=4096).
for i in 1..=4096u64 {
let ctx = PolicyContext {
expected_owner: 1,
region_base: 0x1000,
region_limit: 0x2000,
lease_expiry_ns: 1_000_000_000,
current_time_ns: 500_000_000,
max_delegation_depth: 8,
nonce: i,
};
assert!(verifier.verify_p2(&table, &tree, idx, gen, &ctx).is_ok());
}
// Now insert one more to push nonce 1 out and trigger watermark.
let ctx_new = PolicyContext {
expected_owner: 1,
region_base: 0x1000,
region_limit: 0x2000,
lease_expiry_ns: 1_000_000_000,
current_time_ns: 500_000_000,
max_delegation_depth: 8,
nonce: 4097,
};
assert!(verifier
.verify_p2(&table, &tree, idx, gen, &ctx_new)
.is_ok());
// Nonce 1 should be rejected by the watermark even though it
// has been evicted from the ring.
let ctx_old = PolicyContext {
expected_owner: 1,
region_base: 0x1000,
region_limit: 0x2000,
lease_expiry_ns: 1_000_000_000,
current_time_ns: 500_000_000,
max_delegation_depth: 8,
nonce: 1,
};
assert_eq!(
verifier.verify_p2(&table, &tree, idx, gen, &ctx_old),
Err(ProofError::PolicyViolation)
);
}
#[test]
fn test_watermark_rejects_below_minimum() {
let mut verifier = ProofVerifier::<64>::new(0);
// Manually advance the watermark by filling the ring and wrapping.
// Use nonces 100..100+4096 to set a high watermark.
for i in 100..100 + 4096u64 {
verifier.mark_nonce(i);
}
// Nonce below the watermark should be rejected.
assert!(!verifier.check_nonce(1));
assert!(!verifier.check_nonce(99));
}
}