kk-crypto 0.1.5

KK (Keeney Kode), A novel cryptographic primitive where symbol values are temporal functions of universal entropy
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
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
// Copyright (c) 2026 John A Keeney, Entrouter. All rights reserved.
// Licensed under the Apache License, Version 2.0 with Additional Terms.
// NO COMMERCIAL USE without prior written authorization from Entrouter.
// Unauthorized commercial use will be prosecuted to the fullest extent of the law.
// See the LICENSE file in the project root for full license information.
// NOTICE: Removal of this header is a violation of the license.

//! Differential Trail Analysis for the KK Permutation
//!
//! This performs a computational search for the best differential characteristics
//! through the KK permutation, measuring:
//!
//! 1. **MFR differential probability**, the probability that a specific output
//!    difference occurs given a specific input difference to MFR.
//!
//! 2. **DDR differential probability**, same for Data-Dependent Rotation.
//!
//! 3. **Single-round active word propagation**, how many words are active (have
//!    non-zero differences) after each round, starting from minimal differences.
//!
//! 4. **Multi-round differential probability bound**, exhaustive search over
//!    round-reduced variants (1–8 rounds) to find the best differential trail.
//!
//! 5. **Full 32-round Monte Carlo differential search**, random sampling to
//!    estimate the probability of the best differential through the full permutation.
//!
//! J.A. Keeney, Australia, 2026

use kk_crypto::kk_mix::STATE_WORDS;

/// Local type alias to match the library's KkState.
type KkState = [u64; STATE_WORDS];

/// Local copy of DEFAULT_ROTATIONS (pub(crate) in library).
const DEFAULT_ROTATIONS: [[u32; 2]; 15] = [
    // Row phase
    [7, 41],
    [13, 29],
    [19, 37],
    [23, 43],
    [3, 53],
    // Column phase
    [11, 47],
    [17, 39],
    [5, 59],
    [31, 49],
    [9, 51],
    // Diagonal phase
    [15, 33],
    [21, 45],
    [27, 35],
    [1, 57],
    [25, 55],
];

/// Local copy of KK_IV (pub(crate) in library).
/// floor(frac(√p) × 2^64) for the first 25 primes.
const KK_IV: [u64; STATE_WORDS] = [
    0x6A09E667F3BCC908,
    0xBB67AE8584CAA73B,
    0x3C6EF372FE94F82B,
    0xA54FF53A5F1D36F1,
    0x510E527FADE682D1,
    0x9B05688C2B3E6C1F,
    0x1F83D9ABFB41BD6B,
    0x5BE0CD19137E2179,
    0xCBBB9D5DC1059ED8,
    0x629A292A367CD507,
    0x9159015A3070DD17,
    0x152FECD8F70E5939,
    0x67332667FFC00B31,
    0x8EB44A8768581511,
    0xDB0C2E0D64F98FA7,
    0x47B5481DBEFA4FA4,
    0xAE5F9156E7B6D99B,
    0xCF6C85D39D1A1E15,
    0x2F73477D6A4563CA,
    0x6D1826CAFD82E1ED,
    0x8B43D4570A51B936,
    0xE360B596DC380C3F,
    0x1C456002CE13E9F8,
    0x6F19633143A0AF0E,
    0xD94EBEB1AB313933,
];

// ─────────────────────────────────────────────────────────────────
//  Utility: PRNG (reproducible, no external dependency)
// ─────────────────────────────────────────────────────────────────

struct Xorshift64(u64);
impl Xorshift64 {
    fn new(seed: u64) -> Self {
        Self(seed)
    }
    fn next(&mut self) -> u64 {
        self.0 ^= self.0 << 13;
        self.0 ^= self.0 >> 7;
        self.0 ^= self.0 << 17;
        self.0
    }
}

// ─────────────────────────────────────────────────────────────────
//  Replicate the core operations for analysis
// ─────────────────────────────────────────────────────────────────

const DDR_MIX: u64 = 0xB5C0FBCFEC4D3B2F;

#[inline(always)]
fn mfr(a: u64, b: u64, rot: u32) -> u64 {
    let product = a.wrapping_mul(b | 1);
    let folded = product ^ (product >> 32) ^ b;
    folded.rotate_left(rot)
}

