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.

54 lines
1.1 KiB

  1. package ed25519_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/ed25519"
  8. )
  9. func TestSignAndValidateEd25519(t *testing.T) {
  10. privKey := ed25519.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. // Mutate the signature, just one bit.
  18. // TODO: Replace this with a much better fuzzer, tendermint/ed25519/issues/10
  19. sig[7] ^= byte(0x01)
  20. assert.False(t, pubKey.VerifySignature(msg, sig))
  21. }
  22. func TestBatchSafe(t *testing.T) {
  23. v := ed25519.NewBatchVerifier()
  24. for i := 0; i <= 38; i++ {
  25. priv := ed25519.GenPrivKey()
  26. pub := priv.PubKey()
  27. var msg []byte
  28. if i%2 == 0 {
  29. msg = []byte("easter")
  30. } else {
  31. msg = []byte("egg")
  32. }
  33. sig, err := priv.Sign(msg)
  34. require.NoError(t, err)
  35. err = v.Add(pub, msg, sig)
  36. require.NoError(t, err)
  37. }
  38. require.True(t, v.Verify())
  39. }