-
Notifications
You must be signed in to change notification settings - Fork 4
/
vxeddsa.go
268 lines (227 loc) · 6.43 KB
/
vxeddsa.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
268
package vxeddsa
import (
"bytes"
"crypto/rand"
"crypto/sha512"
"io"
"github.com/Scratch-net/vxeddsa/edwards25519"
"github.com/Scratch-net/vxeddsa/extra25519"
)
// This code is a port of the public domain, VXEdDSA C implementation by Trevor
// Perrin / Open Whisper Systems. Specification:
// https://whispersystems.org/docs/specifications/xeddsa/#vxeddsa
func calculateBv(A [32]byte, msg []byte) (Bv *edwards25519.ExtendedGroupElement) {
/* Calculate SHA512(label(2) || A || msg) */
buf := make([]byte, 32)
buf[0] = 0xFD
for count := 1; count < 32; count++ {
buf[count] = 0xFF
}
buffer := bytes.NewBuffer(buf)
buffer.Write(A[:])
buffer.Write(msg)
Bv = extra25519.HashToPoint(buffer.Bytes())
return
}
func (sk PrivateKey) signInternal(m []byte, randr io.Reader) (signature []byte) {
if randr == nil {
randr = rand.Reader
}
// TODO check for maxMsgLen
var a, aNeg, rB [32]byte
var R edwards25519.ExtendedGroupElement
var x [32]byte
copy(x[:], sk[:])
var edPubKey edwards25519.ExtendedGroupElement
edwards25519.GeScalarMultBase(&edPubKey, &x)
var A [32]byte
edPubKey.ToBytes(&A)
// Force Edwards sign bit to zero
signBit := (A[31] & 0x80) >> 7
copy(a[:], sk[:32])
edwards25519.ScNeg(&aNeg, &a)
edwards25519.ScCMove(&a, &aNeg, int32(signBit))
A[31] &= 0x7F
Bv, V := calculateBvAndV(a, A, m)
/* r = SHA512(label(3) || a || V || random(64)) */
var Rv edwards25519.ExtendedGroupElement
buf := make([]byte, 160)
buf[0] = 0xFC
for i := 1; i < 32; i++ {
buf[i] = 0xFF
}
copy(buf[32:], a[:])
copy(buf[64:], V[:])
random := make([]byte, 64)
if _, err := randr.Read(random); err != nil {
panic("Couldn't read from random")
}
copy(buf[96:], random)
rH := sha512.Sum512(buf[:160])
edwards25519.ScReduce(&rB, &rH)
edwards25519.GeScalarMultBase(&R, &rB)
edwards25519.GeScalarMult(&Rv, &rB, Bv)
/* h = SHA512(label(4) || A || V || R || Rv || M) */
var Rb [32]byte
R.ToBytes(&Rb)
var RvB [32]byte
Rv.ToBytes(&RvB)
buf = append(buf, m...)
buf[0] = 0xFB
for i := 1; i < 32; i++ {
buf[i] = 0xFF
}
copy(buf[32:], A[:])
copy(buf[64:], V[:])
copy(buf[96:], Rb[:])
copy(buf[128:], RvB[:])
//copy(buf[:160], m)
hB := sha512.Sum512(buf[:160+len(m)])
var h [32]byte
edwards25519.ScReduce(&h, &hB)
var s [32]byte
edwards25519.ScMulAdd(&s, &h, &a, &rB)
signature = make([]byte, ProofSize)
copy(signature[:32], V[:])
copy(signature[32:64], h[:])
copy(signature[64:96], s[:])
return
}
func (pkB PublicKey) verifyInteral(m, signature []byte) (bool, [32]byte) {
var vrf [32]byte
if len(signature) != ProofSize || len(pkB) != PublicKeySize {
return false, vrf
}
// TODO check for max. message length
var u edwards25519.FieldElement
pubKey := [32]byte(pkB)
edwards25519.FeFromBytes(&u, &pubKey)
var strict [32]byte
edwards25519.FeToBytes(&strict, &u)
if !(edwards25519.FeCompare(strict, pubKey) == 0) {
return false, vrf
}
var y edwards25519.FieldElement
extra25519.FeMontgomeryXToEdwardsY(&y, &u)
var edPubKey [32]byte
edwards25519.FeToBytes(&edPubKey, &y)
Bv := calculateBv(edPubKey, m)
// verifybuf = V || h || s || m
verifBuf := make([]byte, len(m)+160)
copy(verifBuf, signature[:96])
copy(verifBuf[96:], m)
if verifBuf[63]&224 == 1 {
return false, vrf
}
if verifBuf[95]&224 == 1 {
return false, vrf
}
// Load -A:
var minusA edwards25519.ExtendedGroupElement
edwards25519.FeFromBytes(&minusA.Y, &edPubKey)
if !minusA.FromParityAndY((edPubKey[31]>>7)^0x01, &minusA.Y) {
return false, vrf
}
// Load -V
var minusV edwards25519.ExtendedGroupElement
var Vb [32]byte
copy(Vb[:], signature[:32])
edwards25519.FeFromBytes(&minusV.Y, &Vb)
if !minusV.FromParityAndY((Vb[31]>>7)^0x01, &minusV.Y) {
return false, vrf
}
// Load h, s
var h, s [32]byte
copy(h[:], verifBuf[32:64])
copy(s[:], verifBuf[64:96])
if h[31]&224 == 1 {
return false, vrf
} /* strict parsing of h */
if s[31]&224 == 1 {
return false, vrf
} /* strict parsing of s */
var A, cA, V, cV edwards25519.ExtendedGroupElement
edwards25519.GeNeg(&A, minusA)
edwards25519.GeNeg(&V, minusV)
edwards25519.GeDouble(&cA, &A)
edwards25519.GeDouble(&cA, &cA)
edwards25519.GeDouble(&cA, &cA)
edwards25519.GeDouble(&cV, &V)
edwards25519.GeDouble(&cV, &cV)
edwards25519.GeDouble(&cV, &cV)
if edwards25519.GeIsNeutral(&cA) || edwards25519.GeIsNeutral(&cV) ||
edwards25519.GeIsNeutral(Bv) {
return false, vrf
}
// R = (s*B) + (h * -A))
var R edwards25519.ProjectiveGroupElement
edwards25519.GeDoubleScalarMultVartime(&R, &h, &minusA, &s)
// s * Bv
var sBv edwards25519.ExtendedGroupElement
edwards25519.GeScalarMult(&sBv, &s, Bv)
// h * -V
var hMinusV edwards25519.ExtendedGroupElement
edwards25519.GeScalarMult(&hMinusV, &h, &minusV)
// Rv = (sc * Bv) + (hc * (-V))
var Rv edwards25519.ExtendedGroupElement
edwards25519.GeAdd(&Rv, &sBv, &hMinusV)
// Check h == SHA512(label(4) || A || V || R || Rv || M)
var VBytes, RBytes, RVBytes [32]byte
V.ToBytes(&VBytes)
R.ToBytes(&RBytes)
Rv.ToBytes(&RVBytes)
vrfBuf := make([]byte, 160+len(m))
vrfBuf[0] = 0xFB // label 4
for count := 1; count < 32; count++ {
vrfBuf[count] = 0xFF
}
//copy(vrfBuf, vrfBuf))
copy(vrfBuf[32:], edPubKey[:])
copy(vrfBuf[64:], VBytes[:])
copy(vrfBuf[96:], RBytes[:])
copy(vrfBuf[128:], RVBytes[:])
copy(vrfBuf[160:], verifBuf[96:96+len(m)])
hCheck := sha512.Sum512(vrfBuf[:160+len(m)])
var hCheckReduced [32]byte
edwards25519.ScReduce(&hCheckReduced, &hCheck)
if edwards25519.FeCompare(hCheckReduced, h) == 0 {
// compute VRF from cV:
var cVBytes [32]byte
cV.ToBytes(&cVBytes)
vrfBuf[0] = 0xFA // label 5
copy(vrfBuf[32:], cVBytes[:])
vrfOutput := sha512.Sum512(vrfBuf[:64])
copy(vrf[:], vrfOutput[:32])
// vrf = cV || hash_5(cV) (mod 2^b)
return true, vrf
}
return false, vrf
}
func calculateBvAndV(a, Abytes [32]byte,
msg []byte) (Bv *edwards25519.ExtendedGroupElement, V *[32]byte) {
p3 := edwards25519.ExtendedGroupElement{}
Bv = calculateBv(Abytes, msg)
edwards25519.GeScalarMult(&p3, &a, Bv)
V = new([32]byte)
p3.ToBytes(V)
return
}
func computeVrfFromV(Vbytes [32]byte) (vrf [32]byte) {
var V, cV edwards25519.ExtendedGroupElement
V.FromBytes(&Vbytes)
edwards25519.GeDouble(&cV, &V)
edwards25519.GeDouble(&cV, &cV)
edwards25519.GeDouble(&cV, &cV)
buffer := make([]byte, 32)
buffer[0] = 0xFA // label 5
for count := 1; count < 32; count++ {
buffer[count] = 0xFF
}
var cVBytes [32]byte
cV.ToBytes(&cVBytes)
buf := bytes.NewBuffer(buffer)
buf.Write(cVBytes[:])
hash := sha512.Sum512(buf.Bytes())
copy(vrf[:], hash[:32])
return
}