Binary Exponentiation Implementation in Go
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Binary Exponentiation runs in O(log n) time and O(1) space, in Go as in every other language.
- ✓math/big.Int.Exp(x, y, m) IS binary exponentiation, shipped in the standard library: z.Exp(x, y, m) sets z = x**y mod |m|, or the plain unmodded power if m is nil.
- ✓Call big.Int.Exp directly instead of hand-rolling the loop -- it is the one-line standard-library answer, with the receiver pattern z.Exp(x, y, m) requiring z, x, y, m to all be pre-allocated via new(big.Int) or big.NewInt(n).
- ✓The Go trap: Plain int/uint64 arithmetic in Go overflows SILENTLY -- no panic, no error, just two's-complement wraparound -- so a hand-rolled squaring loop that forgets to reduce mod after EVERY multiply, not just at the return, runs to completion and hands back a wrong number with zero indication anything went wrong.
- ✓big.Int.Exp does the log2(y) squarings the public bound promises, but for a large ODD modulus it converts to Montgomery form first, trading a one-time conversion cost for cheaper modular multiplications on every step after -- genuinely faster than naive binary exponentiation at RSA-key sizes, not just the same algorithm in a faster runtime.
What Binary Exponentiation actually is
Binary Exponentiation in Go: what you actually reach for
Go implementation
package main
import "math/big"
func binPowU64(base, exp, mod uint64) uint64 {
result := uint64(1) % mod
base %= mod
for exp > 0 {
if exp&1 == 1 {
result = result * base % mod // mod every step - overflow wraps silently otherwise
}
base = base * base % mod
exp >>= 1
}
return result
}
func modExpBig(base, exp, mod int64) *big.Int {
z := new(big.Int)
return z.Exp(big.NewInt(base), big.NewInt(exp), big.NewInt(mod)) // Montgomery form for odd mod
}Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe Go-specific trap
Performance in Go, beyond the O(log n)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which Go type or library should I use for Binary Exponentiation?
math/big.Int.Exp(x, y, m) IS binary exponentiation, shipped in the standard library: z.Exp(x, y, m) sets z = x**y mod |m|, or the plain unmodded power if m is nil. For an odd modulus it switches internally to Montgomery multiplication, so it is not just a naive squaring loop -- it is the same algorithm RSA-scale code reaches for.
What is the most common Go mistake when implementing Binary Exponentiation?
Plain int/uint64 arithmetic in Go overflows SILENTLY -- no panic, no error, just two's-complement wraparound -- so a hand-rolled squaring loop that forgets to reduce mod after EVERY multiply, not just at the return, runs to completion and hands back a wrong number with zero indication anything went wrong. The other trap is big.Int's mutable-receiver API: z.Exp(x, y, m) overwrites z, so calling x.Exp(x, y, m) to 'save an allocation' also overwrites x if it is still needed afterward.
How does Binary Exponentiation actually perform in Go?
big.Int.Exp does the log2(y) squarings the public bound promises, but for a large ODD modulus it converts to Montgomery form first, trading a one-time conversion cost for cheaper modular multiplications on every step after -- genuinely faster than naive binary exponentiation at RSA-key sizes, not just the same algorithm in a faster runtime. The hand-rolled uint64 loop has zero allocation and is faster for small fixed-width moduli where Montgomery's setup cost is not worth paying. Time complexity is O(log n) and space is O(1).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialBefore paying for Devin, check its student and free-tier options on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.