1
0
mirror of https://github.com/golang/go synced 2024-09-24 23:20:12 -06:00

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/8409
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
ALTree 2015-04-06 21:18:37 +02:00 committed by Robert Griesemer
parent 0c8fe3463f
commit e21154fe31
2 changed files with 8 additions and 0 deletions

View File

@ -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"},

View File

@ -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))