SilverSight/rust/src/bin/hachimoji_encode.rs
allaun 3b6baec64e wip: durability snapshot of local working tree (pre-existing, uncommitted)
Snapshot of previously-uncommitted local work so nothing is lost after the
power outage. NOT reviewed for correctness — a WIP checkpoint, not a feature:
- multi-language hachimoji encoders (c/cpp/fortran/julia/octave/r/scala/go/rust/coq)
- formal Lean WIP (BraidTree, Eisenstein, HachimojiCapture, MathlibConnect,
  ModularFormBridge, ClusterManifold) + lakefile + E8Sidon edit
- docs/, experiments/ (epyc oisc benches), deploy/, scripts, test scaffolding
- .gitignore: exclude **/target/ and Coq build artifacts

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-02 20:49:53 -05:00

177 lines
5.2 KiB
Rust

use std::collections::HashMap;
const LETTER_NAMES: [&str; 8] = ["\u{03A6}", "\u{039B}", "\u{03A1}", "\u{039A}", "\u{03A9}", "\u{03A3}", "\u{03A0}", "\u{0396}"];
fn sigma3(n: u64) -> u64 {
if n == 0 {
return 0;
}
let mut sum: u64 = 0;
let mut d: u64 = 1;
while d.saturating_mul(d) <= n {
if n % d == 0 {
sum = sum.wrapping_add(d.wrapping_mul(d).wrapping_mul(d));
let other = n / d;
if other != d {
sum = sum.wrapping_add(other.wrapping_mul(other).wrapping_mul(other));
}
}
d = d.wrapping_add(1);
}
sum
}
fn hachimoji_letter(sigma3_value: u64) -> u8 {
(sigma3_value % 8) as u8
}
fn cartan_weight(a: u8, b: u8) -> u64 {
if a == b {
273
} else if a / 2 == b / 2 {
256
} else {
0
}
}
struct GateResult {
passed: bool,
collisions: usize,
energy_remaining: u64,
}
fn angrysphinx_gate(elements: &[u64]) -> GateResult {
let mut sum_counts: HashMap<u64, usize> = HashMap::new();
let n = elements.len();
for i in 0..n {
for j in i..n {
let s = elements[i].wrapping_add(elements[j]);
*sum_counts.entry(s).or_insert(0) += 1;
}
}
let collisions: usize = sum_counts.values().map(|&c| if c > 1 { c - 1 } else { 0 }).sum();
let c = collisions as i64;
let raw: i64 = 273 + 17 * c - 256 * c;
let energy_remaining: u64 = if raw > 0 { raw as u64 } else { 0 };
GateResult {
passed: collisions <= 1,
collisions,
energy_remaining,
}
}
struct EncodeResult {
sigma3_value: u64,
hachimoji_letter: u8,
letter_name: &'static str,
}
fn hachimoji_encode(n: u64) -> EncodeResult {
let s3 = sigma3(n);
let letter = hachimoji_letter(s3);
EncodeResult {
sigma3_value: s3,
hachimoji_letter: letter,
letter_name: LETTER_NAMES[letter as usize],
}
}
fn main() {
println!("=== Hachimoji Encoder + AngrySphinx Gate (Rust) ===");
println!();
let test_numbers: [u64; 10] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let expected_sigma3: [u64; 10] = [1, 9, 28, 73, 126, 252, 344, 585, 757, 1134];
println!("--- Sigma3 Verification ---");
let mut all_sigma3_ok = true;
for (i, &n) in test_numbers.iter().enumerate() {
let s3 = sigma3(n);
let ok = s3 == expected_sigma3[i];
if !ok {
all_sigma3_ok = false;
}
println!(
" n={:>2}: sigma3={:>5} expected={:>5} {}",
n,
s3,
expected_sigma3[i],
if ok { "OK" } else { "FAIL" }
);
}
assert!(all_sigma3_ok, "sigma3 verification failed");
println!(" Sigma3: PASS");
println!();
println!("--- Hachimoji Encoding ---");
for &n in &test_numbers {
let r = hachimoji_encode(n);
println!(
" n={:>2}: sigma3={:>5} letter={} (index={})",
n, r.sigma3_value, r.letter_name, r.hachimoji_letter
);
}
println!();
println!("--- Cartan Weight ---");
println!(" cartan_weight(0, 0) = {} (self)", cartan_weight(0, 0));
println!(" cartan_weight(0, 1) = {} (same pair, opposite sign)", cartan_weight(0, 1));
println!(" cartan_weight(0, 2) = {} (different pairs)", cartan_weight(0, 2));
println!();
println!("--- AngrySphinx Gate Tests ---");
{
let r = angrysphinx_gate(&[1, 2]);
assert_eq!(r.passed, true);
assert_eq!(r.collisions, 0);
assert_eq!(r.energy_remaining, 273);
println!(" elements=[1,2] -> passed={}, collisions={}, energy={} OK", r.passed, r.collisions, r.energy_remaining);
}
{
let r = angrysphinx_gate(&[1, 2, 3]);
assert_eq!(r.passed, true);
assert_eq!(r.collisions, 1);
assert_eq!(r.energy_remaining, 34);
println!(" elements=[1,2,3] -> passed={}, collisions={}, energy={} OK", r.passed, r.collisions, r.energy_remaining);
}
{
let r = angrysphinx_gate(&[1, 2, 3, 4]);
assert_eq!(r.passed, false);
assert_eq!(r.collisions, 3);
assert_eq!(r.energy_remaining, 0);
println!(" elements=[1,2,3,4] -> passed={}, collisions={}, energy={} OK", r.passed, r.collisions, r.energy_remaining);
}
println!(" AngrySphinx: PASS");
println!();
println!("--- E8LevelSet Tests ---");
{
let r = angrysphinx_gate(&[1, 2, 3]);
assert!(r.passed, "E8LevelSet(32) gate should be OPEN");
assert_eq!(r.collisions, 1);
println!(" E8LevelSet(32): elements=[1,2,3] -> gate OPEN ({} collision)", r.collisions);
}
{
let r = angrysphinx_gate(&[1, 2, 3]);
assert!(r.passed, "E8LevelSet(64) gate should be OPEN");
assert_eq!(r.collisions, 1);
println!(" E8LevelSet(64): elements=[1,2,3] -> gate OPEN ({} collision)", r.collisions);
}
{
let r = angrysphinx_gate(&[1, 2, 3, 4, 5]);
assert!(!r.passed, "E8LevelSet(128) gate should be CLOSED");
assert_eq!(r.collisions, 6);
println!(" E8LevelSet(128): elements=[1,2,3,4,5] -> gate CLOSED ({} collisions)", r.collisions);
}
println!(" E8LevelSet: PASS");
println!();
println!("=== All tests passed ===");
}