mirror of
https://github.com/golang/go
synced 2024-11-12 06:40:22 -07: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:
parent
0c8fe3463f
commit
e21154fe31
@ -525,6 +525,7 @@ var expTests = []struct {
|
|||||||
{"1234", "-1", "1", "0"},
|
{"1234", "-1", "1", "0"},
|
||||||
|
|
||||||
// misc
|
// misc
|
||||||
|
{"5", "1", "3", "2"},
|
||||||
{"5", "-7", "", "1"},
|
{"5", "-7", "", "1"},
|
||||||
{"-5", "-7", "", "1"},
|
{"-5", "-7", "", "1"},
|
||||||
{"5", "0", "", "1"},
|
{"5", "0", "", "1"},
|
||||||
|
@ -888,6 +888,13 @@ func (z nat) expNN(x, y, m nat) nat {
|
|||||||
}
|
}
|
||||||
// y > 0
|
// 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 {
|
if len(m) != 0 {
|
||||||
// We likely end up being as long as the modulus.
|
// We likely end up being as long as the modulus.
|
||||||
z = z.make(len(m))
|
z = z.make(len(m))
|
||||||
|
Loading…
Reference in New Issue
Block a user