tacet 0.4.2

Detect timing side channels in cryptographic code
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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
//! Unit tests for the timing_test_checked! macro.
//!
//! This file tests various invocation patterns of the macro to ensure
//! it correctly handles all syntax variations.

#![allow(clippy::redundant_closure)]

use std::time::Duration;
use tacet::{timing_test, timing_test_checked, AttackerModel, Outcome, TimingOracle};

// ===========================================================================
// Basic Invocation Tests
// ===========================================================================

/// Test minimal macro invocation with just required fields.
#[test]
fn macro_minimal_syntax() {
    let result = timing_test_checked! {
        baseline: || 42u64,
        sample: || rand::random::<u64>(),
        measure: |input| {
            std::hint::black_box(input);
        },
    };

    assert!(
        result.is_measurable() || matches!(result, Outcome::Unmeasurable { .. }),
        "Macro should produce valid Outcome"
    );
}

/// Test macro with custom oracle configuration.
#[test]
fn macro_with_oracle() {
    let result = timing_test_checked! {
        oracle: TimingOracle::for_attacker(AttackerModel::AdjacentNetwork).time_budget(Duration::from_secs(10)),
        baseline: || 0u8,
        sample: || rand::random::<u8>(),
        measure: |input| {
            std::hint::black_box(input);
        },
    };

    assert!(result.is_measurable() || matches!(result, Outcome::Unmeasurable { .. }));
}

/// Test macro with pre-measurement setup done before the macro.
/// Variables are captured from outer scope for shared state.
#[test]
fn macro_with_pre_setup() {
    // Pre-measurement work (e.g., cache warming) is done before the macro
    let key = [0u8; 16];
    for _ in 0..10 {
        std::hint::black_box(&key);
    }

    let result = timing_test_checked! {
        oracle: TimingOracle::for_attacker(AttackerModel::AdjacentNetwork).time_budget(Duration::from_secs(10)),
        baseline: || [0u8; 16],
        sample: || rand::random::<[u8; 16]>(),
        measure: |input| {
            // key is captured from outer scope
            std::hint::black_box((&key, input));
        },
    };

    assert!(result.is_measurable() || matches!(result, Outcome::Unmeasurable { .. }));
}

/// Test macro with captured variables from outer scope.
#[test]
fn macro_with_captures() {
    // Variables defined BEFORE macro are capturable
    let multiplier = 42u64;

    let result = timing_test_checked! {
        oracle: TimingOracle::for_attacker(AttackerModel::AdjacentNetwork).time_budget(Duration::from_secs(10)),
        baseline: || 1u64,
        sample: || rand::random::<u64>(),
        measure: |input| {
            // Use variable from outer scope
            let _ = std::hint::black_box(input.wrapping_mul(multiplier));
        },
    };

    assert!(result.is_measurable() || matches!(result, Outcome::Unmeasurable { .. }));
}

/// Test macro with oracle and captures from outer scope.
#[test]
fn macro_all_fields() {
    // Capture from outer scope
    let secret = [0xFFu8; 32];

    let result = timing_test_checked! {
        oracle: TimingOracle::for_attacker(AttackerModel::AdjacentNetwork).time_budget(Duration::from_secs(10)).max_samples(1_000),
        baseline: || [0u8; 32],
        sample: || rand::random::<[u8; 32]>(),
        measure: |input| {
            // Simulate XOR comparison using captured secret
            let mut acc = 0u8;
            for i in 0..32 {
                acc |= secret[i] ^ input[i];
            }
            std::hint::black_box(acc);
        },
    };

    assert!(result.is_measurable() || matches!(result, Outcome::Unmeasurable { .. }));
}

// ===========================================================================
// Field Order Tests
// ===========================================================================

/// Test that field order doesn't matter (oracle at end).
#[test]
fn macro_field_order_oracle_last() {
    let result = timing_test_checked! {
        baseline: || 0u8,
        sample: || rand::random::<u8>(),
        measure: |input| {
            std::hint::black_box(input);
        },
        oracle: TimingOracle::for_attacker(AttackerModel::AdjacentNetwork).time_budget(Duration::from_secs(10)),
    };

    assert!(result.is_measurable() || matches!(result, Outcome::Unmeasurable { .. }));
}

