SilverSight/go/cmd/hachimoji_encode/main.go
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

188 lines
4.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import "fmt"
var letterNames = [8]string{"Φ", "Λ", "Ρ", "Κ", "Ω", "Σ", "Π", "Ζ"}
func sigma3(n uint64) uint64 {
if n == 0 {
return 0
}
var sum uint64 = 0
var d uint64 = 1
for d*d <= n {
if n%d == 0 {
sum += d * d * d
other := n / d
if other != d {
sum += other * other * other
}
}
d++
}
return sum
}
func hachimojiLetter(sigma3Value uint64) uint8 {
return uint8(sigma3Value % 8)
}
func cartanWeight(a, b uint8) uint64 {
if a == b {
return 273
}
if a/2 == b/2 {
return 256
}
return 0
}
type GateResult struct {
passed bool
collisions int
energyRemaining uint64
}
func angrysphinxGate(elements []uint64) GateResult {
sumCounts := make(map[uint64]int)
n := len(elements)
for i := 0; i < n; i++ {
for j := i; j < n; j++ {
sumCounts[elements[i]+elements[j]]++
}
}
collisions := 0
for _, count := range sumCounts {
if count > 1 {
collisions += count - 1
}
}
raw := 273 + 17*collisions - 256*collisions
if raw < 0 {
raw = 0
}
return GateResult{
passed: collisions <= 1,
collisions: collisions,
energyRemaining: uint64(raw),
}
}
type EncodeResult struct {
sigma3Value uint64
hachimojiLetter uint8
letterName string
}
func hachimojiEncode(n uint64) EncodeResult {
s3 := sigma3(n)
letter := hachimojiLetter(s3)
return EncodeResult{
sigma3Value: s3,
hachimojiLetter: letter,
letterName: letterNames[letter],
}
}
func main() {
fmt.Println("=== Hachimoji Encoder + AngrySphinx Gate (Go) ===")
fmt.Println()
testNumbers := []uint64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
expectedSigma3 := []uint64{1, 9, 28, 73, 126, 252, 344, 585, 757, 1134}
fmt.Println("--- Sigma3 Verification ---")
allSigma3Ok := true
for i, n := range testNumbers {
s3 := sigma3(n)
ok := s3 == expectedSigma3[i]
if !ok {
allSigma3Ok = false
}
status := "OK"
if !ok {
status = "FAIL"
}
fmt.Printf(" n=%2d: sigma3=%5d expected=%5d %s\n", n, s3, expectedSigma3[i], status)
}
if !allSigma3Ok {
panic("sigma3 verification failed")
}
fmt.Println(" Sigma3: PASS")
fmt.Println()
fmt.Println("--- Hachimoji Encoding ---")
for _, n := range testNumbers {
r := hachimojiEncode(n)
fmt.Printf(" n=%2d: sigma3=%5d letter=%s (index=%d)\n",
n, r.sigma3Value, r.letterName, r.hachimojiLetter)
}
fmt.Println()
fmt.Println("--- Cartan Weight ---")
fmt.Printf(" cartanWeight(0, 0) = %d (self)\n", cartanWeight(0, 0))
fmt.Printf(" cartanWeight(0, 1) = %d (same pair, opposite sign)\n", cartanWeight(0, 1))
fmt.Printf(" cartanWeight(0, 2) = %d (different pairs)\n", cartanWeight(0, 2))
fmt.Println()
fmt.Println("--- AngrySphinx Gate Tests ---")
{
r := angrysphinxGate([]uint64{1, 2})
if r.passed != true || r.collisions != 0 || r.energyRemaining != 273 {
panic("angrysphinxGate([1,2]) failed")
}
fmt.Printf(" elements=[1,2] -> passed=%v, collisions=%d, energy=%d OK\n",
r.passed, r.collisions, r.energyRemaining)
}
{
r := angrysphinxGate([]uint64{1, 2, 3})
if r.passed != true || r.collisions != 1 || r.energyRemaining != 34 {
panic("angrysphinxGate([1,2,3]) failed")
}
fmt.Printf(" elements=[1,2,3] -> passed=%v, collisions=%d, energy=%d OK\n",
r.passed, r.collisions, r.energyRemaining)
}
{
r := angrysphinxGate([]uint64{1, 2, 3, 4})
if r.passed != false || r.collisions != 3 || r.energyRemaining != 0 {
panic("angrysphinxGate([1,2,3,4]) failed")
}
fmt.Printf(" elements=[1,2,3,4] -> passed=%v, collisions=%d, energy=%d OK\n",
r.passed, r.collisions, r.energyRemaining)
}
fmt.Println(" AngrySphinx: PASS")
fmt.Println()
fmt.Println("--- E8LevelSet Tests ---")
{
r := angrysphinxGate([]uint64{1, 2, 3})
if !r.passed || r.collisions != 1 {
panic("E8LevelSet(32) gate should be OPEN")
}
fmt.Printf(" E8LevelSet(32): elements=[1,2,3] -> gate OPEN (%d collision)\n", r.collisions)
}
{
r := angrysphinxGate([]uint64{1, 2, 3})
if !r.passed || r.collisions != 1 {
panic("E8LevelSet(64) gate should be OPEN")
}
fmt.Printf(" E8LevelSet(64): elements=[1,2,3] -> gate OPEN (%d collision)\n", r.collisions)
}
{
r := angrysphinxGate([]uint64{1, 2, 3, 4, 5})
if r.passed || r.collisions != 6 {
panic("E8LevelSet(128) gate should be CLOSED")
}
fmt.Printf(" E8LevelSet(128): elements=[1,2,3,4,5] -> gate CLOSED (%d collisions)\n", r.collisions)
}
fmt.Println(" E8LevelSet: PASS")
fmt.Println()
fmt.Println("=== All tests passed ===")
}