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
734
735
736
737
738
739
740
741
742
743
744
745
746
use std::ops::Bound;
use crate::generators::aes_ctr::index::{AesIndex, TableIndex};
use crate::generators::aes_ctr::BYTES_PER_BATCH;
use crate::generators::{widening_mul, ByteCount, BytesPerChild, ChildrenCount, ForkError};
/// A pointer to the next byte to be outputted by the generator.
#[derive(Clone, Copy, Debug, PartialOrd, Ord, PartialEq, Eq)]
pub struct BufferPointer(pub usize);
/// State from which we can at least generate 1 byte
#[derive(Clone, Copy, Debug)]
pub(crate) struct Consumable {
// Stores the index preceding the index of the value
// to be generated on the next call to next
table_index: TableIndex,
// **INCLUSIVE** last valid index
inclusive_last: TableIndex,
buffer_pointer: BufferPointer,
offset: AesIndex,
}
impl Consumable {
fn increment(&mut self) -> ShiftAction {
self.table_index.increment();
self.compute_shift_action(1)
}
#[cfg(test)]
fn skip_bytes(&mut self, amount: ByteCount) {
let (mut increased, overflowed) = self.table_index.overflowing_increased(amount.0);
// Saturate the increased value
if overflowed {
increased = TableIndex::LAST;
} else if increased > self.inclusive_last {
increased = self.inclusive_last;
}
let new_ptr = (self.buffer_pointer.0 as u128).saturating_add(amount.0);
self.buffer_pointer.0 = new_ptr.min(BYTES_PER_BATCH as u128) as usize;
self.table_index = increased;
}
#[inline]
fn compute_shift_action(&mut self, shift: u128) -> ShiftAction {
if shift >= self.left_in_buffer() as u128 {
self.buffer_pointer.0 = self.table_index.byte_index.0;
let index = AesIndex(self.table_index.aes_index.0.wrapping_add(self.offset.0));
ShiftAction::RefreshBatchAndOutputByte(index, self.buffer_pointer)
} else {
self.buffer_pointer.0 += shift as usize;
ShiftAction::OutputByte(self.buffer_pointer)
}
}
fn left_in_buffer(&self) -> usize {
BYTES_PER_BATCH - self.buffer_pointer.0
}
}
/// The current state of a generator using the batched AES-CTR approach.
// Due to wrapping behavior, we need a separation between consumable and consumed state
// as FIRST-1=LAST, LAST+1=FIRST.
#[derive(Debug, Clone, Copy)]
pub(crate) enum State {
/// The generator is fully consumed
Consumed,
/// The generator can produce at least 1 more byte
NotConsumed(Consumable),
}
/// A structure representing the action to be taken by the generator after shifting its state.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ShiftAction {
/// All the bytes that could be generated were generated,
/// there are no more bytes.
NoOutput,
/// Outputs the byte pointed to by the 0-th field.
OutputByte(BufferPointer),
/// Refresh the buffer using the AES index, then output the byte at the buffer
/// pointer
RefreshBatchAndOutputByte(AesIndex, BufferPointer),
}
impl State {
/// Creates a new state that will generate bytes starting from `next_table_index`
/// up to the given `end` bound.
///
/// - `Bound::Included(x)` — last valid byte is at `x`
/// - `Bound::Excluded(x)` — last valid byte is at `x.decremented()`
/// - `Bound::Unbounded` — last valid byte is at `TableIndex::LAST`
///
/// The `offset` AesIndex is applied to all AES encryption: AES(Key, counter + offset).
pub(crate) fn new(
next_table_index: TableIndex,
end: Bound<TableIndex>,
offset: AesIndex,
) -> Self {
let inclusive_last = match end {
Bound::Included(x) => x,
// Excluded(FIRST) means the range is empty, no matter next_table_index
// we cant just decrement it as it would wrap to LAST, leading to an Unbounded
// range
Bound::Excluded(TableIndex::FIRST) => {
return Self::Consumed;
}
Bound::Excluded(x) => x.decremented(),
Bound::Unbounded => TableIndex::LAST,
};
// Strict `>` is correct: `next_table_index == last` means one byte remains (at `last`).
if next_table_index > inclusive_last {
State::Consumed
} else {
State::NotConsumed(Consumable {
table_index: next_table_index.decremented(),
inclusive_last,
buffer_pointer: BufferPointer(BYTES_PER_BATCH),
offset,
})
}
}
pub(crate) fn next(&mut self) -> ShiftAction {
match self {
State::Consumed => ShiftAction::NoOutput,
State::NotConsumed(consumable) => {
let action = consumable.increment();
if consumable.table_index == consumable.inclusive_last {
*self = State::Consumed;
}
action
}
}
}
pub(crate) fn next_table_index(&self) -> Option<TableIndex> {
match self {
State::Consumed => None,
State::NotConsumed(consumable) => Some(consumable.table_index.incremented()),
}
}
pub(crate) fn remaining_bytes(self) -> ByteCount {
match self {
State::Consumed => ByteCount(0),
State::NotConsumed(state) => {
let next = state.table_index.incremented();
let dist = TableIndex::distance(&state.inclusive_last, &next)
.expect("NotConsumed state has next_table_index past last");
ByteCount(dist.0.saturating_add(1))
}
}
}
pub(crate) fn check_fork(
&self,
n_children: ChildrenCount,
n_bytes: BytesPerChild,
) -> Result<(TableIndex, AesIndex, Self), ForkError> {
if n_children.0 == 0 {
return Err(ForkError::ZeroChildrenCount);
}
if n_bytes.0 == 0 {
return Err(ForkError::ZeroBytesPerChild);
}
match self {
State::Consumed => Err(ForkError::ForkTooLarge),
State::NotConsumed(consumable) => {
// Always valid (no overflow) as NotConsumed guarantees at least one byte remains
let first_index = consumable.table_index.incremented();
// Children occupy [first_index, first_index + fork_amount).
// `increased` uses wrapping arithmetic on aes_index, so this may wrap past LAST
// back to [FIRST, first_index). The overflow is detected below.
// If no wrap, the result is in (first_index, LAST].
let children_excluded_bound =
first_index.increased(widening_mul(n_children.0, n_bytes.0));
// Convert excluded bound to inclusive last. If children_excluded_bound == FIRST
// (the fork wrapped exactly to the start), FIRST.decremented() == LAST, which
// correctly represents the last included byte of the children's range.
//
// The fork amount is non-zero (both n_children and n_bytes are >= 1), so the
// minimum fork is 1 byte, giving children_included_last >= first_index at worst
let children_included_last = children_excluded_bound.decremented();
// Overflow detection: if children_included_last < first_index, the excluded
// bound wrapped past LAST.
//
// This check has no false negatives: a wrap cannot land back at or past
// first_index because the fork amount (u64 * u64 < 2^128) is strictly less
// than the table index cycle (2^132 values). A full cycle would require
// a shift >= 2^132, which is impossible with a < 2^128 fork amount (it is even
// impossible with a TableIndex value as its max representable value is (2^132 -1))
//
// We use `increased` + some checks instead of `overflowing_increased` because
// the latter would still need correction for the excluded-to-inclusive
// conversion (e.g., excluded bound overflows to FIRST, inclusive last is LAST).
if children_included_last < first_index
|| children_included_last > consumable.inclusive_last
{
return Err(ForkError::ForkTooLarge);
}
// Children may consume all parent bytes (parent becomes Consumed)
let new_parent_state = if children_included_last == consumable.inclusive_last {
// Cannot call Self::new with children_included_last.incremented() here:
// if last == LAST, incrementing wraps to FIRST, and Self::new would create
// a fresh iterator over the entire space instead of an empty one.
State::Consumed
} else {
let parent_first_index = children_included_last.incremented();
Self::new(
parent_first_index,
Bound::Included(consumable.inclusive_last),
consumable.offset,
)
};
Ok((first_index, consumable.offset, new_parent_state))
}
}
}
#[cfg(test)]
pub(crate) fn last(&self) -> Option<TableIndex> {
match self {
State::Consumed => None,
State::NotConsumed(consumable) => Some(consumable.inclusive_last),
}
}
#[cfg(test)]
fn skip_bytes(&mut self, amount: ByteCount) {
if let State::NotConsumed(state) = self {
state.skip_bytes(amount);
if state.table_index >= state.inclusive_last {
*self = State::Consumed;
}
}
}
/// Advances by `n` positions: skips `n` bytes, then returns the next.
/// Matches `Iterator::nth` semantics: nth(0) returns the next element.
#[cfg(test)]
fn nth(&mut self, n: ByteCount) -> ShiftAction {
self.skip_bytes(ByteCount(n.0));
self.next()
}
}
#[cfg(test)]
mod test {
use std::ops::Bound;
use super::*;
use crate::generators::aes_ctr::index::ByteIndex;
use crate::generators::aes_ctr::BYTES_PER_AES_CALL;
use rand::{thread_rng, Rng};
const REPEATS: usize = 1_000_000;
const SMALLER_REPEATS: usize = 10_000;
fn any_table_index() -> impl Iterator<Item = TableIndex> {
std::iter::repeat_with(|| {
TableIndex::new(
AesIndex(thread_rng().gen()),
ByteIndex(thread_rng().gen::<usize>() % BYTES_PER_AES_CALL),
)
})
}
fn any_usize() -> impl Iterator<Item = usize> {
std::iter::repeat_with(|| thread_rng().gen())
}
fn any_u128() -> impl Iterator<Item = u128> {
std::iter::repeat_with(|| thread_rng().gen())
}
fn any_aes_index() -> impl Iterator<Item = AesIndex> {
std::iter::repeat_with(|| AesIndex(thread_rng().gen()))
}
#[test]
/// Check the property:
/// For all table indices t and offsets,
/// State::new(t, LAST, offset).next()
/// = RefreshBatchAndOutputByte(t.aes_index + offset, t.byte_index)
fn prop_state_new_increment() {
for _ in 0..REPEATS {
let (t, mut s, offset) = any_table_index()
.zip(any_aes_index())
.map(|(t, offset)| {
(
t,
State::new(t, Bound::Included(TableIndex::LAST), offset),
offset,
)
})
.next()
.unwrap();
assert!(matches!(
s.next(),
ShiftAction::RefreshBatchAndOutputByte(t_, BufferPointer(p_))
if t_ == AesIndex(t.aes_index.0.wrapping_add(offset.0)) && p_ == t.byte_index.0
))
}
}
#[test]
/// Check the property:
/// For all table indices t, offsets, and positive integers i,
/// if s = State::new(t, LAST, offset), then after skipping i bytes,
/// s.next_table_index() == t.increased(i).
fn prop_state_increase_table_index() {
for _ in 0..REPEATS {
let (t, mut s, i) = any_table_index()
.zip(any_u128())
.zip(any_aes_index())
.map(|((t, i), offset)| {
(
t,
State::new(t, Bound::Included(TableIndex::LAST), offset),
i,
)
})
.next()
.unwrap();
s.skip_bytes(ByteCount(i));
match s.next_table_index() {
// The increase is in range
Some(idx) => assert_eq!(idx, t.increased(i)),
// No next index means the state is consumed
// Since the bound is LAST is means there was an overflow
None => assert!(t.overflowing_increased(i).1),
}
}
}
#[test]
/// For all table indices t, offsets, and non-negative integers i,
/// State::new(t, LAST, offset).nth(i)
/// = RefreshBatchAndOutputByte(
/// t.increased(i).aes_index + offset,
/// t.increased(i).byte_index)
fn prop_state_nth() {
for _ in 0..REPEATS {
let (t, mut s, i, offset) = any_table_index()
.zip(any_usize())
.zip(any_aes_index())
.map(|((t, i), offset)| {
(
t,
State::new(t, Bound::Included(TableIndex::LAST), offset),
i,
offset,
)
})
.next()
.unwrap();
let expected = t.increased(i as u128);
let action = s.nth(ByteCount(i as u128));
assert!(
matches!(
action,
ShiftAction::RefreshBatchAndOutputByte(idx, BufferPointer(p))
if idx == AesIndex(expected.aes_index.0.wrapping_add(offset.0))
&& p == expected.byte_index.0
),
"nth({i}): got {action:?}, expected RefreshBatch at {expected:?}+{offset:?}",
);
}
}
#[test]
fn prop_state_first_is_last() {
for (first, offset) in any_table_index().zip(any_aes_index()).take(REPEATS) {
let last = first;
let mut s = State::new(first, Bound::Included(last), offset);
assert_eq!(s.remaining_bytes().0, 1);
assert_ne!(s.next(), ShiftAction::NoOutput, "Expected a byte");
assert_eq!(
s.next(),
ShiftAction::NoOutput,
"Expected state to be consumed"
);
}
}
#[test]
/// Check the property: For a state starting at FIRST and ending
/// at LAST=FIRST+n, the state is consumed after outputting n+1 bytes.
fn prop_state_consumed_from_first() {
let mut rng = rand::thread_rng();
for offset in any_aes_index().take(SMALLER_REPEATS) {
let n = rng.gen_range(0..=u16::MAX) as u128;
let first = TableIndex::FIRST;
let last = first.increased(n);
let mut s = State::new(first, Bound::Included(last), offset);
assert_eq!(s.remaining_bytes().0, n + 1);
for i in 0..n + 1 {
assert_ne!(
s.next(),
ShiftAction::NoOutput,
"State returned NoOutput at call {i}/{n}"
);
}
assert_eq!(
s.next(),
ShiftAction::NoOutput,
"State should be consumed after {n} calls"
);
}
}
#[test]
/// Check the property:
/// For all table indices first, n in 0..=u16::MAX,
/// a state spanning n+1 bytes from first to first.increased(n)
/// is exhausted after exactly n+1 calls to next().
fn prop_state_consumed_from_random() {
let mut rng = rand::thread_rng();
for (first_index, offset) in any_table_index().zip(any_aes_index()).take(SMALLER_REPEATS) {
let distance_to_last = TableIndex::distance(&TableIndex::LAST, &first_index)
.unwrap()
.0
.saturating_add(1);
let n = distance_to_last.min(rng.gen_range(0..=u16::MAX as u128));
let last = first_index.increased(n as u128);
let mut s = State::new(first_index, Bound::Included(last), offset);
for i in 0..n + 1 {
assert_ne!(
s.next(),
ShiftAction::NoOutput,
"State returned NoOutput at call {i}/{n}"
);
}
assert_eq!(
s.next(),
ShiftAction::NoOutput,
"State should be consumed after {n} calls"
);
}
}
#[test]
/// Check the property:
/// For a state spanning exactly u128::MAX+1 bytes near the end of the
/// table, after skipping so that exactly n+1 bytes remain, the state is
/// exhausted after n+1 calls to next().
fn prop_state_consumed_skip_to_end() {
let mut rng = rand::thread_rng();
for offset in any_aes_index().take(SMALLER_REPEATS) {
let n = rng.gen_range(0..=u16::MAX) as u128;
// Range of exactly u128::MAX+1 bytes: [LAST - u128::MAX, LAST]
let first = TableIndex::LAST.decreased(u128::MAX);
let mut s = State::new(first, Bound::Unbounded, offset);
// Single skip: u128::MAX - n leaves n+1 bytes remaining
s.skip_bytes(ByteCount(u128::MAX - n));
for i in 0..n + 1 {
assert_ne!(
s.next(),
ShiftAction::NoOutput,
"State returned NoOutput at call {i}/{n}"
);
}
assert_eq!(
s.next(),
ShiftAction::NoOutput,
"State should be consumed after {} calls",
n + 1,
);
}
}
#[test]
fn test_excluded_first_is_immediately_consumed() {
for first in any_table_index().take(10) {
let s = State::new(
first,
Bound::Excluded(TableIndex::FIRST),
AesIndex(rand::random()),
);
assert!(matches!(s, State::Consumed));
assert_eq!(s.remaining_bytes().0, 0);
}
}
#[test]
fn test_start_past_end_is_immediately_consumed() {
let mut rng = rand::thread_rng();
for (offset, last) in any_aes_index().zip(any_table_index()).take(SMALLER_REPEATS) {
let gap = rng.gen_range(1..=u16::MAX) as u128;
let (first, overflowed) = last.overflowing_increased(gap);
if overflowed {
continue;
}
let s = State::new(first, Bound::Included(last), offset);
assert!(matches!(s, State::Consumed));
assert_eq!(s.remaining_bytes().0, 0);
}
}
#[test]
fn test_check_fork_on_consumed_state() {
for offset in any_aes_index().take(SMALLER_REPEATS) {
let s = State::new(
TableIndex::FIRST,
Bound::Excluded(TableIndex::FIRST),
offset,
);
assert!(matches!(s, State::Consumed));
assert!(matches!(
s.check_fork(ChildrenCount(1), BytesPerChild(1)),
Err(ForkError::ForkTooLarge),
));
}
}
#[test]
fn test_check_fork_zero_children_or_bytes() {
for offset in any_aes_index().take(SMALLER_REPEATS) {
let s = State::new(TableIndex::FIRST, Bound::Unbounded, offset);
assert!(matches!(
s.check_fork(ChildrenCount(0), BytesPerChild(1)),
Err(ForkError::ZeroChildrenCount),
));
assert!(matches!(
s.check_fork(ChildrenCount(1), BytesPerChild(0)),
Err(ForkError::ZeroBytesPerChild),
));
}
}
#[test]
fn test_check_fork_boundary() {
let mut rng = rand::thread_rng();
// Test where first=FIRST and last=FIRST+something
for offset in any_aes_index().take(SMALLER_REPEATS) {
let n_children = rng.gen_range(1..=16_u64);
let n_bytes = rng.gen_range(1..=16_u64);
let total = widening_mul(n_children, n_bytes);
let first = TableIndex::FIRST;
{
// State with exactly total+1 bytes: fork takes `total`, parent keeps 1
let last = first.increased(total);
let s = State::new(first, Bound::Included(last), offset);
assert_eq!(s.remaining_bytes().0, total + 1);
let result = s.check_fork(ChildrenCount(n_children), BytesPerChild(n_bytes));
assert!(result.is_ok(), "Fork leaving 1 parent byte should succeed");
let (fork_first, _, parent_state) = result.unwrap();
assert_eq!(fork_first, first);
assert_eq!(parent_state.remaining_bytes().0, 1);
}
{
// State with exactly total bytes: fork takes all, no parent bytes left
let last = first.increased(total - 1);
let s = State::new(first, Bound::Included(last), offset);
assert_eq!(s.remaining_bytes().0, total);
let result = s.check_fork(ChildrenCount(n_children), BytesPerChild(n_bytes));
assert!(
result.is_ok(),
"Fork consuming all parent bytes should be ok"
);
let (ret_first_index, ret_offset, new_parent_state) = result.unwrap();
assert_eq!(ret_first_index, first);
assert_eq!(ret_offset, offset);
assert!(matches!(new_parent_state, State::Consumed));
}
{
// Another way to express fork takes all
let last = first.increased(total);
let s = State::new(first, Bound::Excluded(last), offset);
assert_eq!(s.remaining_bytes().0, total);
let result = s.check_fork(ChildrenCount(n_children), BytesPerChild(n_bytes));
assert!(
result.is_ok(),
"Fork consuming all parent bytes should be ok"
);
let (ret_first_index, ret_offset, new_parent_state) = result.unwrap();
assert_eq!(ret_first_index, first);
assert_eq!(ret_offset, offset);
assert!(matches!(new_parent_state, State::Consumed));
}
}
// Test where last=LAST and first=LAST-something (exercises wrapping in increased())
for offset in any_aes_index().take(SMALLER_REPEATS) {
let n_children = rng.gen_range(1..=16_u64);
let n_bytes = rng.gen_range(1..=16_u64);
let total = widening_mul(n_children, n_bytes);
let last = TableIndex::LAST;
{
let first = last.decreased(total);
let s = State::new(first, Bound::Included(last), offset);
assert_eq!(s.remaining_bytes().0, total + 1);
let result = s.check_fork(ChildrenCount(n_children), BytesPerChild(n_bytes));
assert!(result.is_ok(), "Fork leaving 1 parent byte should succeed");
let (fork_first, _, parent_state) = result.unwrap();
assert_eq!(fork_first, first);
assert_eq!(parent_state.remaining_bytes().0, 1);
}
{
// State with exactly total bytes: fork takes all, no parent bytes left
let first = last.decreased(total - 1);
let s = State::new(first, Bound::Included(last), offset);
assert_eq!(s.remaining_bytes().0, total);
let result = s.check_fork(ChildrenCount(n_children), BytesPerChild(n_bytes));
assert!(
result.is_ok(),
"Fork consuming all parent bytes should be ok"
);
let (ret_first_index, ret_offset, new_parent_state) = result.unwrap();
assert_eq!(ret_first_index, first);
assert_eq!(ret_offset, offset);
assert!(matches!(new_parent_state, State::Consumed));
}
{
// Another way to express fork takes all
let first = last.decreased(total);
let s = State::new(first, Bound::Excluded(last), offset);
assert_eq!(s.remaining_bytes().0, total);
let result = s.check_fork(ChildrenCount(n_children), BytesPerChild(n_bytes));
assert!(
result.is_ok(),
"Fork consuming all parent bytes should be ok"
);
let (ret_first_index, ret_offset, new_parent_state) = result.unwrap();
assert_eq!(ret_first_index, first);
assert_eq!(ret_offset, offset);
assert!(matches!(new_parent_state, State::Consumed));
}
}
}
#[test]
fn test_check_fork_overflow() {
for offset in any_aes_index().take(SMALLER_REPEATS) {
let s = State::new(TableIndex::LAST, Bound::Unbounded, offset);
assert_eq!(s.remaining_bytes().0, 1);
// 2 children * u64::MAX bytes each overflows the table index space
assert!(matches!(
s.check_fork(ChildrenCount(2), BytesPerChild(u64::MAX)),
Err(ForkError::ForkTooLarge),
));
}
}
// Test that skipping by an amount >= than the number of bytes possible
// saturates and more importantly, consumes the state
#[test]
fn prop_state_skip_saturates() {
let mut rng = rand::thread_rng();
for offset in any_aes_index().take(SMALLER_REPEATS) {
let n = rng.gen_range(0..=u128::MAX - 1) as u128;
// Check when the end is unbounded, as this relies on overflow detection
{
// to create a state with n+1 bytes remaining
let first = TableIndex::LAST.decreased(n);
let mut s = State::new(first, Bound::Unbounded, offset);
s.skip_bytes(ByteCount(n));
assert_ne!(s.next(), ShiftAction::NoOutput); // One valid byte
assert_eq!(s.next(), ShiftAction::NoOutput);
let mut s = State::new(first, Bound::Unbounded, offset);
s.skip_bytes(ByteCount(n + 1));
assert_eq!(s.next(), ShiftAction::NoOutput); // Not even one valid byte
let mut s = State::new(first, Bound::Unbounded, offset);
let skip = rng.gen_range(n + 1..=u128::MAX);
s.skip_bytes(ByteCount(skip));
assert_eq!(s.next(), ShiftAction::NoOutput); // Not even one valid byte
}
// Test with a bounded (Inclusive)
{
// to create a state with n+1 bytes remaining
let last = TableIndex::LAST.decreased(n);
let first = last.decreased(n);
let mut s = State::new(first, Bound::Included(last), offset);
s.skip_bytes(ByteCount(n));
assert_ne!(s.next(), ShiftAction::NoOutput); // One valid byte
assert_eq!(s.next(), ShiftAction::NoOutput);
let mut s = State::new(first, Bound::Included(last), offset);
s.skip_bytes(ByteCount(n + 1));
assert_eq!(s.next(), ShiftAction::NoOutput); // Not even one valid byte
let mut s = State::new(first, Bound::Included(last), offset);
let skip = rng.gen_range(n + 1..=u128::MAX);
s.skip_bytes(ByteCount(skip));
assert_eq!(s.next(), ShiftAction::NoOutput); // Not even one valid byte
}
// Test with a bounded (Exclusive)
{
// to create a state with n+1 bytes remaining
let last = TableIndex::LAST.decreased(n);
let first = last.decreased(n);
let mut s = State::new(first, Bound::Excluded(last), offset);
s.skip_bytes(ByteCount(n));
assert_eq!(s.next(), ShiftAction::NoOutput); // Not even one valid byte
let mut s = State::new(first, Bound::Excluded(last), offset);
s.skip_bytes(ByteCount(n + 1));
assert_eq!(s.next(), ShiftAction::NoOutput); // Not even one valid byte
let mut s = State::new(first, Bound::Excluded(last), offset);
let skip = rng.gen_range(n + 1..=u128::MAX);
s.skip_bytes(ByteCount(skip));
assert_eq!(s.next(), ShiftAction::NoOutput); // Not even one valid byte
}
}
}
}