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.

46 lines
934 B

  1. package crypto
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/stretchr/testify/require"
  6. )
  7. func TestSignAndValidateEd25519(t *testing.T) {
  8. privKey := GenPrivKeyEd25519()
  9. pubKey := privKey.PubKey()
  10. msg := CRandBytes(128)
  11. sig, err := privKey.Sign(msg)
  12. require.Nil(t, err)
  13. // Test the signature
  14. assert.True(t, pubKey.VerifyBytes(msg, sig))
  15. // Mutate the signature, just one bit.
  16. sigEd := sig.(SignatureEd25519)
  17. sigEd[7] ^= byte(0x01)
  18. sig = sigEd
  19. assert.False(t, pubKey.VerifyBytes(msg, sig))
  20. }
  21. func TestSignAndValidateSecp256k1(t *testing.T) {
  22. privKey := GenPrivKeySecp256k1()
  23. pubKey := privKey.PubKey()
  24. msg := CRandBytes(128)
  25. sig, err := privKey.Sign(msg)
  26. require.Nil(t, err)
  27. assert.True(t, pubKey.VerifyBytes(msg, sig))
  28. // Mutate the signature, just one bit.
  29. sigEd := sig.(SignatureSecp256k1)
  30. sigEd[3] ^= byte(0x01)
  31. sig = sigEd
  32. assert.False(t, pubKey.VerifyBytes(msg, sig))
  33. }