Skip to content

Commit

Permalink
[crypto/ed25519] Preallocate batch size for verification (#695)
Browse files Browse the repository at this point in the history
* preallocate batch size for verification

* fix tests
  • Loading branch information
patrick-ogrady authored Jan 21, 2024
1 parent 1cb53fe commit d27a71b
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 20 deletions.
5 changes: 2 additions & 3 deletions crypto/ed25519/ed25519.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,8 @@ type Batch struct {
bv ed25519consensus.BatchVerifier
}

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

func (b *Batch) Add(msg []byte, p PublicKey, s Signature) {
Expand Down
8 changes: 4 additions & 4 deletions crypto/ed25519/ed25519_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func TestBatchAddVerifyValid(t *testing.T) {
sig := Sign(msg, priv)
sigs[i] = sig
}
bv := NewBatch()
bv := NewBatch(numItems)
for i := 0; i < numItems; i++ {
bv.Add(msgs[i], pubs[i], sigs[i])
}
Expand Down Expand Up @@ -172,7 +172,7 @@ func TestBatchAddVerifyInvalid(t *testing.T) {
}
sigs[i] = sig
}
bv := NewBatch()
bv := NewBatch(numItems)
for i := 0; i < numItems; i++ {
bv.Add(msgs[i], pubs[i], sigs[i])
}
Expand Down Expand Up @@ -292,7 +292,7 @@ func BenchmarkConsensusBatchAddVerify(b *testing.B) {
b.StartTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
bv := ed25519consensus.NewBatchVerifier()
bv := ed25519consensus.NewPreallocatedBatchVerifier(numItems)
for j := 0; j < numItems; j++ {
bv.Add(pubs[j], msgs[j], sigs[j])
}
Expand Down Expand Up @@ -326,7 +326,7 @@ func BenchmarkConsensusBatchVerify(b *testing.B) {
sig := ed25519.Sign(priv, msg)
sigs[i] = sig
}
bv := ed25519consensus.NewBatchVerifier()
bv := ed25519consensus.NewPreallocatedBatchVerifier(numItems)
for i := 0; i < numItems; i++ {
bv.Add(pubs[i], msgs[i], sigs[i])
}
Expand Down
4 changes: 2 additions & 2 deletions examples/morpheusvm/auth/ed25519.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ type ED25519Batch struct {
func (b *ED25519Batch) Add(msg []byte, rauth chain.Auth) func() error {
auth := rauth.(*ED25519)
if b.batch == nil {
b.batch = ed25519.NewBatch()
b.batch = ed25519.NewBatch(b.batchSize)
}
b.batch.Add(msg, auth.Signer, auth.Signature)
b.counter++
Expand All @@ -137,7 +137,7 @@ func (b *ED25519Batch) Add(msg []byte, rauth chain.Auth) func() error {
b.counter = 0
if b.totalCounter < b.total {
// don't create a new batch if we are done
b.batch = ed25519.NewBatch()
b.batch = ed25519.NewBatch(b.batchSize)
}
return last.VerifyAsync()
}
Expand Down
2 changes: 1 addition & 1 deletion examples/morpheusvm/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ require (
github.com/hashicorp/go-bexpr v0.1.10 // indirect
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hdevalence/ed25519consensus v0.1.0 // indirect
github.com/hdevalence/ed25519consensus v0.2.0 // indirect
github.com/holiman/bloomfilter/v2 v2.0.3 // indirect
github.com/holiman/uint256 v1.2.2-0.20230321075855-87b91420868c // indirect
github.com/huin/goupnp v1.0.3 // indirect
Expand Down
4 changes: 2 additions & 2 deletions examples/morpheusvm/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,8 @@ github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d h1:dg1dEPuW
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hdevalence/ed25519consensus v0.1.0 h1:jtBwzzcHuTmFrQN6xQZn6CQEO/V9f7HsjsjeEZ6auqU=
github.com/hdevalence/ed25519consensus v0.1.0/go.mod h1:w3BHWjwJbFU29IRHL1Iqkw3sus+7FctEyM4RqDxYNzo=
github.com/hdevalence/ed25519consensus v0.2.0 h1:37ICyZqdyj0lAZ8P4D1d1id3HqbbG1N3iBb1Tb4rdcU=
github.com/hdevalence/ed25519consensus v0.2.0/go.mod h1:w3BHWjwJbFU29IRHL1Iqkw3sus+7FctEyM4RqDxYNzo=
github.com/holiman/bloomfilter/v2 v2.0.3 h1:73e0e/V0tCydx14a0SCYS/EWCxgwLZ18CZcZKVu0fao=
github.com/holiman/bloomfilter/v2 v2.0.3/go.mod h1:zpoh+gs7qcpqrHr3dB55AMiJwo0iURXE7ZOP9L9hSkA=
github.com/holiman/uint256 v1.2.2-0.20230321075855-87b91420868c h1:DZfsyhDK1hnSS5lH8l+JggqzEleHteTYfutAiVlSUM8=
Expand Down
4 changes: 2 additions & 2 deletions examples/tokenvm/auth/ed25519.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ type ED25519Batch struct {
func (b *ED25519Batch) Add(msg []byte, rauth chain.Auth) func() error {
auth := rauth.(*ED25519)
if b.batch == nil {
b.batch = ed25519.NewBatch()
b.batch = ed25519.NewBatch(b.batchSize)
}
b.batch.Add(msg, auth.Signer, auth.Signature)
b.counter++
Expand All @@ -136,7 +136,7 @@ func (b *ED25519Batch) Add(msg []byte, rauth chain.Auth) func() error {
b.counter = 0
if b.totalCounter < b.total {
// don't create a new batch if we are done
b.batch = ed25519.NewBatch()
b.batch = ed25519.NewBatch(b.batchSize)
}
return last.VerifyAsync()
}
Expand Down
2 changes: 1 addition & 1 deletion examples/tokenvm/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ require (
github.com/hashicorp/go-bexpr v0.1.10 // indirect
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hdevalence/ed25519consensus v0.1.0 // indirect
github.com/hdevalence/ed25519consensus v0.2.0 // indirect
github.com/holiman/bloomfilter/v2 v2.0.3 // indirect
github.com/holiman/uint256 v1.2.2-0.20230321075855-87b91420868c // indirect
github.com/huin/goupnp v1.0.3 // indirect
Expand Down
4 changes: 2 additions & 2 deletions examples/tokenvm/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,8 @@ github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d h1:dg1dEPuW
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hdevalence/ed25519consensus v0.1.0 h1:jtBwzzcHuTmFrQN6xQZn6CQEO/V9f7HsjsjeEZ6auqU=
github.com/hdevalence/ed25519consensus v0.1.0/go.mod h1:w3BHWjwJbFU29IRHL1Iqkw3sus+7FctEyM4RqDxYNzo=
github.com/hdevalence/ed25519consensus v0.2.0 h1:37ICyZqdyj0lAZ8P4D1d1id3HqbbG1N3iBb1Tb4rdcU=
github.com/hdevalence/ed25519consensus v0.2.0/go.mod h1:w3BHWjwJbFU29IRHL1Iqkw3sus+7FctEyM4RqDxYNzo=
github.com/holiman/bloomfilter/v2 v2.0.3 h1:73e0e/V0tCydx14a0SCYS/EWCxgwLZ18CZcZKVu0fao=
github.com/holiman/bloomfilter/v2 v2.0.3/go.mod h1:zpoh+gs7qcpqrHr3dB55AMiJwo0iURXE7ZOP9L9hSkA=
github.com/holiman/uint256 v1.2.2-0.20230321075855-87b91420868c h1:DZfsyhDK1hnSS5lH8l+JggqzEleHteTYfutAiVlSUM8=
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/gorilla/mux v1.8.0
github.com/gorilla/rpc v1.2.0
github.com/gorilla/websocket v1.5.0
github.com/hdevalence/ed25519consensus v0.1.0
github.com/hdevalence/ed25519consensus v0.2.0
github.com/manifoldco/promptui v0.9.0
github.com/near/borsh-go v0.3.1
github.com/neilotoole/errgroup v0.1.6
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hdevalence/ed25519consensus v0.1.0 h1:jtBwzzcHuTmFrQN6xQZn6CQEO/V9f7HsjsjeEZ6auqU=
github.com/hdevalence/ed25519consensus v0.1.0/go.mod h1:w3BHWjwJbFU29IRHL1Iqkw3sus+7FctEyM4RqDxYNzo=
github.com/hdevalence/ed25519consensus v0.2.0 h1:37ICyZqdyj0lAZ8P4D1d1id3HqbbG1N3iBb1Tb4rdcU=
github.com/hdevalence/ed25519consensus v0.2.0/go.mod h1:w3BHWjwJbFU29IRHL1Iqkw3sus+7FctEyM4RqDxYNzo=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/hydrogen18/memlistener v0.0.0-20200120041712-dcc25e7acd91/go.mod h1:qEIFzExnS6016fRpRfxrExeVn2gbClQA99gQhnIcdhE=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
Expand Down

0 comments on commit d27a71b

Please sign in to comment.