SilverSight/c/hachimoji_encode.c
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

179 lines
5.1 KiB
C

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
static const char *LETTER_NAMES[] = {
"Phi", "Lambda", "Rho", "Kappa", "Omega", "Sigma", "Pi", "Zeta"
};
/* sigma3(n) = Sum_{d|n} d^3 (0 for n=0) */
uint64_t sigma3(uint64_t n)
{
if (n == 0) return 0;
uint64_t sum = 0;
for (uint64_t d = 1; d * d <= n; d++) {
if (n % d == 0) {
sum += d * d * d;
uint64_t c = n / d;
if (c != d)
sum += c * c * c;
}
}
return sum;
}
/* Map sigma3 value to Hachimoji letter index 0-7 */
int hachimoji_letter(uint64_t s)
{
return (int)(s % 8);
}
/* Cartan energy between two Hachimoji letter indices */
int cartan_weight(int a, int b)
{
if (a == b) return 273;
if (a / 2 == b / 2) return 256;
return 0;
}
/* AngrySphinx gate: check if integer list passes energy budget.
Returns 1 (pass) if collisions <= 1, else 0.
collisions_out and energy_out are set via pointers. */
int angrysphinx_gate(const int *elements, int n,
int *collisions_out, int *energy_out)
{
int npairs = n * (n + 1) / 2;
int *sums = (int *)malloc((size_t)npairs * sizeof(int));
assert(sums != NULL);
int idx = 0;
for (int i = 0; i < n; i++)
for (int j = i; j < n; j++)
sums[idx++] = elements[i] + elements[j];
int collisions = 0;
for (int i = 0; i < npairs; i++)
for (int j = i + 1; j < npairs; j++)
if (sums[i] == sums[j])
collisions++;
free(sums);
*collisions_out = collisions;
int raw = 273 + 17 * collisions;
if (raw < 256 * collisions)
*energy_out = 0;
else
*energy_out = raw - 256 * collisions;
return collisions <= 1;
}
/* Full encoding: compute sigma3, letter index, print row */
void hachimoji_encode(uint64_t n)
{
uint64_t s = sigma3(n);
int idx = hachimoji_letter(s);
printf(" %-4llu %-9llu %-5d %s\n",
(unsigned long long)n,
(unsigned long long)s,
idx,
LETTER_NAMES[idx]);
}
int main(void)
{
/* ---------- sigma3 assertions ---------- */
assert(sigma3(0) == 0);
assert(sigma3(1) == 1);
assert(sigma3(2) == 9);
assert(sigma3(3) == 28);
assert(sigma3(4) == 73);
assert(sigma3(5) == 126);
assert(sigma3(6) == 252);
assert(sigma3(7) == 344);
assert(sigma3(8) == 585);
assert(sigma3(9) == 757);
assert(sigma3(10) == 1134);
/* ---------- hachimoji_letter assertions ---------- */
assert(hachimoji_letter(1) == 1 % 8);
assert(hachimoji_letter(9) == 9 % 8);
assert(hachimoji_letter(28) == 28 % 8);
assert(hachimoji_letter(73) == 73 % 8);
assert(hachimoji_letter(126) == 126 % 8);
assert(hachimoji_letter(252) == 252 % 8);
assert(hachimoji_letter(344) == 344 % 8);
assert(hachimoji_letter(585) == 585 % 8);
assert(hachimoji_letter(757) == 757 % 8);
assert(hachimoji_letter(1134) == 1134 % 8);
/* ---------- cartan_weight assertions ---------- */
assert(cartan_weight(0, 0) == 273);
assert(cartan_weight(0, 1) == 256);
assert(cartan_weight(0, 2) == 0);
assert(cartan_weight(2, 3) == 256);
assert(cartan_weight(3, 5) == 0);
assert(cartan_weight(7, 7) == 273);
/* ---------- AngrySphinx gate assertions ---------- */
{
int c, e;
assert(angrysphinx_gate((int[]){1,2}, 2, &c, &e) == 1);
assert(c == 0); assert(e == 273);
assert(angrysphinx_gate((int[]){1,2,3}, 3, &c, &e) == 1);
assert(c == 1); assert(e == 34);
assert(angrysphinx_gate((int[]){1,2,3,4}, 4, &c, &e) == 0);
assert(c == 3); assert(e == 0);
}
/* ========== Formatted output ========== */
printf("\nHachimoji Encoder Test Vector\n");
printf("=================================\n");
printf(" n sigma3 Index Letter\n");
printf(" --- ------- ----- ------\n");
for (uint64_t n = 1; n <= 10; n++)
hachimoji_encode(n);
printf("\nAngrySphinx Gate Tests\n");
printf("=============================\n");
printf(" Elements Passed Collisions Energy\n");
printf(" --------------- ------ ---------- ------\n");
const int *tests[] = {
(int[]){1,2}, (int[]){1,2,3}, (int[]){1,2,3,4}
};
int tlen[] = {2, 3, 4};
for (int t = 0; t < 3; t++) {
int c, e;
int p = angrysphinx_gate(tests[t], tlen[t], &c, &e);
printf(" [");
for (int i = 0; i < tlen[t]; i++)
printf("%s%d", i ? "," : "", tests[t][i]);
printf("] %-19s %-6d %-10d %d\n",
p ? "true" : "false", c, e, p);
}
/* ---------- cartan matrix display ---------- */
printf("\nCartan Weight Matrix (8 x 8)\n");
printf("=============================\n");
printf(" ");
for (int b = 0; b < 8; b++)
printf(" %-6s", LETTER_NAMES[b]);
printf("\n");
for (int a = 0; a < 8; a++) {
printf(" %-6s", LETTER_NAMES[a]);
for (int b = 0; b < 8; b++)
printf(" %-6d", cartan_weight(a, b));
printf("\n");
}
printf("\nAll assertions passed.\n");
return 0;
}