You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.0 KiB

  1. package sr25519
  2. import (
  3. "fmt"
  4. schnorrkel "github.com/ChainSafe/go-schnorrkel"
  5. "github.com/tendermint/tendermint/crypto"
  6. )
  7. var _ crypto.BatchVerifier = BatchVerifier{}
  8. // BatchVerifier implements batch verification for sr25519.
  9. // https://github.com/ChainSafe/go-schnorrkel is used for batch verification
  10. type BatchVerifier struct {
  11. *schnorrkel.BatchVerifier
  12. }
  13. func NewBatchVerifier() crypto.BatchVerifier {
  14. return BatchVerifier{schnorrkel.NewBatchVerifier()}
  15. }
  16. func (b BatchVerifier) Add(key crypto.PubKey, msg, sig []byte) error {
  17. var sig64 [SignatureSize]byte
  18. copy(sig64[:], sig)
  19. signature := new(schnorrkel.Signature)
  20. err := signature.Decode(sig64)
  21. if err != nil {
  22. return fmt.Errorf("unable to decode signature: %w", err)
  23. }
  24. signingContext := schnorrkel.NewSigningContext([]byte{}, msg)
  25. var pk [PubKeySize]byte
  26. copy(pk[:], key.Bytes())
  27. return b.BatchVerifier.Add(signingContext, signature, schnorrkel.NewPublicKey(pk))
  28. }
  29. func (b BatchVerifier) Verify() bool {
  30. return b.BatchVerifier.Verify()
  31. }