Skip to content

Commit

Permalink
Refactor to make hash benchmarks more accurate
Browse files Browse the repository at this point in the history
Now each iteration benchmark loop operates on a separate set of felts.
  • Loading branch information
IronGauntlets committed Dec 2, 2024
1 parent ef5a784 commit 636ba6d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 46 deletions.
61 changes: 38 additions & 23 deletions core/crypto/pedersen_hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/juno/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestPedersen(t *testing.T) {
Expand Down Expand Up @@ -126,44 +127,58 @@ var benchHashR *felt.Felt
// go test -bench=. -run=^# -cpu=1,2,4,8,16
func BenchmarkPedersenArray(b *testing.B) {
numOfElems := []int{3, 5, 10, 15, 20, 25, 30, 35, 40}
createRandomFelts := func(n int) []*felt.Felt {
var felts []*felt.Felt
for i := 0; i < n; i++ {
f, err := new(felt.Felt).SetRandom()
if err != nil {
b.Fatalf("error while generating random felt: %x", err)
}
felts = append(felts, f)
}
return felts
}

for _, i := range numOfElems {
b.Run(fmt.Sprintf("Number of felts: %d", i), func(b *testing.B) {
randomFeltSls := genRandomFeltSls(b, i)
var f *felt.Felt
randomFelts := createRandomFelts(i)
b.ResetTimer()
for n := 0; n < b.N; n++ {
f = crypto.PedersenArray(randomFelts...)
f = crypto.PedersenArray(randomFeltSls[n]...)
}
benchHashR = f
})
}
}

func BenchmarkPedersen(b *testing.B) {
e0, err := new(felt.Felt).SetString("0x3d937c035c878245caf64531a5756109c53068da139362728feb561405371cb")
if err != nil {
b.Errorf("Error occurred %s", err)
randFelts := genRandomFeltPairs(b)
var f *felt.Felt
b.ResetTimer()
for n := 0; n < b.N; n++ {
f = crypto.Pedersen(randFelts[n][0], randFelts[n][1])
}
benchHashR = f
}

e1, err := new(felt.Felt).SetString("0x208a0a10250e382e1e4bbe2880906c2791bf6275695e02fbbc6aeff9cd8b31a")
if err != nil {
b.Errorf("Error occurred %s", err)
func genRandomFeltSls(b *testing.B, n int) [][]*felt.Felt {
var randomFeltSls [][]*felt.Felt
for j := 0; j < b.N; j++ {
randomFeltSls = append(randomFeltSls, genRandomFelts(b, n))
}
return randomFeltSls
}

var f *felt.Felt
for n := 0; n < b.N; n++ {
f = crypto.Pedersen(e0, e1)
func genRandomFelts(b *testing.B, n int) []*felt.Felt {
b.Helper()
var felts []*felt.Felt
for i := 0; i < n; i++ {
f, err := new(felt.Felt).SetRandom()
require.NoError(b, err)
felts = append(felts, f)
}
benchHashR = f
return felts
}

func genRandomFeltPairs(b *testing.B) [][2]*felt.Felt {
b.Helper()
var err error
randFelts := make([][2]*felt.Felt, b.N)
for i := 0; i < b.N; i++ {
randFelts[i][0], err = new(felt.Felt).SetRandom()
require.NoError(b, err)
randFelts[i][1], err = new(felt.Felt).SetRandom()
require.NoError(b, err)
}
return randFelts
}
29 changes: 6 additions & 23 deletions core/crypto/poseidon_hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,44 +57,27 @@ func TestPoseidonArray(t *testing.T) {
// go test -bench=. -run=^# -cpu=1,2,4,8,16
func BenchmarkPoseidonArray(b *testing.B) {
numOfElems := []int{3, 5, 10, 15, 20, 25, 30, 35, 40}
createRandomFelts := func(n int) []*felt.Felt {
var felts []*felt.Felt
for i := 0; i < n; i++ {
randF, err := new(felt.Felt).SetRandom()
if err != nil {
b.Fatalf("error while generating random felt: %x", err)
}
felts = append(felts, randF)
}
return felts
}

for _, i := range numOfElems {
b.Run(fmt.Sprintf("Number of felts: %d", i), func(b *testing.B) {
randomFeltSls := genRandomFeltSls(b, i)
var f *felt.Felt
randomFelts := createRandomFelts(i)
b.ResetTimer()
for n := 0; n < b.N; n++ {
f = crypto.PoseidonArray(randomFelts...)
f = crypto.PoseidonArray(randomFeltSls[n]...)
}
benchHashR = f
})
}
}

func BenchmarkPoseidon(b *testing.B) {
e0, err := new(felt.Felt).SetString("0x3d937c035c878245caf64531a5756109c53068da139362728feb561405371cb")
if err != nil {
b.Fatalf("Error occurred %s", err)
}

e1, err := new(felt.Felt).SetString("0x208a0a10250e382e1e4bbe2880906c2791bf6275695e02fbbc6aeff9cd8b31a")
if err != nil {
b.Fatalf("Error occurred %s", err)
}
randFelts := genRandomFeltPairs(b)

var f *felt.Felt
b.ResetTimer()
for n := 0; n < b.N; n++ {
f = crypto.Poseidon(e0, e1)
f = crypto.Poseidon(randFelts[n][0], randFelts[n][1])
}
benchHashR = f
}

0 comments on commit 636ba6d

Please sign in to comment.