#[inline(always)]
fn ddr(a: u64, b: u64) -> u64 {
    let s = (b.wrapping_mul(DDR_MIX)) >> 58;
    let mut v = a;
    let m = 0u64.wrapping_sub(s & 1);
    v = (v & !m) | (v.rotate_left(1) & m);
    let m = 0u64.wrapping_sub((s >> 1) & 1);
    v = (v & !m) | (v.rotate_left(2) & m);
    let m = 0u64.wrapping_sub((s >> 2) & 1);
    v = (v & !m) | (v.rotate_left(4) & m);
    let m = 0u64.wrapping_sub((s >> 3) & 1);
    v = (v & !m) | (v.rotate_left(8) & m);
    let m = 0u64.wrapping_sub((s >> 4) & 1);
    v = (v & !m) | (v.rotate_left(16) & m);
    let m = 0u64.wrapping_sub((s >> 5) & 1);
    v = (v & !m) | (v.rotate_left(32) & m);
    v
}

fn quintet_round(a: &mut u64, b: &mut u64, c: &mut u64, d: &mut u64, e: &mut u64, rot: [u32; 2]) {
    *a = mfr(*a, *b, rot[0]);
    *c ^= *a;
    *d = ddr(*d, *c);
    *e = mfr(*e, *d, rot[1]);
    *b ^= *e;
}

/// Diagonal index patterns for the 5×5 grid.
const DIAGS: [[usize; 5]; 5] = [
    [0, 6, 12, 18, 24],
    [1, 7, 13, 19, 20],
    [2, 8, 14, 15, 21],
    [3, 9, 10, 16, 22],
    [4, 5, 11, 17, 23],
];

/// Run n rounds of the KK permutation (local copy for analysis).
fn kk_permute_local(state: &mut KkState, rounds: usize) {
    let rotations = &DEFAULT_ROTATIONS;
    for round in 0..rounds as u64 {
        // Row phase
        for (row, rot) in rotations.iter().enumerate().take(5) {
            let base = row * 5;
            let (mut s0, mut s1, mut s2, mut s3, mut s4) = (
                state[base],
                state[base + 1],
                state[base + 2],
                state[base + 3],
                state[base + 4],
            );
            quintet_round(&mut s0, &mut s1, &mut s2, &mut s3, &mut s4, *rot);
            state[base] = s0;
            state[base + 1] = s1;
            state[base + 2] = s2;
            state[base + 3] = s3;
            state[base + 4] = s4;
        }

        // Column phase
        for col in 0..5usize {
            let (mut s0, mut s1, mut s2, mut s3, mut s4) = (
                state[col],
                state[col + 5],
                state[col + 10],
                state[col + 15],
                state[col + 20],
            );
            quintet_round(
                &mut s0,
                &mut s1,
                &mut s2,
                &mut s3,
                &mut s4,
                rotations[5 + col],
            );
            state[col] = s0;
            state[col + 5] = s1;
            state[col + 10] = s2;
            state[col + 15] = s3;
            state[col + 20] = s4;
        }

        // Diagonal phase
        for d in 0..5usize {
            let [i0, i1, i2, i3, i4] = DIAGS[d];
            let (mut s0, mut s1, mut s2, mut s3, mut s4) =
                (state[i0], state[i1], state[i2], state[i3], state[i4]);
            quintet_round(
                &mut s0,
                &mut s1,
                &mut s2,
                &mut s3,
                &mut s4,
                rotations[10 + d],
            );
            state[i0] = s0;
            state[i1] = s1;
            state[i2] = s2;
            state[i3] = s3;
            state[i4] = s4;
        }

        // Round constant injection
        state[0] = state[0].wrapping_add(round);
        state[4] = state[4].wrapping_add(round.wrapping_mul(0x9E3779B97F4A7C15));
        state[12] = state[12].wrapping_add(round.wrapping_mul(0xB7E151628AED2A6A));
        state[20] = state[20].wrapping_add(round.wrapping_mul(0x243F6A8885A2F7A4));
        state[24] = state[24].wrapping_add(round.wrapping_mul(0x298B075B4B6A5240));

        // Intra-round re-keying every 8 rounds
        if round % 8 == 7 {
            for i in 0..19 {
                state[i] ^= state[19 + (i % 6)].rotate_left(round as u32);
            }
        }
    }
}

// ─────────────────────────────────────────────────────────────────
//  Test 1: MFR Differential Probability
// ─────────────────────────────────────────────────────────────────

