Commit e21154fe authored by ALTree's avatar ALTree Committed by Robert Griesemer

math/big: fix Exp when exponent is 1

Fixed bug that caused Exp(x, y, m) ( i.e. x**y (mod m) ) to return x
instead of x (mod m) when y == 1. See issue page on github for more
details.

Added test case

Fixes #9826

Change-Id: Ibabb58275a20c4231c9474199b7f1c10e54241ce
Reviewed-on: https://go-review.googlesource.com/8409Reviewed-by: 's avatarRobert Griesemer <gri@golang.org>
parent 0c8fe346
......@@ -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"},
......
......@@ -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))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment