forked from cometbft/cometbft
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from osmosis-labs/adam/batch-verfy-backport
chore: batch verify backport
- Loading branch information
Showing
20 changed files
with
1,135 additions
and
528 deletions.
There are no files selected for viewing
This file contains 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 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,32 @@ | ||
package batch | ||
|
||
import ( | ||
"github.com/cometbft/cometbft/crypto" | ||
"github.com/cometbft/cometbft/crypto/ed25519" | ||
"github.com/cometbft/cometbft/crypto/sr25519" | ||
) | ||
|
||
// CreateBatchVerifier checks if a key type implements the batch verifier interface. | ||
// Currently only ed25519 & sr25519 supports batch verification. | ||
func CreateBatchVerifier(pk crypto.PubKey) (crypto.BatchVerifier, bool) { | ||
switch pk.Type() { | ||
case ed25519.KeyType: | ||
return ed25519.NewBatchVerifier(), true | ||
case sr25519.KeyType: | ||
return sr25519.NewBatchVerifier(), true | ||
} | ||
|
||
// case where the key does not support batch verification | ||
return nil, false | ||
} | ||
|
||
// SupportsBatchVerifier checks if a key type implements the batch verifier | ||
// interface. | ||
func SupportsBatchVerifier(pk crypto.PubKey) bool { | ||
switch pk.Type() { | ||
case ed25519.KeyType, sr25519.KeyType: | ||
return true | ||
} | ||
|
||
return false | ||
} |
This file contains 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 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 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 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 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,46 @@ | ||
package sr25519 | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/oasisprotocol/curve25519-voi/primitives/sr25519" | ||
|
||
"github.com/cometbft/cometbft/crypto" | ||
) | ||
|
||
var _ crypto.BatchVerifier = &BatchVerifier{} | ||
|
||
// BatchVerifier implements batch verification for sr25519. | ||
type BatchVerifier struct { | ||
*sr25519.BatchVerifier | ||
} | ||
|
||
func NewBatchVerifier() crypto.BatchVerifier { | ||
return &BatchVerifier{sr25519.NewBatchVerifier()} | ||
} | ||
|
||
func (b *BatchVerifier) Add(key crypto.PubKey, msg, signature []byte) error { | ||
pk, ok := key.(PubKey) | ||
if !ok { | ||
return fmt.Errorf("sr25519: pubkey is not sr25519") | ||
} | ||
|
||
var srpk sr25519.PublicKey | ||
if err := srpk.UnmarshalBinary(pk); err != nil { | ||
return fmt.Errorf("sr25519: invalid public key: %w", err) | ||
} | ||
|
||
var sig sr25519.Signature | ||
if err := sig.UnmarshalBinary(signature); err != nil { | ||
return fmt.Errorf("sr25519: unable to decode signature: %w", err) | ||
} | ||
|
||
st := signingCtx.NewTranscriptBytes(msg) | ||
b.BatchVerifier.Add(&srpk, st, &sig) | ||
|
||
return nil | ||
} | ||
|
||
func (b *BatchVerifier) Verify() (bool, []bool) { | ||
return b.BatchVerifier.Verify(crypto.CReader()) | ||
} |
This file contains 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.