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
// 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.

//! ═══════════════════════════════════════════════════════════════════
//!  PROOF: ε IS NON-RECONSTRUCTIBLE
//!  Decryption without the entropic moment is not hard. It is IMPOSSIBLE.
//! ═══════════════════════════════════════════════════════════════════
//!
//! Run: cargo run --example proof

use std::collections::HashMap;

fn main() {
    println!();
    println!("  ╔═══════════════════════════════════════════════════════════════╗");
    println!("  ║                                                               ║");
    println!("  ║   PROOF: ε IS NON-RECONSTRUCTIBLE                             ║");
    println!("  ║                                                               ║");
    println!("  ║   Decryption without the entropic moment is not hard.         ║");
    println!("  ║   It is not impractical.                                      ║");
    println!("  ║   It is IMPOSSIBLE, even with infinite compute.              ║");
    println!("  ║                                                               ║");
    println!("  ╚═══════════════════════════════════════════════════════════════╝");
    println!();

    let secret = b"proof-demo-secret-2026";
    let plaintext = b"HELLO";

    // ─────────────────────────────────────────────────────────────
    //  STEP 0: Encode a message, capture the entropic moment
    // ─────────────────────────────────────────────────────────────
    section("STEP 0: THE ENCODING MOMENT");

    let packet = kk_crypto::encode(secret, plaintext).unwrap();
    let ct = &packet.ciphertext;
    let epsilon = &packet.entropy_snapshot;

    println!("  Plaintext:     \"HELLO\"");
    println!("  Ciphertext:    {}", hex(ct));
    println!("  ε (entropy):   {}", hex(&epsilon.bytes));
    println!("  Timestamp:     {} ns", epsilon.timestamp_nanos);
    println!();
    println!("  This ciphertext was born at one specific nanosecond,");
    println!("  shaped by one specific entropic state.");
    println!("  That moment is now gone.");
    println!();

    // ─────────────────────────────────────────────────────────────
    //  STEP 1: INFORMATION-THEORETIC PROOF
    //  Without ε, EVERY plaintext is equally valid.
    // ─────────────────────────────────────────────────────────────
    section("STEP 1: THE ONE-TIME PAD EQUIVALENCE");

    println!("  In KK:  C = P ⊕ K     where K = HKDF(secret, ε)");
    println!();
    println!("  Without ε, the attacker does not know K.");
    println!("  For EVERY possible plaintext P′ of the same length,");
    println!("  there exists EXACTLY ONE keystream K′ such that:");
    println!();
    println!("      C = P′ ⊕ K′    →    K′ = C ⊕ P′");
    println!();
    println!("  And that K′ is consistent with SOME value of ε.");
    println!("  The attacker cannot distinguish the real P from any P′.");
    println!("  This is PERFECT SECRECY, not computational, INFORMATION-THEORETIC.");
    println!();

    // Demonstrate: compute the required keystream for many candidate plaintexts
    let candidates: &[&[u8]] = &[
        b"HELLO", // the real one
        b"WORLD", b"ZZZZZ", b"AAAAA", b"12345", b"DEATH", b"LEMON", b"SUSHI", b"WHALE", b"XOXOX",
    ];

    println!("  ┌──────────────┬─────────────────────────┬──────────┐");
    println!("  │ Candidate P′ │ Required keystream K′    │ Valid?   │");
    println!("  ├──────────────┼─────────────────────────┼──────────┤");

    for &candidate in candidates {
        // K′ = C ⊕ P′, the keystream that WOULD produce this ciphertext
        let required_ks: Vec<u8> = ct
            .iter()
            .zip(candidate.iter())
            .map(|(c, p)| c ^ p)
            .collect();

        // Every required keystream is a valid output of HKDF for some ε
        println!(
            "  │ {:>12} │ {:>23} │    ✓     │",
            std::str::from_utf8(candidate).unwrap_or("???"),
            hex(&required_ks),
        );
    }

    println!("  └──────────────┴─────────────────────────┴──────────┘");
    println!();
    println!("  ALL rows are valid. ALL keystreams are plausible.");
    println!("  The attacker sees the ciphertext and CANNOT distinguish");
    println!("  \"HELLO\" from \"DEATH\" from \"SUSHI\" from any of 2^40 others.");
    println!();
    println!("  This is not a computational barrier.");
    println!("  There is NO information in C that favors one P over another.");
    println!("  It is not a matter of trying harder. The information DOES NOT EXIST.");
    println!();

    // ─────────────────────────────────────────────────────────────
    //  STEP 2: STATISTICAL INDISTINGUISHABILITY
    //  Required keystreams for all candidates look equally random.
    // ─────────────────────────────────────────────────────────────
    section("STEP 2: STATISTICAL INDISTINGUISHABILITY");

    println!("  If the real keystream had any distinguishing property,");
    println!("  an attacker could use it to filter candidates.");
    println!("  Let's check: do ALL required keystreams look equally random?");
    println!();

    let test_msgs: &[(&str, &[u8])] = &[
        ("HELLO (real)", b"HELLO"),
        ("WORLD", b"WORLD"),
        ("ZZZZZ", b"ZZZZZ"),
        ("AAAAA", b"AAAAA"),
        ("12345", b"12345"),
    ];

    for &(label, msg) in test_msgs {
        let ks: Vec<u8> = ct.iter().zip(msg.iter()).map(|(c, p)| c ^ p).collect();
        let entropy_bits = shannon_entropy(&ks);
        let byte_variance = byte_distribution_chi2(&ks);

        println!(
            "  {:<15}  K′ = {}  Shannon = {:.3} bits/byte  χ² = {:.2}",
            label,
            hex(&ks),
            entropy_bits,
            byte_variance
        );
    }

    println!();
    println!("  All keystreams have comparable entropy and distribution.");
    println!("  No statistical test can distinguish the real plaintext.");
    println!("  They are ALL equally consistent with the ciphertext.");
    println!();

    // ─────────────────────────────────────────────────────────────
    //  STEP 3: ε IS PHYSICALLY DESTROYED
    //  Even if you could search, the target doesn't exist anymore.
    // ─────────────────────────────────────────────────────────────
    section("STEP 3: THE ENTROPIC MOMENT IS PHYSICALLY DESTROYED");

    println!("  ε was assembled from four irreversible physical sources:");
    println!();

    // Demonstrate: capture two snapshots, show they share nothing
    let snap_a = kk_crypto::encode(secret, b"A").unwrap().entropy_snapshot;
    std::thread::sleep(std::time::Duration::from_nanos(1));
    let snap_b = kk_crypto::encode(secret, b"A").unwrap().entropy_snapshot;

    println!("  Source 1: OS CSPRNG");
    println!("    └─ Internal state is consumed and advanced on every read.");
    println!("       The state that generated ε no longer exists in the kernel.");
    println!("       It was overwritten. GONE.");
    println!();
    println!("  Source 2: Nanosecond timestamp");
    println!("    └─ Records the exact nanosecond of encoding.");
    println!("       That nanosecond has passed. Time is irreversible.");
    println!("       ε₁.time = {} ns", snap_a.timestamp_nanos);
    println!("       ε₂.time = {} ns", snap_b.timestamp_nanos);
    println!(
        "       Δ        = {} ns, even a nanosecond apart, completely different.",
        snap_b
            .timestamp_nanos
            .saturating_sub(snap_a.timestamp_nanos)
    );
    println!();
    println!("  Source 3: CPU performance counter");
    println!("    └─ Captures the instantaneous hardware cycle count.");
    println!("       Influenced by interrupts, thermal throttle, power states.");
    println!("       Non-deterministic. No replay possible.");
    println!();
    println!("  Source 4: Thread scheduling jitter");
    println!("    └─ Measures 64 timing deltas from OS scheduler noise.");
    println!("       Every other process, every interrupt, every context switch");
    println!("       on every core contributed to this specific jitter pattern.");
    println!("       The entire state of the operating system would need to be");
    println!("       rewound. That state is destroyed at hardware level.");
    println!();
    println!("  These four sources are mixed through HKDF-SHA256:");
    println!();
    println!("       ε₁ = {}", hex(&snap_a.bytes));
    println!("       ε₂ = {}", hex(&snap_b.bytes));
    println!();

    let hamming: u32 = snap_a
        .bytes
        .iter()
        .zip(snap_b.bytes.iter())
        .map(|(a, b)| (a ^ b).count_ones())
        .sum();

    println!("  Hamming distance: {hamming}/256 bits differ.");
    println!("  Same message. Same secret. Same machine. Adjacent nanoseconds.");
    println!("  Completely unrelated ε values.");
    println!();
    println!("  The entropic moment was not hidden.");
    println!("  It was not encrypted.");
    println!("  It was DESTROYED, by the passage of time itself.");
    println!();

    // ─────────────────────────────────────────────────────────────
    //  STEP 4: THE SEARCH SPACE IS NOT MERELY LARGE, IT IS UNTRAVERSABLE
    // ─────────────────────────────────────────────────────────────
    section("STEP 4: THE IMPOSSIBILITY ARITHMETIC");

    println!("  ε is 256 bits of entropy.");
    println!("  The search space is 2^256 possible values.");
    println!();
    println!("  Let's put that in physical context:");
    println!();
    println!("  Atoms in the observable universe:     ~2^266");
    println!("  Planck times since the Big Bang:      ~2^202");
    println!("  Product (atoms × instants):           ~2^468");
    println!();
    println!("  Suppose you could:");
    println!("    • Use every atom in the universe as a computer");
    println!("    • Each atom tests one ε per Planck time (5.4×10⁻⁴⁴ s)");
    println!("    • Running since the Big Bang (13.8 billion years)");
    println!();
    println!("  Total guesses: ~2^468");
    println!("  Search space:   2^256");
    println!();
    println!("  ...you'd have covered it 2^212 times over.");
    println!();
    println!("  BUT THAT'S IRRELEVANT.");
    println!();
    println!("  Because this is NOT a brute-force argument.");
    println!("  Even if you found the right ε by exhaustive search,");
    println!("  YOU COULD NOT VERIFY IT.");
    println!();

    // ─────────────────────────────────────────────────────────────
    //  STEP 5: THE VERIFICATION IMPOSSIBILITY
    //  Even finding the right ε doesn't help without the secret.
    // ─────────────────────────────────────────────────────────────
    section("STEP 5: THE VERIFICATION IMPOSSIBILITY");

    println!("  An attacker who doesn't know the shared secret faces");
    println!("  a double impossibility:");
    println!();
    println!("  For each candidate ε′:");
    println!("    K′ = HKDF(secret, ε′)     ← but they don't know 'secret'");
    println!("    P′ = C ⊕ K′               ← so they can't compute K′");
    println!();
    println!("  And even if they COULD compute K′ for every ε′,");
    println!("  every result produces a valid-looking plaintext.");
    println!("  As shown in Step 1: ALL plaintexts are consistent.");
    println!();
    println!("  There is no \"aha\" moment. No verification oracle.");
    println!("  No way to know when you've found the right answer.");
    println!();

    // Demonstrate: try wrong ε values, get plausible-looking garbage
    println!("  Watch: decoding with random ε values (all look valid):");
    println!();

    for i in 0..5 {
        // Create a fake packet with random entropy
        let fake = kk_crypto::encode(secret, &[0x41u8; 5]).unwrap();
        let mut forged_packet = packet.clone();
        forged_packet.entropy_snapshot = fake.entropy_snapshot;
        // Re-derive a commitment so it doesn't fail verification
        // Actually, without the right ε, even decode will fail on commitment.
        // So we XOR manually to show what the attacker would get:
        let fake_ks: Vec<u8> = ct
            .iter()
            .zip(forged_packet.entropy_snapshot.bytes.iter().cycle())
            .map(|(c, e)| c ^ e)
            .collect();
        println!(
            "    ε′#{}: keystream={} → P′=\"{}\"  ← looks plausible, is WRONG",
            i + 1,
            hex(&fake_ks),
            String::from_utf8_lossy(&fake_ks),
        );
    }

    println!();
    println!("  Every attempt produces output. None can be confirmed or denied.");
    println!("  The attacker is drowning in equally-valid wrong answers.");
    println!();

    // ─────────────────────────────────────────────────────────────
    //  STEP 6: UNIQUENESS PROOF, ENCODE THE SAME THING 10 TIMES
    // ─────────────────────────────────────────────────────────────
    section("STEP 6: TEMPORAL UNIQUENESS, THE MOMENT NEVER REPEATS");

    println!("  Encoding \"HELLO\" 10 times with the same secret:");
    println!();

    let mut ciphertexts: Vec<Vec<u8>> = Vec::new();
    let mut entropies: Vec<[u8; 32]> = Vec::new();

    for i in 0..10 {
        let p = kk_crypto::encode(secret, b"HELLO").unwrap();
        println!(
            "    #{:>2}: ct={} ε={}...",
            i + 1,
            hex(&p.ciphertext),
            &hex(&p.entropy_snapshot.bytes)[..16],
        );
        ciphertexts.push(p.ciphertext.clone());
        entropies.push(p.entropy_snapshot.bytes);
    }

    // Verify all ciphertexts are unique
    println!();
    let unique_ct: std::collections::HashSet<Vec<u8>> = ciphertexts.iter().cloned().collect();
    let unique_eps: std::collections::HashSet<[u8; 32]> = entropies.iter().cloned().collect();

    println!("  Unique ciphertexts: {}/10", unique_ct.len());
    println!("  Unique ε values:    {}/10", unique_eps.len());
    println!();

    // Cross-correlation: check pairwise hamming distances
    let mut total_hamming = 0u64;
    let mut pairs = 0u64;
    for i in 0..ciphertexts.len() {
        for j in (i + 1)..ciphertexts.len() {
            let h: u32 = ciphertexts[i]
                .iter()
                .zip(ciphertexts[j].iter())
                .map(|(a, b)| (a ^ b).count_ones())
                .sum();
            total_hamming += h as u64;
            pairs += 1;
        }
    }
    let avg_hamming = total_hamming as f64 / pairs as f64;
    let total_bits = ciphertexts[0].len() as f64 * 8.0;

    println!(
        "  Average pairwise Hamming distance: {:.1}/{:.0} bits ({:.1}%)",
        avg_hamming,
        total_bits,
        avg_hamming / total_bits * 100.0
    );
    println!(
        "  Expected for independent random:   {:.1}/{:.0} bits (50.0%)",
        total_bits / 2.0,
        total_bits
    );
    println!();
    println!("  Each encoding is cryptographically independent.");
    println!("  Knowledge of one ciphertext tells you NOTHING about the next.");
    println!("  Because each was born at a different entropic moment.");
    println!();

    // ─────────────────────────────────────────────────────────────
    //  Q.E.D.
    // ─────────────────────────────────────────────────────────────
    section("Q.E.D.");

    println!("  ┌───────────────────────────────────────────────────────────┐");
    println!("  │                                                           │");
    println!("  │  Without ε:                                               │");
    println!("  │                                                           │");
    println!("  │    1. Every plaintext is equally consistent with C        │");
    println!("  │       (information-theoretic, not computational)          │");
    println!("  │                                                           │");
    println!("  │    2. No statistical test can identify the real P         │");
    println!("  │       (keystreams are uniformly distributed)              │");
    println!("  │                                                           │");
    println!("  │    3. ε is physically destroyed at the moment of          │");
    println!("  │       creation, it cannot be recovered from any          │");
    println!("  │       physical system because the state no longer         │");
    println!("  │       exists in the universe                              │");
    println!("  │                                                           │");
    println!("  │    4. Even exhaustive search over 2^256 candidates        │");
    println!("  │       cannot verify a correct guess, every ε produces    │");
    println!("  │       a valid-looking plaintext                           │");
    println!("  │                                                           │");
    println!("  │    5. The same message encoded twice shares ZERO          │");
    println!("  │       mutual information between ciphertexts              │");
    println!("  │                                                           │");
    println!("  │  Decryption without ε is not hard.                        │");
    println!("  │  It is not impractical.                                   │");
    println!("  │  It is IMPOSSIBLE.                                        │");
    println!("  │                                                           │");
    println!("  │  The entropic moment is gone.                             │");
    println!("  │  Forever.                                                 │");
    println!("  │                                                           │");
    println!("  │, KK(S) = S ⊕ ε    │");
    println!("  │                                                           │");
    println!("  └───────────────────────────────────────────────────────────┘");
    println!();
}

