/
vrf_ec.go
191 lines (166 loc) · 5.35 KB
/
vrf_ec.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
// Copyright (c) 2018 The MATRIX Authors
// Distributed under the MIT software license, see the accompanying
// file COPYING or or http://www.opensource.org/licenses/mit-license.php
package vrf
import (
"bytes"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"encoding/binary"
"errors"
"hash"
"math/big"
"github.com/MatrixAINetwork/go-matrix/crypto/vrf/ec"
)
var (
ErrInvalidVRF = errors.New("invalid VRF proof")
ErrInvalidHash = errors.New("hash function does not match elliptic curve bitsize")
)
// hashToCurve hashes to a point on elliptic curve
func hashToCurve(curve elliptic.Curve, h hash.Hash, m []byte) (x, y *big.Int) {
var i uint32
params := curve.Params()
byteLen := (params.BitSize + 7) >> 3
for x == nil && i < 100 {
// TODO: Use a NIST specified DRBG.
h.Reset()
binary.Write(h, binary.BigEndian, i)
h.Write(m)
r := []byte{2} // Set point encoding to "compressed", y=0.
r = h.Sum(r)
p, err := ec.DecodePublicKey(r[:byteLen+1], curve)
if err != nil {
x, y = nil, nil
} else {
x, y = p.X, p.Y
}
i++
}
return
}
var one = big.NewInt(1)
// hashToInt hashes to an integer [1,N-1]
func hashToInt(curve elliptic.Curve, h hash.Hash, m []byte) *big.Int {
// NIST SP 800-90A § A.5.1: Simple discard method.
params := curve.Params()
byteLen := (params.BitSize + 7) >> 3
for i := uint32(0); ; i++ {
// TODO: Use a NIST specified DRBG.
h.Reset()
binary.Write(h, binary.BigEndian, i)
h.Write(m)
b := h.Sum(nil)
k := new(big.Int).SetBytes(b[:byteLen])
if k.Cmp(new(big.Int).Sub(params.N, one)) == -1 {
return k.Add(k, one)
}
}
}
// Evaluate returns the verifiable unpredictable(random) function evaluated at m
func Evaluate(pri *ecdsa.PrivateKey, h hash.Hash, m []byte) (index [32]byte, proof []byte) {
curve := pri.Curve
params := curve.Params()
nilIndex := [32]byte{}
byteLen := (params.BitSize + 7) >> 3
if byteLen != h.Size() {
return nilIndex, nil
}
// Prover chooses r <-- [1,N-1]
r, _, _, err := elliptic.GenerateKey(curve, rand.Reader)
if err != nil {
return nilIndex, nil
}
ri := new(big.Int).SetBytes(r)
// H = hashToCurve(pk || m)
var buf bytes.Buffer
buf.Write(elliptic.Marshal(curve, pri.PublicKey.X, pri.PublicKey.Y))
buf.Write(m)
Hx, Hy := hashToCurve(curve, h, buf.Bytes())
// VRF_pri(m) = [pri]H
sHx, sHy := curve.ScalarMult(Hx, Hy, pri.D.Bytes())
vrf := elliptic.Marshal(curve, sHx, sHy) // 2*byteLen+1 bytes.
// G is the base point
// s = hashToInt(G, H, [pri]G, VRF, [r]G, [r]H)
rGx, rGy := curve.ScalarBaseMult(r)
rHx, rHy := curve.ScalarMult(Hx, Hy, r)
var b bytes.Buffer
b.Write(elliptic.Marshal(curve, params.Gx, params.Gy))
b.Write(elliptic.Marshal(curve, Hx, Hy))
b.Write(elliptic.Marshal(curve, pri.PublicKey.X, pri.PublicKey.Y))
b.Write(vrf)
b.Write(elliptic.Marshal(curve, rGx, rGy))
b.Write(elliptic.Marshal(curve, rHx, rHy))
s := hashToInt(curve, h, b.Bytes())
// t = r−s*pri mod N
t := new(big.Int).Sub(ri, new(big.Int).Mul(s, pri.D))
t.Mod(t, params.N)
// Index = SHA256(vrf)
index = sha256.Sum256(vrf)
// Write s, t, and vrf to a proof blob. Also write leading zeros before s and t
// if needed.
buf.Reset()
buf.Write(make([]byte, byteLen-len(s.Bytes())))
buf.Write(s.Bytes())
buf.Write(make([]byte, byteLen-len(t.Bytes())))
buf.Write(t.Bytes())
buf.Write(vrf) //byteLen*2 + byteLen*2 + 1
return index, buf.Bytes()
}
// ProofToHash asserts that proof is correct for m and outputs index.
func ProofToHash(pk *ecdsa.PublicKey, h hash.Hash, m, proof []byte) (index [32]byte, err error) {
nilIndex := [32]byte{}
curve := pk.Curve
params := curve.Params()
byteLen := (params.BitSize + 7) >> 3
if byteLen != h.Size() {
return nilIndex, ErrInvalidHash
}
// verifier checks that s == hashToInt(m, [t]G + [s]([pri]G), [t]hashToCurve(pk, m) + [s]VRF_pri(m))
if got, want := len(proof), (2*byteLen)+(2*byteLen+1); got != want {
return nilIndex, ErrInvalidVRF
}
// Parse proof into s, t, and vrf.
s := proof[0:byteLen]
t := proof[byteLen : 2*byteLen]
vrf := proof[2*byteLen : 2*byteLen+2*byteLen+1]
uHx, uHy := elliptic.Unmarshal(curve, vrf)
if uHx == nil {
return nilIndex, ErrInvalidVRF
}
// [t]G + [s]([pri]G) = [t+pri*s]G
tGx, tGy := curve.ScalarBaseMult(t)
ksGx, ksGy := curve.ScalarMult(pk.X, pk.Y, s)
tksGx, tksGy := curve.Add(tGx, tGy, ksGx, ksGy)
// H = hashToCurve(pk || m)
// [t]H + [s]VRF = [t+pri*s]H
buf := new(bytes.Buffer)
buf.Write(elliptic.Marshal(curve, pk.X, pk.Y))
buf.Write(m)
Hx, Hy := hashToCurve(pk, h, buf.Bytes())
tHx, tHy := curve.ScalarMult(Hx, Hy, t)
sHx, sHy := curve.ScalarMult(uHx, uHy, s)
tksHx, tksHy := curve.Add(tHx, tHy, sHx, sHy)
// hashToInt(G, H, [pri]G, VRF, [t]G + [s]([pri]G), [t]H + [s]VRF)
// = hashToInt(G, H, [pri]G, VRF, [t+pri*s]G, [t+pri*s]H)
// = hashToInt(G, H, [pri]G, VRF, [r]G, [r]H)
var b bytes.Buffer
b.Write(elliptic.Marshal(curve, params.Gx, params.Gy))
b.Write(elliptic.Marshal(curve, Hx, Hy))
b.Write(elliptic.Marshal(curve, pk.X, pk.Y))
b.Write(vrf)
b.Write(elliptic.Marshal(curve, tksGx, tksGy))
b.Write(elliptic.Marshal(curve, tksHx, tksHy))
h2 := hashToInt(curve, h, b.Bytes())
// Left pad h2 with zeros if needed. This will ensure that h2 is padded
// the same way s is.
buf.Reset()
buf.Write(make([]byte, byteLen-len(h2.Bytes())))
buf.Write(h2.Bytes())
if !hmac.Equal(s, buf.Bytes()) {
return nilIndex, ErrInvalidVRF
}
return sha256.Sum256(vrf), nil
}