code wiki / (root) / nx_hash_facade_test.nx

nx_hash_facade_test.nx source

↩ module page · 60 lines · 2221 B

1// nx_hash_facade_test.nx -- smoke for nx_hash_facade. 2 3import "nx_syscalls.nx" 4import "nx_hash_facade.nx" 5 6func main() -> i64 { 7 if NX_HA_N_ALGOS != 3 { return 1 } 8 if nx_ha_is_valid(NX_HA_BLAKE2B) != 1 { return 2 } 9 if nx_ha_is_valid(NX_HA_SHA512) != 1 { return 3 } 10 if nx_ha_is_valid(3) != 0 { return 4 } 11 12 // Algorithm pick: sovereign -> BLAKE3 13 if nx_hash_facade_pick_algo(1024) != NX_HA_BLAKE3 { return 5 } 14 // High commodity -> BLAKE3 (819 is threshold) 15 if nx_hash_facade_pick_algo(819) != NX_HA_BLAKE3 { return 6 } 16 // Below 819 -> BLAKE2B 17 if nx_hash_facade_pick_algo(818) != NX_HA_BLAKE2B { return 7 } 18 if nx_hash_facade_pick_algo(0) != NX_HA_BLAKE2B { return 8 } 19 20 // Hash determinism 21 let buf: *u8 = (sys_mmap(32)) as *u8 22 var i: nx_size = 0 23 while i < 16 { 24 buf[i] = (65 + i) as u8 // 'A'..'P' 25 i = i + 1 26 } 27 let h1: nx_size = nx_hash_facade_compute_bytes(buf, 16) 28 let h2: nx_size = nx_hash_facade_compute_bytes(buf, 16) 29 if h1 != h2 { return 9 } // deterministic 30 31 // Different content -> different hash 32 buf[0] = 99 as u8 33 let h3: nx_size = nx_hash_facade_compute_bytes(buf, 16) 34 if h1 == h3 { return 10 } 35 36 // Empty content -> deterministic but distinguishable 37 let h_empty: nx_size = nx_hash_facade_compute_bytes(buf, 0) 38 if h_empty == h1 { return 11 } 39 40 // Request struct path 41 let req: *NxHashRequest = nx_hash_request_new(NX_HA_BLAKE2B, buf, 16, 819) 42 let out: *i64 = (sys_mmap(8)) as *i64 43 if nx_hash_facade_compute(req, out) != NX_HF_OK { return 12 } 44 if out[0] != h3 as i64 { return 13 } // matches compute_bytes after buf[0] mutation 45 46 // Bad algo refused 47 let req_bad: *NxHashRequest = nx_hash_request_new(99, buf, 16, 819) 48 if nx_hash_facade_compute(req_bad, out) != NX_HF_ERR_BAD_ALGO { return 14 } 49 50 // Null content refused 51 let req_null: *NxHashRequest = nx_hash_request_new(NX_HA_BLAKE2B, 52 (0 as i64) as *u8, 16, 819) 53 if nx_hash_facade_compute(req_null, out) != NX_HF_ERR_BAD_INPUT { return 15 } 54 55 // Compare 56 if nx_hash_facade_compare(h1, h1) != 1 { return 16 } 57 if nx_hash_facade_compare(h1, h3) != 0 { return 17 } 58 59 return 0 60}