tc39 / proposal-bigint-math

Draft specification for supporting BigInts in JavaScript’s Math methods.

Home Page:https://tc39.es/proposal-bigint-math/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fast exponentiation for bigint + modulo

Jamesernator opened this issue · comments

One of the more common things I've used when using bigints is wanting power + modulo, generally this involves writing my own fast exponentiation function (using exponentation by squaring), it would be nice for Math.pow to support the third argument when used with bigints to enable this.

Most languages I've looked at already support this (e.g. in Python via pow(...), Java by bigInteger.modPow, and so on) so there is a lot of prior art.

Thanks for bringing this to attention; it’s an important use case. For others, see Wikipedia’s article on modular exponentiation.

One thing that is not yet clear to me is whether modular exponentiation would be useable by both BigInts and by integral Number values. The answer would probably be yes.

I’m also not sure whether we should consider this to be in scope of this particular BigInt Math proposal. What I anticipate happening is that this proposal would stick to extending the existing Math methods, followed by a separate proposal devoted to a Math.modPow method. (I had already been planning on following up this proposal myself with a separate proposal for a Math.popcnt method that does bitwise zero popcount.)

I’ll leave this issue open for a bit for others to chime in for a bit, and then I might turn it into a Discussion or something. I’ll also ping here if I end up making a repository for a Math.modPow proposal.

I think I’ll probably close this for now, in favor of a future proposal. The Committee reasonably wanted me to greatly reduce the functions covered by this proposal. We can deal with modular exponentiation in a future proposal. Thanks!