From: apocelipes Date: Thu, 14 Mar 2024 12:06:36 +0000 (+0000) Subject: math/big: use built-in clear to simplify code X-Git-Tag: go1.23rc1~888 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=c841ba3a3e4f320c7939a8097f8e54b752644d81;p=gostls13.git math/big: use built-in clear to simplify code Change-Id: I07c3a498ce1e462c3d1703d77e7d7824e9334651 GitHub-Last-Rev: 2ba8c4c705eaeb0772109ece7296978b62467eb3 GitHub-Pull-Request: golang/go#66312 Reviewed-on: https://go-review.googlesource.com/c/go/+/571636 Reviewed-by: Keith Randall Reviewed-by: Cherry Mui Reviewed-by: Keith Randall Auto-Submit: Keith Randall LUCI-TryBot-Result: Go LUCI --- diff --git a/src/math/big/int.go b/src/math/big/int.go index b79b459270..8e9b306f07 100644 --- a/src/math/big/int.go +++ b/src/math/big/int.go @@ -533,10 +533,8 @@ func (x *Int) Bytes() []byte { // // If the absolute value of x doesn't fit in buf, FillBytes will panic. func (x *Int) FillBytes(buf []byte) []byte { - // Clear whole buffer. (This gets optimized into a memclr.) - for i := range buf { - buf[i] = 0 - } + // Clear whole buffer. + clear(buf) x.abs.bytes(buf) return buf } diff --git a/src/math/big/nat.go b/src/math/big/nat.go index ecb7d363d4..1d702c7726 100644 --- a/src/math/big/nat.go +++ b/src/math/big/nat.go @@ -44,12 +44,6 @@ func (z nat) String() string { return "0x" + string(z.itoa(false, 16)) } -func (z nat) clear() { - for i := range z { - z[i] = 0 - } -} - func (z nat) norm() nat { i := len(z) for i > 0 && z[i-1] == 0 { @@ -196,7 +190,7 @@ func (z nat) mulAddWW(x nat, y, r Word) nat { // basicMul multiplies x and y and leaves the result in z. // The (non-normalized) result is placed in z[0 : len(x) + len(y)]. func basicMul(z, x, y nat) { - z[0 : len(x)+len(y)].clear() // initialize z + clear(z[0 : len(x)+len(y)]) // initialize z for i, d := range y { if d != 0 { z[len(x)+i] = addMulVVW(z[i:i+len(x)], x, d) @@ -222,7 +216,7 @@ func (z nat) montgomery(x, y, m nat, k Word, n int) nat { panic("math/big: mismatched montgomery number lengths") } z = z.make(n * 2) - z.clear() + clear(z) var c Word for i := 0; i < n; i++ { d := y[i] @@ -443,8 +437,8 @@ func (z nat) mul(x, y nat) nat { y0 := y[0:k] // y0 is not normalized z = z.make(max(6*k, m+n)) // enough space for karatsuba of x0*y0 and full result of x*y karatsuba(z, x0, y0) - z = z[0 : m+n] // z has final length but may be incomplete - z[2*k:].clear() // upper portion of z is garbage (and 2*k <= m+n since k <= n <= m) + z = z[0 : m+n] // z has final length but may be incomplete + clear(z[2*k:]) // upper portion of z is garbage (and 2*k <= m+n since k <= n <= m) // If xh != 0 or yh != 0, add the missing terms to z. For // @@ -497,7 +491,7 @@ func basicSqr(z, x nat) { n := len(x) tp := getNat(2 * n) t := *tp // temporary variable to hold the products - t.clear() + clear(t) z[1], z[0] = mulWW(x[0], x[0]) // the initial square for i := 1; i < n; i++ { d := x[i] @@ -592,7 +586,7 @@ func (z nat) sqr(x nat) nat { z = z.make(max(6*k, 2*n)) karatsubaSqr(z, x0) // z = x0^2 z = z[0 : 2*n] - z[2*k:].clear() + clear(z[2*k:]) if k < n { tp := getNat(2 * k) @@ -723,7 +717,7 @@ func (z nat) shl(x nat, s uint) nat { n := m + int(s/_W) z = z.make(n + 1) z[n] = shlVU(z[n-m:n], x, s%_W) - z[0 : n-m].clear() + clear(z[0 : n-m]) return z.norm() } @@ -769,7 +763,7 @@ func (z nat) setBit(x nat, i uint, b uint) nat { case 1: if j >= n { z = z.make(j + 1) - z[n:].clear() + clear(z[n:]) } else { z = z.make(n) } diff --git a/src/math/big/natdiv.go b/src/math/big/natdiv.go index 14233a2ddb..2172eeca40 100644 --- a/src/math/big/natdiv.go +++ b/src/math/big/natdiv.go @@ -734,7 +734,7 @@ func (z nat) divRecursive(u, v nat) { tmp := getNat(3 * len(v)) temps := make([]*nat, recDepth) - z.clear() + clear(z) z.divRecursiveStep(u, v, 0, tmp, temps) // Free temporaries. @@ -758,7 +758,7 @@ func (z nat) divRecursiveStep(u, v nat, depth int, tmp *nat, temps []*nat) { u = u.norm() v = v.norm() if len(u) == 0 { - z.clear() + clear(z) return } @@ -816,7 +816,7 @@ func (z nat) divRecursiveStep(u, v nat, depth int, tmp *nat, temps []*nat) { // Compute the 2-by-1 guess q̂, leaving r̂ in uu[s:B+n]. qhat := *temps[depth] - qhat.clear() + clear(qhat) qhat.divRecursiveStep(uu[s:B+n], v[s:], depth+1, tmp, temps) qhat = qhat.norm() @@ -833,7 +833,7 @@ func (z nat) divRecursiveStep(u, v nat, depth int, tmp *nat, temps []*nat) { // But we can do the subtraction directly, as in the comment above // and in long division, because we know that q̂ is wrong by at most one. qhatv := tmp.make(3 * n) - qhatv.clear() + clear(qhatv) qhatv = qhatv.mul(qhat, v[:s]) for i := 0; i < 2; i++ { e := qhatv.cmp(uu.norm()) @@ -864,11 +864,11 @@ func (z nat) divRecursiveStep(u, v nat, depth int, tmp *nat, temps []*nat) { // Choose shift = B-1 again. s := B - 1 qhat := *temps[depth] - qhat.clear() + clear(qhat) qhat.divRecursiveStep(u[s:].norm(), v[s:], depth+1, tmp, temps) qhat = qhat.norm() qhatv := tmp.make(3 * n) - qhatv.clear() + clear(qhatv) qhatv = qhatv.mul(qhat, v[:s]) // Set the correct remainder as before. for i := 0; i < 2; i++ {