1
0
mirror of https://github.com/golang/go synced 2024-11-22 00:54:43 -07:00

big: prevent errors in Exp in the face of aliasing

R=gri
CC=golang-dev, golang-dev
https://golang.org/cl/1244044
This commit is contained in:
Adam Langley 2010-05-24 14:32:55 -04:00
parent 977475fd49
commit eadebba36f
3 changed files with 8 additions and 2 deletions

View File

@ -434,8 +434,9 @@ func (z *Int) BitLen() int {
// See Knuth, volume 2, section 4.6.3. // See Knuth, volume 2, section 4.6.3.
func (z *Int) Exp(x, y, m *Int) *Int { func (z *Int) Exp(x, y, m *Int) *Int {
if y.neg || len(y.abs) == 0 { if y.neg || len(y.abs) == 0 {
neg := x.neg
z.SetInt64(1) z.SetInt64(1)
z.neg = x.neg z.neg = neg
return z return z
} }

View File

@ -602,7 +602,7 @@ func TestExp(t *testing.T) {
continue continue
} }
z := new(Int).Exp(x, y, m) z := y.Exp(x, y, m)
if !isNormalized(z) { if !isNormalized(z) {
t.Errorf("#%d: %v is not normalized", i, *z) t.Errorf("#%d: %v is not normalized", i, *z)
} }

View File

@ -920,6 +920,11 @@ func (z nat) random(rand *rand.Rand, limit nat, n int) nat {
// If m != nil, expNN calculates x**y mod m. Otherwise it calculates x**y. It // If m != nil, expNN calculates x**y mod m. Otherwise it calculates x**y. It
// reuses the storage of z if possible. // reuses the storage of z if possible.
func (z nat) expNN(x, y, m nat) nat { func (z nat) expNN(x, y, m nat) nat {
if alias(z, x) || alias(z, y) {
// We cannot allow in place modification of x or y.
z = nil
}
if len(y) == 0 { if len(y) == 0 {
z = z.make(1) z = z.make(1)
z[0] = 1 z[0] = 1