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.

38 lines
1004 B

  1. // +build !libsecp256k1
  2. package secp256k1
  3. import (
  4. "testing"
  5. secp256k1 "github.com/btcsuite/btcd/btcec"
  6. "github.com/stretchr/testify/require"
  7. )
  8. // Ensure that signature verification works, and that
  9. // non-canonical signatures fail.
  10. // Note: run with CGO_ENABLED=0 or go test -tags !cgo.
  11. func TestSignatureVerificationAndRejectUpperS(t *testing.T) {
  12. msg := []byte("We have lingered long enough on the shores of the cosmic ocean.")
  13. for i := 0; i < 500; i++ {
  14. priv := GenPrivKey()
  15. sigStr, err := priv.Sign(msg)
  16. require.NoError(t, err)
  17. sig := signatureFromBytes(sigStr)
  18. require.False(t, sig.S.Cmp(secp256k1halfN) > 0)
  19. pub := priv.PubKey()
  20. require.True(t, pub.VerifyBytes(msg, sigStr))
  21. // malleate:
  22. sig.S.Sub(secp256k1.S256().CurveParams.N, sig.S)
  23. require.True(t, sig.S.Cmp(secp256k1halfN) > 0)
  24. malSigStr := serializeSig(sig)
  25. require.False(t, pub.VerifyBytes(msg, malSigStr),
  26. "VerifyBytes incorrect with malleated & invalid S. sig=%v, key=%v",
  27. sig,
  28. priv,
  29. )
  30. }
  31. }