github.com/Finschia/ostracon@v1.1.5/crypto/ed25519/internal/r2ishiguro/vrf_test.go (about) 1 package r2ishiguro_test 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 10 "github.com/Finschia/ostracon/crypto/ed25519" 11 "github.com/Finschia/ostracon/crypto/ed25519/internal/r2ishiguro" 12 "github.com/Finschia/ostracon/crypto/ed25519/internal/r2ishiguro/testutil" 13 ) 14 15 func TestVerify(t *testing.T) { 16 privKey := ed25519.GenPrivKey() 17 pubKey := privKey.PubKey().Bytes() 18 message := []byte("hello, world") 19 20 proof, err := testutil.Prove(privKey, message) 21 assert.NoError(t, err) 22 assert.NotNil(t, proof) 23 24 cases := map[string]struct { 25 message []byte 26 valid bool 27 }{ 28 "valid": { 29 message: message, 30 valid: true, 31 }, 32 "invalid": { 33 message: []byte("deadbeef"), 34 }, 35 } 36 37 for name, tc := range cases { 38 t.Run(name, func(t *testing.T) { 39 valid, _ := r2ishiguro.Verify(pubKey, proof, tc.message) 40 require.Equal(t, tc.valid, valid) 41 }) 42 } 43 } 44 45 func TestProofToHash(t *testing.T) { 46 privKey := ed25519.GenPrivKey() 47 message := []byte("hello, world") 48 49 t.Run("to hash r2ishiguro proof", func(t *testing.T) { 50 proof, err := testutil.Prove(privKey, message) 51 require.NoError(t, err) 52 require.NotNil(t, proof) 53 54 output, err := r2ishiguro.ProofToHash(proof) 55 require.NoError(t, err) 56 require.NotNil(t, output) 57 }) 58 59 t.Run("to hash other algo proof", func(t *testing.T) { 60 proof := []byte("proof of test") 61 output, err := r2ishiguro.ProofToHash(proof) 62 require.Error(t, err) 63 require.Nil(t, output) 64 }) 65 } 66 67 func BenchmarkProveAndVerify(b *testing.B) { 68 privKey := ed25519.GenPrivKey() 69 pubKey := privKey.PubKey().Bytes() 70 message := []byte("hello, world") 71 72 var proof []byte 73 var err error 74 b.Run("VRF prove", func(b *testing.B) { 75 b.ResetTimer() 76 for i := 0; i < b.N; i++ { 77 proof, err = testutil.Prove(privKey, message) 78 } 79 }) 80 require.NoError(b, err) 81 b.Run("VRF verify", func(b *testing.B) { 82 b.ResetTimer() 83 isValid, _ := r2ishiguro.Verify(pubKey, proof, message) 84 if !isValid { 85 err = fmt.Errorf("invalid") 86 } else { 87 err = nil 88 } 89 }) 90 require.NoError(b, err) 91 }