-
Notifications
You must be signed in to change notification settings - Fork 524
crypto: add go-based BatchVerifier implementation #6440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0795529
add gobatchverifier implementation
cce c1acd6c
fix comment
cce b809fa7
fix shuffling in testBatchVectors
cce f999d8b
fix lint
cce 9fad317
Apply suggestions from code review
cce 1d30606
add test and vectors based on go/src/crypto/ed25519/ed25519vectors_te…
cce 88a5b94
CR fixes
cce acc256d
CR fixes
cce dd9c530
CR fixes
cce f317118
use [32]byte for hasSmallOrder and isCanonicalPoint
cce 0e95eac
update ed25519ConsensusVerifySingle for [32]byte
cce File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| // Copyright (C) 2019-2025 Algorand, Inc. | ||
| // This file is part of go-algorand | ||
| // | ||
| // go-algorand is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU Affero General Public License as | ||
| // published by the Free Software Foundation, either version 3 of the | ||
| // License, or (at your option) any later version. | ||
| // | ||
| // go-algorand is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU Affero General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU Affero General Public License | ||
| // along with go-algorand. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| package crypto | ||
|
|
||
| import ( | ||
| cryptorand "crypto/rand" | ||
| "io" | ||
| "testing" | ||
|
|
||
| "github.com/algorand/go-algorand/test/partitiontest" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func randSignedMsg(t testing.TB, r io.Reader) (SignatureVerifier, Hashable, Signature) { | ||
| mlen := 100 | ||
| msg := TestingHashable{data: make([]byte, mlen)} | ||
| n, err := r.Read(msg.data) | ||
| require.NoError(t, err) | ||
| require.Equal(t, n, mlen) | ||
| var s Seed | ||
| n, err = r.Read(s[:]) | ||
| require.NoError(t, err) | ||
| require.Equal(t, 32, n) | ||
| secrets := GenerateSignatureSecrets(s) | ||
| return secrets.SignatureVerifier, msg, secrets.Sign(msg) | ||
| } | ||
|
|
||
| // BenchmarkBatchVerifierImpls benchmarks different batch verification implementations | ||
| // with realistic batch sizes (100 batches of 64 signatures each) | ||
| func BenchmarkBatchVerifierImpls(b *testing.B) { | ||
| partitiontest.PartitionTest(b) | ||
|
|
||
| numBatches := 100 | ||
| batchSize := 64 | ||
| msgs := make([][]Hashable, numBatches) | ||
| pks := make([][]SignatureVerifier, numBatches) | ||
| sigs := make([][]Signature, numBatches) | ||
| r := cryptorand.Reader | ||
| for i := 0; i < numBatches; i++ { | ||
| for j := 0; j < batchSize; j++ { | ||
| pk, msg, sig := randSignedMsg(b, r) | ||
| msgs[i] = append(msgs[i], msg) | ||
| pks[i] = append(pks[i], pk) | ||
| sigs[i] = append(sigs[i], sig) | ||
| } | ||
| } | ||
|
|
||
| b.Log("running with", b.N, "iterations using", len(msgs), "batches of", batchSize, "signatures") | ||
| runImpl := func(b *testing.B, bv BatchVerifier, | ||
| msgs [][]Hashable, pks [][]SignatureVerifier, sigs [][]Signature) { | ||
| b.ResetTimer() | ||
| for i := 0; i < b.N; i++ { | ||
| batchIdx := i % numBatches | ||
| for j := range msgs[batchIdx] { | ||
| bv.EnqueueSignature(pks[batchIdx][j], msgs[batchIdx][j], sigs[batchIdx][j]) | ||
| } | ||
| require.NoError(b, bv.Verify()) | ||
| } | ||
| } | ||
|
|
||
| b.Run("libsodium_single", func(b *testing.B) { | ||
| bv := makeLibsodiumBatchVerifier(batchSize) | ||
| bv.(*cgoBatchVerifier).useSingle = true | ||
| runImpl(b, bv, msgs, pks, sigs) | ||
| }) | ||
| b.Run("libsodium_batch", func(b *testing.B) { | ||
| bv := makeLibsodiumBatchVerifier(batchSize) | ||
| bv.(*cgoBatchVerifier).useSingle = false | ||
| runImpl(b, bv, msgs, pks, sigs) | ||
| }) | ||
| b.Run("ed25519consensus", func(b *testing.B) { | ||
| bv := makeEd25519ConsensusBatchVerifier(batchSize) | ||
| runImpl(b, bv, msgs, pks, sigs) | ||
| }) | ||
| } | ||
|
|
||
| func BenchmarkCanonicalityCheck(b *testing.B) { | ||
| partitiontest.PartitionTest(b) | ||
|
|
||
| const maxN = 10000 | ||
| pubkeys := make([]SignatureVerifier, maxN) | ||
| sigs := make([]Signature, maxN) | ||
| for i := 0; i < maxN; i++ { | ||
| var s Seed | ||
| RandBytes(s[:]) | ||
| sigSecrets := GenerateSignatureSecrets(s) | ||
| pubkeys[i] = sigSecrets.SignatureVerifier | ||
| msg := randString() | ||
| sigs[i] = sigSecrets.Sign(msg) | ||
| } | ||
|
|
||
| b.Run("pubkey_check", func(b *testing.B) { | ||
| for i := 0; i < b.N; i++ { | ||
| _ = isCanonicalPoint(pubkeys[i%maxN]) | ||
| } | ||
| }) | ||
|
|
||
| b.Run("signature_R_check", func(b *testing.B) { | ||
| for i := 0; i < b.N; i++ { | ||
| _ = isCanonicalPoint([32]byte(sigs[i%maxN][:32])) | ||
| } | ||
| }) | ||
|
|
||
| b.Run("both_checks", func(b *testing.B) { | ||
| for i := 0; i < b.N; i++ { | ||
| _ = !isCanonicalPoint(pubkeys[i%maxN]) || !isCanonicalPoint([32]byte(sigs[i%maxN][:32])) | ||
| } | ||
| }) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.