-
Notifications
You must be signed in to change notification settings - Fork 524
Add batch verification #2578
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
Add batch verification #2578
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
89981d0
a87eb20
Payset module now enqueue transactions into the batch verifier
96704f5
b82a9fa
c6c1fd4
Merge branch 'algorand:master' into add-batch-verification
id-ms 2571f7b
Merge branch 'algorand:master' into add-batch-verification
id-ms 3d1a102
Merge branch 'algorand:master' into add-batch-verification
id-ms 4ddff84
Merge branch 'algorand:master' into add-batch-verification
id-ms ca33235
fixing lint
2483487
Merge branch 'add-batch-verification' of github.com:algoidan/go-algor…
3997626
adding more mutlisig tests
978bc0c
Merge branch 'algorand:master' into add-batch-verification
id-ms b39af7d
fixing some CRs + using the batchverifier for every txn check
7bcbaee
optimizing the errors
e97e7ae
Merge branch 'algorand:master' into add-batch-verification
id-ms 76d1979
Merge branch 'add-batch-verification' of github.com:algoidan/go-algor…
b49ffc1
06baf86
Merge branch 'algorand:master' into add-batch-verification
id-ms 9cfdc96
Merge branch 'algorand:master' into add-batch-verification
id-ms a3b2930
Merge branch 'algorand:master' into add-batch-verification
id-ms 9aca551
Merge branch 'algorand:master' into add-batch-verification
id-ms 65d9f57
fixing errors according to convention. + fix typo
437ca32
Merge branch 'algorand:master' into add-batch-verification
id-ms 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| // Copyright (C) 2019-2021 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 "errors" | ||
|
|
||
| // BatchVerifier enqueues signatures to be validated in batch. | ||
| type BatchVerifier struct { | ||
| messages []Hashable // contains a slice of messages to be hashed. Each message is varible length | ||
| publicKeys []SignatureVerifier // contains a slice of public keys. Each individual public key is 32 bytes. | ||
| signatures []Signature // contains a slice of signatures keys. Each individual signature is 64 bytes. | ||
| } | ||
|
|
||
| const minBatchVerifierAlloc = 16 | ||
|
|
||
| // Batch verifications errors | ||
| var ( | ||
| ErrBatchVerificationFailed = errors.New("At least on signature didn't pass verification") | ||
| ErrZeroTranscationsInBatch = errors.New("Could not validate empty signature set") | ||
| ) | ||
|
|
||
| // MakeBatchVerifierDefaultSize create a BatchVerifier instance. This function pre-allocates | ||
| // amount of free space to enqueue signatures without exapneding | ||
| func MakeBatchVerifierDefaultSize() *BatchVerifier { | ||
| return MakeBatchVerifier(minBatchVerifierAlloc) | ||
| } | ||
|
|
||
| // MakeBatchVerifier create a BatchVerifier instance. This function pre-allocates | ||
| // a given space so it will not expaned the storage | ||
| func MakeBatchVerifier(hint int) *BatchVerifier { | ||
| // preallocate enough storage for the expected usage. We will reallocate as needed. | ||
| if hint < minBatchVerifierAlloc { | ||
| hint = minBatchVerifierAlloc | ||
| } | ||
| return &BatchVerifier{ | ||
| messages: make([]Hashable, 0, hint), | ||
| publicKeys: make([]SignatureVerifier, 0, hint), | ||
| signatures: make([]Signature, 0, hint), | ||
| } | ||
| } | ||
|
|
||
| // EnqueueSignature enqueues a signature to be enqueued | ||
| func (b *BatchVerifier) EnqueueSignature(sigVerifier SignatureVerifier, message Hashable, sig Signature) { | ||
| // do we need to reallocate ? | ||
| if len(b.messages) == cap(b.messages) { | ||
| b.expand() | ||
| } | ||
| b.messages = append(b.messages, message) | ||
| b.publicKeys = append(b.publicKeys, sigVerifier) | ||
| b.signatures = append(b.signatures, sig) | ||
| } | ||
|
|
||
| func (b *BatchVerifier) expand() { | ||
| messages := make([]Hashable, len(b.messages), len(b.messages)*2) | ||
| publicKeys := make([]SignatureVerifier, len(b.publicKeys), len(b.publicKeys)*2) | ||
| signatures := make([]Signature, len(b.signatures), len(b.signatures)*2) | ||
| copy(messages, b.messages) | ||
| copy(publicKeys, b.publicKeys) | ||
| copy(signatures, b.signatures) | ||
| b.messages = messages | ||
| b.publicKeys = publicKeys | ||
| b.signatures = signatures | ||
| } | ||
|
|
||
| // GetNumberOfEnqueuedSignatures returns the number of signatures current enqueue onto the bacth verifier object | ||
| func (b *BatchVerifier) GetNumberOfEnqueuedSignatures() int { | ||
| return len(b.messages) | ||
| } | ||
|
|
||
| // Verify verifies that all the signatures are valid. in that case nil is returned | ||
| // if the batch is zero an appropriate error is return. | ||
| func (b *BatchVerifier) Verify() error { | ||
| if b.GetNumberOfEnqueuedSignatures() == 0 { | ||
| return ErrZeroTranscationsInBatch | ||
| } | ||
|
|
||
| for i := range b.messages { | ||
| verifier := SignatureVerifier(b.publicKeys[i]) | ||
| if !verifier.Verify(b.messages[i], b.signatures[i]) { | ||
| return ErrBatchVerificationFailed | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
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,119 @@ | ||
| // Copyright (C) 2019-2021 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 ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestBatchVerifierSingle(t *testing.T) { | ||
| // test expected success | ||
| bv := MakeBatchVerifier(1) | ||
| msg := randString() | ||
| var s Seed | ||
| RandBytes(s[:]) | ||
| sigSecrets := GenerateSignatureSecrets(s) | ||
| sig := sigSecrets.Sign(msg) | ||
| bv.EnqueueSignature(sigSecrets.SignatureVerifier, msg, sig) | ||
| require.NoError(t, bv.Verify()) | ||
|
|
||
| // test expected failuire | ||
| bv = MakeBatchVerifier(1) | ||
| msg = randString() | ||
| RandBytes(s[:]) | ||
| sigSecrets = GenerateSignatureSecrets(s) | ||
| sig = sigSecrets.Sign(msg) | ||
| // break the signature: | ||
| sig[0] = sig[0] + 1 | ||
| bv.EnqueueSignature(sigSecrets.SignatureVerifier, msg, sig) | ||
| require.Error(t, bv.Verify()) | ||
| } | ||
|
|
||
| func TestBatchVerifierBulk(t *testing.T) { | ||
| for i := 1; i < 64*2+3; i++ { | ||
| n := i | ||
| bv := MakeBatchVerifier(n) | ||
| var s Seed | ||
|
|
||
| for i := 0; i < n; i++ { | ||
| msg := randString() | ||
| RandBytes(s[:]) | ||
| sigSecrets := GenerateSignatureSecrets(s) | ||
id-ms marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| sig := sigSecrets.Sign(msg) | ||
| bv.EnqueueSignature(sigSecrets.SignatureVerifier, msg, sig) | ||
| } | ||
| require.Equal(t, n, bv.GetNumberOfEnqueuedSignatures()) | ||
| require.NoError(t, bv.Verify()) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| func TestBatchVerifierBulkWithExpand(t *testing.T) { | ||
id-ms marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| n := 64 | ||
| bv := MakeBatchVerifier(1) | ||
| var s Seed | ||
| RandBytes(s[:]) | ||
|
|
||
| for i := 0; i < n; i++ { | ||
| msg := randString() | ||
| sigSecrets := GenerateSignatureSecrets(s) | ||
| sig := sigSecrets.Sign(msg) | ||
| bv.EnqueueSignature(sigSecrets.SignatureVerifier, msg, sig) | ||
| } | ||
| require.NoError(t, bv.Verify()) | ||
| } | ||
|
|
||
| func TestBatchVerifierWithInvalidSiganture(t *testing.T) { | ||
| n := 64 | ||
| bv := MakeBatchVerifier(1) | ||
| var s Seed | ||
| RandBytes(s[:]) | ||
|
|
||
| for i := 0; i < n-1; i++ { | ||
| msg := randString() | ||
| sigSecrets := GenerateSignatureSecrets(s) | ||
| sig := sigSecrets.Sign(msg) | ||
| bv.EnqueueSignature(sigSecrets.SignatureVerifier, msg, sig) | ||
| } | ||
|
|
||
| msg := randString() | ||
| sigSecrets := GenerateSignatureSecrets(s) | ||
| sig := sigSecrets.Sign(msg) | ||
| sig[0] = sig[0] + 1 | ||
| bv.EnqueueSignature(sigSecrets.SignatureVerifier, msg, sig) | ||
|
|
||
| require.Error(t, bv.Verify()) | ||
| } | ||
|
|
||
| func BenchmarkBatchVerifier(b *testing.B) { | ||
| c := makeCurve25519Secret() | ||
| bv := MakeBatchVerifier(1) | ||
| for i := 0; i < b.N; i++ { | ||
| str := randString() | ||
| bv.EnqueueSignature(c.SignatureVerifier, str, c.Sign(str)) | ||
| } | ||
|
|
||
| b.ResetTimer() | ||
| require.NoError(b, bv.Verify()) | ||
| } | ||
|
|
||
| func TestEmpty(t *testing.T) { | ||
| bv := MakeBatchVerifierDefaultSize() | ||
| require.Error(t, bv.Verify()) | ||
| } | ||
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
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.