diff --git a/src/math/big/int_test.go b/src/math/big/int_test.go index a972a7249b..fa4ae2d311 100644 --- a/src/math/big/int_test.go +++ b/src/math/big/int_test.go @@ -525,6 +525,7 @@ var expTests = []struct { {"1234", "-1", "1", "0"}, // misc + {"5", "1", "3", "2"}, {"5", "-7", "", "1"}, {"-5", "-7", "", "1"}, {"5", "0", "", "1"}, diff --git a/src/math/big/nat.go b/src/math/big/nat.go index 2a279d186c..7157a5487b 100644 --- a/src/math/big/nat.go +++ b/src/math/big/nat.go @@ -888,6 +888,13 @@ func (z nat) expNN(x, y, m nat) nat { } // y > 0 + // x**1 mod m == x mod m + if len(y) == 1 && y[0] == 1 && len(m) != 0 { + _, z = z.div(z, x, m) + return z + } + // y > 1 + if len(m) != 0 { // We likely end up being as long as the modulus. z = z.make(len(m))