mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-08 23:18:15 -05:00
* Ran gopls modernize to fix everything go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./... * Override rules_go provided dependency for golang.org/x/tools to v0.38.0. To update this, checked out rules_go, then ran `bazel run //go/tools/releaser -- upgrade-dep -mirror=false org_golang_x_tools` and copied the patches. * Fix buildtag violations and ignore buildtag violations in external * Introduce modernize analyzer package. * Add modernize "any" analyzer. * Fix violations of any analyzer * Add modernize "appendclipped" analyzer. * Fix violations of appendclipped * Add modernize "bloop" analyzer. * Add modernize "fmtappendf" analyzer. * Add modernize "forvar" analyzer. * Add modernize "mapsloop" analyzer. * Add modernize "minmax" analyzer. * Fix violations of minmax analyzer * Add modernize "omitzero" analyzer. * Add modernize "rangeint" analyzer. * Fix violations of rangeint. * Add modernize "reflecttypefor" analyzer. * Fix violations of reflecttypefor analyzer. * Add modernize "slicescontains" analyzer. * Add modernize "slicessort" analyzer. * Add modernize "slicesdelete" analyzer. This is disabled by default for now. See https://go.dev/issue/73686. * Add modernize "stringscutprefix" analyzer. * Add modernize "stringsbuilder" analyzer. * Fix violations of stringsbuilder analyzer. * Add modernize "stringsseq" analyzer. * Add modernize "testingcontext" analyzer. * Add modernize "waitgroup" analyzer. * Changelog fragment * gofmt * gazelle * Add modernize "newexpr" analyzer. * Disable newexpr until go1.26 * Add more details in WORKSPACE on how to update the override * @nalepae feedback on min() * gofmt * Fix violations of forvar
107 lines
2.9 KiB
Go
107 lines
2.9 KiB
Go
package hash_test
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"testing"
|
|
|
|
"github.com/OffchainLabs/prysm/v7/crypto/bls"
|
|
"github.com/OffchainLabs/prysm/v7/crypto/hash"
|
|
"github.com/OffchainLabs/prysm/v7/encoding/bytesutil"
|
|
ethpb "github.com/OffchainLabs/prysm/v7/proto/prysm/v1alpha1"
|
|
pb "github.com/OffchainLabs/prysm/v7/proto/testing"
|
|
"github.com/OffchainLabs/prysm/v7/testing/assert"
|
|
"github.com/OffchainLabs/prysm/v7/testing/require"
|
|
fuzz "github.com/google/gofuzz"
|
|
)
|
|
|
|
func TestHash(t *testing.T) {
|
|
hashOf0 := [32]byte{110, 52, 11, 156, 255, 179, 122, 152, 156, 165, 68, 230, 187, 120, 10, 44, 120, 144, 29, 63, 179, 55, 56, 118, 133, 17, 163, 6, 23, 175, 160, 29}
|
|
h := hash.Hash([]byte{0})
|
|
assert.Equal(t, hashOf0, h)
|
|
|
|
hashOf1 := [32]byte{75, 245, 18, 47, 52, 69, 84, 197, 59, 222, 46, 187, 140, 210, 183, 227, 209, 96, 10, 214, 49, 195, 133, 165, 215, 204, 226, 60, 119, 133, 69, 154}
|
|
h = hash.Hash([]byte{1})
|
|
assert.Equal(t, hashOf1, h)
|
|
assert.Equal(t, false, hashOf0 == hashOf1)
|
|
}
|
|
|
|
func BenchmarkHash(b *testing.B) {
|
|
for b.Loop() {
|
|
hash.Hash([]byte("abc"))
|
|
}
|
|
}
|
|
|
|
func TestHashKeccak256(t *testing.T) {
|
|
hashOf0 := [32]byte{188, 54, 120, 158, 122, 30, 40, 20, 54, 70, 66, 41, 130, 143, 129, 125, 102, 18, 247, 180, 119, 214, 101, 145, 255, 150, 169, 224, 100, 188, 201, 138}
|
|
h := hash.Keccak256([]byte{0})
|
|
assert.Equal(t, hashOf0, h)
|
|
|
|
hashOf1 := [32]byte{95, 231, 249, 119, 231, 29, 186, 46, 161, 166, 142, 33, 5, 123, 238, 187, 155, 226, 172, 48, 198, 65, 10, 163, 141, 79, 63, 190, 65, 220, 255, 210}
|
|
h = hash.Keccak256([]byte{1})
|
|
assert.Equal(t, hashOf1, h)
|
|
assert.Equal(t, false, hashOf0 == hashOf1)
|
|
|
|
// Same hashing test from go-ethereum for keccak256
|
|
hashOfabc, err := hex.DecodeString("4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45")
|
|
require.NoError(t, err)
|
|
h = hash.Keccak256([]byte("abc"))
|
|
h32 := bytesutil.ToBytes32(hashOfabc)
|
|
assert.Equal(t, h32, h)
|
|
}
|
|
|
|
func BenchmarkHashKeccak256(b *testing.B) {
|
|
for b.Loop() {
|
|
hash.Keccak256([]byte("abc"))
|
|
}
|
|
}
|
|
|
|
func TestHashProto(t *testing.T) {
|
|
msg1 := &pb.Puzzle{
|
|
Challenge: "hello",
|
|
}
|
|
msg2 := &pb.Puzzle{
|
|
Challenge: "hello",
|
|
}
|
|
h1, err := hash.Proto(msg1)
|
|
require.NoError(t, err)
|
|
h2, err := hash.Proto(msg2)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, h1, h2)
|
|
}
|
|
|
|
func TestHashProtoFuzz(t *testing.T) {
|
|
f := fuzz.New().NilChance(.2)
|
|
defer func(tt *testing.T) {
|
|
if r := recover(); r != nil {
|
|
t.Log("cannot hash nil proto")
|
|
}
|
|
}(t)
|
|
|
|
for range 1000 {
|
|
msg := &pb.AddressBook{}
|
|
f.Fuzz(msg)
|
|
_, err := hash.Proto(msg)
|
|
_ = err
|
|
}
|
|
}
|
|
|
|
func BenchmarkHashProto(b *testing.B) {
|
|
att := ðpb.Attestation{
|
|
AggregationBits: nil,
|
|
Data: ðpb.AttestationData{
|
|
Slot: 5,
|
|
CommitteeIndex: 3,
|
|
BeaconBlockRoot: []byte{},
|
|
Source: nil,
|
|
Target: nil,
|
|
},
|
|
Signature: bls.NewAggregateSignature().Marshal(),
|
|
}
|
|
|
|
for b.Loop() {
|
|
if _, err := hash.Proto(att); err != nil {
|
|
b.Log(err)
|
|
}
|
|
}
|
|
}
|