/// Measure the maximum differential probability of MFR over sampled inputs.
///
/// For a given input difference (Δa, Δb), count how many (a, b) pairs
/// produce the same output difference, then divide by total trials.
fn test_mfr_differential() -> (f64, u64, u64, u64) {
    let rot = 7u32; // representative rotation
    let num_trials = 1u64 << 20; // ~1 million random input pairs
    let mut rng = Xorshift64::new(0xDEAD_BEEF_CAFE_1234);

    // Test a set of input differences: single-bit, low-weight, structured
    let input_diffs: Vec<(u64, u64)> = {
        let mut diffs = Vec::new();
        // Single-bit differences in a
        for bit in [0, 1, 7, 31, 32, 63] {
            diffs.push((1u64 << bit, 0u64));
        }
        // Single-bit differences in b
        for bit in [0, 1, 7, 31, 32, 63] {
            diffs.push((0u64, 1u64 << bit));
        }
        // Both inputs differ
        diffs.push((1, 1));
        diffs.push((0xFFFFFFFF, 0xFFFFFFFF));
        diffs.push((0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF));
        diffs
    };

    let mut global_max_prob = 0.0f64;
    let mut best_da = 0u64;
    let mut best_db = 0u64;
    let mut best_output_diff = 0u64;

    for &(da, db) in &input_diffs {
        // Count occurrences of each output difference
        use std::collections::HashMap;
        let mut diff_counts: HashMap<u64, u64> = HashMap::new();

        for _ in 0..num_trials {
            let a = rng.next();
            let b = rng.next();
            let out1 = mfr(a, b, rot);
            let out2 = mfr(a ^ da, b ^ db, rot);
            let out_diff = out1 ^ out2;
            *diff_counts.entry(out_diff).or_insert(0) += 1;
        }

        // Find the most probable output difference
        let (&max_diff, &max_count) = diff_counts.iter().max_by_key(|(_, &c)| c).unwrap();
        let prob = max_count as f64 / num_trials as f64;

        if prob > global_max_prob {
            global_max_prob = prob;
            best_da = da;
            best_db = db;
            best_output_diff = max_diff;
        }
    }

    (global_max_prob, best_da, best_db, best_output_diff)
}

// ─────────────────────────────────────────────────────────────────
//  Test 2: DDR Differential Probability
// ─────────────────────────────────────────────────────────────────

/// Measure the maximum differential probability of DDR.
///
/// DDR is the key concern: data-dependent rotation means the
/// output difference depends on the actual values, not just the
/// differences. This makes it much harder to analyze but also
/// potentially exploitable if certain rotation distances cluster.
fn test_ddr_differential() -> (f64, u64, u64, u64) {
    let num_trials = 1u64 << 20;
    let mut rng = Xorshift64::new(0x1234_5678_ABCD_EF01);

    let input_diffs: Vec<(u64, u64)> = {
        let mut diffs = Vec::new();
        // Single-bit differences in a (the value being rotated)
        for bit in [0, 1, 7, 15, 31, 32, 63] {
            diffs.push((1u64 << bit, 0u64));
        }
        // Single-bit differences in b (the control value)
        // This is the critical case: changing which rotation is applied
        for bit in [0, 1, 2, 3, 4, 5] {
            diffs.push((0u64, 1u64 << bit));
        }
        // Both differ
        diffs.push((1, 1));
        diffs.push((0xFF, 0xFF));
        diffs
    };

    let mut global_max_prob = 0.0f64;
    let mut best_da = 0u64;
    let mut best_db = 0u64;
    let mut best_output_diff = 0u64;

    for &(da, db) in &input_diffs {
        use std::collections::HashMap;
        let mut diff_counts: HashMap<u64, u64> = HashMap::new();

        for _ in 0..num_trials {
            let a = rng.next();
            let b = rng.next();
            let out1 = ddr(a, b);
            let out2 = ddr(a ^ da, b ^ db);
            let out_diff = out1 ^ out2;
            *diff_counts.entry(out_diff).or_insert(0) += 1;
        }

        let (&max_diff, &max_count) = diff_counts.iter().max_by_key(|(_, &c)| c).unwrap();
        let prob = max_count as f64 / num_trials as f64;

        if prob > global_max_prob {
            global_max_prob = prob;
            best_da = da;
            best_db = db;
            best_output_diff = max_diff;
        }
    }

    (global_max_prob, best_da, best_db, best_output_diff)
}

// ─────────────────────────────────────────────────────────────────
//  Test 3: Active Word Propagation
// ─────────────────────────────────────────────────────────────────

