github.com/line/ostracon@v1.0.10-0.20230328032236-7f20145f065d/crypto/ed25519/ed25519_test.go (about)

     1  package ed25519_test
     2  
     3  import (
     4  	"encoding/hex"
     5  	"testing"
     6  
     7  	coniks "github.com/coniks-sys/coniks-go/crypto/vrf"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/line/ostracon/crypto"
    13  	"github.com/line/ostracon/crypto/ed25519"
    14  )
    15  
    16  func TestSignAndValidateEd25519(t *testing.T) {
    17  
    18  	privKey := ed25519.GenPrivKey()
    19  	pubKey := privKey.PubKey()
    20  
    21  	msg := crypto.CRandBytes(128)
    22  	sig, err := privKey.Sign(msg)
    23  	require.Nil(t, err)
    24  
    25  	// Test the signature
    26  	assert.True(t, pubKey.VerifySignature(msg, sig))
    27  
    28  	// Mutate the signature, just one bit.
    29  	// TODO: Replace this with a much better fuzzer, tendermint/ed25519/issues/10
    30  	sig[7] ^= byte(0x01)
    31  
    32  	assert.False(t, pubKey.VerifySignature(msg, sig))
    33  }
    34  
    35  func TestVRFProveAndVRFVerify(t *testing.T) {
    36  
    37  	privKey := ed25519.GenPrivKey()
    38  	pubKey := privKey.PubKey()
    39  
    40  	message, _ := hex.DecodeString("0000000000000000000000000000000000000000000000000000000000000000")
    41  	proof, err := privKey.VRFProve(message)
    42  	assert.Nil(t, err)
    43  	assert.NotNil(t, proof)
    44  
    45  	output, err := pubKey.VRFVerify(proof, message)
    46  	assert.Nil(t, err)
    47  	assert.NotNil(t, output)
    48  
    49  	// error
    50  	{
    51  		message, _ = hex.DecodeString("0000000000000000000000000000000000000000000000000000000000000001")
    52  		output, err = pubKey.VRFVerify(proof, message)
    53  		assert.NotNil(t, err)
    54  		assert.Nil(t, output)
    55  	}
    56  
    57  	// invalid
    58  	{
    59  		privateKey, _ := coniks.GenerateKey(nil)
    60  		copy(privKey[:], privateKey)
    61  		pubKey = privKey.PubKey()
    62  
    63  		proof, err = privKey.VRFProve(message)
    64  		assert.Nil(t, err)
    65  		assert.NotNil(t, proof)
    66  
    67  		output, err = pubKey.VRFVerify(proof, message)
    68  		assert.NotNil(t, err)
    69  		assert.Nil(t, output)
    70  	}
    71  }