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.

56 lines
1.1 KiB

  1. package sr25519_test
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/stretchr/testify/require"
  6. "github.com/tendermint/tendermint/crypto"
  7. "github.com/tendermint/tendermint/crypto/sr25519"
  8. )
  9. func TestSignAndValidateSr25519(t *testing.T) {
  10. privKey := sr25519.GenPrivKey()
  11. pubKey := privKey.PubKey()
  12. msg := crypto.CRandBytes(128)
  13. sig, err := privKey.Sign(msg)
  14. require.Nil(t, err)
  15. // Test the signature
  16. assert.True(t, pubKey.VerifySignature(msg, sig))
  17. assert.True(t, pubKey.VerifySignature(msg, sig))
  18. // Mutate the signature, just one bit.
  19. // TODO: Replace this with a much better fuzzer, tendermint/ed25519/issues/10
  20. sig[7] ^= byte(0x01)
  21. assert.False(t, pubKey.VerifySignature(msg, sig))
  22. }
  23. func TestBatchSafe(t *testing.T) {
  24. v := sr25519.NewBatchVerifier()
  25. for i := 0; i <= 38; i++ {
  26. priv := sr25519.GenPrivKey()
  27. pub := priv.PubKey()
  28. var msg []byte
  29. if i%2 == 0 {
  30. msg = []byte("easter")
  31. } else {
  32. msg = []byte("egg")
  33. }
  34. sig, err := priv.Sign(msg)
  35. require.NoError(t, err)
  36. err = v.Add(pub, msg, sig)
  37. require.NoError(t, err)
  38. }
  39. if !v.Verify() {
  40. t.Error("failed batch verification")
  41. }
  42. }