Debian Codegraph @

vrf/ecvrf_test.go

raw symbols SCIP
1 // Copyright (c) 2021 Oasis Labs Inc. All rights reserved.
2 // Copyright (c) 2021 Yawning Angel. All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // 1. Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
25 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 package vrf
32
33 import (
34 "bytes"
35 "crypto/ed25519"
36 "encoding/hex"
37 "strings"
38 "testing"
39 )
40
41 func TestECVRF(t *testing.T) {
42 t.Run("TestVectors", testIETFVectors)
43 }
44
45 func testIETFVectors(t *testing.T) {
46 testVectors := []struct {
47 sk []byte
48 pk []byte
49 alpha []byte
50 pi []byte
51 beta []byte
52 v10 bool
53 }{
54 // Old (v10 and prior) semantics
55 {
56 sk: mustUnhex(t, "9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60"),
57 pk: mustUnhex(t, "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a"),
58 alpha: []byte{},
59 pi: mustUnhex(t, "7d9c633ffeee27349264cf5c667579fc583b4bda63ab71d001f89c10003ab46f25898f6bd7d4ed4c75f0282b0f7bb9d0e61b387b76db60b3cbf34bf09109ccb33fab742a8bddc0c8ba3caf5c0b75bb04"),
60 beta: mustUnhex(t, "9d574bf9b8302ec0fc1e21c3ec5368269527b87b462ce36dab2d14ccf80c53cccf6758f058c5b1c856b116388152bbe509ee3b9ecfe63d93c3b4346c1fbc6c54"),
61 v10: true,
62 },
63 {
64 sk: mustUnhex(t, "4ccd089b28ff96da9db6c346ec114e0f5b8a319f35aba624da8cf6ed4fb8a6fb"),
65 pk: mustUnhex(t, "3d4017c3e843895a92b70aa74d1b7ebc9c982ccf2ec4968cc0cd55f12af4660c"),
66 alpha: []byte{0x72},
67 pi: mustUnhex(t, "47b327393ff2dd81336f8a2ef10339112401253b3c714eeda879f12c509072ef9bf1a234f833f72d8fff36075fd9b836da28b5569e74caa418bae7ef521f2ddd35f5727d271ecc70b4a83c1fc8ebc40c"),
68 beta: mustUnhex(t, "38561d6b77b71d30eb97a062168ae12b667ce5c28caccdf76bc88e093e4635987cd96814ce55b4689b3dd2947f80e59aac7b7675f8083865b46c89b2ce9cc735"),
69 v10: true,
70 },
71 {
72 sk: mustUnhex(t, "c5aa8df43f9f837bedb7442f31dcb7b166d38535076f094b85ce3a2e0b4458f7"),
73 pk: mustUnhex(t, "fc51cd8e6218a1a38da47ed00230f0580816ed13ba3303ac5deb911548908025"),
74 alpha: []byte{0xaf, 0x82},
75 pi: mustUnhex(t, "926e895d308f5e328e7aa159c06eddbe56d06846abf5d98c2512235eaa57fdce6187befa109606682503b3a1424f0f729ca0418099fbd86a48093e6a8de26307b8d93e02da927e6dd5b73c8f119aee0f"),
76 beta: mustUnhex(t, "121b7f9b9aaaa29099fc04a94ba52784d44eac976dd1a3cca458733be5cd090a7b5fbd148444f17f8daf1fb55cb04b1ae85a626e30a54b4b0f8abf4a43314a58"),
77 v10: true,
78 },
79 // New (v11 and latter) semantics
80 {
81 sk: mustUnhex(t, "9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60"),
82 pk: mustUnhex(t, "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a"),
83 alpha: []byte{},
84 pi: mustUnhex(t, "7d9c633ffeee27349264cf5c667579fc583b4bda63ab71d001f89c10003ab46f14adf9a3cd8b8412d9038531e865c341cafa73589b023d14311c331a9ad15ff2fb37831e00f0acaa6d73bc9997b06501"),
85 beta: mustUnhex(t, "9d574bf9b8302ec0fc1e21c3ec5368269527b87b462ce36dab2d14ccf80c53cccf6758f058c5b1c856b116388152bbe509ee3b9ecfe63d93c3b4346c1fbc6c54"),
86 },
87 {
88 sk: mustUnhex(t, "4ccd089b28ff96da9db6c346ec114e0f5b8a319f35aba624da8cf6ed4fb8a6fb"),
89 pk: mustUnhex(t, "3d4017c3e843895a92b70aa74d1b7ebc9c982ccf2ec4968cc0cd55f12af4660c"),
90 alpha: []byte{0x72},
91 pi: mustUnhex(t, "47b327393ff2dd81336f8a2ef10339112401253b3c714eeda879f12c509072ef055b48372bb82efbdce8e10c8cb9a2f9d60e93908f93df1623ad78a86a028d6bc064dbfc75a6a57379ef855dc6733801"),
92 beta: mustUnhex(t, "38561d6b77b71d30eb97a062168ae12b667ce5c28caccdf76bc88e093e4635987cd96814ce55b4689b3dd2947f80e59aac7b7675f8083865b46c89b2ce9cc735"),
93 },
94 {
95 sk: mustUnhex(t, "c5aa8df43f9f837bedb7442f31dcb7b166d38535076f094b85ce3a2e0b4458f7"),
96 pk: mustUnhex(t, "fc51cd8e6218a1a38da47ed00230f0580816ed13ba3303ac5deb911548908025"),
97 alpha: []byte{0xaf, 0x82},
98 pi: mustUnhex(t, "926e895d308f5e328e7aa159c06eddbe56d06846abf5d98c2512235eaa57fdce35b46edfc655bc828d44ad09d1150f31374e7ef73027e14760d42e77341fe05467bb286cc2c9d7fde29120a0b2320d04"),
99 beta: mustUnhex(t, "121b7f9b9aaaa29099fc04a94ba52784d44eac976dd1a3cca458733be5cd090a7b5fbd148444f17f8daf1fb55cb04b1ae85a626e30a54b4b0f8abf4a43314a58"),
100 },
101 }
102 for i, vec := range testVectors {
103 sk := ed25519.NewKeyFromSeed(vec.sk)
104 pk := sk.Public().(ed25519.PublicKey)
105
106 var (
107 proveFn func(ed25519.PrivateKey, []byte) []byte
108 verifyFn func(ed25519.PublicKey, []byte, []byte) (bool, []byte)
109 )
110 switch vec.v10 {
111 case false:
112 proveFn = Prove
113 verifyFn = Verify
114 case true:
115 proveFn = Prove_v10
116 verifyFn = Verify_v10
117 }
118
119 pi := proveFn(sk, vec.alpha)
120 if !bytes.Equal(vec.pi, pi) {
121 t.Fatalf("[%d] pi mismatch (Got: %x)", i, pi)
122 }
123
124 ok, beta := verifyFn(pk, pi, vec.alpha)
125 if !ok {
126 t.Fatalf("[%d] Verify() failed", i)
127 }
128 if !bytes.Equal(vec.beta, beta) {
129 t.Fatalf("[%d] beta mismatch (Got: %x)", i, beta)
130 }
131
132 pi[0] ^= 0xa5
133 ok, _ = verifyFn(pk, pi, vec.alpha)
134 if ok {
135 t.Fatalf("[%d] bad pi, Verify() passed", i)
136 }
137 }
138 }
139
140 func BenchmarkECVRF(b *testing.B) {
141 b.Run("Prove", benchProve)
142 b.Run("ProofToHash", benchProofToHash)
143 b.Run("Verify", benchVerify)
144 }
145
146 func benchProve(b *testing.B) {
147 _, sk, err := ed25519.GenerateKey(nil)
148 if err != nil {
149 b.Fatalf("GenerateKey: %v", err)
150 }
151
152 b.ReportAllocs()
153 b.ResetTimer()
154 for i := 0; i < b.N; i++ {
155 _ = Prove(sk, []byte("test-alpha-pls-ignore"))
156 }
157 }
158
159 func benchProofToHash(b *testing.B) {
160 _, sk, err := ed25519.GenerateKey(nil)
161 if err != nil {
162 b.Fatalf("GenerateKey: %v", err)
163 }
164 pi := Prove(sk, []byte("test-alpha-pls-ignore"))
165
166 b.ReportAllocs()
167 b.ResetTimer()
168 for i := 0; i < b.N; i++ {
169 _, err := ProofToHash(pi)
170 if err != nil {
171 b.Fatalf("ProofToHash: %v", err)
172 }
173 }
174 }
175
176 func benchVerify(b *testing.B) {
177 pk, sk, err := ed25519.GenerateKey(nil)
178 if err != nil {
179 b.Fatalf("GenerateKey: %v", err)
180 }
181 alpha := []byte("test-alpha-pls-ignore")
182 pi := Prove(sk, alpha)
183
184 b.ReportAllocs()
185 b.ResetTimer()
186 for i := 0; i < b.N; i++ {
187 ok, _ := Verify(pk, pi, alpha)
188 if !ok {
189 b.Fatalf("Verify() failed")
190 }
191 }
192 }
193
194 func mustUnhex(t *testing.T, x string) []byte {
195 b, err := hex.DecodeString(strings.ReplaceAll(x, " ", ""))
196 if err != nil {
197 t.Fatalf("failed to parse hex: %v", err)
198 }
199
200 return b
201 }