/// Test that field order doesn't matter (measure before baseline/sample).
#[test]
fn macro_field_order_measure_first() {
    let result = timing_test_checked! {
        oracle: TimingOracle::for_attacker(AttackerModel::AdjacentNetwork).time_budget(Duration::from_secs(10)),
        measure: |input| {
            std::hint::black_box(input);
        },
        baseline: || 0u8,
        sample: || rand::random::<u8>(),
    };

    assert!(result.is_measurable() || matches!(result, Outcome::Unmeasurable { .. }));
}

// ===========================================================================
// Input Type Tests
// ===========================================================================

/// Test macro with tuple inputs.
#[test]
#[ignore] // Slow (31s) - just tests tuple type, run with --ignored
fn macro_tuple_input() {
    let result = timing_test_checked! {
        oracle: TimingOracle::for_attacker(AttackerModel::AdjacentNetwork).time_budget(Duration::from_secs(10)),
        baseline: || ([0u8; 12], [0u8; 64]),
        sample: || (rand::random::<[u8; 12]>(), rand::random::<[u8; 64]>()),
        measure: |(nonce, plaintext)| {
            std::hint::black_box((nonce, plaintext));
        },
    };

    assert!(result.is_measurable() || matches!(result, Outcome::Unmeasurable { .. }));
}

/// Test macro with Vec inputs.
#[test]
#[ignore] // Slow (28s) - just tests Vec type, run with --ignored
fn macro_vec_input() {
    let result = timing_test_checked! {
        oracle: TimingOracle::for_attacker(AttackerModel::AdjacentNetwork).time_budget(Duration::from_secs(10)),
        baseline: || vec![0u8; 64],
        sample: || (0..64).map(|_| rand::random::<u8>()).collect::<Vec<_>>(),
        measure: |input| {
            std::hint::black_box(&input[..]);
        },
    };

    assert!(result.is_measurable() || matches!(result, Outcome::Unmeasurable { .. }));
}

/// Test macro with String input.
#[test]
fn macro_string_input() {
    let result = timing_test_checked! {
        oracle: TimingOracle::for_attacker(AttackerModel::AdjacentNetwork).time_budget(Duration::from_secs(10)),
        baseline: || String::from("constant_password"),
        sample: || format!("random_{:016x}", rand::random::<u64>()),
        measure: |input| {
            std::hint::black_box(input.len());
        },
    };

    assert!(result.is_measurable() || matches!(result, Outcome::Unmeasurable { .. }));
}

/// Test macro with primitive u8.
#[test]
fn macro_primitive_u8() {
    let result = timing_test_checked! {
        oracle: TimingOracle::for_attacker(AttackerModel::AdjacentNetwork).time_budget(Duration::from_secs(10)),
        baseline: || 0u8,
        sample: || rand::random::<u8>(),
        measure: |input| {
            std::hint::black_box(*input);
        },
    };

    assert!(result.is_measurable() || matches!(result, Outcome::Unmeasurable { .. }));
}

/// Test macro with larger arrays.
#[test]
#[ignore] // Slow (87s) - just tests array size, redundant with crypto tests
fn macro_large_array() {
    let result = timing_test_checked! {
        oracle: TimingOracle::for_attacker(AttackerModel::AdjacentNetwork).time_budget(Duration::from_secs(10)),
        baseline: || [0u8; 256],
        sample: || {
            let mut arr = [0u8; 256];
            for b in &mut arr {
                *b = rand::random();
            }
            arr
        },
        measure: |input| {
            std::hint::black_box(&input[..]);
        },
    };

    assert!(result.is_measurable() || matches!(result, Outcome::Unmeasurable { .. }));
}

// ===========================================================================
// Oracle Configuration Tests
// ===========================================================================

/// Test macro with balanced oracle preset.
#[test]
fn macro_oracle_balanced() {
    let result = timing_test_checked! {
        oracle: TimingOracle::for_attacker(AttackerModel::AdjacentNetwork).time_budget(Duration::from_secs(30)),
        baseline: || 0u64,
        sample: || rand::random::<u64>(),
        measure: |input| {
            std::hint::black_box(input);
        },
    };

    assert!(result.is_measurable() || matches!(result, Outcome::Unmeasurable { .. }));
}

