Skip to content

Commit c841ba3

Browse files
apocelipesgopherbot
authored andcommitted
math/big: use built-in clear to simplify code
Change-Id: I07c3a498ce1e462c3d1703d77e7d7824e9334651 GitHub-Last-Rev: 2ba8c4c GitHub-Pull-Request: #66312 Reviewed-on: https://go-review.googlesource.com/c/go/+/571636 Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Cherry Mui <[email protected]> Reviewed-by: Keith Randall <[email protected]> Auto-Submit: Keith Randall <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 1304d98 commit c841ba3

File tree

3 files changed

+16
-24
lines changed

3 files changed

+16
-24
lines changed

src/math/big/int.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -533,10 +533,8 @@ func (x *Int) Bytes() []byte {
533533
//
534534
// If the absolute value of x doesn't fit in buf, FillBytes will panic.
535535
func (x *Int) FillBytes(buf []byte) []byte {
536-
// Clear whole buffer. (This gets optimized into a memclr.)
537-
for i := range buf {
538-
buf[i] = 0
539-
}
536+
// Clear whole buffer.
537+
clear(buf)
540538
x.abs.bytes(buf)
541539
return buf
542540
}

src/math/big/nat.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,6 @@ func (z nat) String() string {
4444
return "0x" + string(z.itoa(false, 16))
4545
}
4646

47-
func (z nat) clear() {
48-
for i := range z {
49-
z[i] = 0
50-
}
51-
}
52-
5347
func (z nat) norm() nat {
5448
i := len(z)
5549
for i > 0 && z[i-1] == 0 {
@@ -196,7 +190,7 @@ func (z nat) mulAddWW(x nat, y, r Word) nat {
196190
// basicMul multiplies x and y and leaves the result in z.
197191
// The (non-normalized) result is placed in z[0 : len(x) + len(y)].
198192
func basicMul(z, x, y nat) {
199-
z[0 : len(x)+len(y)].clear() // initialize z
193+
clear(z[0 : len(x)+len(y)]) // initialize z
200194
for i, d := range y {
201195
if d != 0 {
202196
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 {
222216
panic("math/big: mismatched montgomery number lengths")
223217
}
224218
z = z.make(n * 2)
225-
z.clear()
219+
clear(z)
226220
var c Word
227221
for i := 0; i < n; i++ {
228222
d := y[i]
@@ -443,8 +437,8 @@ func (z nat) mul(x, y nat) nat {
443437
y0 := y[0:k] // y0 is not normalized
444438
z = z.make(max(6*k, m+n)) // enough space for karatsuba of x0*y0 and full result of x*y
445439
karatsuba(z, x0, y0)
446-
z = z[0 : m+n] // z has final length but may be incomplete
447-
z[2*k:].clear() // upper portion of z is garbage (and 2*k <= m+n since k <= n <= m)
440+
z = z[0 : m+n] // z has final length but may be incomplete
441+
clear(z[2*k:]) // upper portion of z is garbage (and 2*k <= m+n since k <= n <= m)
448442

449443
// If xh != 0 or yh != 0, add the missing terms to z. For
450444
//
@@ -497,7 +491,7 @@ func basicSqr(z, x nat) {
497491
n := len(x)
498492
tp := getNat(2 * n)
499493
t := *tp // temporary variable to hold the products
500-
t.clear()
494+
clear(t)
501495
z[1], z[0] = mulWW(x[0], x[0]) // the initial square
502496
for i := 1; i < n; i++ {
503497
d := x[i]
@@ -592,7 +586,7 @@ func (z nat) sqr(x nat) nat {
592586
z = z.make(max(6*k, 2*n))
593587
karatsubaSqr(z, x0) // z = x0^2
594588
z = z[0 : 2*n]
595-
z[2*k:].clear()
589+
clear(z[2*k:])
596590

597591
if k < n {
598592
tp := getNat(2 * k)
@@ -723,7 +717,7 @@ func (z nat) shl(x nat, s uint) nat {
723717
n := m + int(s/_W)
724718
z = z.make(n + 1)
725719
z[n] = shlVU(z[n-m:n], x, s%_W)
726-
z[0 : n-m].clear()
720+
clear(z[0 : n-m])
727721

728722
return z.norm()
729723
}
@@ -769,7 +763,7 @@ func (z nat) setBit(x nat, i uint, b uint) nat {
769763
case 1:
770764
if j >= n {
771765
z = z.make(j + 1)
772-
z[n:].clear()
766+
clear(z[n:])
773767
} else {
774768
z = z.make(n)
775769
}

src/math/big/natdiv.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ func (z nat) divRecursive(u, v nat) {
734734
tmp := getNat(3 * len(v))
735735
temps := make([]*nat, recDepth)
736736

737-
z.clear()
737+
clear(z)
738738
z.divRecursiveStep(u, v, 0, tmp, temps)
739739

740740
// Free temporaries.
@@ -758,7 +758,7 @@ func (z nat) divRecursiveStep(u, v nat, depth int, tmp *nat, temps []*nat) {
758758
u = u.norm()
759759
v = v.norm()
760760
if len(u) == 0 {
761-
z.clear()
761+
clear(z)
762762
return
763763
}
764764

@@ -816,7 +816,7 @@ func (z nat) divRecursiveStep(u, v nat, depth int, tmp *nat, temps []*nat) {
816816

817817
// Compute the 2-by-1 guess q̂, leaving r̂ in uu[s:B+n].
818818
qhat := *temps[depth]
819-
qhat.clear()
819+
clear(qhat)
820820
qhat.divRecursiveStep(uu[s:B+n], v[s:], depth+1, tmp, temps)
821821
qhat = qhat.norm()
822822

@@ -833,7 +833,7 @@ func (z nat) divRecursiveStep(u, v nat, depth int, tmp *nat, temps []*nat) {
833833
// But we can do the subtraction directly, as in the comment above
834834
// and in long division, because we know that q̂ is wrong by at most one.
835835
qhatv := tmp.make(3 * n)
836-
qhatv.clear()
836+
clear(qhatv)
837837
qhatv = qhatv.mul(qhat, v[:s])
838838
for i := 0; i < 2; i++ {
839839
e := qhatv.cmp(uu.norm())
@@ -864,11 +864,11 @@ func (z nat) divRecursiveStep(u, v nat, depth int, tmp *nat, temps []*nat) {
864864
// Choose shift = B-1 again.
865865
s := B - 1
866866
qhat := *temps[depth]
867-
qhat.clear()
867+
clear(qhat)
868868
qhat.divRecursiveStep(u[s:].norm(), v[s:], depth+1, tmp, temps)
869869
qhat = qhat.norm()
870870
qhatv := tmp.make(3 * n)
871-
qhatv.clear()
871+
clear(qhatv)
872872
qhatv = qhatv.mul(qhat, v[:s])
873873
// Set the correct remainder as before.
874874
for i := 0; i < 2; i++ {

0 commit comments

Comments
 (0)