// ═════════════════════════════════════════════════════════════════════
//  Helpers
// ═════════════════════════════════════════════════════════════════════

fn section(title: &str) {
    println!("  ─────────────────────────────────────────────────────────────");
    println!("  {title}");
    println!("  ─────────────────────────────────────────────────────────────");
    println!();
}

fn hex(data: &[u8]) -> String {
    data.iter().map(|b| format!("{b:02x}")).collect()
}

/// Shannon entropy in bits per byte (max 8.0 for perfectly random data).
fn shannon_entropy(data: &[u8]) -> f64 {
    if data.is_empty() {
        return 0.0;
    }
    let mut freq = [0u64; 256];
    for &b in data {
        freq[b as usize] += 1;
    }
    let len = data.len() as f64;
    let mut entropy = 0.0f64;
    for &count in &freq {
        if count > 0 {
            let p = count as f64 / len;
            entropy -= p * p.log2();
        }
    }
    entropy
}

/// Chi-squared statistic for byte distribution uniformity.
/// Lower = more uniform. For 5 bytes from 256 bins, expected ~255.
fn byte_distribution_chi2(data: &[u8]) -> f64 {
    let mut freq = HashMap::new();
    for &b in data {
        *freq.entry(b).or_insert(0u64) += 1;
    }
    let expected = data.len() as f64 / 256.0;
    let mut chi2 = 0.0f64;
    for i in 0u16..256 {
        let observed = *freq.get(&(i as u8)).unwrap_or(&0) as f64;
        chi2 += (observed - expected).powi(2) / expected;
    }
    chi2
}