code wiki / _hdl_build / nx_weights_pull.nx
nx_weights_pull.nx source
↩ module page · 29 lines · 1943 B
1// nx_weights_pull.nx -- LIB: the per-shard VERIFIED-PULL decision logic that ties the weights ladder together.
2// Composes nx_stream_store (M3: stream-hash + incremental store) -> given a shard's bytes and the expected sha256
3// (the HF lfs.oid from nx_hf_tree_parse / M2b), it streams the bytes to disk while hashing, then VERIFIES the
4// streaming digest against the expected digest. FAIL-CLOSED: a corrupt / tampered / truncated shard NEVER passes
5// (the live orchestrator unlinks dest on a 0 verdict). This is the network-independent brain of the pull; the live
6// transport (nx_https_fetch_follow feeding the chunks) is the only piece that needs a healthy network. No TLS here
7// -> shares a compilation unit with nx_sha256 cleanly (no M32 collision). license_tier: ORIGINAL
8import "nx_stream_store.nx" // ss_stream_store (-> nx_sha256 streaming)
9import "nx_syscalls.nx"
10
11func wp_hexenc(inp: *u8, n: i64, out: *u8) -> i64 {
12 let hx: *u8 = "0123456789abcdef" as *u8
13 var i: i64 = 0
14 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 }
15 out[n*2]=0 as u8; return n*2
16}
17func wp_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 }
18
19// Pull one shard: stream src[0..n) to dest (hash + append in `chunk` windows), then verify the streaming digest
20// against expected_hex (the tree's lfs.oid). Returns 1 = VERIFIED (stored, digest matches), 0 = REJECTED
21// (mismatch/truncation/tamper -> fail-closed; caller unlinks dest, never treats it as a valid shard).
22func wp_pull_shard(src: *u8, n: i64, expected_hex: *u8, dest: *u8, chunk: i64) -> i64 {
23 let dig: *u8 = sys_mmap(32)
24 let wrote: i64 = ss_stream_store(src, n, chunk, dest, dig)
25 if wrote < 0 { return 0 }
26 let got_hex: *u8 = sys_mmap(80); wp_hexenc(dig, 32, got_hex)
27 if wp_streq(got_hex, expected_hex) == 1 { return 1 }
28 return 0
29}