-
Notifications
You must be signed in to change notification settings - Fork 0
/
prove.go
executable file
·384 lines (316 loc) · 8.64 KB
/
prove.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package dleq
import (
"crypto/rand"
"errors"
"fmt"
"github.com/athanorlabs/go-dleq/types"
)
type Curve = types.Curve
type Point = types.Point
type Scalar = types.Scalar
// Proof represents a DLEq proof and commitment to the witness.
type Proof struct {
CommitmentA, CommitmentB Point
proofs []bitProof
signatureA, signatureB signature
}
type signature struct {
inner []byte
}
// bitProof represents the proof for 1 bit of the witness.
type bitProof struct {
commitmentA, commitmentB commitment
ringSig ringSignature
}
type commitment struct {
// note: the blinder is only needed for proof construction,
// it's not used for verification or included in a serialized proof.
blinder Scalar
commitment Point
}
type ringSignature struct {
eCurveA, eCurveB Scalar
a0, a1 Scalar // in A
b0, b1 Scalar // in B
}
// GenerateSecretForCurves generates a secret value that has a corresponding
// commitment on both curves.
func GenerateSecretForCurves(curveA, curveB Curve) ([32]byte, error) {
bits := min(curveA.BitSize(), curveB.BitSize())
return generateRandomBits(bits)
}
// NewProof returns a new proof for the given secret on the given curves.
// The witness x must be in little-endian and smaller than the minimum order
// of the two curves.
func NewProof(curveA, curveB Curve, x [32]byte) (*Proof, error) {
bits := min(curveA.BitSize(), curveB.BitSize())
err := checkWitnessSize(x, bits)
if err != nil {
return nil, err
}
xA := curveA.ScalarFromBytes(x)
xB := curveB.ScalarFromBytes(x)
XA := curveA.ScalarBaseMul(xA)
XB := curveB.ScalarBaseMul(xB)
// generate commitments for each curve
commitmentsA, err := generateCommitments(curveA, x[:], bits)
if err != nil {
return nil, err
}
err = verifyCommitmentsSum(curveA, commitmentsA, XA)
if err != nil {
return nil, err
}
commitmentsB, err := generateCommitments(curveB, x[:], bits)
if err != nil {
return nil, err
}
err = verifyCommitmentsSum(curveB, commitmentsB, XB)
if err != nil {
return nil, err
}
proofs := make([]bitProof, bits)
for i := 0; i < int(bits); i++ {
bit := getBit(x[:], uint64(i))
ringSig, err := generateRingSignature(curveA, curveB, bit, commitmentsA[i], commitmentsB[i])
if err != nil {
return nil, err
}
proofs[i] = bitProof{
commitmentA: commitmentsA[i],
commitmentB: commitmentsB[i],
ringSig: *ringSig,
}
}
sigA, err := curveA.Sign(xA, XA)
if err != nil {
return nil, err
}
sigB, err := curveB.Sign(xB, XB)
if err != nil {
return nil, err
}
return &Proof{
CommitmentA: XA,
CommitmentB: XB,
proofs: proofs,
signatureA: signature{
sigA,
},
signatureB: signature{
sigB,
},
}, nil
}
func checkWitnessSize(x [32]byte, bits uint64) error {
// number of leading bits that should be cleared
cleared := 256 - bits
// zero out bits that don't have to be zero
bitmask := byte(0xff) << (8 - cleared%8)
if x[bits/8]&bitmask != 0 {
return fmt.Errorf("secret must be under %d bits", bits)
}
if cleared/8 == 0 {
return nil
}
for _, b := range x[(bits/8)+1:] {
if b != 0 {
return fmt.Errorf("secret must be under %d bits", bits)
}
}
return nil
}
// verifyCommitmentsSum verifies that all the commitments sum to the given point.
func verifyCommitmentsSum(curve Curve, commitments []commitment, point Point) error {
sum := commitments[0].commitment.Copy()
two := curve.ScalarFromInt(2)
currPowerOfTwo := curve.ScalarFromInt(2)
for _, c := range commitments[1:] {
sum = sum.Add(c.commitment.ScalarMul(currPowerOfTwo))
currPowerOfTwo = currPowerOfTwo.Mul(two)
}
if sum.Equals(point) {
return nil
}
return errors.New("commitments do not sum to given point")
}
// generate commitments to x for a curve.
// x is expressed as bits b_0 ... b_n where n == bits.
func generateCommitments(curve Curve, x []byte, bits uint64) ([]commitment, error) {
// make n blinders
blinders := make([]Scalar, bits)
commitments := make([]commitment, bits)
two := curve.ScalarFromInt(2)
currPowerOfTwo := curve.ScalarFromInt(1)
sum := curve.ScalarFromInt(0)
for i := uint64(0); i < bits; i++ {
if i == bits-1 {
// (2^(n-1))^(-1)
currPowerOfTwoInv := currPowerOfTwo.Inverse()
// set r_(n-1)
blinders[i] = sum.Negate().Mul(currPowerOfTwoInv)
// sanity check
lastBlinderTimesPowerOfTwo := blinders[i].Mul(currPowerOfTwo)
sum = sum.Add(lastBlinderTimesPowerOfTwo)
if !sum.IsZero() {
panic("sum of blinders is not zero")
}
} else {
blinders[i] = curve.NewRandomScalar()
// r_i * 2^i
blinderTimesPowerOfTwo := blinders[i].Mul(currPowerOfTwo)
// sum(r_i * 2^i)
sum = sum.Add(blinderTimesPowerOfTwo)
// set 2^(i+1) for next iteration
currPowerOfTwo = currPowerOfTwo.Mul(two)
if currPowerOfTwo.IsZero() {
panic("power of two should not be zero")
}
}
if blinders[i].IsZero() {
panic(fmt.Sprintf("blinder %d is zero", i))
}
// generate commitment
// b_i * G' + r_i * G
b := curve.ScalarFromInt(uint32(getBit(x, i)))
bG := curve.ScalarBaseMul(b)
rG := curve.ScalarMul(blinders[i], curve.AltBasePoint())
c := bG.Add(rG)
if c.IsZero() {
panic("commitment should not be zero")
}
// sanity check, can remove later
if getBit(x, i) == 0 {
if !c.Equals(rG) {
panic("commitment should be rG if bit isn't set")
}
}
commitments[i] = commitment{
blinder: blinders[i],
commitment: c,
}
}
return commitments, nil
}
func generateRingSignature(
curveA, curveB Curve,
x byte,
commitmentA, commitmentB commitment,
) (*ringSignature, error) {
j, k := curveA.NewRandomScalar(), curveB.NewRandomScalar()
eA, err := hashToScalar(
curveA,
commitmentA.commitment,
commitmentB.commitment,
curveA.ScalarMul(j, curveA.AltBasePoint()),
curveB.ScalarMul(k, curveB.AltBasePoint()),
)
if err != nil {
return nil, err
}
eB, err := hashToScalar(
curveB,
commitmentA.commitment,
commitmentB.commitment,
curveA.ScalarMul(j, curveA.AltBasePoint()),
curveB.ScalarMul(k, curveB.AltBasePoint()),
)
if err != nil {
return nil, err
}
switch x {
case 0:
a0, b0 := curveA.NewRandomScalar(), curveB.NewRandomScalar()
commitmentAMinusOne := commitmentA.commitment.Sub(curveA.BasePoint())
commitmentBMinusOne := commitmentB.commitment.Sub(curveB.BasePoint())
ecA := commitmentAMinusOne.ScalarMul(eA)
ecB := commitmentBMinusOne.ScalarMul(eB)
A0 := curveA.ScalarMul(a0, curveA.AltBasePoint())
B0 := curveB.ScalarMul(b0, curveB.AltBasePoint())
eA0, err := hashToScalar(curveA, commitmentA.commitment, commitmentB.commitment,
A0.Sub(ecA), B0.Sub(ecB))
if err != nil {
return nil, err
}
eB0, err := hashToScalar(curveB, commitmentA.commitment, commitmentB.commitment,
A0.Sub(ecA), B0.Sub(ecB))
if err != nil {
return nil, err
}
a1 := j.Add(eA0.Mul(commitmentA.blinder))
b1 := k.Add(eB0.Mul(commitmentB.blinder))
return &ringSignature{
eCurveA: eA0,
eCurveB: eB0,
a0: a0,
a1: a1,
b0: b0,
b1: b1,
}, nil
case 1:
a1, b1 := curveA.NewRandomScalar(), curveB.NewRandomScalar()
ecA := commitmentA.commitment.ScalarMul(eA)
ecB := commitmentB.commitment.ScalarMul(eB)
A0 := curveA.ScalarMul(a1, curveA.AltBasePoint())
B0 := curveB.ScalarMul(b1, curveB.AltBasePoint())
eA1, err := hashToScalar(curveA, commitmentA.commitment, commitmentB.commitment,
A0.Sub(ecA), B0.Sub(ecB))
if err != nil {
return nil, err
}
eB1, err := hashToScalar(curveB, commitmentA.commitment, commitmentB.commitment,
A0.Sub(ecA), B0.Sub(ecB))
if err != nil {
return nil, err
}
a0 := j.Add(eA1.Mul(commitmentA.blinder))
b0 := k.Add(eB1.Mul(commitmentB.blinder))
return &ringSignature{
eCurveA: eA,
eCurveB: eB,
a0: a0,
a1: a1,
b0: b0,
b1: b1,
}, nil
default:
return nil, errors.New("input byte must be 0 or 1")
}
}
func hashToScalar(curve Curve, elements ...interface{}) (Scalar, error) {
preimage := []byte{}
for _, e := range elements {
switch el := e.(type) {
case Scalar:
b := el.Encode()
preimage = append(preimage, b...)
case Point:
b := el.Encode()
preimage = append(preimage, b...)
default:
return nil, errors.New("input element must be scalar or point")
}
}
return curve.HashToScalar(preimage)
}
func min(a, b uint64) uint64 {
if a < b {
return a
}
return b
}
// generateRandomBits generates up to 256 random bits.
func generateRandomBits(bits uint64) ([32]byte, error) {
x := [32]byte{}
_, err := rand.Read(x[:])
if err != nil {
return x, err
}
toClear := 256 - bits
x[31] &= 0xff >> toClear
return x, nil
}
// getBit returns the bit at the given index (in little endian)
func getBit(x []byte, i uint64) byte {
return (x[i/8] >> (i % 8)) & 1
}