diff --git a/txnprovider/shutter/internal/crypto/hash.go b/txnprovider/shutter/internal/crypto/hash.go index 5ae402623ca..4cb87f100b7 100644 --- a/txnprovider/shutter/internal/crypto/hash.go +++ b/txnprovider/shutter/internal/crypto/hash.go @@ -35,11 +35,16 @@ func keccak256(ds ...[]byte) []byte { } func hashWithPrefix(p byte, b []byte) []byte { - return keccak256(append([]byte{p}, b...)) + buf := make([]byte, 1+len(b)) + buf[0] = p + copy(buf[1:], b) + return keccak256(buf) } func Hash1(b []byte) *blst.P1Affine { - bWithPrefix := append([]byte{1}, b...) + bWithPrefix := make([]byte, 1+len(b)) + bWithPrefix[0] = 1 + copy(bWithPrefix[1:], b) p := blst.HashToG1(bWithPrefix, []byte(HashToG1DST)) return p.ToAffine() } diff --git a/txnprovider/shutter/internal/crypto/helpers_test.go b/txnprovider/shutter/internal/crypto/helpers_test.go index a1341915777..c79baf9dfff 100644 --- a/txnprovider/shutter/internal/crypto/helpers_test.go +++ b/txnprovider/shutter/internal/crypto/helpers_test.go @@ -34,3 +34,26 @@ func EnsureGobable(t *testing.T, src, dst any) { require.NoError(t, err) assert.Equal(t, src, dst) } + +var globalBuf []byte + +func BenchmarkAppend(b *testing.B) { + data := make([]byte, 32) + p := byte(1) + b.ResetTimer() + for i := 0; i < b.N; i++ { + globalBuf = append([]byte{p}, data...) + } +} + +func BenchmarkMakeCopy(b *testing.B) { + data := make([]byte, 32) + p := byte(1) + b.ResetTimer() + for i := 0; i < b.N; i++ { + buf := make([]byte, 1+len(data)) + buf[0] = p + copy(buf[1:], data) + globalBuf = buf + } +}