asupersync 0.3.1

Spec-first, cancel-correct, capability-secure async runtime for Rust.
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
#![allow(clippy::all)]
//! Comprehensive validation tests for GF(256) finite field operations.
//!
//! This module provides systematic validation of the GF(256) implementation
//! against known mathematical properties and RFC 6330 requirements.

use super::*;
use std::collections::HashSet;

/// Test basic field properties: associativity, commutativity, distributivity
#[test]
fn test_field_properties() {
    // Test a representative sample of elements
    let test_elements = [0, 1, 2, 3, 17, 42, 85, 127, 170, 255];

    for &a in &test_elements {
        for &b in &test_elements {
            for &c in &test_elements {
                let gf_a = Gf256::new(a);
                let gf_b = Gf256::new(b);
                let gf_c = Gf256::new(c);

                // Addition is commutative: a + b = b + a
                assert_eq!(
                    gf_a + gf_b,
                    gf_b + gf_a,
                    "Addition not commutative for {} + {}",
                    a,
                    b
                );

                // Addition is associative: (a + b) + c = a + (b + c)
                assert_eq!(
                    (gf_a + gf_b) + gf_c,
                    gf_a + (gf_b + gf_c),
                    "Addition not associative for ({} + {}) + {} vs {} + ({} + {})",
                    a,
                    b,
                    c,
                    a,
                    b,
                    c
                );

                // Multiplication is commutative: a * b = b * a
                assert_eq!(
                    gf_a.mul_field(gf_b),
                    gf_b.mul_field(gf_a),
                    "Multiplication not commutative for {} * {}",
                    a,
                    b
                );

                if c != 0 {
                    // Multiplication is associative: (a * b) * c = a * (b * c)
                    assert_eq!(
                        gf_a.mul_field(gf_b).mul_field(gf_c),
                        gf_a.mul_field(gf_b.mul_field(gf_c)),
                        "Multiplication not associative for ({} * {}) * {} vs {} * ({} * {})",
                        a,
                        b,
                        c,
                        a,
                        b,
                        c
                    );
                }

                // Distributivity: a * (b + c) = a * b + a * c
                assert_eq!(
                    gf_a.mul_field(gf_b + gf_c),
                    gf_a.mul_field(gf_b) + gf_a.mul_field(gf_c),
                    "Distributivity fails for {} * ({} + {}) vs {} * {} + {} * {}",
                    a,
                    b,
                    c,
                    a,
                    b,
                    a,
                    c
                );
            }
        }
    }
}

/// Test field identity elements
#[test]
fn test_identity_elements() {
    let zero = Gf256::ZERO;
    let one = Gf256::ONE;

    for i in 0..=255u8 {
        let gf_i = Gf256::new(i);

        // Additive identity: a + 0 = a
        assert_eq!(gf_i + zero, gf_i, "Additive identity fails for {}", i);
        assert_eq!(zero + gf_i, gf_i, "Additive identity fails for 0 + {}", i);

        // Multiplicative identity: a * 1 = a
        assert_eq!(
            gf_i.mul_field(one),
            gf_i,
            "Multiplicative identity fails for {}",
            i
        );
        assert_eq!(
            one.mul_field(gf_i),
            gf_i,
            "Multiplicative identity fails for 1 * {}",
            i
        );

        // Additive inverse: a + a = 0 (in GF(2^n), every element is its own additive inverse)
        assert_eq!(
            gf_i + gf_i,
            zero,
            "Additive inverse fails for {} + {}",
            i,
            i
        );
    }
}

/// Test multiplicative inverses
#[test]
fn test_multiplicative_inverses() {
    let one = Gf256::ONE;

    for i in 1..=255u8 {
        // Skip 0 as it has no multiplicative inverse
        let gf_i = Gf256::new(i);
        let gf_i_inv = gf_i.inv();

        // a * inv(a) = 1
        assert_eq!(
            gf_i.mul_field(gf_i_inv),
            one,
            "Multiplicative inverse fails: {} * inv({}) != 1",
            i,
            i
        );
        assert_eq!(
            gf_i_inv.mul_field(gf_i),
            one,
            "Multiplicative inverse fails: inv({}) * {} != 1",
            i,
            i
        );
    }
}