/// Test macro with calibration oracle preset.
#[test]
fn macro_oracle_calibration() {
    let result = timing_test_checked! {
        oracle: TimingOracle::for_attacker(AttackerModel::Research).time_budget(Duration::from_secs(5)),
        baseline: || 0u64,
        sample: || rand::random::<u64>(),
        measure: |input| {
            std::hint::black_box(input);
        },
    };

    assert!(result.is_measurable() || matches!(result, Outcome::Unmeasurable { .. }));
}

/// Test macro with chained oracle configuration.
#[test]
fn macro_oracle_chained() {
    let result = timing_test_checked! {
        oracle: TimingOracle::for_attacker(AttackerModel::AdjacentNetwork).time_budget(Duration::from_secs(10)).max_samples(2_000).warmup(100),
        baseline: || 0u64,
        sample: || rand::random::<u64>(),
        measure: |input| {
            std::hint::black_box(input);
        },
    };

    assert!(result.is_measurable() || matches!(result, Outcome::Unmeasurable { .. }));
}

// ===========================================================================
// Complex Test Bodies
// ===========================================================================

/// Test macro with multi-statement measure body.
#[test]
fn macro_complex_measure_body() {
    let result = timing_test_checked! {
        oracle: TimingOracle::for_attacker(AttackerModel::AdjacentNetwork).time_budget(Duration::from_secs(10)),
        baseline: || [0u8; 32],
        sample: || rand::random::<[u8; 32]>(),
        measure: |input| {
            // Multi-statement body
            let mut sum = 0u64;
            for byte in input.iter() {
                sum = sum.wrapping_add(*byte as u64);
            }
            let result = sum % 256;
            std::hint::black_box(result);
        },
    };

    assert!(result.is_measurable() || matches!(result, Outcome::Unmeasurable { .. }));
}

/// Test macro with early return in measure body.
#[test]
fn macro_measure_with_early_return() {
    let result = timing_test_checked! {
        oracle: TimingOracle::for_attacker(AttackerModel::AdjacentNetwork).time_budget(Duration::from_secs(10)),
        baseline: || 0u64,
        sample: || rand::random::<u64>(),
        measure: |input| {
            if *input == 0 {
                return;
            }
            std::hint::black_box(input);
        },
    };

    assert!(result.is_measurable() || matches!(result, Outcome::Unmeasurable { .. }));
}

/// Test macro with nested closures in measure body.
#[test]
fn macro_nested_closures() {
    let result = timing_test_checked! {
        oracle: TimingOracle::for_attacker(AttackerModel::AdjacentNetwork).time_budget(Duration::from_secs(10)),
        baseline: || [0u8; 16],
        sample: || rand::random::<[u8; 16]>(),
        measure: |input| {
            let process = |data: &[u8]| -> u64 {
                data.iter().map(|&b| b as u64).sum()
            };
            std::hint::black_box(process(input));
        },
    };

    assert!(result.is_measurable() || matches!(result, Outcome::Unmeasurable { .. }));
}

// ===========================================================================
// Result Access Tests
// ===========================================================================

/// Test that we can access outcome fields.
#[test]
fn macro_result_access() {
    let outcome = timing_test_checked! {
        oracle: TimingOracle::for_attacker(AttackerModel::AdjacentNetwork).time_budget(Duration::from_secs(10)),
        baseline: || 0u64,
        sample: || rand::random::<u64>(),
        measure: |input| {
            std::hint::black_box(input);
        },
    };

    match outcome {
        Outcome::Pass {
            leak_probability,
            effect,
            ..
        }
        | Outcome::Fail {
            leak_probability,
            effect,
            ..
        }
        | Outcome::Inconclusive {
            leak_probability,
            effect,
            ..
        } => {
            // Verify fields are accessible
            let _leak_prob = leak_probability;
            let _effect = &effect;

            // Leak probability should be valid
            assert!(
                (0.0..=1.0).contains(&leak_probability),
                "Leak probability should be between 0 and 1"
            );
        }
        Outcome::Unmeasurable { recommendation, .. } => {
            // Unmeasurable is also valid, just verify we can access fields
            assert!(!recommendation.is_empty());
        }
        Outcome::Research(_) => {}
    }
}

