-
Notifications
You must be signed in to change notification settings - Fork 11
/
proofs.go
388 lines (341 loc) · 11.9 KB
/
proofs.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
385
386
387
388
// Copyright 2016 Maarten Everts. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gabi
import (
"github.com/privacybydesign/gabi/big"
"github.com/privacybydesign/gabi/gabikeys"
"github.com/privacybydesign/gabi/internal/common"
"github.com/privacybydesign/gabi/rangeproof"
"github.com/privacybydesign/gabi/revocation"
"github.com/go-errors/errors"
)
// Proof represents a non-interactive zero-knowledge proof
type Proof interface {
VerifyWithChallenge(pk *gabikeys.PublicKey, reconstructedChallenge *big.Int) bool
SecretKeyResponse() *big.Int
ChallengeContribution(pk *gabikeys.PublicKey) ([]*big.Int, error)
MergeProofP(proofP *ProofP, pk *gabikeys.PublicKey)
}
// createChallenge creates a challenge based on context, nonce and the
// contributions.
func createChallenge(context, nonce *big.Int, contributions []*big.Int, issig bool) *big.Int {
// Basically, sandwich the contributions between context and nonce
input := make([]*big.Int, 2+len(contributions))
input[0] = context
copy(input[1:1+len(contributions)], contributions)
input[len(input)-1] = nonce
return common.HashCommit(input, issig)
}
// ProofU represents a proof of correctness of the commitment in the first phase
// of the issuance protocol.
type ProofU struct {
U *big.Int `json:"U"`
C *big.Int `json:"c"`
VPrimeResponse *big.Int `json:"v_prime_response"`
SResponse *big.Int `json:"s_response"`
MUserResponses map[int]*big.Int `json:"m_user_responses,omitempty"`
}
func (p *ProofU) MergeProofP(proofP *ProofP, pk *gabikeys.PublicKey) {
if proofP.P == nil { // new keyshare protocol version
p.C.Set(proofP.C)
p.SResponse.Set(proofP.SResponse)
} else {
p.U.Mod(
p.U.Mul(p.U, proofP.P),
pk.N,
)
p.SResponse.Add(p.SResponse, proofP.SResponse)
}
}
// Verify verifies whether the proof is correct.
func (p *ProofU) Verify(pk *gabikeys.PublicKey, context, nonce *big.Int) bool {
contrib, err := p.ChallengeContribution(pk)
if err != nil {
return false
}
return p.VerifyWithChallenge(pk, createChallenge(context, nonce, contrib, false))
}
// correctResponseSizes checks the sizes of the elements in the ProofU proof.
func (p *ProofU) correctResponseSizes(pk *gabikeys.PublicKey) bool {
minimum := big.NewInt(0)
maximum := new(big.Int).Lsh(big.NewInt(1), pk.Params.LvPrimeCommit+1)
maximum.Sub(maximum, big.NewInt(1))
return p.VPrimeResponse.Cmp(minimum) >= 0 && p.VPrimeResponse.Cmp(maximum) <= 0
}
// VerifyWithChallenge verifies whether the proof is correct.
func (p *ProofU) VerifyWithChallenge(pk *gabikeys.PublicKey, reconstructedChallenge *big.Int) bool {
return p.correctResponseSizes(pk) && p.C.Cmp(reconstructedChallenge) == 0
}
// reconstructUcommit reconstructs U from the information in the proof and the
// provided public key.
func (p *ProofU) reconstructUcommit(pk *gabikeys.PublicKey) (*big.Int, error) {
// Reconstruct Ucommit
// U_commit = U^{-C} * S^{VPrimeResponse} * R_0^{SResponse}
Uc, err := common.ModPow(p.U, new(big.Int).Neg(p.C), pk.N)
if err != nil {
return nil, err
}
Sv, err := common.ModPow(pk.S, p.VPrimeResponse, pk.N)
if err != nil {
return nil, err
}
R0s, err := common.ModPow(pk.R[0], p.SResponse, pk.N)
if err != nil {
return nil, err
}
Ucommit := new(big.Int).Mul(Uc, Sv)
Ucommit.Mul(Ucommit, R0s).Mod(Ucommit, pk.N)
for i, miUserResponse := range p.MUserResponses {
Rimi, err := common.ModPow(pk.R[i], miUserResponse, pk.N)
if err != nil {
return nil, err
}
Ucommit.Mul(Ucommit, Rimi).Mod(Ucommit, pk.N)
}
return Ucommit, nil
}
// SecretKeyResponse returns the secret key response (as part of Proof
// interface).
func (p *ProofU) SecretKeyResponse() *big.Int {
return p.SResponse
}
// Challenge returns the challenge in the proof (part of the Proof interface).
func (p *ProofU) Challenge() *big.Int {
return p.C
}
// ChallengeContribution returns the contribution of this proof to the
// challenge.
func (p *ProofU) ChallengeContribution(pk *gabikeys.PublicKey) ([]*big.Int, error) {
Ucommit, err := p.reconstructUcommit(pk)
if err != nil {
return nil, err
}
return []*big.Int{p.U, Ucommit}, nil
}
// ProofS represents a proof.
type ProofS struct {
C *big.Int `json:"c"`
EResponse *big.Int `json:"e_response"`
}
// Verify verifies the proof against the given public key, signature, context,
// and nonce.
func (p *ProofS) Verify(pk *gabikeys.PublicKey, signature *CLSignature, context, nonce *big.Int) bool {
// Reconstruct A_commit
// ACommit = A^{C + EResponse * e}
exponent := new(big.Int).Mul(p.EResponse, signature.E)
exponent.Add(p.C, exponent)
ACommit := new(big.Int).Exp(signature.A, exponent, pk.N)
// Reconstruct Q
Q := new(big.Int).Exp(signature.A, signature.E, pk.N)
// Recalculate hash
cPrime := common.HashCommit([]*big.Int{context, Q, signature.A, nonce, ACommit}, false)
return p.C.Cmp(cPrime) == 0
}
// ProofD represents a proof in the showing protocol.
type ProofD struct {
C *big.Int `json:"c"`
A *big.Int `json:"A"`
EResponse *big.Int `json:"e_response"`
VResponse *big.Int `json:"v_response"`
AResponses map[int]*big.Int `json:"a_responses"`
ADisclosed map[int]*big.Int `json:"a_disclosed"`
NonRevocationProof *revocation.Proof `json:"nonrev_proof,omitempty"`
RangeProofs map[int][]*rangeproof.Proof `json:"rangeproofs,omitempty"`
cachedRangeStructures map[int][]*rangeproof.ProofStructure
}
// MergeProofP merges a ProofP into the ProofD.
func (p *ProofD) MergeProofP(proofP *ProofP, _ *gabikeys.PublicKey) {
if proofP.P == nil { // new protocol version
p.C.Set(proofP.C)
p.AResponses[0].Set(proofP.SResponse)
} else {
p.AResponses[0].Add(p.AResponses[0], proofP.SResponse)
}
}
func (p *ProofD) reconstructRangeProofStructures(pk *gabikeys.PublicKey) error {
p.cachedRangeStructures = make(map[int][]*rangeproof.ProofStructure)
for index, proofs := range p.RangeProofs {
p.cachedRangeStructures[index] = []*rangeproof.ProofStructure{}
for _, proof := range proofs {
s, err := proof.ExtractStructure(index, pk)
if err != nil {
return err
}
p.cachedRangeStructures[index] = append(p.cachedRangeStructures[index], s)
}
}
return nil
}
// correctResponseSizes checks the sizes of the elements in the ProofD proof.
func (p *ProofD) correctResponseSizes(pk *gabikeys.PublicKey) bool {
minimum := big.NewInt(0)
// Check range on the AResponses
maximum := new(big.Int).Lsh(big.NewInt(1), pk.Params.LmCommit+1)
maximum.Sub(maximum, big.NewInt(1))
for _, aResponse := range p.AResponses {
if aResponse.Cmp(minimum) < 0 || aResponse.Cmp(maximum) > 0 {
return false
}
}
// Check range EResponse
maximum.Lsh(big.NewInt(1), pk.Params.LeCommit+1)
maximum.Sub(maximum, big.NewInt(1))
return p.EResponse.Cmp(minimum) >= 0 && p.EResponse.Cmp(maximum) <= 0
}
// reconstructZ reconstructs Z from the information in the proof and the
// provided public key.
func (p *ProofD) reconstructZ(pk *gabikeys.PublicKey) (*big.Int, error) {
// known = Z / ( prod_{disclosed} R_i^{a_i} * A^{2^{l_e - 1}} )
numerator := new(big.Int).Lsh(big.NewInt(1), pk.Params.Le-1)
numerator.Exp(p.A, numerator, pk.N)
for i, attribute := range p.ADisclosed {
exp := attribute
if exp.BitLen() > int(pk.Params.Lm) {
exp = common.IntHashSha256(exp.Bytes())
}
numerator.Mul(numerator, new(big.Int).Exp(pk.R[i], exp, pk.N))
}
known := new(big.Int).ModInverse(numerator, pk.N)
if known == nil {
return nil, common.ErrNoModInverse
}
known.Mul(pk.Z, known)
knownC, err := common.ModPow(known, new(big.Int).Neg(p.C), pk.N)
if err != nil {
return nil, err
}
Ae, err := common.ModPow(p.A, p.EResponse, pk.N)
if err != nil {
return nil, err
}
Sv, err := common.ModPow(pk.S, p.VResponse, pk.N)
if err != nil {
return nil, err
}
Rs := big.NewInt(1)
for i, response := range p.AResponses {
t, err := common.ModPow(pk.R[i], response, pk.N)
if err != nil {
return nil, err
}
Rs.Mul(Rs, t)
}
Z := new(big.Int).Mul(knownC, Ae)
Z.Mul(Z, Rs).Mul(Z, Sv).Mod(Z, pk.N)
return Z, nil
}
// Verify verifies the proof against the given public key, context, and nonce.
func (p *ProofD) Verify(pk *gabikeys.PublicKey, context, nonce1 *big.Int, issig bool) bool {
contrib, err := p.ChallengeContribution(pk)
if err != nil {
return false
}
return p.VerifyWithChallenge(pk, createChallenge(context, nonce1, contrib, issig))
}
func (p *ProofD) HasNonRevocationProof() bool {
return p.NonRevocationProof != nil
}
// VerifyWithChallenge verifies the proof against the given public key and the provided
// reconstructed challenge.
func (p *ProofD) VerifyWithChallenge(pk *gabikeys.PublicKey, reconstructedChallenge *big.Int) bool {
var notrevoked bool
// Validate non-revocation
if p.HasNonRevocationProof() {
revIdx := p.revocationAttrIndex()
if revIdx < 0 || p.AResponses[revIdx] == nil {
return false
}
notrevoked = p.NonRevocationProof.VerifyWithChallenge(pk, reconstructedChallenge) &&
p.NonRevocationProof.Responses["alpha"].Cmp(p.AResponses[revIdx]) == 0
} else {
notrevoked = true
}
// Range proofs were already validated during challenge reconstruction
return notrevoked &&
p.correctResponseSizes(pk) &&
p.C.Cmp(reconstructedChallenge) == 0
}
// ChallengeContribution returns the contribution of this proof to the
// challenge.
func (p *ProofD) ChallengeContribution(pk *gabikeys.PublicKey) ([]*big.Int, error) {
z, err := p.reconstructZ(pk)
if err != nil {
return nil, errors.WrapPrefix(err, "Could not reconstruct Z", 0)
}
l := []*big.Int{p.A, z}
if p.NonRevocationProof != nil {
revIdx := p.revocationAttrIndex()
if revIdx < 0 || p.AResponses[revIdx] == nil {
return nil, errors.New("no revocation response found")
}
if err := p.NonRevocationProof.SetExpected(pk, p.C, p.AResponses[revIdx]); err != nil {
return nil, err
}
contrib := p.NonRevocationProof.ChallengeContributions(pk)
l = append(l, contrib...)
}
if p.RangeProofs != nil {
if p.cachedRangeStructures == nil {
if err := p.reconstructRangeProofStructures(pk); err != nil {
return nil, err
}
}
// need stable attribute order for rangeproof contributions, so determine max undisclosed attribute
maxAttribute := 0
for k := range p.AResponses {
if k > maxAttribute {
maxAttribute = k
}
}
for index := 0; index <= maxAttribute; index++ {
structures, ok := p.cachedRangeStructures[index]
if !ok {
continue
}
for i, s := range structures {
p.RangeProofs[index][i].MResponse = new(big.Int).Set(p.AResponses[index])
if !s.VerifyProofStructure(pk, p.RangeProofs[index][i]) {
return nil, errors.New("Invalid range proof")
}
l = append(l, s.CommitmentsFromProof(pk, p.RangeProofs[index][i], p.C)...)
}
}
}
return l, nil
}
// SecretKeyResponse returns the secret key response (as part of Proof
// interface).
func (p *ProofD) SecretKeyResponse() *big.Int {
return p.AResponses[0]
}
func (p *ProofD) revocationAttrIndex() int {
params := revocation.Parameters
max := new(big.Int).Lsh(big.NewInt(1), params.AttributeSize+params.ChallengeLength+params.ZkStat+1)
for idx, i := range p.AResponses {
if i.Cmp(max) < 0 {
return idx
}
}
return -1
}
// Challenge returns the challenge in the proof (part of the Proof interface).
func (p *ProofD) Challenge() *big.Int {
return p.C
}
// ProofP is a keyshare server's knowledge of its part of the secret key.
type ProofP struct {
P *big.Int `json:"P,omitempty"`
C *big.Int `json:"c"`
SResponse *big.Int `json:"s_response"`
}
// ProofPCommitment is a keyshare server's first message in its proof of knowledge
// of its part of the secret key.
type ProofPCommitment struct {
P *big.Int
Pcommit *big.Int
}
// GenerateNonce generates a nonce for use in proofs
func GenerateNonce() (*big.Int, error) {
return common.RandomBigInt(gabikeys.DefaultSystemParameters[2048].Lstatzk)
}