code wiki / (root) / nx_sha256_diff_probe.nx

nx_sha256_diff_probe.nx source

↩ module page · 39 lines · 1850 B

1// nx_sha256_diff_probe.nx -- differential oracle for the SHA-256 optimization. 2// Hashes deterministic inputs of EVERY length 0..256 (covers all padding boundaries: 3// 55/56/57 spillover, 63/64/65, 119/120, 127/128) and rolls a single hash over ALL output 4// bytes. Run on the CURRENT (trusted, production) sha256 -> GOLDEN number; run again after the 5// optimization -> must print the SAME number = byte-identical across the whole boundary space. 6 7import "nx_sha256.nx" 8const K_MAGIC_1000000007: i64 = 1000000007 9const K_MAGIC_680824791: i64 = 680824791 10 11func _p_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 12func _p_putn(v: i64) -> i64 { 13 let bb: *u8 = sys_mmap(28); var m: i64 = v 14 let t: *u8 = sys_mmap(28); var k: i64 = 0; if m == 0 { t[0] = 48; k = 1 } 15 while m > 0 { t[k] = 48 + (m % 10); m = m / 10; k = k + 1 } 16 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } sys_write(1, bb, k); return 0 17} 18 19func main() -> i64 { 20 let buf: *u8 = sys_mmap(300) 21 var i: i64 = 0 22 while i < 300 { buf[i] = ((i * 7 + 13) & 0xff) as u8; i = i + 1 } 23 let out: *u8 = sys_mmap(32) 24 var roll: i64 = 0 25 var len: i64 = 0 26 while len <= 256 { 27 sha256_digest(buf, len, out) 28 var j: i64 = 0 29 while j < 32 { roll = (roll * 131 + (out[j] as i64)) % K_MAGIC_1000000007; j = j + 1 } 30 len = len + 1 31 } 32 _p_puts("SHA256_DIFF_ROLL=" as *u8); _p_putn(roll); _p_puts("\n" as *u8) 33 // GOLDEN captured from the pre-optimization (production) sha256 over lengths 0..256. 34 // This is now a permanent regression gate: any change to nx_sha256 that alters ANY 35 // output at ANY padding boundary trips it. exit 0 = byte-identical, 1 = regression. 36 if roll != K_MAGIC_680824791 { sys_exit(1); return 1 } 37 sys_exit(0) 38 return 0 39}