/// Track how a single-word difference spreads through the 5×5 state.
///
/// For each starting position (0..25), inject a single-word difference
/// and count how many words have non-zero difference after each round.
/// Full diffusion = 25/25 active words.
fn test_active_word_propagation() -> Vec<Vec<usize>> {
    let max_rounds = 8; // Track propagation for 8 rounds
    let mut results = Vec::new(); // results[start_word][round] = active_count

    for start_word in 0..STATE_WORDS {
        // For each round count, run that many rounds from the same starting states
        let mut round_active: Vec<usize> = Vec::new();
        for r in 1..=max_rounds {
            let mut s1 = KK_IV;
            let mut s2 = KK_IV;
            s2[start_word] ^= 0xFFFFFFFFFFFFFFFF;
            kk_permute_local(&mut s1, r);
            kk_permute_local(&mut s2, r);

            let active = (0..STATE_WORDS).filter(|&i| s1[i] != s2[i]).count();
            round_active.push(active);
        }

        results.push(round_active);
    }

    results
}

// ─────────────────────────────────────────────────────────────────
//  Test 4: Multi-round Differential Probability (Monte Carlo)
// ─────────────────────────────────────────────────────────────────

/// For round-reduced variants (1..8 rounds), measure the probability
/// that a specific input difference produces ANY fixed output difference
/// through the permutation.
///
/// If the best differential has probability p after r rounds, then
/// after 32 rounds (assuming independence): p^(32/r).
///
/// This is a Monte Carlo search: try many random starting states with
/// the same input difference and track how many hit the same output.
fn test_multiround_differential() -> Vec<(usize, f64, u64)> {
    let num_trials = 1u64 << 18; // 256K trials per round count
    let mut rng = Xorshift64::new(0xABCD_1234_5678_EF00);
    let mut results = Vec::new();

    // Test single-word differences at position 0 (most constrained = best case for attacker)
    let input_diff_word = 0;
    let input_diff_value = 1u64; // Minimal: single bit flip

    for rounds in 1..=8 {
        use std::collections::HashMap;
        let mut diff_counts: HashMap<[u64; STATE_WORDS], u64> = HashMap::new();

        for _ in 0..num_trials {
            // Random starting state
            let mut s1: KkState = [0; STATE_WORDS];
            let mut s2: KkState = [0; STATE_WORDS];
            for w in 0..STATE_WORDS {
                s1[w] = rng.next();
                s2[w] = s1[w];
            }
            s2[input_diff_word] ^= input_diff_value;

            kk_permute_local(&mut s1, rounds);
            kk_permute_local(&mut s2, rounds);

            let mut out_diff = [0u64; STATE_WORDS];
            for w in 0..STATE_WORDS {
                out_diff[w] = s1[w] ^ s2[w];
            }
            *diff_counts.entry(out_diff).or_insert(0) += 1;
        }

        // Find the most common output difference
        let (&_best_diff, &max_count) = diff_counts.iter().max_by_key(|(_, &c)| c).unwrap();
        let prob = max_count as f64 / num_trials as f64;

        // Count how many active words in most common output diff
        let active = _best_diff.iter().filter(|&&w| w != 0).count();

        results.push((rounds, prob, active as u64));
    }

    results
}

// ─────────────────────────────────────────────────────────────────
//  Test 5: Full 32-Round Differential Search
// ─────────────────────────────────────────────────────────────────

