github.com/devwanda/aphelion-staking@v0.33.9/crypto/secp256k1/secp256k1_nocgo_test.go (about)

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