-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain_bench_test.go
128 lines (112 loc) · 2.81 KB
/
main_bench_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"crypto/rand"
"fmt"
"math/big"
"testing"
"github.com/QED-it/go-jubjub/pkg/pedersenhash"
"github.com/iden3/go-iden3-crypto/poseidon"
"golang.org/x/crypto/blake2b"
)
func benchmarkHashing(b *testing.B, kind string, logarraysize int) {
datasize := (1 << (logarraysize)) * 20
b.Run(fmt.Sprintf("%s;%d", kind, logarraysize), func(b *testing.B) {
bytes := make([]byte, datasize)
b.ResetTimer()
for bn := 0; bn < b.N; bn++ {
b.StopTimer()
_, err := rand.Read(bytes)
if err != nil {
panic(fmt.Sprintf("Error randomly generating data: %v\n", err))
}
b.StartTimer()
if kind == "Blake2b256" {
_ = blake2b.Sum256(bytes)
} else if kind == "Poseidon" {
poseidon.HashBytes(bytes)
}
}
})
}
func benchmarkPoseidon(b *testing.B) {
datasize := 32
b.Run(fmt.Sprintf("Poseidon;"), func(b *testing.B) {
bytes := make([]byte, datasize)
b.ResetTimer()
for bn := 0; bn < b.N; bn++ {
b.StopTimer()
_, _ = rand.Read(bytes)
A, _ := poseidon.HashBytes(bytes)
_, _ = rand.Read(bytes)
B, _ := poseidon.HashBytes(bytes)
array := []*big.Int{A, B}
b.StartTimer()
poseidon.Hash(array)
}
})
}
func benchmarkBlake2b256(b *testing.B) {
datasize := 32
b.Run(fmt.Sprintf("Blake2b256;"), func(b *testing.B) {
bytes := make([]byte, datasize)
b.ResetTimer()
for bn := 0; bn < b.N; bn++ {
b.StopTimer()
_, _ = rand.Read(bytes)
A := blake2b.Sum256(bytes)
_, _ = rand.Read(bytes)
B := blake2b.Sum256(bytes)
array := make([]byte, 0, 2*len(A))
array = append(array, A[:]...)
array = append(array, B[:]...)
b.StartTimer()
_ = blake2b.Sum256(bytes)
}
})
}
func convert_bytes_to_bool(byteArray []byte) []bool {
X := make([]bool, len(byteArray)*8)
for _, in := range byteArray {
for i := 7; i > 0; i-- {
if in&(1<<i) > 0 {
X[i] = true
}
}
}
return X
}
func benchmarkPedersenHash(b *testing.B) {
datasize := 32
bytesX := make([]byte, datasize/2)
bytesY := make([]byte, datasize/2)
_, _ = rand.Read(bytesX)
_, _ = rand.Read(bytesY)
X := convert_bytes_to_bool(bytesX)
Y := convert_bytes_to_bool(bytesY)
hasher, _ := pedersenhash.NewPedersenHasher()
p, _ := hasher.PedersenHashForBits(X, Y)
bytesX = p.X().Bytes()
bytesY = p.Y().Bytes()
b.Run(fmt.Sprintf("Pedersen128;"), func(b *testing.B) {
b.ResetTimer()
for bn := 0; bn < b.N; bn++ {
b.StopTimer()
bytesX = p.X().Bytes()
bytesY = p.Y().Bytes()
X = convert_bytes_to_bool(bytesX)
Y = convert_bytes_to_bool(bytesY)
b.StartTimer()
p, _ = hasher.PedersenHashForBits(X, Y)
}
})
}
func BenchmarkHashing(b *testing.B) {
logarraysize := []int{6, 7}
for i := range logarraysize {
benchmarkHashing(b, "Poseidon", logarraysize[i])
benchmarkHashing(b, "Blake2b256", logarraysize[i])
}
benchmarkPoseidon(b)
benchmarkBlake2b256(b)
// benchmarkPedersenHash(b)
}