code wiki / _hdl_build / nx_hr_pow.nx
nx_hr_pow.nx
buildroot/runtime/_hdl_build/nx_hr_pow.nx
about
nx_hr_pow.nx -- NISHI HR, the PROOF-OF-WORK challenge (the "cost" limb of behavioral bot defense). Privacy-
preserving: NO IP/PII -- a visitor just has to PAY a small compute cost the server can cheaply verify, which is
negligible for one human but ruinous for a bot hammering many requests. STATELESS + tamper-proof: the challenge
is HMAC-bound to a server secret (so the server verifies it issued it without storing it) and carries an EXPIRY
(so it can't be replayed forever). Sovereign crypto: composes sha256_digest. The client must find a `solution`
integer s.t. sha256("<nonce>/<solution>") has >= `difficulty` leading zero BITS.
challenge wire form: "<nonce>:<difficulty>:<expiry>:<mac-hex>" (mac = sha256(secret || "<nonce>:<diff>:<exp>"))
license_tier: ORIGINAL
dependencies 2 imports · 1 importers
imports: nx_sha256.nxnx_syscalls.nx
imported by: nx_hr_pow_gate.nx
structs
| none |
consts
| none |
functions
| 12 | func pow_puts(dst: *u8, o: i64, s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8){ dst[o+i]=s[i]; i=i+1 } return o+i } |
| 13 | func pow_putn(dst: *u8, o: i64, v: i64) -> i64 { if v==0 { dst[o]=48 as u8; return o+1 } var m: i64=v; if m<0{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} var j:i64=0; while j<k{dst[o+j]=t[k-1-j];j=j+1} return o+k } |
| 14 | func pow_hex(inp: *u8, n: i64, out: *u8) -> i64 { let hx: *u8="0123456789abcdef" as *u8; var i: i64=0; while i<n { out[i*2]=hx[((inp[i] as i64)>>4)&15]; out[i*2+1]=hx[(inp[i] as i64)&15]; i=i+1 } return n*2 } called by 1: pow_mac |
| 15 | func pow_atoi(buf: *u8, s: i64, e: i64) -> i64 { var v: i64=0; var i: i64=s; while i<e { let c: i64=buf[i] as i64; if c>=48 { if c<=57 { v=v*10+(c-48); i=i+1 } else {i=e} } else {i=e} } return v } called by 1: pow_verify |
| 16 | func pow_colon(buf: *u8, start: i64, n: i64) -> i64 { var i: i64=start; while i<n { if buf[i]==(58 as u8) { return i } i=i+1 } return n } |
| 17 | func pow_eqrange(a: *u8, as0: i64, ae: i64, b: *u8, bs: i64, be: i64) -> i64 { if (ae-as0)!=(be-bs) { return 0 } var k: i64=0; while k<(ae-as0) { if a[as0+k]!=b[bs+k] { return 0 } k=k+1 } return 1 } called by 1: pow_verify |
| 20 | func pow_lead_zero_bits(h: *u8) -> i64 called by 1: pow_solution_ok |
| 33 | func pow_mac(secret: *u8, slen: i64, payload: *u8, plen: i64, out_hex: *u8) -> i64 |
| 41 | func pow_solution_ok(nonce: *u8, noncelen: i64, solution: i64, difficulty: i64) -> i64 |
| 51 | func pow_make(secret: *u8, slen: i64, nonce: i64, difficulty: i64, expiry_ts: i64, out: *u8) -> i64 |
| 61 | func pow_verify(chal: *u8, chlen: i64, solution: i64, secret: *u8, slen: i64, now_ts: i64) -> i64 |