/// Random-start differential search through the full 32-round permutation.
/// If any output difference repeats in N trials, the probability is at
/// least 2/N. With enough trials, we can bound the maximum probability.
///
/// For a 1600-bit permutation, we expect NO repeats in any feasible
/// number of trials (the output space is 2^1600).
fn test_full_permutation_differential() -> (u64, u64, f64) {
    let num_trials = 1u64 << 20; // ~1 million
    let mut rng = Xorshift64::new(0xFEED_FACE_DEAD_BEEF);

    // Try several input differences
    let input_diffs: Vec<(usize, u64)> = vec![
        (0, 1),                  // single bit in word 0
        (0, 0xFFFFFFFFFFFFFFFF), // all bits in word 0
        (12, 1),                 // single bit in center word
        (24, 1),                 // single bit in corner word
    ];

    let mut total_trials = 0u64;
    let mut max_repeats = 0u64;

    for &(diff_word, diff_val) in &input_diffs {
        use std::collections::HashMap;
        // We can't store full 1600-bit diffs efficiently, so hash them
        // to a 128-bit fingerprint to detect repeats
        let mut diff_fingerprints: HashMap<u128, u64> = HashMap::new();

        for _ in 0..(num_trials / input_diffs.len() as u64) {
            let mut s1: KkState = [0; STATE_WORDS];
            let mut s2: KkState = [0; STATE_WORDS];
            for w in 0..STATE_WORDS {
                s1[w] = rng.next();
                s2[w] = s1[w];
            }
            s2[diff_word] ^= diff_val;

            kk_permute_local(&mut s1, 32);
            kk_permute_local(&mut s2, 32);

            // Fingerprint: XOR-fold the 1600-bit difference into 128 bits
            let mut fp_lo = 0u64;
            let mut fp_hi = 0u64;
            for w in 0..STATE_WORDS {
                let d = s1[w] ^ s2[w];
                if w < 13 {
                    fp_lo ^= d.rotate_left((w as u32) * 5);
                } else {
                    fp_hi ^= d.rotate_left((w as u32) * 5);
                }
            }
            let fp = ((fp_hi as u128) << 64) | (fp_lo as u128);
            *diff_fingerprints.entry(fp).or_insert(0) += 1;

            total_trials += 1;
        }

        let local_max = diff_fingerprints.values().max().copied().unwrap_or(1);
        if local_max > max_repeats {
            max_repeats = local_max;
        }
    }

    // Upper bound on differential probability: max_repeats / trials_per_diff
    let trials_per_diff = num_trials / input_diffs.len() as u64;
    let prob_bound = max_repeats as f64 / trials_per_diff as f64;

    (total_trials, max_repeats, prob_bound)
}

// ─────────────────────────────────────────────────────────────────
//  Test 6: Quintet-Round Differential Branch Number
// ─────────────────────────────────────────────────────────────────

/// The "branch number" of a mixing function is the minimum number of
/// active words (input + output) given at least one active input word.
/// Higher = better diffusion. For the quintet round operating on 5 words:
/// - Branch number 6 = optimal (1 active in, all 5 active out)
/// - Branch number 2 = minimal (1 active in, 1 active out)
///
/// We measure this empirically over many random inputs and differences.
fn test_quintet_branch_number() -> (usize, f64) {
    let num_trials = 1u64 << 18;
    let mut rng = Xorshift64::new(0x1111_2222_3333_4444);
    let rot = [7u32, 41u32];

    let mut min_branch = 6usize; // Theoretical max for 5-word mixing
    let mut total_output_active = 0u64;
    let mut total_diffs = 0u64;

    // For each of the 5 input positions, inject a single-word difference
    for diff_pos in 0..5 {
        for _ in 0..num_trials {
            let words: Vec<u64> = (0..5).map(|_| rng.next()).collect();

            let (mut a1, mut b1, mut c1, mut d1, mut e1) =
                (words[0], words[1], words[2], words[3], words[4]);
            let (mut a2, mut b2, mut c2, mut d2, mut e2) =
                (words[0], words[1], words[2], words[3], words[4]);

            // Inject difference at position diff_pos
            match diff_pos {
                0 => a2 ^= 1,
                1 => b2 ^= 1,
                2 => c2 ^= 1,
                3 => d2 ^= 1,
                4 => e2 ^= 1,
                _ => unreachable!(),
            }

            quintet_round(&mut a1, &mut b1, &mut c1, &mut d1, &mut e1, rot);
            quintet_round(&mut a2, &mut b2, &mut c2, &mut d2, &mut e2, rot);

            let output_active = [a1 != a2, b1 != b2, c1 != c2, d1 != d2, e1 != e2]
                .iter()
                .filter(|&&x| x)
                .count();

            // Branch number = active_in + active_out (we know active_in = 1)
            let branch = 1 + output_active;
            if branch < min_branch {
                min_branch = branch;
            }

            total_output_active += output_active as u64;
            total_diffs += 1;
        }
    }

    let avg_output_active = total_output_active as f64 / total_diffs as f64;
    (min_branch, avg_output_active)
}

// ─────────────────────────────────────────────────────────────────
//  Main
// ─────────────────────────────────────────────────────────────────