/// Test division operations
#[test]
fn test_division() {
    let test_elements = [1, 2, 3, 17, 42, 85, 127, 170, 255];

    for &a in &test_elements {
        for &b in &test_elements {
            let gf_a = Gf256::new(a);
            let gf_b = Gf256::new(b);

            // Division is multiplication by inverse: a / b = a * inv(b)
            let div_result = gf_a.div_field(gf_b);
            let mul_inv_result = gf_a.mul_field(gf_b.inv());
            assert_eq!(
                div_result, mul_inv_result,
                "Division inconsistent: {} / {} != {} * inv({})",
                a, b, a, b
            );

            // Division identity: (a / b) * b = a
            assert_eq!(
                div_result.mul_field(gf_b),
                gf_a,
                "Division identity fails: ({} / {}) * {} != {}",
                a,
                b,
                b,
                a
            );
        }
    }
}

/// Test exponentiation properties
#[test]
fn test_exponentiation() {
    let test_bases = [1, 2, 3, 17, 42, 85, 170];
    let test_exps = [0, 1, 2, 3, 7, 15, 31, 63, 127, 255];

    for &base in &test_bases {
        let gf_base = Gf256::new(base);

        for &exp in &test_exps {
            let pow_result = gf_base.pow(exp);

            // Manual calculation for verification
            let mut manual_result = Gf256::ONE;
            for _ in 0..exp {
                manual_result = manual_result.mul_field(gf_base);
            }

            assert_eq!(
                pow_result, manual_result,
                "Exponentiation mismatch: {}^{}",
                base, exp
            );
        }

        // Special cases
        assert_eq!(gf_base.pow(0), Gf256::ONE, "{}^0 should be 1", base);
        assert_eq!(gf_base.pow(1), gf_base, "{}^1 should be {}", base, base);
    }
}

/// Test that all non-zero elements are invertible (field property)
#[test]
fn test_field_completeness() {
    let mut inverses_seen = HashSet::new();

    for i in 1..=255u8 {
        let gf_i = Gf256::new(i);
        let inv_val = gf_i.inv().raw();

        // Every non-zero element should have a unique inverse
        assert!(inv_val != 0, "Element {} has zero inverse", i);
        assert!(
            !inverses_seen.contains(&inv_val),
            "Duplicate inverse {} for element {}",
            inv_val,
            i
        );
        inverses_seen.insert(inv_val);
    }

    // We should have seen exactly 255 unique inverses
    assert_eq!(inverses_seen.len(), 255, "Should have 255 unique inverses");
}

/// Test slice operations match element-wise operations
#[test]
fn test_slice_operations_correctness() {
    let test_data_a = [1, 17, 42, 85, 127, 170, 255, 0];
    let test_data_b = [255, 170, 127, 85, 42, 17, 1, 128];
    let multiplier = Gf256::new(123);

    // Test gf256_add_slice
    let mut slice_result = test_data_a;
    gf256_add_slice(&mut slice_result, &test_data_b);

    for i in 0..test_data_a.len() {
        let expected = Gf256::new(test_data_a[i]) + Gf256::new(test_data_b[i]);
        assert_eq!(
            slice_result[i],
            expected.raw(),
            "Slice addition mismatch at index {}: {} + {} = {} (expected {})",
            i,
            test_data_a[i],
            test_data_b[i],
            slice_result[i],
            expected.raw()
        );
    }

    // Test gf256_mul_slice
    let mut mul_result = test_data_a;
    gf256_mul_slice(&mut mul_result, multiplier);

    for i in 0..test_data_a.len() {
        let expected = Gf256::new(test_data_a[i]).mul_field(multiplier);
        assert_eq!(
            mul_result[i],
            expected.raw(),
            "Slice multiplication mismatch at index {}: {} * {} = {} (expected {})",
            i,
            test_data_a[i],
            multiplier.raw(),
            mul_result[i],
            expected.raw()
        );
    }
}

