]> Cypherpunks repositories - gostls13.git/commitdiff
math/big: fix Exp when exponent is 1
authorALTree <alb.donizetti@gmail.com>
Mon, 6 Apr 2015 19:18:37 +0000 (21:18 +0200)
committerRobert Griesemer <gri@golang.org>
Tue, 7 Apr 2015 21:04:09 +0000 (21:04 +0000)
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>
src/math/big/int_test.go
src/math/big/nat.go

index a972a7249bbec4024688f31a1cd2aeb73802e6bf..fa4ae2d311695167b7ec5a88e097f47caabfc056 100644 (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"},
index 2a279d186c4c7ad76b0da1e96c590672774c127c..7157a5487ba64b229cbe490b68de7ff0f94e70c1 100644 (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))