def pow_mod_odd
import bend-collections@1.0.0.0/src/math/natural.bend as Naturalimport Base
275# right-to-left binary exponentiation, reducing mod m after every product:
276# acc * base^e == b^e0 (mod m) throughout
277def pow_mod_odd(+m: Nat, +bit: Nat, +base: Nat, +acc: Nat) -> Nat:
278 match bit:
279 case 0n:
280 acc
281 case 1n+z:
282 Nat.mod(Nat.mul(acc, base), m)