code wiki / _hdl_build / nx_trust_store_corroborated_test.nx
nx_trust_store_corroborated_test.nx source
↩ module page · 56 lines · 3362 B
1// nx_trust_store_corroborated_test.nx -- proves the spore builds its trust store by corroboration across a set
2// of candidate roots: only roots that >=K independent sources AGREE on are admitted; disagreement (hearsay) and
3// poison are rejected. 3 candidate roots, 3 sources each, K=2:
4// root0: all 3 sources identical -> strength 3 -> ADMIT
5// root1: all 3 sources DIFFERENT (no agree) -> strength 1 -> REJECT (no corroboration)
6// root2: 2 sources agree, 1 poisoned -> strength 2 -> ADMIT (poison excluded)
7// Expected store = {root0, root2}. Exit 0 iff so + soundness holds.
8import "nx_trust_store_corroborated.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 }
13func fill_root(buf: *u8, len: i64, seed: i64) -> i64 { var i: i64=0; while i<len { buf[i]=((i*7+seed*31+3)&0xff) as u8; i=i+1 } return 0 }
14
15func main() -> i64 {
16 tp("=== SPORE builds its trust store by CORROBORATION (admit iff >=K independent sources agree) ===\n" as *u8)
17 let M: i64 = 3; let N: i64 = 3; let DER: i64 = 96; let STRIDE: i64 = 128; let K: i64 = 2
18 let roots: *u8 = sys_mmap(M * N * STRIDE)
19 let RB: i64 = N * STRIDE // bytes per root
20
21 // root0: 3 identical sources
22 fill_root(roots + 0*RB + 0*STRIDE, DER, 10)
23 fill_root(roots + 0*RB + 1*STRIDE, DER, 10)
24 fill_root(roots + 0*RB + 2*STRIDE, DER, 10)
25 // root1: 3 different sources (genuine disagreement -> no corroboration)
26 fill_root(roots + 1*RB + 0*STRIDE, DER, 20)
27 fill_root(roots + 1*RB + 1*STRIDE, DER, 21)
28 fill_root(roots + 1*RB + 2*STRIDE, DER, 22)
29 // root2: 2 agree, 1 poisoned
30 fill_root(roots + 2*RB + 0*STRIDE, DER, 30)
31 fill_root(roots + 2*RB + 1*STRIDE, DER, 30)
32 fill_root(roots + 2*RB + 2*STRIDE, DER, 30); roots[2*RB + 2*STRIDE + 5] = (roots[2*RB + 2*STRIDE + 5] + 1) as u8
33
34 let admitted: *i64 = sys_mmap(8 * M) as *i64
35 let count: i64 = tsc_build(roots, M, N, STRIDE, DER, K, admitted)
36 let sound: i64 = tsc_all_corroborated(roots, admitted, count, N, STRIDE, DER, K)
37 let usable: i64 = tsc_usable(count)
38
39 tp(" admitted " as *u8); tn(count); tp(" roots into the store: {" as *u8)
40 var i: i64 = 0; while i < count { tn(admitted[i]); if i < count-1 { tp("," as *u8) } i = i + 1 } tp("}\n" as *u8)
41 tp(" soundness(every admitted >=K corroborated)=" as *u8); tn(sound); tp(" usable=" as *u8); tn(usable); tp("\n" as *u8)
42
43 var ok: i64 = 1
44 if count != 2 { ok = 0 }
45 if admitted[0] != 0 { ok = 0 } // root0 admitted
46 if admitted[1] != 2 { ok = 0 } // root2 admitted (root1 rejected)
47 if sound != 1 { ok = 0 }
48 if usable != 1 { ok = 0 }
49 tp("----\n" as *u8)
50 if ok == 1 {
51 tp(" PROVEN: the spore's trust store contains ONLY corroborated roots (root0, root2); the disagreeing root1\n" as *u8)
52 tp(" was rejected and the poisoned presentation excluded. This store is what nx_https_get validates chains against.\n" as *u8)
53 sys_exit(0); return 0
54 }
55 tp(" FAIL\n" as *u8); sys_exit(1); return 1
56}