code wiki / _hdl_build / nx_stream_store_gate.nx
nx_stream_store_gate.nx source
↩ module page · 59 lines · 4193 B
1// nx_stream_store_gate.nx -- GATE for nx_stream_store (M3: stream-hash + incremental store for multi-GB shards).
2// T1 STREAM == ONE-SHOT digest of a 1000-byte pattern streamed in 7-byte chunks == its one-shot sha256.
3// T2 BYTE-IDENTICAL the chunk-by-chunk stored file, read back, equals the input exactly (never-store-corrupt).
4// T3 CHUNK-SIZE-INVARIANT digest streaming in 1-byte chunks == streaming in 7-byte chunks (window size irrelevant).
5// T4 NIST ANCHOR "abc" streamed in 1-byte chunks == the FIPS-180-4 vector ba7816bf...f20015ad.
6// Proves a 16GB shard can be hashed+stored through a tiny window, digest-exact -> feeds the fail-closed pull.
7// GREEN iff T1-T4 pass. Sovereign nx_cc->nxasm. expect_exit: 0 license_tier: ORIGINAL
8import "nx_stream_store.nx"
9import "nx_syscalls.nx"
10
11func w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
12func wn(v: i64) -> i64 { var m: i64=v; if m<0{w("-" as *u8);m=0-m} let t:*u8=sys_mmap(24); var k:i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var i:i64=0; let o:*u8=sys_mmap(24); while i<k{o[i]=t[k-1-i];i=i+1} sys_write(1,o,k); return 0 }
13func memeq(a: *u8, b: *u8, n: i64) -> i64 { var i: i64=0; while i<n { if a[i]!=b[i] { return 0 } i=i+1 } return 1 }
14func hexenc(inp: *u8, n: i64, out: *u8) -> i64 { let hx: *u8="0123456789abcdef" as *u8; var i: i64=0; while i<n { out[i*2]=hx[((inp[i] as i64)>>4)&15]; out[i*2+1]=hx[(inp[i] as i64)&15]; i=i+1 } out[n*2]=0 as u8; return n*2 }
15func streq(a: *u8, b: *u8) -> i64 { var i: i64=0; while 1==1 { if a[i]!=b[i] {return 0} if a[i]==(0 as u8) {return 1} i=i+1 } return 1 }
16
17func main() -> i64 {
18 w("=== nx_stream_store_gate: streaming sha256 + incremental store (M3) ===\n" as *u8)
19 var pass: i64=0; var total: i64=0
20
21 // 1000-byte deterministic pattern (exercises every byte value + partial final chunk).
22 let N: i64 = 1000
23 let buf: *u8 = sys_mmap(2048)
24 var i: i64=0
25 while i < N { buf[i] = (((i*31)+7) & 255) as u8; i=i+1 }
26
27 let sha_a: *u8 = sys_mmap(32); ss_oneshot(buf, N, sha_a)
28 let sha_b: *u8 = sys_mmap(32); let wb: i64 = ss_stream_store(buf, N, 7, "/tmp/ss_stream_7.bin" as *u8, sha_b)
29 let sha_c: *u8 = sys_mmap(32); ss_stream_store(buf, N, 1, "/tmp/ss_stream_1.bin" as *u8, sha_c)
30
31 // ---- T1: streaming (7-byte) == one-shot ----
32 var t1: i64=1; if wb != N { t1=0 } if memeq(sha_a, sha_b, 32)!=1 { t1=0 }
33 total=total+1; if t1==1 { pass=pass+1; w(" [PASS] " as *u8) } else { w(" [FAIL] " as *u8) }
34 w("T1 stream(7)==one-shot: wrote=" as *u8); wn(wb); w("\n" as *u8)
35
36 // ---- T2: stored file byte-identical ----
37 let rb: *u8 = sys_mmap(2048)
38 let rfd: i64 = sys_openat_rd("/tmp/ss_stream_7.bin" as *u8)
39 var rn: i64=0; if rfd>=0 { rn = sys_read(rfd, rb, 2048); sys_close(rfd) }
40 var t2: i64=1; if rn != N { t2=0 } else { if memeq(buf, rb, N)!=1 { t2=0 } }
41 total=total+1; if t2==1 { pass=pass+1; w(" [PASS] " as *u8) } else { w(" [FAIL] " as *u8) }
42 w("T2 stored byte-identical: read-back=" as *u8); wn(rn); w("\n" as *u8)
43
44 // ---- T3: chunk-size-invariant (1-byte == 7-byte) ----
45 var t3: i64=1; if memeq(sha_b, sha_c, 32)!=1 { t3=0 }
46 total=total+1; if t3==1 { pass=pass+1; w(" [PASS] " as *u8) } else { w(" [FAIL] " as *u8) }
47 w("T3 chunk-size-invariant (1B == 7B digest)\n" as *u8)
48
49 // ---- T4: NIST anchor "abc" ----
50 let sha_d: *u8 = sys_mmap(32); ss_stream_store("abc" as *u8, 3, 1, "/tmp/ss_abc.bin" as *u8, sha_d)
51 let hx: *u8 = sys_mmap(80); hexenc(sha_d, 32, hx)
52 var t4: i64=1; if streq(hx, "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" as *u8)!=1 { t4=0 }
53 total=total+1; if t4==1 { pass=pass+1; w(" [PASS] " as *u8) } else { w(" [FAIL] " as *u8) }
54 w("T4 NIST anchor: streamed sha256(\"abc\")=" as *u8); w(hx); w("\n" as *u8)
55
56 w("\n=== nx_stream_store_gate " as *u8); wn(pass); w("/" as *u8); wn(total)
57 if pass==total { w(" GREEN (stream-hash + incremental store: digest-exact, byte-identical, chunk-invariant; multi-GB shard needs no full buffer)\n" as *u8); sys_exit(0); return 0 }
58 w(" RED\n" as *u8); sys_exit(1); return 1
59}