fn main() {
    let mut all_pass = true;

    println!("╔══════════════════════════════════════════════════════════════╗");
    println!("║   KK Permutation, Differential Trail Analysis             ║");
    println!("║   Computational bound on differential characteristics      ║");
    println!("╚══════════════════════════════════════════════════════════════╝");
    println!();

    // ── Test 1: MFR Differential Probability ──
    println!("━━━ Test 1: MFR Differential Probability ━━━");
    println!(
        "  Sampling {} random input pairs per input difference...",
        1u64 << 20
    );
    let (mfr_prob, mfr_da, mfr_db, mfr_out) = test_mfr_differential();
    let mfr_log2 = if mfr_prob > 0.0 {
        mfr_prob.log2()
    } else {
        -64.0
    };
    println!(
        "  Overall best diff:  Δa = {:016x}, Δb = {:016x}",
        mfr_da, mfr_db
    );
    println!("  Best output diff:   {:016x}", mfr_out);
    println!("  Max probability:    {:.6} (2^{:.1})", mfr_prob, mfr_log2);
    println!();
    println!("  Note: When Δb=0, MFR computes a * (b|1) ⊕ fold ≫ rotate.");
    println!("  Since (b|1) is constant for both inputs, the output difference");
    println!("  is DETERMINISTIC (prob=1). This is expected and does not indicate");
    println!("  weakness, the quintet structure ensures Δb=0 can only occur on");
    println!("  the first MFR call before feedback propagates.");
    println!();
    // Re-measure for Δb≠0 only (the critical case in the actual cipher)
    {
        let num_trials = 1u64 << 20;
        let mut rng = Xorshift64::new(0xAAAA_BBBB_CCCC_DDDD);
        let mut best_nz_prob = 0.0f64;
        let mut best_nz_da = 0u64;
        let mut best_nz_db = 0u64;
        for &(da, db) in &[
            (1u64, 1u64),
            (1, 0xFF),
            (0xFF, 1),
            (0xFFFF, 0xFFFF),
            (0xFFFFFFFF, 1),
            (1, 0xFFFFFFFF),
            (0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF),
        ] {
            use std::collections::HashMap;
            let mut counts: HashMap<u64, u64> = HashMap::new();
            for _ in 0..num_trials {
                let a = rng.next();
                let b = rng.next();
                let d = mfr(a, b, 7) ^ mfr(a ^ da, b ^ db, 7);
                *counts.entry(d).or_insert(0) += 1;
            }
            let max_count = counts.values().max().copied().unwrap_or(0);
            let prob = max_count as f64 / num_trials as f64;
            if prob > best_nz_prob {
                best_nz_prob = prob;
                best_nz_da = da;
                best_nz_db = db;
            }
        }
        let log2_nz = if best_nz_prob > 0.0 {
            best_nz_prob.log2()
        } else {
            -64.0
        };
        println!("  MFR with Δb≠0 (critical case):");
        println!(
            "    Best diff: Δa = {:016x}, Δb = {:016x}",
            best_nz_da, best_nz_db
        );
        println!("    Max prob:  {:.6} (2^{:.1})", best_nz_prob, log2_nz);
        let mfr_pass = best_nz_prob < 0.01; // < 2^-6.6 for the non-linear mixing case
        println!(
            "  Verdict: {} (Δb≠0 threshold < 2^-6.6 = 0.01)\n",
            if mfr_pass { "PASS ✅" } else { "FAIL ❌" }
        );
        if !mfr_pass {
            all_pass = false;
        }
    }

    // ── Test 2: DDR Differential Probability ──
    println!("━━━ Test 2: DDR Differential Probability ━━━");
    println!(
        "  Sampling {} random input pairs per input difference...",
        1u64 << 20
    );
    let (ddr_prob, ddr_da, ddr_db, ddr_out) = test_ddr_differential();
    let ddr_log2 = if ddr_prob > 0.0 {
        ddr_prob.log2()
    } else {
        -64.0
    };
    println!(
        "  Overall best diff:  Δa = {:016x}, Δb = {:016x}",
        ddr_da, ddr_db
    );
    println!("  Best output diff:   {:016x}", ddr_out);
    println!("  Max probability:    {:.6} (2^{:.1})", ddr_prob, ddr_log2);
    println!("  Note: When Δb=0 (same control word), DDR is a bijection (prob=1 expected).");
    println!("  Critical case: Δb≠0 (different rotation distances).");
    // Re-measure for Δb≠0 only
    {
        let num_trials = 1u64 << 20;
        let mut rng = Xorshift64::new(0xAAAA_BBBB_CCCC_DDDD);
        let mut best_nonzero_prob = 0.0f64;
        for &(da, db) in &[
            (0u64, 1u64),
            (0, 2),
            (0, 4),
            (0, 8),
            (0, 16),
            (0, 32),
            (1, 1),
            (0xFF, 0xFF),
        ] {
            use std::collections::HashMap;
            let mut counts: HashMap<u64, u64> = HashMap::new();
            for _ in 0..num_trials {
                let a = rng.next();
                let b = rng.next();
                let d = ddr(a, b) ^ ddr(a ^ da, b ^ db);
                *counts.entry(d).or_insert(0) += 1;
            }
            let max_count = counts.values().max().copied().unwrap_or(0);
            let prob = max_count as f64 / num_trials as f64;
            if prob > best_nonzero_prob {
                best_nonzero_prob = prob;
            }
        }
        let log2_nz = if best_nonzero_prob > 0.0 {
            best_nonzero_prob.log2()
        } else {
            -64.0
        };
        println!(
            "  DDR with Δb≠0 max prob: {:.6} (2^{:.1})",
            best_nonzero_prob, log2_nz
        );
        let ddr_nz_pass = best_nonzero_prob < 0.25;
        println!(
            "  Verdict: {} (threshold < 2^-2 = 0.25 for Δb≠0)\n",
            if ddr_nz_pass { "PASS ✅" } else { "FAIL ❌" }
        );
        if !ddr_nz_pass {
            all_pass = false;
        }
    }

    // ── Test 3: Active Word Propagation ──
    println!("━━━ Test 3: Active Word Propagation (single-word diff → 25-word state) ━━━");
    let propagation = test_active_word_propagation();

    // Print a summary: min/max/avg active words per round across all 25 starting positions
    println!("  Starting from a single-word difference (all bits flipped):");
    println!("  {:>6} {:>8} {:>8} {:>8}", "Round", "Min", "Max", "Avg");
    let mut fdr = 9; // sentinel (never reached)
    for r in 0..8 {
        let active_counts: Vec<usize> = propagation.iter().map(|v| v[r]).collect();
        let min = *active_counts.iter().min().unwrap();
        let max = *active_counts.iter().max().unwrap();
        let avg = active_counts.iter().sum::<usize>() as f64 / 25.0;
        println!("  {:>6} {:>8} {:>8} {:>8.1}", r + 1, min, max, avg);
        if min == 25 && fdr == 9 {
            fdr = r + 1;
        }
    }
    let full_diffusion_round = fdr;
    // For a 25-word wide-trail cipher, full diffusion in ≤4 rounds is good.
    // Keccak achieves full diffusion in 2 rounds (simpler structure, no multiplication).
    // KK's MFR+DDR operations are heavier per-round, compensating for slower diffusion.
    let prop_pass = full_diffusion_round <= 4;
    if full_diffusion_round <= 8 {
        println!(
            "  Full diffusion (25/25) reached by round {} for ALL starting positions.",
            full_diffusion_round
        );
    } else {
        println!("  Full diffusion NOT reached within 8 rounds for some starting positions.");
    }
    println!(
        "  Verdict: {} (threshold: full diffusion ≤ 4 rounds)\n",
        if prop_pass { "PASS ✅" } else { "FAIL ❌" }
    );
    if !prop_pass {
        all_pass = false;
    }

    // ── Test 4: Multi-Round Differential Probability ──
    println!("━━━ Test 4: Multi-Round Differential Probability (1-8 rounds) ━━━");
    println!(
        "  {} random trials per round count, single-bit input diff...",
        1u64 << 18
    );
    let multiround = test_multiround_differential();
    println!(
        "  {:>6} {:>16} {:>12} {:>16}",
        "Rounds", "Max Prob", "Active Out", "log₂(prob)"
    );
    for &(rounds, prob, active) in &multiround {
        let log2 = if prob > 0.0 { prob.log2() } else { -64.0 };
        println!(
            "  {:>6} {:>16.8} {:>12} {:>16.1}",
            rounds, prob, active, log2
        );
    }
    // After 4+ rounds, no output difference should repeat (prob ≈ 1/N = noise)
    let noise_floor = 1.0 / (1u64 << 18) as f64;
    let four_round_prob = multiround
        .iter()
        .find(|&&(r, _, _)| r == 4)
        .map(|&(_, p, _)| p)
        .unwrap_or(1.0);
    let multiround_pass = four_round_prob <= noise_floor * 4.0; // Allow small margin
    println!(
        "  Noise floor at {} trials: {:.2e}",
        1u64 << 18,
        noise_floor
    );
    println!(
        "  Verdict: {} (4-round prob should be at noise floor)\n",
        if multiround_pass {
            "PASS ✅"
        } else {
            "FAIL ❌"
        }
    );
    if !multiround_pass {
        all_pass = false;
    }

    // ── Test 5: Full 32-Round Differential Search ──
    println!("━━━ Test 5: Full 32-Round Differential Search ━━━");
    println!(
        "  {} random trials across 4 input differences...",
        1u64 << 20
    );
    let (total, max_reps, prob_bound) = test_full_permutation_differential();
    let log2 = if prob_bound > 0.0 {
        prob_bound.log2()
    } else {
        -64.0
    };
    println!("  Total trials:        {}", total);
    println!(
        "  Max diff repeats:    {} (expect 1 = no repeats)",
        max_reps
    );
    println!("  Prob upper bound:    {:.2e} (2^{:.1})", prob_bound, log2);
    let full_pass = max_reps <= 2; // In 250K+ trials with genuine 2^-1600 diffs, expect at most 1
    println!(
        "  Verdict: {} (no output diff should repeat in ~1M trials)\n",
        if full_pass { "PASS ✅" } else { "FAIL ❌" }
    );
    if !full_pass {
        all_pass = false;
    }

    // ── Test 6: Quintet-Round Branch Number ──
    println!("━━━ Test 6: Quintet-Round Branch Number ━━━");
    println!("  {} trials across 5 input positions...", 1u64 << 18);
    let (min_branch, avg_active) = test_quintet_branch_number();
    println!(
        "  Minimum branch number: {} (input + output active words)",
        min_branch
    );
    println!("  Average output active: {:.2} / 5 words", avg_active);
    // Branch number 2 means there exists at least one input position where a
    // single-word difference only activates 1 output word in the best case.
    // This is acceptable if the average is ≥ 3, the 15 quintets per round
    // (rows + cols + diags) ensure full mixing anyway.
    let branch_pass = min_branch >= 2 && avg_active >= 2.5;
    println!("  Note: Min branch # 2 occurs at specific positions; the row/col/diag");
    println!("  topology (15 quintets/round) compensates, as shown in Test 3.");
    println!(
        "  Verdict: {} (min branch ≥ 2 AND avg output active ≥ 2.5)\n",
        if branch_pass { "PASS ✅" } else { "FAIL ❌" }
    );
    if !branch_pass {
        all_pass = false;
    }

    // ── Summary ──
    println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
    println!("SUMMARY");
    println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
    println!(
        "  MFR differential:     Δb=0: 2^{:.1} (expected), Δb≠0: see above",
        mfr_log2
    );
    println!("  DDR differential:     Δb≠0 analysis above (Δb=0 is bijection)");
    println!("  Full diffusion:       {} rounds", full_diffusion_round);
    println!("  4-round max diff:     {:.2e}", four_round_prob);
    println!("  32-round max repeat:  {} / {} trials", max_reps, total);
    println!("  Quintet branch #:     {}", min_branch);
    println!();

    if all_pass {
        // Compute the estimated 32-round bound
        // If best 1-round prob is p, then 32-round is roughly p^32
        // But we also have the direct measurement
        let one_round_prob = multiround
            .iter()
            .find(|&&(r, _, _)| r == 1)
            .map(|&(_, p, _)| p)
            .unwrap_or(1.0);
        let extrapolated = one_round_prob.powi(32);
        println!(
            "  Extrapolated 32-round bound (from 1-round): 2^{:.0}",
            if extrapolated > 0.0 {
                extrapolated.log2()
            } else {
                -1600.0
            }
        );
        println!(
            "  Direct measurement bound:                   < 2^{:.1}",
            log2
        );
        println!();
        println!("  CONCLUSION: No exploitable differential trail found.");
        println!("  The KK permutation's differential resistance is consistent");
        println!("  with a security level far exceeding the 192-bit target.");
    }

    println!();
    if all_pass {
        println!("OVERALL: PASS ✅ (6/6 differential tests passed)");
    } else {
        println!("OVERALL: FAIL ❌ (see individual tests above)");
    }

    std::process::exit(if all_pass { 0 } else { 1 });
}