github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/crypto/ed25519/ed25519_test.go (about)

     1  package ed25519_test
     2  
     3  import (
     4  	"encoding/hex"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/gnolang/gno/tm2/pkg/crypto"
    11  	"github.com/gnolang/gno/tm2/pkg/crypto/ed25519"
    12  )
    13  
    14  func TestSignAndValidateEd25519(t *testing.T) {
    15  	t.Parallel()
    16  
    17  	privKey := ed25519.GenPrivKey()
    18  	pubKey := privKey.PubKey()
    19  
    20  	msg := crypto.CRandBytes(128)
    21  	sig, err := privKey.Sign(msg)
    22  	require.Nil(t, err)
    23  
    24  	// Test the signature
    25  	assert.True(t, pubKey.VerifyBytes(msg, sig))
    26  
    27  	// Mutate the signature, just one bit.
    28  	// TODO: Replace this with a much better fuzzer, tendermint/ed25519/issues/10
    29  	sig[7] ^= byte(0x01)
    30  
    31  	assert.False(t, pubKey.VerifyBytes(msg, sig))
    32  }
    33  
    34  const (
    35  	privKeySecretGolden = "secret_golden"
    36  	msgGolden           = "msg_golden"
    37  	signedGolden        = "f9d4e6a665dfb6cd7e2fedf0d46a1725472e640a5e93d654ce4caa986e5defd23c8b3af76aa6e39c24c582f0ebee860f66254b29cf6d034ce461ae2773133703"
    38  )
    39  
    40  func TestSignAndVerifyGolden(t *testing.T) {
    41  	privKey := ed25519.GenPrivKeyFromSecret([]byte(privKeySecretGolden))
    42  	// pubKey := privKey.PubKey()
    43  	out, err := privKey.Sign([]byte(msgGolden))
    44  	require.NoError(t, err)
    45  
    46  	hexOut := hex.EncodeToString(out)
    47  	require.Equal(t, signedGolden, hexOut)
    48  }