Skip to content

Commit

Permalink
Add support for preallocating batch
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-ogrady committed Dec 8, 2023
1 parent 7232f88 commit 97fc320
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
9 changes: 9 additions & 0 deletions batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ func NewBatchVerifier() BatchVerifier {
}
}

// NewPreallocatedBatchVerifier creates a new BatchVerifier with
// a preallocated capacity. If you know the size of the batch you plan
// to create ahead of time, this can prevent needless memory copies.
func NewPreallocatedBatchVerifier(size int) BatchVerifier {
return BatchVerifier{
entries: make([]entry, 0, size),
}
}

// Add adds a (public key, message, sig) triple to the current batch.
func (v *BatchVerifier) Add(publicKey ed25519.PublicKey, message, sig []byte) {
// Compute the challenge scalar for this entry upfront, so that we don't
Expand Down
48 changes: 48 additions & 0 deletions batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,54 @@ func BenchmarkVerifyBatch(b *testing.B) {
}
}

func BenchmarkCreateBatch(b *testing.B) {
for _, n := range []int{1, 8, 64, 1024, 4096, 16384} {
b.Run(fmt.Sprint(n), func(b *testing.B) {
b.StopTimer()
msg := []byte("CreateBatch")
pubs := make([][]byte, n)
sigs := make([][]byte, n)
for i := 0; i < n; i++ {
pub, priv, _ := ed25519.GenerateKey(nil)
pubs[i] = pub
sigs[i] = ed25519.Sign(priv, msg)
}
b.StartTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
v := NewBatchVerifier()
for j := 0; j < n; j++ {
v.Add(pubs[j], msg, sigs[j])
}
}
})
}
}

func BenchmarkCreatePreallocatedBatch(b *testing.B) {
for _, n := range []int{1, 8, 64, 1024, 4096, 16384} {
b.Run(fmt.Sprint(n), func(b *testing.B) {
b.StopTimer()
msg := []byte("CreatePreallocatedBatch")
pubs := make([][]byte, n)
sigs := make([][]byte, n)
for i := 0; i < n; i++ {
pub, priv, _ := ed25519.GenerateKey(nil)
pubs[i] = pub
sigs[i] = ed25519.Sign(priv, msg)
}
b.StartTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
v := NewPreallocatedBatchVerifier(n)
for j := 0; j < n; j++ {
v.Add(pubs[j], msg, sigs[j])
}
}
})
}
}

// populateBatchVerifier populates a verifier with multiple entries
func populateBatchVerifier(t *testing.T, v *BatchVerifier) {
*v = NewBatchVerifier()
Expand Down

0 comments on commit 97fc320

Please sign in to comment.