nx_sha256_shared_gate.nx source
↩ module page · 118 lines · 10188 B
1// Native Linux x86-64 qualification of existing shared allocation and canonical hash reuse.
2// No ENOMEM claim: allocation-failure control is actual zero-length mapping refusal.
3import "nx_sha256.nx"
4import "nx_gate_verdict.nx"
5const SG_DIGEST: i64 = 32
6const SG_HEX: i64 = 64
7const SG_CANARY: i64 = 165
8const SG_BOUNDARY_MAX: i64 = 130
9const SG_PATTERN_BYTES: i64 = 257
10const SG_PATTERN_FACTOR: i64 = 31
11const SG_PATTERN_ADD: i64 = 7
12const SG_BYTE_VALUES: i64 = 256
13const SG_MILLION: i64 = 1000000
14const SG_STREAM_CHUNK: i64 = 63
15func sg_len(s: *u8) -> i64 { var n: i64=0; while s[n] != (0 as u8) { n=n+1 }; return n }
16func sg_uint(s: *u8) -> i64 {
17 let n: i64=sg_len(s); if n == 0 { return 0-1 }; var p: i64=0; var v: i64=0
18 while p < n { let d: i64=(s[p] as i64)-48; if d < 0 || d > 9 || v > (SHA256_SIGNED_MAX-d)/10 { return 0-1 }; v=v*10+d; p=p+1 }; return v
19}
20func sg_equal(a: *u8, b: *u8, n: i64) -> i64 {
21 var i: i64=0; while i < n { if a[i] != b[i] { return 0 }; i=i+1 }; return 1
22}
23func sg_fill(b: *u8, n: i64, v: i64) -> i64 { var i: i64=0; while i < n { b[i]=v as u8; i=i+1 }; return 0 }
24func sg_all(b: *u8, n: i64, v: i64) -> i64 { var i: i64=0; while i < n { if b[i] != (v as u8) { return 0 }; i=i+1 }; return 1 }
25func sg_hex(b: *u8, out: *u8) -> i64 {
26 let digits: *u8="0123456789abcdef" as *u8; var i: i64=0
27 while i < SG_DIGEST { let v: i64=b[i] as i64; out[2*i]=digits[v/16]; out[2*i+1]=digits[v%16]; i=i+1 }; out[SG_HEX]=0 as u8; return 0
28}
29func sg_vector(input: *u8, n: i64, expected: *u8, output: *u8, legacy: *u8, hex: *u8) -> i64 {
30 sg_fill(output,SG_DIGEST+2,SG_CANARY)
31 let middle: *u8=((output as i64)+1) as *u8
32 if sha256_digest_checked_native(input,n,middle) != 0 { return 0 }
33 sha256_digest(input,n,legacy); sg_hex(middle,hex)
34 return sg_equal(hex,expected,SG_HEX) && sg_equal(middle,legacy,SG_DIGEST) && output[0] == (SG_CANARY as u8) && output[SG_DIGEST+1] == (SG_CANARY as u8)
35}
36func sg_read_exact(path: *u8, expected: i64, out: *u8) -> i64 {
37 let fd: i64=sys_openat_rd(path); if fd < 0 { return 0 }
38 var ok: i64=1; var got: i64=0
39 if sys_lseek(fd,0,2) != expected { ok=0 }; if sys_lseek(fd,0,0) != 0 { ok=0 }
40 while ok == 1 && got < expected {
41 let n: i64=sys_read(fd,((out as i64)+got) as *u8,expected-got)
42 if n <= 0 { ok=0 } else { got=got+n }
43 }
44 if sys_close(fd) != 0 { ok=0 }; return ok
45}
46func main(argc: i64, argv: *i64) -> i64 {
47 if argc != 4 { gv_puts("usage: sha256-shared-gate exact-source-path source-byte-count lowercase-source-sha256\n" as *u8); return 3 }
48 let source_path: *u8=argv[1] as *u8; let source_n: i64=sg_uint(argv[2] as *u8); let expected: *u8=argv[3] as *u8
49 if source_n <= 0 || source_n > SHA256_SIGNED_MAX/SHA256_BITS_PER_BYTE || sg_len(expected) != SG_HEX { return 3 }
50 var h: i64=0; while h < SG_HEX { let c: i64=expected[h] as i64; if (c < 48 || c > 57) && (c < 97 || c > 102) { return 3 }; h=h+1 }
51 let ctr: *i64=gv_ctr(); gv_head("SHA256-SHARED-CHECKED / native owner reuse" as *u8)
52 let output: *u8=sys_mmap(SG_DIGEST+2); let legacy: *u8=sys_mmap(SG_DIGEST); let hex: *u8=sys_mmap(SG_HEX+1)
53 if (output as i64) <= 0 || (legacy as i64) <= 0 || (hex as i64) <= 0 { return 3 }
54 let middle: *u8=((output as i64)+1) as *u8
55 gv_check("empty known answer and incumbent parity" as *u8,sg_vector("" as *u8,0,"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" as *u8,output,legacy,hex),ctr)
56 gv_check("abc known answer and incumbent parity" as *u8,sg_vector("abc" as *u8,3,"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" as *u8,output,legacy,hex),ctr)
57 let two: *u8="abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" as *u8
58 gv_check("56-byte padding known answer" as *u8,sg_vector(two,sg_len(two),"248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1" as *u8,output,legacy,hex),ctr)
59 let million: *u8=sys_mmap(SG_MILLION); if (million as i64) <= 0 { return 3 }; sg_fill(million,SG_MILLION,97)
60 gv_check("million-a known answer" as *u8,sg_vector(million,SG_MILLION,"cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0" as *u8,output,legacy,hex),ctr)
61 sys_munmap(million,SG_MILLION)
62 let needed: i64=sha256_workspace_bytes(); let workspace: *u8=sys_mmap_shared(needed)
63 gv_check("actual shared workspace allocation returned" as *u8,(workspace as i64) > 0,ctr)
64 if (workspace as i64) <= 0 { return gv_verdict("SHA256-SHARED-CHECKED" as *u8,ctr,"workspace unavailable" as *u8) }
65 gv_check("native context and workspace alignment" as *u8,__size_of(Sha256) > 0 && sha256_context_aligned_bytes() >= __size_of(Sha256) && sha256_context_aligned_bytes()%SHA256_WORD_ALIGN == 0 && (workspace as i64)%SHA256_WORD_ALIGN == 0 && needed > NXA_SMALL_MAX,ctr)
66 let pattern: *u8=sys_mmap(SG_PATTERN_BYTES); if (pattern as i64) <= 0 { return 3 }
67 var i: i64=0; while i < SG_PATTERN_BYTES { pattern[i]=((i*SG_PATTERN_FACTOR+SG_PATTERN_ADD)%SG_BYTE_VALUES) as u8; i=i+1 }
68 gv_check("257-byte binary pattern equals independent hashlib digest" as *u8,sg_vector(pattern,SG_PATTERN_BYTES,"442d170f22f13ae4fc17edbd88c498df9072b962790944f588ffaeee2a9ec0eb" as *u8,output,legacy,hex),ctr)
69 var boundary_ok: i64=1; var software_ok: i64=1; var streamed_ok: i64=1; var n: i64=0; var hardware_available: i64=0
70 while n <= SG_BOUNDARY_MAX {
71 let rc: i64=sha256_digest_workspace(pattern,n,middle,workspace,needed)
72 sha256_digest(pattern,n,legacy)
73 if rc != 0 || sg_equal(middle,legacy,SG_DIGEST) != 1 { boundary_ok=0 }
74 let ctx: *Sha256=workspace as *Sha256
75 hardware_available=ctx.ni_ok
76 sha256_seed_allocated(ctx); ctx.ni_ok=0
77 sha256_update(ctx,pattern,n); sha256_final(ctx,middle)
78 if sg_equal(middle,legacy,SG_DIGEST) != 1 { software_ok=0 }
79 sha256_seed_allocated(ctx); var p: i64=0
80 while p < n { var take: i64=SG_STREAM_CHUNK; if take > n-p { take=n-p }; sha256_update(ctx,((pattern as i64)+p) as *u8,take); p=p+take }
81 sha256_final(ctx,middle)
82 if sg_equal(middle,legacy,SG_DIGEST) != 1 { streamed_ok=0 }
83 n=n+1
84 }
85 gv_check("all lengths0..130 include padding/block seams" as *u8,boundary_ok,ctr)
86 gv_check("software path agrees with incumbent at every seam" as *u8,software_ok,ctr)
87 gv_check("63-byte streaming seams agree with incumbent" as *u8,streamed_ok,ctr)
88 gv_puts("native_context_bytes=" as *u8); gv_num(__size_of(Sha256)); gv_puts(" workspace_bytes=" as *u8); gv_num(needed); gv_puts(" sha_ni_available=" as *u8); gv_num(hardware_available); gv_puts("\n" as *u8)
89 let ctx: *Sha256=workspace as *Sha256
90 gv_check("partition offsets are aligned and non-overlapping" as *u8,ctx.bufptr == (workspace as i64)+sha256_context_aligned_bytes() && ctx.kptr == ctx.bufptr+SHA256_BLOCK_BYTES && ctx.wptr == ctx.kptr+SHA256_ROUND_WORDS*SHA256_WIDE_WORD && ctx.k32ptr == ctx.wptr+SHA256_ROUND_WORDS*SHA256_WIDE_WORD && ctx.st8ptr == ctx.k32ptr+SHA256_ROUND_WORDS*SHA256_PACKED_WORD && ctx.st8ptr+SHA256_STATE_WORDS*SHA256_PACKED_WORD == (workspace as i64)+needed,ctr)
91 sg_fill(output,SG_DIGEST+2,SG_CANARY)
92 gv_check("short workspace refused without output mutation" as *u8,sha256_digest_workspace(pattern,1,middle,workspace,needed-1) == SHA256_E_WORKSPACE && sg_all(output,SG_DIGEST+2,SG_CANARY),ctr)
93 gv_check("misaligned workspace refused without output mutation" as *u8,sha256_digest_workspace(pattern,1,middle,((workspace as i64)+1) as *u8,needed) == SHA256_E_WORKSPACE && sg_all(output,SG_DIGEST+2,SG_CANARY),ctr)
94 gv_check("null workspace refused" as *u8,sha256_digest_workspace(pattern,1,middle,0 as *u8,needed) == SHA256_E_WORKSPACE && sg_all(output,SG_DIGEST+2,SG_CANARY),ctr)
95 gv_check("input overlapping workspace refused" as *u8,sha256_digest_workspace(workspace,1,middle,workspace,needed) == SHA256_E_WORKSPACE && sg_all(output,SG_DIGEST+2,SG_CANARY),ctr)
96 gv_check("output overlapping workspace refused" as *u8,sha256_digest_workspace(pattern,1,workspace,workspace,needed) == SHA256_E_WORKSPACE,ctr)
97 gv_check("negative input length refused before output mutation" as *u8,sha256_digest_checked_native(pattern,0-1,middle) == SHA256_E_INPUT && sg_all(output,SG_DIGEST+2,SG_CANARY),ctr)
98 gv_check("unrepresentable signed bit length refused" as *u8,sha256_digest_checked_native(pattern,SHA256_SIGNED_MAX/SHA256_BITS_PER_BYTE+1,middle) == SHA256_E_INPUT && sg_all(output,SG_DIGEST+2,SG_CANARY),ctr)
99 gv_check("null nonempty input refused" as *u8,sha256_digest_checked_native(0 as *u8,1,middle) == SHA256_E_INPUT && sg_all(output,SG_DIGEST+2,SG_CANARY),ctr)
100 // Actual shared allocator invalid-length failure; deliberately not an ENOMEM claim.
101 let refused: i64=sys_mmap_shared(0) as i64
102 gv_check("existing allocator rejects zero mapping length" as *u8,refused <= 0,ctr)
103 gv_check("actual failed mapping result propagates unchanged output" as *u8,refused <= 0 && sha256_digest_mapping_native(pattern,1,middle,refused) == SHA256_E_MAPPING && sg_all(output,SG_DIGEST+2,SG_CANARY),ctr)
104 gv_check("whole borrowed workspace released once" as *u8,sys_munmap(workspace,needed) == 0,ctr)
105 let source: *u8=sys_mmap(source_n); if (source as i64) <= 0 { return 3 }
106 let read: i64=sg_read_exact(source_path,source_n,source)
107 gv_check("actual representative source read exactly" as *u8,read,ctr)
108 if read == 1 {
109 gv_check("representative native source equals independent SHA" as *u8,sg_vector(source,source_n,expected,output,legacy,hex),ctr)
110 let first: u8=source[0]; source[0]=((first as i64)^1) as u8
111 let mutated: i64=sha256_digest_checked_native(source,source_n,middle); sg_hex(middle,hex)
112 gv_check("one-byte source mutation changes digest" as *u8,mutated == 0 && sg_equal(hex,expected,SG_HEX) == 0,ctr)
113 source[0]=first
114 gv_check("restoring exact source restores digest" as *u8,sg_vector(source,source_n,expected,output,legacy,hex),ctr)
115 }
116 sys_munmap(source,source_n)
117 return gv_verdict("SHA256-SHARED-CHECKED" as *u8,ctr,"native x86-64 only; actual invalid-length allocation refusal, no ENOMEM or unsupported-backend claim" as *u8)
118}