Skip to content

IxVM kernel: klimbs_pow via binary exponentiation#455

Open
arthurpaulino wants to merge 1 commit into
mainfrom
ap/klimbs-pow-binary-exp
Open

IxVM kernel: klimbs_pow via binary exponentiation#455
arthurpaulino wants to merge 1 commit into
mainfrom
ap/klimbs-pow-binary-exp

Conversation

@arthurpaulino

Copy link
Copy Markdown
Member

Replace klimbs_mul(base, klimbs_pow(base, klimbs_dec(exp)))'s O(exp) per-step memo growth with a binary-exponentiation recursion of depth O(log exp). Both exp / 2 and exp % 2 route through the native unconstrained_big_uint_div_mod op, so each level pays O(1) for the division — log2(exp) memo entries for klimbs_pow itself + the klimbs_mul(base, base) doublings.

lake test -- --ignored ixvm on IxVMPrim.nat_pow_big ((2 ^ 16384 : Nat) - 2 ^ 16384 = 0, measured against the current ap/utf8-mod-witness baseline):
3_941_276_885 → 71_856_587 FFT (-98.2%).

Tests/Ix/IxVM.lean:

  • new IxVMPrim.nat_pow_big synthetic — exercises klimbs_pow at a non-trivial exponent (2 ^ 16384) so future regressions in the body shift its FFT pin.
  • small pin shifts in nat_shl_lit, nat_shr_lit, nat_div_lit, nat_mod_lit, nat_gcd_lit, bv_to_nat_lit — all reduce via klimbs_pow / klimbs_div_mod and absorb the new cost shape.

Replace `klimbs_mul(base, klimbs_pow(base, klimbs_dec(exp)))`'s O(exp)
per-step memo growth with a binary-exponentiation recursion of depth
O(log exp). Both `exp / 2` and `exp % 2` route through the native
`unconstrained_big_uint_div_mod` op, so each level pays O(1) for the
division — `log2(exp)` memo entries for `klimbs_pow` itself + the
`klimbs_mul(base, base)` doublings.

`lake test -- --ignored ixvm` on `IxVMPrim.nat_pow_big`
(`(2 ^ 16384 : Nat) - 2 ^ 16384 = 0`, measured against the current
`ap/utf8-mod-witness` baseline):
3_941_276_885 → 71_856_587 FFT (-98.2%).

Tests/Ix/IxVM.lean:
* new `IxVMPrim.nat_pow_big` synthetic — exercises `klimbs_pow` at a
  non-trivial exponent (`2 ^ 16384`) so future regressions in the body
  shift its FFT pin.
* small pin shifts in `nat_shl_lit`, `nat_shr_lit`, `nat_div_lit`,
  `nat_mod_lit`, `nat_gcd_lit`, `bv_to_nat_lit` — all reduce via
  `klimbs_pow` / `klimbs_div_mod` and absorb the new cost shape.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant