kk-crypto 0.1.2

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

//! KK Visual Demo, Watch the Keeney Kode primitive in action.
//!
//! Run: cargo run --example visual

use std::io;
use std::time::{Duration, Instant};

use crossterm::{
    event::{self, Event, KeyCode, KeyEventKind},
    execute,
    terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::{
    layout::{Constraint, Direction, Layout},
    prelude::CrosstermBackend,
    style::{Color, Modifier, Style, Stylize},
    text::{Line, Span, Text},
    widgets::{Block, Borders, Paragraph, Wrap},
    Frame, Terminal,
};

struct App {
    input: String,
    cursor_pos: usize,
    shared_secret: String,
    encode_result: Option<EncodeResult>,
    status: String,
    encode_count: u64,
}

struct EncodeResult {
    ciphertext_hex: String,
    entropy_hex: String,
    timestamp_nanos: u128,
    commitment_hex: String,
    decoded_text: String,
    wire_size: usize,
    encode_time: Duration,
    decode_time: Duration,
    match_ok: bool,
}

impl App {
    fn new() -> Self {
        Self {
            input: String::new(),
            cursor_pos: 0,
            shared_secret: "kk-demo-secret-2026".into(),
            encode_result: None,
            status: "Type a message and press Enter to encode. Tab = edit secret. Esc = quit."
                .into(),
            encode_count: 0,
        }
    }

    fn do_encode(&mut self) {
        if self.input.is_empty() {
            self.status = "Nothing to encode, type something first!".into();
            return;
        }

        let secret = self.shared_secret.as_bytes();
        let plaintext = self.input.as_bytes();

        // Encode
        let t0 = Instant::now();
        let packet = match kk_crypto::encode(secret, plaintext) {
            Ok(p) => p,
            Err(e) => {
                self.status = format!("Encode error: {e}");
                return;
            }
        };
        let encode_time = t0.elapsed();

        // Serialize wire format
        let wire = packet.to_bytes();

        // Decode
        let t1 = Instant::now();
        let decoded = match kk_crypto::decode(secret, &packet) {
            Ok(d) => d,
            Err(e) => {
                self.status = format!("Decode error: {e}");
                return;
            }
        };
        let decode_time = t1.elapsed();

        let decoded_text = String::from_utf8_lossy(&decoded).to_string();
        let match_ok = plaintext == decoded.as_slice();

        self.encode_count += 1;
        self.encode_result = Some(EncodeResult {
            ciphertext_hex: hex_encode(&packet.ciphertext),
            entropy_hex: hex_encode(&packet.entropy_snapshot.bytes),
            timestamp_nanos: packet.entropy_snapshot.timestamp_nanos,
            commitment_hex: hex_encode(&packet.commitment.mac),
            decoded_text,
            wire_size: wire.len(),
            encode_time,
            decode_time,
            match_ok,
        });
        self.status = format!(
            "#{}, Encoded & decoded in {:.1}µs + {:.1}µs | Press Enter to re-encode (new ε each time)",
            self.encode_count,
            encode_time.as_nanos() as f64 / 1000.0,
            decode_time.as_nanos() as f64 / 1000.0,
        );
    }
}

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

fn main() -> Result<(), Box<dyn std::error::Error>> {
    enable_raw_mode()?;
    let mut stdout = io::stdout();
    execute!(stdout, EnterAlternateScreen)?;
    let backend = CrosstermBackend::new(stdout);
    let mut terminal = Terminal::new(backend)?;

    let mut app = App::new();
    let mut editing_secret = false;

    loop {
        terminal.draw(|f| ui(f, &app, editing_secret))?;

        if event::poll(Duration::from_millis(50))? {
            if let Event::Key(key) = event::read()? {
                if key.kind != KeyEventKind::Press {
                    continue;
                }
                match key.code {
                    KeyCode::Esc => break,
                    KeyCode::Tab => {
                        editing_secret = !editing_secret;
                        if editing_secret {
                            app.status = "Editing shared secret. Tab to go back to message.".into();
                        } else {
                            app.status = "Type a message and press Enter to encode.".into();
                        }
                    }
                    KeyCode::Enter => {
                        if !editing_secret {
                            app.do_encode();
                        }
                    }
                    KeyCode::Backspace => {
                        let target = if editing_secret {
                            &mut app.shared_secret
                        } else {
                            &mut app.input
                        };
                        if !target.is_empty() {
                            if editing_secret {
                                target.pop();
                            } else if app.cursor_pos > 0 {
                                app.cursor_pos -= 1;
                                target.remove(app.cursor_pos);
                            }
                        }
                    }
                    KeyCode::Left => {
                        if !editing_secret && app.cursor_pos > 0 {
                            app.cursor_pos -= 1;
                        }
                    }
                    KeyCode::Right => {
                        if !editing_secret && app.cursor_pos < app.input.len() {
                            app.cursor_pos += 1;
                        }
                    }
                    KeyCode::Char(c) => {
                        if editing_secret {
                            app.shared_secret.push(c);
                        } else {
                            app.input.insert(app.cursor_pos, c);
                            app.cursor_pos += 1;
                        }
                    }
                    _ => {}
                }
            }
        }
    }

    disable_raw_mode()?;
    execute!(terminal.backend_mut(), LeaveAlternateScreen)?;
    Ok(())
}

fn ui(f: &mut Frame, app: &App, editing_secret: bool) {
    let outer = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(3), // title
            Constraint::Length(3), // secret
            Constraint::Length(3), // input
            Constraint::Min(10),   // results
            Constraint::Length(2), // status bar
        ])
        .split(f.area());

    // ─── TITLE ───────────────────────────────────────────
    let title = Paragraph::new(Line::from(vec![
        Span::styled(
            "  KK ",
            Style::default().fg(Color::Black).bg(Color::Cyan).bold(),
        ),
        Span::raw("  "),
        Span::styled(
            "Keeney Kode Visual Demo",
            Style::default().fg(Color::Cyan).bold(),
        ),
        Span::raw(" - "),
        Span::styled("KK(S) = S ⊕ ε", Style::default().fg(Color::Yellow).italic()),
    ]))
    .block(
        Block::default()
            .borders(Borders::BOTTOM)
            .border_style(Style::default().fg(Color::DarkGray)),
    );
    f.render_widget(title, outer[0]);

    // ─── SHARED SECRET ───────────────────────────────────
    let secret_style = if editing_secret {
        Style::default()
            .fg(Color::Yellow)
            .add_modifier(Modifier::BOLD)
    } else {
        Style::default().fg(Color::DarkGray)
    };
    let secret_block = Block::default()
        .title(Span::styled(" Shared Secret (Tab to edit) ", secret_style))
        .borders(Borders::ALL)
        .border_style(if editing_secret {
            Style::default().fg(Color::Yellow)
        } else {
            Style::default().fg(Color::DarkGray)
        });
    let secret_text = Paragraph::new(Line::from(vec![
        Span::raw("  "),
        Span::styled(&app.shared_secret, Style::default().fg(Color::Yellow)),
    ]))
    .block(secret_block);
    f.render_widget(secret_text, outer[1]);

    // ─── MESSAGE INPUT ───────────────────────────────────
    let input_style = if !editing_secret {
        Style::default().fg(Color::Green).bold()
    } else {
        Style::default().fg(Color::DarkGray)
    };
    let input_block = Block::default()
        .title(Span::styled(" Message (Enter to encode) ", input_style))
        .borders(Borders::ALL)
        .border_style(if !editing_secret {
            Style::default().fg(Color::Green)
        } else {
            Style::default().fg(Color::DarkGray)
        });
    let input_text = Paragraph::new(Line::from(vec![
        Span::raw("  "),
        Span::styled(&app.input, Style::default().fg(Color::White).bold()),
    ]))
    .block(input_block);
    f.render_widget(input_text, outer[2]);

    // Cursor
    if !editing_secret {
        f.set_cursor_position((outer[2].x + 3 + app.cursor_pos as u16, outer[2].y + 1));
    }

    // ─── RESULTS ─────────────────────────────────────────
    let results_layout = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
        .split(outer[3]);

    // Left: Encoded
    let encode_block = Block::default()
        .title(Span::styled(
            " ⚡ Encoded Packet ",
            Style::default().fg(Color::Magenta).bold(),
        ))
        .borders(Borders::ALL)
        .border_style(Style::default().fg(Color::Magenta));

    let encode_content = if let Some(r) = &app.encode_result {
        let ct_display = wrap_hex(&r.ciphertext_hex, 48);
        let mut lines = vec![Line::from(vec![Span::styled(
            "  Ciphertext: ",
            Style::default().fg(Color::DarkGray),
        )])];
        for chunk in ct_display {
            lines.push(Line::from(vec![
                Span::raw("    "),
                Span::styled(chunk, Style::default().fg(Color::Magenta)),
            ]));
        }
        lines.push(Line::raw(""));
        lines.push(Line::from(vec![
            Span::styled("  Entropy ε:  ", Style::default().fg(Color::DarkGray)),
            Span::styled(&r.entropy_hex[..16], Style::default().fg(Color::Cyan)),
            Span::styled("...", Style::default().fg(Color::DarkGray)),
        ]));
        lines.push(Line::from(vec![
            Span::styled("  Timestamp:  ", Style::default().fg(Color::DarkGray)),
            Span::styled(
                format!("{} ns", r.timestamp_nanos),
                Style::default().fg(Color::Cyan),
            ),
        ]));
        lines.push(Line::from(vec![
            Span::styled("  HMAC:       ", Style::default().fg(Color::DarkGray)),
            Span::styled(&r.commitment_hex[..16], Style::default().fg(Color::Red)),
            Span::styled("...", Style::default().fg(Color::DarkGray)),
        ]));
        lines.push(Line::from(vec![
            Span::styled("  Wire size:  ", Style::default().fg(Color::DarkGray)),
            Span::styled(
                format!("{} bytes", r.wire_size),
                Style::default().fg(Color::White),
            ),
        ]));
        lines.push(Line::from(vec![
            Span::styled("  Encode:     ", Style::default().fg(Color::DarkGray)),
            Span::styled(
                format!("{:.1} µs", r.encode_time.as_nanos() as f64 / 1000.0),
                Style::default().fg(Color::Yellow),
            ),
        ]));
        Text::from(lines)
    } else {
        Text::from(vec![
            Line::raw(""),
            Line::from(Span::styled(
                "    Press Enter to encode your message...",
                Style::default().fg(Color::DarkGray).italic(),
            )),
        ])
    };
    f.render_widget(
        Paragraph::new(encode_content)
            .block(encode_block)
            .wrap(Wrap { trim: false }),
        results_layout[0],
    );

    // Right: Decoded
    let decode_block = Block::default()
        .title(Span::styled(
            " ✓ Decoded Output ",
            Style::default().fg(Color::Green).bold(),
        ))
        .borders(Borders::ALL)
        .border_style(Style::default().fg(Color::Green));

    let decode_content = if let Some(r) = &app.encode_result {
        let match_indicator = if r.match_ok {
            Span::styled(" ✓ MATCH", Style::default().fg(Color::Green).bold())
        } else {
            Span::styled(" ✗ MISMATCH!", Style::default().fg(Color::Red).bold())
        };

        Text::from(vec![
            Line::raw(""),
            Line::from(vec![Span::styled(
                "  Plaintext: ",
                Style::default().fg(Color::DarkGray),
            )]),
            Line::from(vec![
                Span::raw("    "),
                Span::styled(&r.decoded_text, Style::default().fg(Color::White).bold()),
            ]),
            Line::raw(""),
            Line::from(vec![
                Span::styled("  Integrity: ", Style::default().fg(Color::DarkGray)),
                match_indicator,
            ]),
            Line::from(vec![
                Span::styled("  Decode:    ", Style::default().fg(Color::DarkGray)),
                Span::styled(
                    format!("{:.1} µs", r.decode_time.as_nanos() as f64 / 1000.0),
                    Style::default().fg(Color::Yellow),
                ),
            ]),
            Line::raw(""),
            Line::from(Span::styled(
                "  Press Enter again, same message,",
                Style::default().fg(Color::DarkGray).italic(),
            )),
            Line::from(Span::styled(
                "  new ε, completely different ciphertext.",
                Style::default().fg(Color::DarkGray).italic(),
            )),
        ])
    } else {
        Text::from(vec![
            Line::raw(""),
            Line::from(Span::styled(
                "    Decoded output will appear here...",
                Style::default().fg(Color::DarkGray).italic(),
            )),
        ])
    };
    f.render_widget(
        Paragraph::new(decode_content)
            .block(decode_block)
            .wrap(Wrap { trim: false }),
        results_layout[1],
    );

    // ─── STATUS BAR ──────────────────────────────────────
    let status = Paragraph::new(Line::from(vec![
        Span::styled(" ", Style::default()),
        Span::styled(&app.status, Style::default().fg(Color::DarkGray).italic()),
    ]));
    f.render_widget(status, outer[4]);
}

fn wrap_hex(hex: &str, width: usize) -> Vec<String> {
    hex.as_bytes()
        .chunks(width)
        .map(|c| String::from_utf8_lossy(c).to_string())
        .collect()
}