Skip to content

Commit 39f7ed1

Browse files
authored
Merge pull request #2603 from CortexFoundation/dev
deprecate XORBytes in favor of stdlib crypto/subtle
2 parents 29bc49b + d3d53c6 commit 39f7ed1

2 files changed

Lines changed: 24 additions & 41 deletions

File tree

common/bitutil/bitutil.go

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
package bitutil
99

1010
import (
11+
"crypto/subtle"
1112
"runtime"
1213
"unsafe"
1314
)
@@ -17,46 +18,16 @@ const supportsUnaligned = runtime.GOARCH == "386" || runtime.GOARCH == "amd64" |
1718

1819
// XORBytes xors the bytes in a and b. The destination is assumed to have enough
1920
// space. Returns the number of bytes xor'd.
21+
//
22+
// If dst does not have length at least n,
23+
// XORBytes panics without writing anything to dst.
24+
//
25+
// dst and x or y may overlap exactly or not at all,
26+
// otherwise XORBytes may panic.
27+
//
28+
// Deprecated: use crypto/subtle.XORBytes
2029
func XORBytes(dst, a, b []byte) int {
21-
if supportsUnaligned {
22-
return fastXORBytes(dst, a, b)
23-
}
24-
return safeXORBytes(dst, a, b)
25-
}
26-
27-
// fastXORBytes xors in bulk. It only works on architectures that support
28-
// unaligned read/writes.
29-
func fastXORBytes(dst, a, b []byte) int {
30-
n := len(a)
31-
if len(b) < n {
32-
n = len(b)
33-
}
34-
w := n / wordSize
35-
if w > 0 {
36-
dw := *(*[]uintptr)(unsafe.Pointer(&dst))
37-
aw := *(*[]uintptr)(unsafe.Pointer(&a))
38-
bw := *(*[]uintptr)(unsafe.Pointer(&b))
39-
for i := 0; i < w; i++ {
40-
dw[i] = aw[i] ^ bw[i]
41-
}
42-
}
43-
for i := n - n%wordSize; i < n; i++ {
44-
dst[i] = a[i] ^ b[i]
45-
}
46-
return n
47-
}
48-
49-
// safeXORBytes xors one by one. It works on all architectures, independent if
50-
// it supports unaligned read/writes or not.
51-
func safeXORBytes(dst, a, b []byte) int {
52-
n := len(a)
53-
if len(b) < n {
54-
n = len(b)
55-
}
56-
for i := 0; i < n; i++ {
57-
dst[i] = a[i] ^ b[i]
58-
}
59-
return n
30+
return subtle.XORBytes(dst, a, b)
6031
}
6132

6233
// ANDBytes ands the bytes in a and b. The destination is assumed to have enough

common/bitutil/bitutil_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestXOR(t *testing.T) {
2929
d2 := make([]byte, 1023+alignD)[alignD:]
3030

3131
XORBytes(d1, p, q)
32-
safeXORBytes(d2, p, q)
32+
naiveXOR(d2, p, q)
3333
if !bytes.Equal(d1, d2) {
3434
t.Error("not equal", d1, d2)
3535
}
@@ -38,6 +38,18 @@ func TestXOR(t *testing.T) {
3838
}
3939
}
4040

41+
// naiveXOR xors bytes one by one.
42+
func naiveXOR(dst, a, b []byte) int {
43+
n := len(a)
44+
if len(b) < n {
45+
n = len(b)
46+
}
47+
for i := 0; i < n; i++ {
48+
dst[i] = a[i] ^ b[i]
49+
}
50+
return n
51+
}
52+
4153
// Tests that bitwise AND works for various alignments.
4254
func TestAND(t *testing.T) {
4355
for alignP := 0; alignP < 2; alignP++ {
@@ -134,7 +146,7 @@ func benchmarkBaseXOR(b *testing.B, size int) {
134146
p, q := make([]byte, size), make([]byte, size)
135147

136148
for i := 0; i < b.N; i++ {
137-
safeXORBytes(p, p, q)
149+
naiveXOR(p, p, q)
138150
}
139151
}
140152

0 commit comments

Comments
 (0)