Skip to content

constantTimeModInvPrime

constantTimeModInvPrime(a, modulus, bits): bigint

Defined in: mod-inv-prime.ts:29

a⁻¹ mod modulus, for prime modulus, by Fermat’s little theorem: a^(modulus - 2) mod modulus.

This is not new arithmetic — it is modExp with an exponent derived from the (public) modulus, computed here with ordinary bigint subtraction. A second constant-time inversion algorithm implemented independently would only be a second ladder to keep in sync with the first; composing the existing one is both less code and less risk. A general odd-modulus inverse (Bernstein–Yang / safegcd) is not provided — nothing in the current plan needs an inverse against a composite modulus, and it is a materially different algorithm to get right without an existing reference.

bits is the public bit-length the modulus is understood to have for this call (e.g. 256 for a P-256 scalar) — every exponentiation this performs processes exactly that many exponent bits, so the running time depends on bits, never on the actual value of modulus - 2.

bigint

bigint

number

bigint

When modulus is not an odd positive integer that fits in bits bits.