code wiki / (root) / nx_bigint_modexp.nx

nx_bigint_modexp.nx

buildroot/runtime/nx_bigint_modexp.nx

5820 B96 linesdepth 3pulls 4 transitivereach 19 importersview sourcekind tooltopic bigint
docsdependenciesstructsconstsfunctions

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

nx_bigint.nx nx_bigint_modexp.nx nx_mse_dh.nx

imports: nx_bigint.nx

imported by: nx_mse_dh.nx

call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown

main me_p me_case me_set bi_zero bi_modexp bi_zero ↻ bi_copy bi_mod bi_zero ↻ bi_shl1 bi_cmp bi_sub bi_mul bi_zero ↻ me_eq2 me_p ↻ me_n me_n ↻

structs

none

consts

13const K_MAGIC_4294967281: i64 = 4294967281

functions

15func 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 }
called by 2: me_casemain
16func 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 }
called by 2: me_casemain
20func bi_mod(rem: *i64, a: *i64, a_len: i64, m_w: *i64, w: i64) -> i64
34func bi_modexp(result: *i64, base: *i64, exp: *i64, m: *i64, n: i64) -> i64
56func 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 }
called by 1: me_case calls 1: bi_zero
57func me_eq2(r: *i64, n: i64, lo: i64, hi: i64) -> i64
called by 1: me_case
63func 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
called by 1: main calls 5: me_setbi_modexpme_eq2me_pme_n
77func main() -> i64
calls 3: me_pme_caseme_n