/// Test dual slice operations
#[test]
fn test_dual_slice_operations() {
    let mut test_data_a = [1, 17, 42, 85, 127, 170, 255, 0];
    let test_data_a_orig = test_data_a;
    let test_data_b = [255, 170, 127, 85, 42, 17, 1, 128];
    let mut test_data_c = [128, 64, 32, 16, 8, 4, 2, 1];
    let test_data_c_orig = test_data_c;
    let test_data_d = [1, 2, 4, 8, 16, 32, 64, 128];
    let multiplier = Gf256::new(73);

    // Test gf256_add_slices2
    gf256_add_slices2(
        &mut test_data_a,
        &test_data_b,
        &mut test_data_c,
        &test_data_d,
    );

    for i in 0..test_data_a.len() {
        let expected_a = Gf256::new(test_data_a_orig[i]) + Gf256::new(test_data_b[i]);
        let expected_c = Gf256::new(test_data_c_orig[i]) + Gf256::new(test_data_d[i]);

        assert_eq!(
            test_data_a[i],
            expected_a.raw(),
            "Dual slice addition mismatch for slice A at index {}",
            i
        );
        assert_eq!(
            test_data_c[i],
            expected_c.raw(),
            "Dual slice addition mismatch for slice C at index {}",
            i
        );
    }

    // Reset and test gf256_mul_slices2
    test_data_a = test_data_a_orig;
    test_data_c = test_data_c_orig;
    gf256_mul_slices2(&mut test_data_a, &mut test_data_c, multiplier);

    for i in 0..test_data_a.len() {
        let expected_a = Gf256::new(test_data_a_orig[i]).mul_field(multiplier);
        let expected_c = Gf256::new(test_data_c_orig[i]).mul_field(multiplier);

        assert_eq!(
            test_data_a[i],
            expected_a.raw(),
            "Dual slice multiplication mismatch for slice A at index {}",
            i
        );
        assert_eq!(
            test_data_c[i],
            expected_c.raw(),
            "Dual slice multiplication mismatch for slice C at index {}",
            i
        );
    }
}

/// Test kernel dispatch determinism
#[test]
fn test_kernel_dispatch_determinism() {
    // Test that kernel selection is deterministic
    let kernel1 = active_kernel();
    let kernel2 = active_kernel();
    assert_eq!(kernel1, kernel2, "Kernel selection should be deterministic");

    // Test dual kernel decisions are consistent
    let test_sizes = [(100, 100), (1000, 1000), (10000, 10000)];

    for &(len_a, len_b) in &test_sizes {
        let decision1 = dual_mul_kernel_decision(len_a, len_b);
        let decision2 = dual_mul_kernel_decision(len_a, len_b);
        assert_eq!(
            decision1, decision2,
            "Dual mul kernel decision should be deterministic for sizes ({}, {})",
            len_a, len_b
        );

        let addmul_decision1 = dual_addmul_kernel_decision(len_a, len_b);
        let addmul_decision2 = dual_addmul_kernel_decision(len_a, len_b);
        assert_eq!(
            addmul_decision1, addmul_decision2,
            "Dual addmul kernel decision should be deterministic for sizes ({}, {})",
            len_a, len_b
        );
    }
}

/// Test profile pack consistency
#[test]
fn test_profile_pack_consistency() {
    let manifest = gf256_profile_pack_manifest_snapshot();
    let policy = dual_kernel_policy_snapshot();

    // Verify that active profile pack is consistent
    assert!(
        !manifest.profile_pack_catalog.is_empty(),
        "Should have at least one profile pack"
    );
    assert!(
        manifest
            .profile_pack_catalog
            .iter()
            .any(|metadata| metadata.profile_pack == policy.profile_pack),
        "Active profile pack should be in profile pack catalog"
    );

    // Verify dual kernel thresholds are reasonable.
    //
    // Some tuned profile packs intentionally publish a "sequential-biased"
    // sentinel window — `mul_min_total = usize::MAX` and `mul_max_total = 0` —
    // to signal that the fused dual-mul kernel is disabled on that architecture
    // and all traffic stays on the scalar/sequential path. Treat that sentinel
    // as a legitimate configuration instead of a window-inversion bug.
    fn window_is_wellformed_or_sequential_sentinel(min_total: usize, max_total: usize) -> bool {
        if min_total == usize::MAX && max_total == 0 {
            return true;
        }
        min_total > 0 && max_total >= min_total
    }

    match policy.mode {
        DualKernelMode::Auto => {
            assert!(
                window_is_wellformed_or_sequential_sentinel(
                    policy.mul_min_total,
                    policy.mul_max_total
                ),
                "Mul window should either be well-formed or the sequential-biased sentinel (got min={}, max={})",
                policy.mul_min_total,
                policy.mul_max_total
            );
            assert!(
                window_is_wellformed_or_sequential_sentinel(
                    policy.addmul_min_total,
                    policy.addmul_max_total
                ),
                "Addmul window should either be well-formed or the sequential-biased sentinel (got min={}, max={})",
                policy.addmul_min_total,
                policy.addmul_max_total
            );
            assert!(
                policy.max_lane_ratio > 0,
                "Lane ratio threshold should be positive"
            );
        }
        DualKernelMode::Sequential => {
            // Thresholds don't matter in this mode
        }
        DualKernelMode::Fused => {
            // Should use dual kernels regardless of size
        }
    }
}

