Modular Exponentiation in Golang
- Assume all arguments are positive.
ModExpGoBigInteger
calculates modular exponentiation usingmath/big
package.ModExpGoBigIntegerExp
calculates modular exponentiation using nativeExp
method frommath/big
package.ModExp
calculates modular exponentiationin O(exponent)
.ModExpWithSquaring
calculates modular exponentiation with exponentiation by squaring,O(log exponent)
.
# run tests
go test .
# benchmark
go test -bench .
Tested under Golang 1.9.2, 2.8 GHz Intel Core i7, macOS 10.13, calculating 81792 ^ 73363 mod 233
.
goos: darwin
goarch: amd64
BenchmarkModExp-8 1000 1341863 ns/op
BenchmarkModExpWithSquaring-8 2000000 754 ns/op
BenchmarkModExpGoBigInteger-8 50 26960333 ns/op
BenchmarkModExpGoBigIntegerExp-8 500000 2352 ns/op