code wiki / _hdl_build / nx_js_octane_crypto.nx

nx_js_octane_crypto.nx source

↩ module page · 43 lines · 2949 B

1// nx_js_octane_crypto.nx -- run the REAL Octane Crypto benchmark (unmodified Google source: jsbn RSA-1024, 2// harness stubbed). encrypt() RSA-encrypts a 114-char TEXT with a real 1024-bit key; decrypt() decrypts with 3// the private key (Montgomery + CRT) and THROWS unless the plaintext round-trips exactly -> "CRYPTO_OK" is a 4// full correctness proof of the whole bignum stack (am3 28-bit limbs, mont-reduce, modPow, radix I/O). 5// V8 pin (node v22, this box, warmed): 13085us. expect_exit: 0 license_tier: ORIGINAL 6import "nx_js_vm.nx" 7const K_MAGIC_262144: i64 = 262144 8const K_MAGIC_13085: i64 = 13085 9func ow(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 10func on(v: i64) -> i64 { if v==0 { sys_write(1,"0" as *u8,1); return 0 } var m: i64=v; if m<0 { sys_write(1,"-" as *u8,1); m=0-m } let t: *u8=sys_mmap(24); var k: i64=0; while m>0 { t[k]=(48+(m%10)) as u8; m=m/10; k=k+1 } let o: *u8=sys_mmap(24); var q: i64=k-1; var x: i64=0; while q>=0 { o[x]=t[q]; x=x+1; q=q-1 } sys_write(1,o,x); return 0 } 11func main(argc: i64, argv: *i64) -> i64 { 12 ow("=== nx_js_octane_crypto: real Octane Crypto (jsbn RSA-1024) on our sovereign VM/JIT ===\n" as *u8) 13 let fd: i64 = sys_openat_rd("knowledge/octane/crypto_eng.js\x00" as *u8) 14 if fd < 0 { ow(" RED: cannot open knowledge/octane/crypto_eng.js\n" as *u8); return 1 } 15 let cap: i64 = K_MAGIC_262144 16 let buf: *u8 = sys_mmap(cap + 8) 17 var total: i64 = 0 18 var done: i64 = 0 19 while done == 0 { let n: i64 = sys_read(fd, ((buf as i64) + total) as *u8, cap - total); if n <= 0 { done = 1 } else { total = total + n } } 20 sys_close(fd) 21 buf[total] = 0 as u8 22 ow(" loaded crypto_eng.js: " as *u8); on(total); ow(" bytes\n" as *u8) 23 let out: *i64 = sys_mmap(16) as *i64 24 let t0: i64 = sys_now_us() 25 let rc: i64 = compile_run(buf, out) 26 let t1: i64 = sys_now_us() 27 ow(" compile_run rc=" as *u8); on(rc); ow(" result tag=" as *u8); on(out[0]) 28 if out[0] == VAL_STR { ow(" '" as *u8); sys_write(1, ev_str_bytes((out[1]) as *i64), ev_str_len((out[1]) as *i64)); ow("'" as *u8) } 29 ow("\n" as *u8) 30 var okr: i64 = 0 31 if rc == 0 { if out[0] == VAL_STR { 32 let rb: *u8 = ev_str_bytes((out[1]) as *i64) 33 let rl: i64 = ev_str_len((out[1]) as *i64) 34 if rl == 9 { okr = 1; var i: i64 = 0; let exp: *u8 = "CRYPTO_OK" as *u8; while i < 9 { if (rb[i] & 0xff) != (exp[i] & 0xff) { okr = 0; i = 9 } else { i = i + 1 } } } 35 } } 36 if okr == 0 { ow("=== RED: Crypto did not complete (feature gap or round-trip mismatch) ===\n" as *u8); return 1 } 37 var us: i64 = t1 - t0 38 if us < 1 { us = 1 } 39 let v8: i64 = K_MAGIC_13085 40 ow(" ours(parse+1 encrypt+1 decrypt)=" as *u8); on(us); ow("us V8(warm)=" as *u8); on(v8); ow("us\n" as *u8) 41 ow("=== GREEN: real Octane Crypto RAN -- RSA-1024 encrypt+decrypt round-trip EXACT (jsbn self-validated) ===\n" as *u8) 42 return 0 43}