nx_bigint_modexp.nx
buildroot/runtime/nx_bigint_modexp.nx
about
nx_bigint_modexp.nx -- modular reduction + modular exponentiation on nx_bigint (the missing keystone).
nx_bigint has add/sub/mul/shift/cmp but NO reduction, so it couldn't do a^e mod m -- the primitive under
Diffie-Hellman (hence BitTorrent MSE/PE anti-throttle encryption), RSA, and any modular crypto. This adds:
bi_mod -- rem = a mod m via bit-by-bit long division (shift-subtract), using only bi_shl1/cmp/sub.
bi_modexp -- result = base^exp mod m via square-and-multiply, reducing after every multiply.
Little-endian u32 limbs, same representation as nx_bigint. Caller owns all limb buffers (BI3). Correctness
first (not constant-time yet -- a DH/RSA hardening note; fine for the KAT + the anti-throttle handshake bring-up).
nx_bigint_modexp -- no-arg = the GATE (classic + Fermat + multi-limb-reduction vectors)
license_tier: ORIGINAL module: nishi-core.crypto.bigint_modexp depends: nishi-core.bigint
dependencies 1 imports · 1 importers
imports: nx_bigint.nx
imported by: nx_mse_dh.nx
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| none |
consts
| 13 | const K_MAGIC_4294967281: i64 = 4294967281 |
functions
| 15 | func me_p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } |
| 16 | func me_n(v: i64) -> i64 { let t: *u8=sys_mmap(28); var m: i64=v; var k: i64=0; if m==0 {t[0]=48 as u8;k=1} while m>0 {t[k]=(48+(m%10)) as u8; m=m/10; k=k+1} let o: *u8=sys_mmap(28); var i: i64=0; while i<k {o[i]=t[k-1-i];i=i+1} sys_write(1,o,k); return 0 } |
| 20 | func bi_mod(rem: *i64, a: *i64, a_len: i64, m_w: *i64, w: i64) -> i64 |
| 34 | func bi_modexp(result: *i64, base: *i64, exp: *i64, m: *i64, n: i64) -> i64 |
| 56 | func me_set(dst: *i64, n: i64, lo: i64, hi: i64) -> i64 { bi_zero(dst, n); dst[0]=lo & 0xFFFFFFFF; dst[1]=hi & 0xFFFFFFFF; return 0 } |
| 57 | func me_eq2(r: *i64, n: i64, lo: i64, hi: i64) -> i64 called by 1: me_case |
| 63 | func me_case(label: *u8, base_lo: i64, base_hi: i64, exp_lo: i64, exp_hi: i64, m_lo: i64, m_hi: i64, want_lo: i64, want_hi: i64, pass: *i64, tot: *i64) -> i64 |
| 77 | func main() -> i64 |