code wiki / _hdl_build / nx_trust_corroborate_test.nx
nx_trust_corroborate_test.nx source
↩ module page · 64 lines · 3960 B
1// nx_trust_corroborate_test.nx -- proves the SPORE trust bootstrap with the team's OWN SHA-256. Scenarios:
2// (1) CORROBORATION: a root presented identically by 3 independent sources -> ADMITTED (K=2).
3// (2) HEARSAY REJECTED: a root from only 1 source -> REJECTED (uncorroborated, like the >=2-source research gate).
4// (3) POISON RESISTANT: a 4th source presents a TAMPERED copy (1 byte flipped) -> its team-computed hash diverges
5// -> it does NOT count toward agreement -> the 3 honest sources still corroborate; one poisoned source alone
6// could never reach K. Trust is on bytes WE hashed, never a source's word.
7// Exit 0 iff all hold.
8import "nx_trust_corroborate.nx"
9import "nx_syscalls.nx"
10
11func tp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
12func tn(v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(1,"-" as *u8,1)}; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48;k=1}; while m>0{t[k]=48+(m%10);m=m/10;k=k+1}; var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(1,bb,k); return 0 }
13
14// fill a synthetic "root DER" of len bytes with a deterministic pattern keyed by seed.
15func fill_root(buf: *u8, len: i64, seed: i64) -> i64 {
16 var i: i64 = 0
17 while i < len { buf[i] = ((i * 7 + seed * 31 + 3) & 0xff) as u8; i = i + 1 }
18 return 0
19}
20
21func main() -> i64 {
22 tp("=== SPORE TRUST BOOTSTRAP: trust a root iff >=K independent sources agree (team's own SHA-256) ===\n" as *u8)
23 let DER: i64 = 96; let STRIDE: i64 = 128; let K: i64 = 2
24
25 // ---- (1)+(3): root A from 4 sources -- 3 honest (identical), 1 poisoned (byte 5 flipped) ----
26 let srcA: *u8 = sys_mmap(STRIDE * 4)
27 fill_root(srcA + 0*STRIDE, DER, 42)
28 fill_root(srcA + 1*STRIDE, DER, 42)
29 fill_root(srcA + 2*STRIDE, DER, 42)
30 fill_root(srcA + 3*STRIDE, DER, 42); srcA[3*STRIDE + 5] = (srcA[3*STRIDE + 5] + 1) as u8 // POISON one byte
31 let strengthA: i64 = tc_corroboration_strength(srcA, STRIDE, DER, 4)
32 let admitA: i64 = tc_admit_root(srcA, STRIDE, DER, 4, K)
33 tp(" root A: 3 honest + 1 poisoned source -> corroboration strength=" as *u8); tn(strengthA); tp("/4 admitted=" as *u8); tn(admitA); tp("\n" as *u8)
34
35 // ---- (2): root B from only 1 source -> hearsay ----
36 let srcB: *u8 = sys_mmap(STRIDE * 1)
37 fill_root(srcB, DER, 77)
38 let strengthB: i64 = tc_corroboration_strength(srcB, STRIDE, DER, 1)
39 let admitB: i64 = tc_admit_root(srcB, STRIDE, DER, 1, K)
40 tp(" root B: 1 lone source -> corroboration strength=" as *u8); tn(strengthB); tp("/1 admitted=" as *u8); tn(admitB); tp(" (hearsay rejected)\n" as *u8)
41
42 // ---- (3b): a poisoned root from a SINGLE source can never reach K alone ----
43 let srcP: *u8 = sys_mmap(STRIDE * 1)
44 fill_root(srcP, DER, 42); srcP[5] = (srcP[5] + 9) as u8 // attacker's lone poisoned root
45 let admitP: i64 = tc_admit_root(srcP, STRIDE, DER, 1, K)
46 tp(" poisoned lone root -> admitted=" as *u8); tn(admitP); tp(" (an attacker's single source cannot forge trust)\n" as *u8)
47
48 // verdict
49 var ok: i64 = 1
50 if strengthA != 3 { ok = 0 } // exactly the 3 honest sources corroborate (poisoned one excluded)
51 if admitA != 1 { ok = 0 } // >=2 corroborate -> admitted
52 if strengthB != 1 { ok = 0 } // lone source
53 if admitB != 0 { ok = 0 } // hearsay -> rejected
54 if admitP != 0 { ok = 0 } // poisoned lone -> rejected
55 if tc_admit(1, 2) != 0 { ok = 0 } // K<2 hearsay floor
56 if tc_admit(5, 1) != 0 { ok = 0 } // K=1 is never trust (hearsay floor)
57 tp("----\n" as *u8)
58 if ok == 1 {
59 tp(" PROVEN: the spore trusts a root ONLY when >=K independent sources agree on bytes IT hashed itself.\n" as *u8)
60 tp(" Poison diverges + is excluded; hearsay is rejected. No trusted blob, no substrate store -- bits-up trust.\n" as *u8)
61 sys_exit(0); return 0
62 }
63 tp(" FAIL\n" as *u8); sys_exit(1); return 1
64}