Skip to content

Commit 260720c

Browse files
committed
optimize slice allocation in shutter crypto
1 parent 94d8e90 commit 260720c

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

txnprovider/shutter/internal/crypto/hash.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,16 @@ func keccak256(ds ...[]byte) []byte {
3535
}
3636

3737
func hashWithPrefix(p byte, b []byte) []byte {
38-
return keccak256(append([]byte{p}, b...))
38+
buf := make([]byte, 1+len(b))
39+
buf[0] = p
40+
copy(buf[1:], b)
41+
return keccak256(buf)
3942
}
4043

4144
func Hash1(b []byte) *blst.P1Affine {
42-
bWithPrefix := append([]byte{1}, b...)
45+
bWithPrefix := make([]byte, 1+len(b))
46+
bWithPrefix[0] = 1
47+
copy(bWithPrefix[1:], b)
4348
p := blst.HashToG1(bWithPrefix, []byte(HashToG1DST))
4449
return p.ToAffine()
4550
}

txnprovider/shutter/internal/crypto/helpers_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,26 @@ func EnsureGobable(t *testing.T, src, dst any) {
3434
require.NoError(t, err)
3535
assert.Equal(t, src, dst)
3636
}
37+
38+
var globalBuf []byte
39+
40+
func BenchmarkAppend(b *testing.B) {
41+
data := make([]byte, 32)
42+
p := byte(1)
43+
b.ResetTimer()
44+
for i := 0; i < b.N; i++ {
45+
globalBuf = append([]byte{p}, data...)
46+
}
47+
}
48+
49+
func BenchmarkMakeCopy(b *testing.B) {
50+
data := make([]byte, 32)
51+
p := byte(1)
52+
b.ResetTimer()
53+
for i := 0; i < b.N; i++ {
54+
buf := make([]byte, 1+len(data))
55+
buf[0] = p
56+
copy(buf[1:], data)
57+
globalBuf = buf
58+
}
59+
}

0 commit comments

Comments
 (0)