Skip to content

Commit d27a71b

Browse files
[crypto/ed25519] Preallocate batch size for verification (#695)
* preallocate batch size for verification * fix tests
1 parent 1cb53fe commit d27a71b

File tree

10 files changed

+19
-20
lines changed

10 files changed

+19
-20
lines changed

crypto/ed25519/ed25519.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,8 @@ type Batch struct {
7878
bv ed25519consensus.BatchVerifier
7979
}
8080

81-
func NewBatch() *Batch {
82-
// TODO: add support for pre-allocating batch (#652)
83-
return &Batch{bv: ed25519consensus.NewBatchVerifier()}
81+
func NewBatch(size int) *Batch {
82+
return &Batch{bv: ed25519consensus.NewPreallocatedBatchVerifier(size)}
8483
}
8584

8685
func (b *Batch) Add(msg []byte, p PublicKey, s Signature) {

crypto/ed25519/ed25519_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func TestBatchAddVerifyValid(t *testing.T) {
138138
sig := Sign(msg, priv)
139139
sigs[i] = sig
140140
}
141-
bv := NewBatch()
141+
bv := NewBatch(numItems)
142142
for i := 0; i < numItems; i++ {
143143
bv.Add(msgs[i], pubs[i], sigs[i])
144144
}
@@ -172,7 +172,7 @@ func TestBatchAddVerifyInvalid(t *testing.T) {
172172
}
173173
sigs[i] = sig
174174
}
175-
bv := NewBatch()
175+
bv := NewBatch(numItems)
176176
for i := 0; i < numItems; i++ {
177177
bv.Add(msgs[i], pubs[i], sigs[i])
178178
}
@@ -292,7 +292,7 @@ func BenchmarkConsensusBatchAddVerify(b *testing.B) {
292292
b.StartTimer()
293293
b.ReportAllocs()
294294
for i := 0; i < b.N; i++ {
295-
bv := ed25519consensus.NewBatchVerifier()
295+
bv := ed25519consensus.NewPreallocatedBatchVerifier(numItems)
296296
for j := 0; j < numItems; j++ {
297297
bv.Add(pubs[j], msgs[j], sigs[j])
298298
}
@@ -326,7 +326,7 @@ func BenchmarkConsensusBatchVerify(b *testing.B) {
326326
sig := ed25519.Sign(priv, msg)
327327
sigs[i] = sig
328328
}
329-
bv := ed25519consensus.NewBatchVerifier()
329+
bv := ed25519consensus.NewPreallocatedBatchVerifier(numItems)
330330
for i := 0; i < numItems; i++ {
331331
bv.Add(pubs[i], msgs[i], sigs[i])
332332
}

examples/morpheusvm/auth/ed25519.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ type ED25519Batch struct {
127127
func (b *ED25519Batch) Add(msg []byte, rauth chain.Auth) func() error {
128128
auth := rauth.(*ED25519)
129129
if b.batch == nil {
130-
b.batch = ed25519.NewBatch()
130+
b.batch = ed25519.NewBatch(b.batchSize)
131131
}
132132
b.batch.Add(msg, auth.Signer, auth.Signature)
133133
b.counter++
@@ -137,7 +137,7 @@ func (b *ED25519Batch) Add(msg []byte, rauth chain.Auth) func() error {
137137
b.counter = 0
138138
if b.totalCounter < b.total {
139139
// don't create a new batch if we are done
140-
b.batch = ed25519.NewBatch()
140+
b.batch = ed25519.NewBatch(b.batchSize)
141141
}
142142
return last.VerifyAsync()
143143
}

examples/morpheusvm/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ require (
6565
github.com/hashicorp/go-bexpr v0.1.10 // indirect
6666
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d // indirect
6767
github.com/hashicorp/hcl v1.0.0 // indirect
68-
github.com/hdevalence/ed25519consensus v0.1.0 // indirect
68+
github.com/hdevalence/ed25519consensus v0.2.0 // indirect
6969
github.com/holiman/bloomfilter/v2 v2.0.3 // indirect
7070
github.com/holiman/uint256 v1.2.2-0.20230321075855-87b91420868c // indirect
7171
github.com/huin/goupnp v1.0.3 // indirect

examples/morpheusvm/go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,8 @@ github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d h1:dg1dEPuW
337337
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
338338
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
339339
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
340-
github.com/hdevalence/ed25519consensus v0.1.0 h1:jtBwzzcHuTmFrQN6xQZn6CQEO/V9f7HsjsjeEZ6auqU=
341-
github.com/hdevalence/ed25519consensus v0.1.0/go.mod h1:w3BHWjwJbFU29IRHL1Iqkw3sus+7FctEyM4RqDxYNzo=
340+
github.com/hdevalence/ed25519consensus v0.2.0 h1:37ICyZqdyj0lAZ8P4D1d1id3HqbbG1N3iBb1Tb4rdcU=
341+
github.com/hdevalence/ed25519consensus v0.2.0/go.mod h1:w3BHWjwJbFU29IRHL1Iqkw3sus+7FctEyM4RqDxYNzo=
342342
github.com/holiman/bloomfilter/v2 v2.0.3 h1:73e0e/V0tCydx14a0SCYS/EWCxgwLZ18CZcZKVu0fao=
343343
github.com/holiman/bloomfilter/v2 v2.0.3/go.mod h1:zpoh+gs7qcpqrHr3dB55AMiJwo0iURXE7ZOP9L9hSkA=
344344
github.com/holiman/uint256 v1.2.2-0.20230321075855-87b91420868c h1:DZfsyhDK1hnSS5lH8l+JggqzEleHteTYfutAiVlSUM8=

examples/tokenvm/auth/ed25519.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ type ED25519Batch struct {
126126
func (b *ED25519Batch) Add(msg []byte, rauth chain.Auth) func() error {
127127
auth := rauth.(*ED25519)
128128
if b.batch == nil {
129-
b.batch = ed25519.NewBatch()
129+
b.batch = ed25519.NewBatch(b.batchSize)
130130
}
131131
b.batch.Add(msg, auth.Signer, auth.Signature)
132132
b.counter++
@@ -136,7 +136,7 @@ func (b *ED25519Batch) Add(msg []byte, rauth chain.Auth) func() error {
136136
b.counter = 0
137137
if b.totalCounter < b.total {
138138
// don't create a new batch if we are done
139-
b.batch = ed25519.NewBatch()
139+
b.batch = ed25519.NewBatch(b.batchSize)
140140
}
141141
return last.VerifyAsync()
142142
}

examples/tokenvm/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ require (
6969
github.com/hashicorp/go-bexpr v0.1.10 // indirect
7070
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d // indirect
7171
github.com/hashicorp/hcl v1.0.0 // indirect
72-
github.com/hdevalence/ed25519consensus v0.1.0 // indirect
72+
github.com/hdevalence/ed25519consensus v0.2.0 // indirect
7373
github.com/holiman/bloomfilter/v2 v2.0.3 // indirect
7474
github.com/holiman/uint256 v1.2.2-0.20230321075855-87b91420868c // indirect
7575
github.com/huin/goupnp v1.0.3 // indirect

examples/tokenvm/go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,8 @@ github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d h1:dg1dEPuW
339339
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
340340
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
341341
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
342-
github.com/hdevalence/ed25519consensus v0.1.0 h1:jtBwzzcHuTmFrQN6xQZn6CQEO/V9f7HsjsjeEZ6auqU=
343-
github.com/hdevalence/ed25519consensus v0.1.0/go.mod h1:w3BHWjwJbFU29IRHL1Iqkw3sus+7FctEyM4RqDxYNzo=
342+
github.com/hdevalence/ed25519consensus v0.2.0 h1:37ICyZqdyj0lAZ8P4D1d1id3HqbbG1N3iBb1Tb4rdcU=
343+
github.com/hdevalence/ed25519consensus v0.2.0/go.mod h1:w3BHWjwJbFU29IRHL1Iqkw3sus+7FctEyM4RqDxYNzo=
344344
github.com/holiman/bloomfilter/v2 v2.0.3 h1:73e0e/V0tCydx14a0SCYS/EWCxgwLZ18CZcZKVu0fao=
345345
github.com/holiman/bloomfilter/v2 v2.0.3/go.mod h1:zpoh+gs7qcpqrHr3dB55AMiJwo0iURXE7ZOP9L9hSkA=
346346
github.com/holiman/uint256 v1.2.2-0.20230321075855-87b91420868c h1:DZfsyhDK1hnSS5lH8l+JggqzEleHteTYfutAiVlSUM8=

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require (
1212
github.com/gorilla/mux v1.8.0
1313
github.com/gorilla/rpc v1.2.0
1414
github.com/gorilla/websocket v1.5.0
15-
github.com/hdevalence/ed25519consensus v0.1.0
15+
github.com/hdevalence/ed25519consensus v0.2.0
1616
github.com/manifoldco/promptui v0.9.0
1717
github.com/near/borsh-go v0.3.1
1818
github.com/neilotoole/errgroup v0.1.6

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,8 @@ github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09
260260
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
261261
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
262262
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
263-
github.com/hdevalence/ed25519consensus v0.1.0 h1:jtBwzzcHuTmFrQN6xQZn6CQEO/V9f7HsjsjeEZ6auqU=
264-
github.com/hdevalence/ed25519consensus v0.1.0/go.mod h1:w3BHWjwJbFU29IRHL1Iqkw3sus+7FctEyM4RqDxYNzo=
263+
github.com/hdevalence/ed25519consensus v0.2.0 h1:37ICyZqdyj0lAZ8P4D1d1id3HqbbG1N3iBb1Tb4rdcU=
264+
github.com/hdevalence/ed25519consensus v0.2.0/go.mod h1:w3BHWjwJbFU29IRHL1Iqkw3sus+7FctEyM4RqDxYNzo=
265265
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
266266
github.com/hydrogen18/memlistener v0.0.0-20200120041712-dcc25e7acd91/go.mod h1:qEIFzExnS6016fRpRfxrExeVn2gbClQA99gQhnIcdhE=
267267
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=

0 commit comments

Comments
 (0)