-
Notifications
You must be signed in to change notification settings - Fork 1
/
vrf.go
267 lines (218 loc) · 7.63 KB
/
vrf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
//Copyright (c) 2020 - 2023 vechain.org.
//Copyright (c) 2023 digioracle.link
//Licensed under the MIT license.
// Package ecvrf is the Elliptic Curve Verifiable Random Function (VRF) library.
package ecvrf
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/sha256"
"errors"
"math/big"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
)
// VRF is the interface that wraps VRF methods.
type VRF interface {
// Prove constructs a VRF proof `pi` for the given input `alpha`,
// using the private key `sk`. The hash output is returned as `beta`.
Prove(sk *ecdsa.PrivateKey, alpha []byte) (beta, pi []byte, err error)
ProveSecp256k1(sk *ecdsa.PrivateKey, alpha []byte) (beta, pi []byte, err error)
// Verify checks the proof `pi` of the message `alpha` against the given
// public key `pk`. The hash output is returned as `beta`.
Verify(pk *ecdsa.PublicKey, alpha, pi []byte) (beta []byte, err error)
VerifySecp256k1(pk *ecdsa.PublicKey, alpha, pi []byte) (beta []byte, err error)
}
var (
// Secp256k1Sha256Tai is the pre-configured VRF object with secp256k1/SHA256 and hash_to_curve_try_and_increment algorithm.
Secp256k1Sha256Tai = New(&Config{
Curve: secp256k1.S256(),
SuiteString: 0xfe,
Cofactor: 0x01,
NewHasher: sha256.New,
Decompress: func(c elliptic.Curve, pk []byte) (x, y *big.Int) {
var fx, fy secp256k1.FieldVal
// Reject unsupported public key formats for the given length.
format := pk[0]
switch format {
case secp256k1.PubKeyFormatCompressedEven, secp256k1.PubKeyFormatCompressedOdd:
default:
return
}
// Parse the x coordinate while ensuring that it is in the allowed
// range.
if overflow := fx.SetByteSlice(pk[1:33]); overflow {
return
}
// Attempt to calculate the y coordinate for the given x coordinate such
// that the result pair is a point on the secp256k1 curve and the
// solution with desired oddness is chosen.
wantOddY := format == secp256k1.PubKeyFormatCompressedOdd
if !secp256k1.DecompressY(&fx, wantOddY, &fy) {
return
}
fy.Normalize()
return new(big.Int).SetBytes(fx.Bytes()[:]), new(big.Int).SetBytes(fy.Bytes()[:])
},
})
// P256Sha256Tai is the pre-configured VRF object with P256/SHA256 and hash_to_curve_try_and_increment algorithm.
P256Sha256Tai = New(&Config{
Curve: elliptic.P256(),
SuiteString: 0x01,
Cofactor: 0x01,
NewHasher: sha256.New,
Decompress: elliptic.UnmarshalCompressed,
})
)
// New creates and initializes a VRF object using customized config.
func New(cfg *Config) VRF {
return &vrf{cfg: *cfg}
}
type vrf struct {
cfg Config
}
// Prove constructs VRF proof following [draft-irtf-cfrg-vrf-06 section 5.1](https://tools.ietf.org/id/draft-irtf-cfrg-vrf-06.html#rfc.section.5.1).
func (v *vrf) Prove(sk *ecdsa.PrivateKey, alpha []byte) (beta, pi []byte, err error) {
var (
core = core{Config: &v.cfg}
q = core.Q()
)
// step 1 is done by the caller.
// step 2: H = ECVRF_hash_to_curve(suite_string, Y, alpha_string)
// currently, try_and_increment algorithm is supported
H, err := core.HashToCurveTryAndIncrement(&point{sk.X, sk.Y}, alpha)
if err != nil {
return
}
// step 3: h_string = point_to_string(H)
hbytes := core.Marshal(H)
// step 4: Gamma = x * H
gamma := core.ScalarMult(H, sk.D.Bytes())
// step 5: k = ECVRF_nonce_generation(SK, h_string)
// it follows RFC6979
kbytes := core.rfc6979nonce(sk.D, hbytes)
k := new(big.Int).SetBytes(kbytes)
// step 6: c = ECVRF_hash_points(H, Gamma, k*B, k*H)
kB := core.ScalarBaseMult(kbytes)
kH := core.ScalarMult(H, kbytes)
c := core.HashPoints(
H,
gamma,
kB,
kH)
// step 7: s = (k + c*x) mod q
s := new(big.Int).Mul(c, sk.D)
s.Add(s, k)
s.Mod(s, q)
// step 8: encode (gamma, c, s) as pi_string = point_to_string(Gamma) || int_to_string(c, n) || int_to_string(s, qLen)
pi = core.EncodeProof(gamma, c, s)
// step 9: Output pi_string
// here also returns beta
beta = core.GammaToHash(gamma)
return
}
// ProveV2 constructs VRF proof following https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-vrf-10.html#name-ecvrf-proving
func (v *vrf) ProveSecp256k1(sk *ecdsa.PrivateKey, alpha []byte) (beta, pi []byte, err error) {
var (
core = core{Config: &v.cfg}
q = core.Q()
)
// step 1 is done by the caller.
// step 2: H = ECVRF_hash_to_curve(suite_string, Y, alpha_string)
// currently, try_and_increment algorithm is supported
H, err := core.HashToCurveTryAndIncrementSecp256k1(&point{sk.X, sk.Y}, alpha)
if err != nil {
return
}
// step 3: h_string = point_to_string(H)
hbytes := core.Marshal(H)
// step 4: Gamma = x * H
gamma := core.ScalarMult(H, sk.D.Bytes())
// step 5: k = ECVRF_nonce_generation(SK, h_string)
// it follows RFC6979
//kbytes := core.rfc6979nonce(sk.D, hbytes)
kbytes := core.rfc6979nonceSecp256k1(sk.D, hbytes)
k := new(big.Int).SetBytes(kbytes)
// step 6: c = ECVRF_hash_points(H, Gamma, k*B, k*H)
kB := core.ScalarBaseMult(kbytes)
kH := core.ScalarMult(H, kbytes)
c := core.HashPointsSecp256k1(
H,
gamma,
kB,
kH)
// step 7: s = (k + c*x) mod q
s := new(big.Int).Mul(c, sk.D)
s.Add(s, k)
s.Mod(s, q)
// step 8: encode (gamma, c, s) as pi_string = point_to_string(Gamma) || int_to_string(c, n) || int_to_string(s, qLen)
pi = core.EncodeProof(gamma, c, s)
// step 9: Output pi_string
// here also returns beta
beta = core.GammaToHashSecp256k1(gamma)
return
}
// Verify checks the correctness of proof following [draft-irtf-cfrg-vrf-06 section 5.3](https://tools.ietf.org/id/draft-irtf-cfrg-vrf-06.html#rfc.section.5.3).
func (v *vrf) Verify(pk *ecdsa.PublicKey, alpha, pi []byte) (beta []byte, err error) {
core := core{Config: &v.cfg}
// step 1: D = ECVRF_decode_proof(pi_string)
gamma, c, s, err := core.DecodeProof(pi)
// step 2: If D is "INVALID", output "INVALID" and stop
if err != nil {
return
}
// step 3: (Gamma, c, s) = D
// step 4: H = ECVRF_hash_to_curve(suite_string, Y, alpha_string)
H, err := core.HashToCurveTryAndIncrement(&point{pk.X, pk.Y}, alpha)
if err != nil {
return
}
// step 5: U = s*B - c*Y
sB := core.ScalarBaseMult(s.Bytes())
cY := core.ScalarMult(&point{pk.X, pk.Y}, c.Bytes())
U := core.Sub(sB, cY)
// step 6: V = s*H - c*Gamma
sH := core.ScalarMult(H, s.Bytes())
cGamma := core.ScalarMult(gamma, c.Bytes())
V := core.Sub(sH, cGamma)
// step 7: c' = ECVRF_hash_points(H, Gamma, U, V)
derivedC := core.HashPoints(H, gamma, U, V)
// step 8: If c and c' are equal, output ("VALID", ECVRF_proof_to_hash(pi_string)); else output "INVALID"
if derivedC.Cmp(c) != 0 {
err = errors.New("invalid proof")
return
}
beta = core.GammaToHash(gamma)
return
}
func (v *vrf) VerifySecp256k1(pk *ecdsa.PublicKey, alpha, pi []byte) (beta []byte, err error) {
core := core{Config: &v.cfg}
// step 1: D = ECVRF_decode_proof(pi_string)
gamma, c, s, err := core.DecodeProof(pi)
// step 2: If D is "INVALID", output "INVALID" and stop
if err != nil {
return
}
// step 3: (Gamma, c, s) = D
// step 4: H = ECVRF_hash_to_curve(suite_string, Y, alpha_string)
H, err := core.HashToCurveTryAndIncrementSecp256k1(&point{pk.X, pk.Y}, alpha)
if err != nil {
return
}
// step 5: U = s*B - c*Y
sB := core.ScalarBaseMult(s.Bytes())
cY := core.ScalarMult(&point{pk.X, pk.Y}, c.Bytes())
U := core.Sub(sB, cY)
// step 6: V = s*H - c*Gamma
sH := core.ScalarMult(H, s.Bytes())
cGamma := core.ScalarMult(gamma, c.Bytes())
V := core.Sub(sH, cGamma)
// step 7: c' = ECVRF_hash_points(H, Gamma, U, V)
derivedC := core.HashPointsSecp256k1(H, gamma, U, V)
// step 8: If c and c' are equal, output ("VALID", ECVRF_proof_to_hash(pi_string)); else output "INVALID"
if derivedC.Cmp(c) != 0 {
err = errors.New("invalid proof")
return
}
beta = core.GammaToHashSecp256k1(gamma)
return
}