From d27a71b3b49231cda90ab1aafbc481bf77831eb6 Mon Sep 17 00:00:00 2001 From: Patrick O'Grady Date: Sun, 21 Jan 2024 07:46:05 -0800 Subject: [PATCH] [crypto/ed25519] Preallocate batch size for verification (#695) * preallocate batch size for verification * fix tests --- crypto/ed25519/ed25519.go | 5 ++--- crypto/ed25519/ed25519_test.go | 8 ++++---- examples/morpheusvm/auth/ed25519.go | 4 ++-- examples/morpheusvm/go.mod | 2 +- examples/morpheusvm/go.sum | 4 ++-- examples/tokenvm/auth/ed25519.go | 4 ++-- examples/tokenvm/go.mod | 2 +- examples/tokenvm/go.sum | 4 ++-- go.mod | 2 +- go.sum | 4 ++-- 10 files changed, 19 insertions(+), 20 deletions(-) diff --git a/crypto/ed25519/ed25519.go b/crypto/ed25519/ed25519.go index 00f1139f8e..e942b67da8 100644 --- a/crypto/ed25519/ed25519.go +++ b/crypto/ed25519/ed25519.go @@ -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) { diff --git a/crypto/ed25519/ed25519_test.go b/crypto/ed25519/ed25519_test.go index f302f3eb8f..f0904d50d3 100644 --- a/crypto/ed25519/ed25519_test.go +++ b/crypto/ed25519/ed25519_test.go @@ -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]) } @@ -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]) } @@ -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]) } @@ -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]) } diff --git a/examples/morpheusvm/auth/ed25519.go b/examples/morpheusvm/auth/ed25519.go index be0c8d0a7e..98b93d10ac 100644 --- a/examples/morpheusvm/auth/ed25519.go +++ b/examples/morpheusvm/auth/ed25519.go @@ -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++ @@ -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() } diff --git a/examples/morpheusvm/go.mod b/examples/morpheusvm/go.mod index 7049274691..af76a48f7d 100644 --- a/examples/morpheusvm/go.mod +++ b/examples/morpheusvm/go.mod @@ -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 diff --git a/examples/morpheusvm/go.sum b/examples/morpheusvm/go.sum index 35f64fd911..aa63609e14 100644 --- a/examples/morpheusvm/go.sum +++ b/examples/morpheusvm/go.sum @@ -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= diff --git a/examples/tokenvm/auth/ed25519.go b/examples/tokenvm/auth/ed25519.go index c4c1634351..a1079b0e77 100644 --- a/examples/tokenvm/auth/ed25519.go +++ b/examples/tokenvm/auth/ed25519.go @@ -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++ @@ -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() } diff --git a/examples/tokenvm/go.mod b/examples/tokenvm/go.mod index 8be906041e..473dbb47f7 100644 --- a/examples/tokenvm/go.mod +++ b/examples/tokenvm/go.mod @@ -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 diff --git a/examples/tokenvm/go.sum b/examples/tokenvm/go.sum index 80df850cf9..d527fa6fc2 100644 --- a/examples/tokenvm/go.sum +++ b/examples/tokenvm/go.sum @@ -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= diff --git a/go.mod b/go.mod index 95362a0eb2..56adf345c9 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 78d3de0a7f..ba7a0a6270 100644 --- a/go.sum +++ b/go.sum @@ -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=