// ===========================================================================
// No Trailing Comma Tests
// ===========================================================================

/// Test macro without trailing comma.
#[test]
fn macro_no_trailing_comma() {
    let result = timing_test_checked! {
        oracle: TimingOracle::for_attacker(AttackerModel::AdjacentNetwork).time_budget(Duration::from_secs(10)),
        baseline: || 0u8,
        sample: || rand::random::<u8>(),
        measure: |input| {
            std::hint::black_box(input);
        }
    };

    assert!(result.is_measurable() || matches!(result, Outcome::Unmeasurable { .. }));
}

// ===========================================================================
// Closure Syntax Variations
// ===========================================================================

/// Test macro with move closure in sample.
#[test]
fn macro_move_closure() {
    let seed = 42u64;

    let result = timing_test_checked! {
        oracle: TimingOracle::for_attacker(AttackerModel::AdjacentNetwork).time_budget(Duration::from_secs(10)),
        baseline: || 0u64,
        sample: move || rand::random::<u64>().wrapping_add(seed),
        measure: |input| {
            std::hint::black_box(input);
        },
    };

    assert!(result.is_measurable() || matches!(result, Outcome::Unmeasurable { .. }));
}

/// Test macro with complex sample generator.
#[test]
fn macro_complex_sample_generator() {
    let result = timing_test_checked! {
        oracle: TimingOracle::for_attacker(AttackerModel::AdjacentNetwork).time_budget(Duration::from_secs(10)),
        baseline: || [0u8; 32],
        sample: || {
            // Complex multi-line generator
            let mut arr = [0u8; 32];
            for (i, byte) in arr.iter_mut().enumerate() {
                *byte = ((i as u8).wrapping_mul(17)).wrapping_add(rand::random::<u8>());
            }
            arr
        },
        measure: |input| {
            std::hint::black_box(input);
        },
    };

    assert!(result.is_measurable() || matches!(result, Outcome::Unmeasurable { .. }));
}

/// Test macro with mutable references in measure body.
#[test]
fn macro_mutable_state_captured() {
    use std::cell::Cell;

    // Use Cell for interior mutability since closures are FnMut
    let counter = Cell::new(0u64);

    let result = timing_test_checked! {
        oracle: TimingOracle::for_attacker(AttackerModel::AdjacentNetwork).time_budget(Duration::from_secs(10)),
        baseline: || 1u64,
        sample: || rand::random::<u64>(),
        measure: |input| {
            // Use wrapping_add to avoid overflow panic
            counter.set(counter.get().wrapping_add(*input));
            std::hint::black_box(counter.get());
        },
    };

    assert!(result.is_measurable() || matches!(result, Outcome::Unmeasurable { .. }));
}

// ===========================================================================
// Edge Case Tests
// ===========================================================================

/// Test macro with zero-sized type.
#[test]
fn macro_unit_type() {
    let result = timing_test_checked! {
        oracle: TimingOracle::for_attacker(AttackerModel::AdjacentNetwork).time_budget(Duration::from_secs(10)),
        baseline: || (),
        sample: || (),
        measure: |_input| {
            std::hint::black_box(42);
        },
    };

    assert!(result.is_measurable() || matches!(result, Outcome::Unmeasurable { .. }));
}

/// Test macro with bool type.
#[test]
fn macro_bool_type() {
    let result = timing_test_checked! {
        oracle: TimingOracle::for_attacker(AttackerModel::AdjacentNetwork).time_budget(Duration::from_secs(10)),
        baseline: || false,
        sample: || rand::random::<bool>(),
        measure: |input| {
            std::hint::black_box(*input);
        },
    };

    assert!(result.is_measurable() || matches!(result, Outcome::Unmeasurable { .. }));
}

// ===========================================================================
// Coverage Tests: Both timing_test! and timing_test_checked!
// ===========================================================================

