2020-06-25 00:47:51 +00:00
|
|
|
package herumi_test
|
2019-08-06 00:11:38 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-06-25 00:47:51 +00:00
|
|
|
"github.com/herumi/bls-eth-go-binary/bls"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/bls/herumi"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/bls/iface"
|
2019-12-03 20:29:05 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/hashutil"
|
2020-07-18 16:31:42 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2019-08-06 00:11:38 +00:00
|
|
|
)
|
|
|
|
|
2019-12-03 20:29:05 +00:00
|
|
|
func BenchmarkPairing(b *testing.B) {
|
2020-07-18 16:31:42 +00:00
|
|
|
require.NoError(b, bls.Init(bls.BLS12_381))
|
2020-06-25 00:47:51 +00:00
|
|
|
if err := bls.SetETHmode(bls.EthModeDraft07); err != nil {
|
2020-05-07 12:05:53 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
2020-06-25 00:47:51 +00:00
|
|
|
newGt := &bls.GT{}
|
|
|
|
newG1 := &bls.G1{}
|
|
|
|
newG2 := &bls.G2{}
|
2019-12-03 20:29:05 +00:00
|
|
|
|
|
|
|
newGt.SetInt64(10)
|
|
|
|
hash := hashutil.Hash([]byte{})
|
2020-07-18 16:31:42 +00:00
|
|
|
require.NoError(b, newG1.HashAndMapTo(hash[:]))
|
|
|
|
require.NoError(b, newG2.HashAndMapTo(hash[:]))
|
2019-12-03 20:29:05 +00:00
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-06-25 00:47:51 +00:00
|
|
|
bls.Pairing(newGt, newG1, newG2)
|
2019-12-03 20:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
func BenchmarkSignature_Verify(b *testing.B) {
|
2020-06-25 00:47:51 +00:00
|
|
|
sk := herumi.RandKey()
|
2019-12-03 20:29:05 +00:00
|
|
|
|
2019-08-06 00:11:38 +00:00
|
|
|
msg := []byte("Some msg")
|
2020-05-07 12:05:53 +00:00
|
|
|
sig := sk.Sign(msg)
|
2019-08-06 00:11:38 +00:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-07-18 16:31:42 +00:00
|
|
|
require.Equal(b, true, sig.Verify(sk.PublicKey(), msg))
|
2019-08-06 00:11:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 12:05:53 +00:00
|
|
|
func BenchmarkSignature_AggregateVerify(b *testing.B) {
|
2019-08-06 00:11:38 +00:00
|
|
|
sigN := 128 // MAX_ATTESTATIONS per block.
|
|
|
|
|
2020-06-25 00:47:51 +00:00
|
|
|
var pks []iface.PublicKey
|
|
|
|
var sigs []iface.Signature
|
2020-05-07 12:05:53 +00:00
|
|
|
var msgs [][32]byte
|
2019-08-06 00:11:38 +00:00
|
|
|
for i := 0; i < sigN; i++ {
|
2020-05-07 12:05:53 +00:00
|
|
|
msg := [32]byte{'s', 'i', 'g', 'n', 'e', 'd', byte(i)}
|
2020-06-25 00:47:51 +00:00
|
|
|
sk := herumi.RandKey()
|
2020-05-07 12:05:53 +00:00
|
|
|
sig := sk.Sign(msg[:])
|
2019-08-06 00:11:38 +00:00
|
|
|
pks = append(pks, sk.PublicKey())
|
2020-05-07 12:05:53 +00:00
|
|
|
sigs = append(sigs, sig)
|
|
|
|
msgs = append(msgs, msg)
|
2019-08-06 00:11:38 +00:00
|
|
|
}
|
2020-06-25 00:47:51 +00:00
|
|
|
aggregated := herumi.Aggregate(sigs)
|
2019-08-06 00:11:38 +00:00
|
|
|
|
|
|
|
b.ResetTimer()
|
2019-10-07 07:50:58 +00:00
|
|
|
b.ReportAllocs()
|
2019-08-06 00:11:38 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
2020-07-18 16:31:42 +00:00
|
|
|
require.Equal(b, true, aggregated.AggregateVerify(pks, msgs))
|
2019-08-06 00:11:38 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-22 23:40:39 +00:00
|
|
|
|
|
|
|
func BenchmarkSecretKey_Marshal(b *testing.B) {
|
2020-06-25 00:47:51 +00:00
|
|
|
key := herumi.RandKey()
|
2020-03-22 23:40:39 +00:00
|
|
|
d := key.Marshal()
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-06-25 00:47:51 +00:00
|
|
|
_, err := herumi.SecretKeyFromBytes(d)
|
2020-05-07 12:05:53 +00:00
|
|
|
_ = err
|
2020-03-22 23:40:39 +00:00
|
|
|
}
|
|
|
|
}
|