diff --git a/crypto/blake2b/blake2b_test.go b/crypto/blake2b/blake2b_test.go index 9d24444a27..8f210445fc 100644 --- a/crypto/blake2b/blake2b_test.go +++ b/crypto/blake2b/blake2b_test.go @@ -303,8 +303,7 @@ func benchmarkSum(b *testing.B, size int, sse4, avx, avx2 bool) { data := make([]byte, size) b.SetBytes(int64(size)) - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { Sum512(data) } } @@ -319,8 +318,7 @@ func benchmarkWrite(b *testing.B, size int, sse4, avx, avx2 bool) { data := make([]byte, size) h, _ := New512(nil) b.SetBytes(int64(size)) - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { h.Write(data) } } diff --git a/crypto/crypto_test.go b/crypto/crypto_test.go index e620d6ee3a..d803ab27c5 100644 --- a/crypto/crypto_test.go +++ b/crypto/crypto_test.go @@ -60,7 +60,7 @@ func TestToECDSAErrors(t *testing.T) { func BenchmarkSha3(b *testing.B) { a := []byte("hello world") - for i := 0; i < b.N; i++ { + for b.Loop() { Keccak256(a) } } @@ -310,7 +310,7 @@ func BenchmarkKeccak256Hash(b *testing.B) { rand.Read(input[:]) b.ReportAllocs() - for i := 0; i < b.N; i++ { + for b.Loop() { Keccak256Hash(input[:]) } } @@ -329,7 +329,7 @@ func BenchmarkHashData(b *testing.B) { rand.Read(input[:]) b.ReportAllocs() - for i := 0; i < b.N; i++ { + for b.Loop() { HashData(buffer, input[:]) } } diff --git a/crypto/ecies/ecies_test.go b/crypto/ecies/ecies_test.go index e3da71010e..e822db2783 100644 --- a/crypto/ecies/ecies_test.go +++ b/crypto/ecies/ecies_test.go @@ -164,7 +164,7 @@ func TestTooBigSharedKey(t *testing.T) { // Benchmark the generation of P256 keys. func BenchmarkGenerateKeyP256(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { if _, err := GenerateKey(rand.Reader, elliptic.P256(), nil); err != nil { b.Fatal(err) } @@ -177,8 +177,7 @@ func BenchmarkGenSharedKeyP256(b *testing.B) { if err != nil { b.Fatal(err) } - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { _, err := prv.GenerateShared(&prv.PublicKey, 16, 16) if err != nil { b.Fatal(err) @@ -192,8 +191,7 @@ func BenchmarkGenSharedKeyS256(b *testing.B) { if err != nil { b.Fatal(err) } - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { _, err := prv.GenerateShared(&prv.PublicKey, 16, 16) if err != nil { b.Fatal(err) diff --git a/crypto/kzg4844/kzg4844_test.go b/crypto/kzg4844/kzg4844_test.go index 7e73efd850..d398a0f48b 100644 --- a/crypto/kzg4844/kzg4844_test.go +++ b/crypto/kzg4844/kzg4844_test.go @@ -105,8 +105,7 @@ func benchmarkBlobToCommitment(b *testing.B, ckzg bool) { blob := randBlob() - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { BlobToCommitment(blob) } } @@ -125,8 +124,7 @@ func benchmarkComputeProof(b *testing.B, ckzg bool) { point = randFieldElement() ) - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { ComputeProof(blob, point) } } @@ -147,8 +145,7 @@ func benchmarkVerifyProof(b *testing.B, ckzg bool) { proof, claim, _ = ComputeProof(blob, point) ) - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { VerifyProof(commitment, point, claim, proof) } } @@ -167,8 +164,7 @@ func benchmarkComputeBlobProof(b *testing.B, ckzg bool) { commitment, _ = BlobToCommitment(blob) ) - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { ComputeBlobProof(blob, commitment) } } @@ -188,8 +184,7 @@ func benchmarkVerifyBlobProof(b *testing.B, ckzg bool) { proof, _ = ComputeBlobProof(blob, commitment) ) - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { VerifyBlobProof(blob, commitment, proof) } } diff --git a/crypto/secp256k1/secp256_test.go b/crypto/secp256k1/secp256_test.go index 4827cc5b25..c7485bca08 100644 --- a/crypto/secp256k1/secp256_test.go +++ b/crypto/secp256k1/secp256_test.go @@ -221,9 +221,7 @@ func TestRecoverSanity(t *testing.T) { func BenchmarkSign(b *testing.B) { _, seckey := generateKeyPair() msg := csprngEntropy(32) - b.ResetTimer() - - for i := 0; i < b.N; i++ { + for b.Loop() { Sign(msg, seckey) } } @@ -232,9 +230,7 @@ func BenchmarkRecover(b *testing.B) { msg := csprngEntropy(32) _, seckey := generateKeyPair() sig, _ := Sign(msg, seckey) - b.ResetTimer() - - for i := 0; i < b.N; i++ { + for b.Loop() { RecoverPubkey(msg, sig) } } diff --git a/crypto/signature_test.go b/crypto/signature_test.go index 74d683b507..b1f2960254 100644 --- a/crypto/signature_test.go +++ b/crypto/signature_test.go @@ -135,7 +135,7 @@ func TestPubkeyRandom(t *testing.T) { } func BenchmarkEcrecoverSignature(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { if _, err := Ecrecover(testmsg, testsig); err != nil { b.Fatal("ecrecover error", err) } @@ -144,7 +144,7 @@ func BenchmarkEcrecoverSignature(b *testing.B) { func BenchmarkVerifySignature(b *testing.B) { sig := testsig[:len(testsig)-1] // remove recovery id - for i := 0; i < b.N; i++ { + for b.Loop() { if !VerifySignature(testpubkey, testmsg, sig) { b.Fatal("verify error") } @@ -152,7 +152,7 @@ func BenchmarkVerifySignature(b *testing.B) { } func BenchmarkDecompressPubkey(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { if _, err := DecompressPubkey(testpubkeyc); err != nil { b.Fatal(err) }