/// Test that timing_test! returns Outcome directly.
#[test]
#[ignore] // Slow (48s) - just tests return type, run with --ignored
fn timing_test_returns_outcome() {
    let outcome = timing_test! {
        oracle: TimingOracle::for_attacker(AttackerModel::AdjacentNetwork).time_budget(Duration::from_secs(10)),
        baseline: || [0u8; 128],
        sample: || {
            let mut arr = [0u8; 128];
            for byte in &mut arr {
                *byte = rand::random();
            }
            arr
        },
        measure: |arr| {
            // More complex operation to ensure measurability
            let mut acc = 0u32;
            for byte in arr {
                acc = acc.wrapping_mul(31).wrapping_add(*byte as u32);
            }
            std::hint::black_box(acc);
        },
    };

    // Should be able to access Outcome methods
    if let Some(leak_prob) = outcome.leak_probability() {
        assert!((0.0..=1.0).contains(&leak_prob));
    }
    if let Some(samples) = outcome.samples_used() {
        assert!(samples > 0);
    }
}

/// Test that timing_test! can be used with helper methods.
#[test]
fn timing_test_no_pattern_matching_needed() {
    // This demonstrates the convenience of timing_test! helper methods
    let outcome = timing_test! {
        oracle: TimingOracle::for_attacker(AttackerModel::AdjacentNetwork).time_budget(Duration::from_secs(10)),
        baseline: || [0u8; 64],
        sample: || {
            let mut arr = [0u8; 64];
            for byte in &mut arr {
                *byte = rand::random();
            }
            arr
        },
        measure: |arr| {
            // Hash-like operation
            let mut val = 0u8;
            for byte in arr {
                val = val.wrapping_add(*byte).wrapping_mul(31);
            }
            std::hint::black_box(val);
        },
    };

    // Use helper methods without full pattern matching
    if let Some(leak_prob) = outcome.leak_probability() {
        println!("Leak probability: {:.2}%", leak_prob * 100.0);
    }
    // Verify we got some result
    assert!(outcome.is_measurable() || matches!(outcome, Outcome::Unmeasurable { .. }));
}

/// Test that timing_test_checked! returns Outcome for explicit handling.
#[test]
fn timing_test_checked_returns_outcome() {
    let outcome: Outcome = timing_test_checked! {
        oracle: TimingOracle::for_attacker(AttackerModel::AdjacentNetwork).time_budget(Duration::from_secs(10)),
        baseline: || 1u64,
        sample: || rand::random::<u64>(),
        measure: |x| {
            std::hint::black_box(*x);
        },
    };

    // Must pattern match or use helper methods on Outcome
    match outcome {
        Outcome::Pass {
            leak_probability, ..
        }
        | Outcome::Fail {
            leak_probability, ..
        }
        | Outcome::Inconclusive {
            leak_probability, ..
        } => {
            assert!(leak_probability >= 0.0);
        }
        Outcome::Unmeasurable { .. } => {
            // Test passes - this is a valid outcome
        }
        Outcome::Research(_) => {}
    }
}

/// Test timing_test_checked! with explicit unmeasurable handling.
#[test]
fn timing_test_checked_explicit_unmeasurable_handling() {
    let outcome = timing_test_checked! {
        oracle: TimingOracle::for_attacker(AttackerModel::AdjacentNetwork).time_budget(Duration::from_secs(10)),
        baseline: || (),
        sample: || (),
        measure: |_| {
            // Extremely fast operation - might be unmeasurable
        },
    };

    // This pattern is useful when you want to handle unmeasurable gracefully
    match outcome {
        Outcome::Pass {
            leak_probability, ..
        }
        | Outcome::Fail {
            leak_probability, ..
        }
        | Outcome::Inconclusive {
            leak_probability, ..
        } => {
            println!("Measurable: {:.3}", leak_probability);
        }
        Outcome::Unmeasurable { recommendation, .. } => {
            println!("Unmeasurable: {}", recommendation);
        }
        Outcome::Research(_) => {}
    };
}