/// Test RFC 6330 specific requirements
#[test]
fn test_rfc6330_compliance() {
    // RFC 6330 specifies the primitive polynomial x^8 + x^4 + x^3 + x^2 + 1
    // This corresponds to 0x11D, but since we work in GF(2^8), we use 0x1D

    // Test that the field has exactly 256 elements (0-255)
    let mut all_results = HashSet::new();
    for i in 0..=255u8 {
        all_results.insert(Gf256::new(i).raw());
    }
    assert_eq!(
        all_results.len(),
        256,
        "Field should have exactly 256 elements"
    );

    // Test that multiplication by 2 (α) works correctly
    // In GF(256), α is a primitive element
    let alpha = Gf256::new(2);
    let alpha_squared = alpha.mul_field(alpha);
    assert_eq!(alpha_squared.raw(), 4, "α² should equal 4 in GF(256)");

    // Test that α^255 = 1 (since α is primitive in GF(2^8))
    let alpha_255 = alpha.pow(255);
    assert_eq!(alpha_255.raw(), 1, "α^255 should equal 1 in GF(256)");

    // Test some known values from RFC 6330 examples if available
    // These would be specific test vectors from the RFC
    let test_cases = [
        (0, 0, 0),   // 0 * anything = 0
        (1, 42, 42), // 1 * x = x
        (2, 2, 4),   // 2 * 2 = 4
    ];

    for &(a, b, expected) in &test_cases {
        let result = Gf256::new(a).mul_field(Gf256::new(b));
        assert_eq!(
            result.raw(),
            expected,
            "RFC test case failed: {} * {} = {} (expected {})",
            a,
            b,
            result.raw(),
            expected
        );
    }
}

/// Stress test with random operations to catch edge cases
#[test]
fn test_random_operations_stress() {
    use std::collections::hash_map::DefaultHasher;
    use std::hash::{Hash, Hasher};

    // Use deterministic "random" based on hash for reproducible tests
    let mut hasher = DefaultHasher::new();
    "stress_test_seed".hash(&mut hasher);
    let mut seed = hasher.finish();

    // Simple linear congruential generator for deterministic pseudo-random numbers
    let mut next_rand = move || {
        seed = seed.wrapping_mul(1103515245).wrapping_add(12345);
        (seed >> 8) as u8
    };

    // Perform many random operations
    for _ in 0..1000 {
        let a = next_rand();
        let b = next_rand();
        let c = next_rand();

        let gf_a = Gf256::new(a);
        let gf_b = Gf256::new(b);
        let gf_c = Gf256::new(c);

        // Test that operations don't panic and maintain field properties
        let sum = gf_a + gf_b + gf_c;
        let product = if b != 0 && c != 0 {
            Some(gf_a.mul_field(gf_b).mul_field(gf_c))
        } else {
            None
        };

        // Verify no panics occurred and results are valid field elements
        let _ = sum.raw();
        if let Some(prod) = product {
            let _ = prod.raw();
        }

        // Test division when possible
        if b != 0 {
            let division = gf_a.div_field(gf_b);
            let _ = division.raw();

            // Verify division correctness: (a / b) * b = a
            assert_eq!(
                division.mul_field(gf_b),
                gf_a,
                "Division verification failed for {} / {}",
                a,
                b
            );
        }
    }
}