github.com/Finschia/ostracon@v1.1.5/crypto/ed25519/migration_test.go (about)

     1  package ed25519_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  
     8  	"github.com/Finschia/ostracon/crypto/ed25519"
     9  	voivrf "github.com/oasisprotocol/curve25519-voi/primitives/ed25519/extra/ecvrf"
    10  
    11  	r2vrf "github.com/Finschia/ostracon/crypto/ed25519/internal/r2ishiguro"
    12  	r2vrftestutil "github.com/Finschia/ostracon/crypto/ed25519/internal/r2ishiguro/testutil"
    13  )
    14  
    15  func TestVerify(t *testing.T) {
    16  	pubkey, message := []byte("pubkey"), []byte("message")
    17  	valid, _ := ed25519.VRFVerify(pubkey, make([]byte, 1), message)
    18  	require.False(t, valid)
    19  
    20  	cases := map[string]struct {
    21  		proof []byte
    22  		valid bool
    23  	}{
    24  		"invalid format": {
    25  			proof: make([]byte, 1),
    26  		},
    27  		"voi invalid proof": {
    28  			proof: make([]byte, voivrf.ProofSize),
    29  		},
    30  		"r2ishiguro invalid proof": {
    31  			proof: make([]byte, r2vrf.ProofSize),
    32  		},
    33  	}
    34  
    35  	for name, tc := range cases {
    36  		t.Run(name, func(t *testing.T) {
    37  			valid, _ := ed25519.NewVersionedVrfNoProve().Verify(pubkey, tc.proof, message)
    38  			require.Equal(t, tc.valid, valid)
    39  		})
    40  	}
    41  }
    42  
    43  func TestProofToHash(t *testing.T) {
    44  	_, err := ed25519.ProofToHash(make([]byte, 1))
    45  	require.Error(t, err)
    46  
    47  	cases := map[string]struct {
    48  		proof []byte
    49  		valid bool
    50  	}{
    51  		"invalid format": {
    52  			proof: make([]byte, 1),
    53  		},
    54  		"voi invalid proof": {
    55  			proof: make([]byte, voivrf.ProofSize),
    56  			valid: true,
    57  		},
    58  		"r2ishiguro proof": {
    59  			proof: make([]byte, r2vrf.ProofSize),
    60  		},
    61  	}
    62  
    63  	for name, tc := range cases {
    64  		t.Run(name, func(t *testing.T) {
    65  			_, err := ed25519.NewVersionedVrfNoProve().ProofToHash(tc.proof)
    66  			if !tc.valid {
    67  				require.Error(t, err)
    68  				return
    69  			}
    70  			require.NoError(t, err)
    71  		})
    72  	}
    73  }
    74  
    75  func TestValidateProof(t *testing.T) {
    76  	cases := map[string]struct {
    77  		h     []byte
    78  		valid bool
    79  	}{
    80  		"empty proof": {
    81  			h:     []byte{},
    82  			valid: false,
    83  		},
    84  		"voi invalid proof": {
    85  			h:     make([]byte, voivrf.ProofSize),
    86  			valid: true,
    87  		},
    88  		"r2ishiguro proof": {
    89  			h:     make([]byte, r2vrf.ProofSize),
    90  			valid: true,
    91  		},
    92  		"invalid proof": {
    93  			h:     make([]byte, 1),
    94  			valid: false,
    95  		},
    96  	}
    97  
    98  	for name, tc := range cases {
    99  		t.Run(name, func(t *testing.T) {
   100  			err := ed25519.ValidateProof(tc.h)
   101  			if !tc.valid {
   102  				require.Error(t, err)
   103  				return
   104  			}
   105  			require.NoError(t, err)
   106  		})
   107  	}
   108  }
   109  
   110  func TestVersionControl(t *testing.T) {
   111  	vrf := ed25519.NewVersionedVrfNoProve()
   112  
   113  	privKey := ed25519.GenPrivKey()
   114  	message := []byte("hello, world")
   115  
   116  	// generate proofs
   117  	oldProof, err := r2vrftestutil.Prove(privKey, message)
   118  	require.NoError(t, err)
   119  	newProof, err := privKey.VRFProve(message)
   120  	require.NoError(t, err)
   121  
   122  	// old one is valid for now
   123  	_, err = vrf.ProofToHash(oldProof)
   124  	require.NoError(t, err)
   125  
   126  	// new one is valid
   127  	_, err = vrf.ProofToHash(newProof)
   128  	require.NoError(t, err)
   129  
   130  	// old one is not valid anymore
   131  	_, err = vrf.ProofToHash(oldProof)
   132  	require.Error(t, err)
   133  }