/// Test side-by-side comparison of both macros with identical operations.
#[test]
#[ignore] // Slow (54s) - runs 2 full oracle tests, run with --ignored
fn both_macros_with_identical_config() {
    // timing_test! - returns Outcome
    let outcome_direct = timing_test! {
        oracle: TimingOracle::for_attacker(AttackerModel::AdjacentNetwork).time_budget(Duration::from_secs(10)),
        baseline: || [100u8; 64],
        sample: || {
            let mut arr = [0u8; 64];
            for byte in &mut arr {
                *byte = rand::random();
            }
            arr
        },
        measure: |arr| {
            // More complex operation
            let mut val = 0u32;
            for byte in arr {
                val = val.wrapping_mul(2).wrapping_add(*byte as u32);
            }
            std::hint::black_box(val);
        },
    };

    // timing_test_checked! - returns Outcome
    let outcome_checked = timing_test_checked! {
        oracle: TimingOracle::for_attacker(AttackerModel::AdjacentNetwork).time_budget(Duration::from_secs(10)),
        baseline: || [100u8; 64],
        sample: || {
            let mut arr = [0u8; 64];
            for byte in &mut arr {
                *byte = rand::random();
            }
            arr
        },
        measure: |arr| {
            // Same operation
            let mut val = 0u32;
            for byte in arr {
                val = val.wrapping_mul(2).wrapping_add(*byte as u32);
            }
            std::hint::black_box(val);
        },
    };

    // Both should produce valid results
    if let Some(prob) = outcome_direct.leak_probability() {
        assert!(prob >= 0.0);
    }

    if let Some(prob) = outcome_checked.leak_probability() {
        assert!(prob >= 0.0);
    }
}

/// Test that timing_test! works with all optional fields.
#[test]
fn timing_test_all_optional_fields() {
    let multiplier = 7u32;
    // Pre-measurement work done before macro
    std::hint::black_box(multiplier);

    let outcome = timing_test! {
        oracle: TimingOracle::for_attacker(AttackerModel::AdjacentNetwork).time_budget(Duration::from_secs(10)).max_samples(1000),
        baseline: || [10u8; 64],
        sample: || {
            let mut arr = [0u8; 64];
            for byte in &mut arr {
                *byte = rand::random();
            }
            arr
        },
        measure: |arr| {
            // Make it measurable
            let mut val = multiplier;
            for byte in arr {
                val = val.wrapping_mul(*byte as u32);
            }
            std::hint::black_box(val);
        },
    };

    // Note: actual samples may be adjusted based on batching
    if let Some(samples) = outcome.samples_used() {
        assert!(samples > 0);
    }
}

/// Test that timing_test_checked! works with all optional fields.
#[test]
fn timing_test_checked_all_optional_fields() {
    let divisor = 3u64;
    // Pre-measurement work done before macro
    std::hint::black_box(divisor);

    let outcome = timing_test_checked! {
        oracle: TimingOracle::for_attacker(AttackerModel::AdjacentNetwork).time_budget(Duration::from_secs(10)).max_samples(500),
        baseline: || 99u64,
        sample: || rand::random::<u64>() | 1, // Ensure non-zero
        measure: |x| {
            std::hint::black_box(x.wrapping_div(*x.max(&1)));
        },
    };

    assert!(outcome.is_measurable() || matches!(outcome, Outcome::Unmeasurable { .. }));
}

/// Test timing_test! with complex types (arrays).
#[test]
fn timing_test_complex_array_type() {
    let outcome = timing_test! {
        oracle: TimingOracle::for_attacker(AttackerModel::AdjacentNetwork).time_budget(Duration::from_secs(10)),
        baseline: || [0u8; 64],  // Larger array
        sample: || {
            let mut arr = [0u8; 64];
            for byte in &mut arr {
                *byte = rand::random();
            }
            arr
        },
        measure: |arr| {
            // More complex operation to make it measurable
            let mut acc = 0u8;
            for byte in arr {
                acc ^= byte;
                acc = acc.wrapping_mul(7).wrapping_add(13);
            }
            std::hint::black_box(acc);
        },
    };

    if let Some(prob) = outcome.leak_probability() {
        assert!(prob <= 1.0);
    }
}

/// Test timing_test_checked! with complex types (Vec).
#[test]
fn timing_test_checked_complex_vec_type() {
    let outcome = timing_test_checked! {
        oracle: TimingOracle::for_attacker(AttackerModel::AdjacentNetwork).time_budget(Duration::from_secs(10)),
        baseline: || vec![0u32; 10],
        sample: || {
            (0..10).map(|_| rand::random::<u32>()).collect()
        },
        measure: |v| {
            let sum: u32 = v.iter().fold(0u32, |acc, x| acc.wrapping_add(*x));
            std::hint::black_box(sum);
        },
    };

    assert!(outcome.is_reliable() || !outcome.is_reliable()); // Always true, just testing API
}