diff options
author | Brian Kessler <brian.m.kessler@gmail.com> | 2017-11-30 09:32:07 -0700 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2018-04-05 22:02:33 +0000 |
commit | 7818b82fc8f7d2f33f3ddfb99755b1ef06e2d281 (patch) | |
tree | 359581e99b8600b42bb1d17a01759798372f39e5 /src/math | |
parent | 638f112d6982e83132051ccb4f0b27684b9c0f34 (diff) | |
download | go-git-7818b82fc8f7d2f33f3ddfb99755b1ef06e2d281.tar.gz |
math/big: clean up z.div(z, x, y) calls
Updates #22830
Due to not checking if the output slices alias in divLarge,
calls of the form z.div(z, x, y) caused the slice z
to attempt to be used to store both the quotient and the
remainder of the division. CL 78995 applies an alias
check to correct that error. This CL cleans up the
additional div calls that attempt to supply the same slice
to hold both the quotient and remainder.
Note that the call in expNN was responsible for the reported
error in r.Exp(x, 1, m) when r was initialized to a non-zero value.
The second instance in expNNMontgomery did not result in an error
due to the size of the arguments.
// RR = 2**(2*_W*len(m)) mod m
RR := nat(nil).setWord(1)
zz := nat(nil).shl(RR, uint(2*numWords*_W))
_, RR = RR.div(RR, zz, m)
Specifically,
cap(RR) == 5 after setWord(1) due to const e = 4 in z.make(1)
len(zz) == 2*len(m) + 1 after shifting left, numWords = len(m)
Reusing the backing array for z and z2 in div was only triggered if
cap(RR) >= len(zz) + 1 and len(m) > 1 so that divLarge was called.
But, 5 < 2*len(m) + 2 if len(m) > 1, so new arrays were allocated
and the error was never triggered in this case.
Change-Id: Iedac80dbbde13216c94659e84d28f6f4be3aaf24
Reviewed-on: https://go-review.googlesource.com/81055
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
Diffstat (limited to 'src/math')
-rw-r--r-- | src/math/big/nat.go | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/src/math/big/nat.go b/src/math/big/nat.go index 1208ea76c8..9ec81270a3 100644 --- a/src/math/big/nat.go +++ b/src/math/big/nat.go @@ -985,7 +985,7 @@ func (z nat) expNN(x, y, m nat) nat { // x**1 mod m == x mod m if len(y) == 1 && y[0] == 1 && len(m) != 0 { - _, z = z.div(z, x, m) + _, z = nat(nil).div(z, x, m) return z } // y > 1 @@ -1158,7 +1158,7 @@ func (z nat) expNNMontgomery(x, y, m nat) nat { // RR = 2**(2*_W*len(m)) mod m RR := nat(nil).setWord(1) zz := nat(nil).shl(RR, uint(2*numWords*_W)) - _, RR = RR.div(RR, zz, m) + _, RR = nat(nil).div(RR, zz, m) if len(RR) < numWords { zz = zz.make(numWords) copy(zz, RR) |