-
Notifications
You must be signed in to change notification settings - Fork 2
/
proofs.go
227 lines (190 loc) · 7.21 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
// 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 "math/big"
// Proof represents a non-interactive zero-knowledge proof
type Proof interface {
VerifyWithChallenge(pk *PublicKey, reconstructedChallenge *big.Int) bool
SecretKeyResponse() *big.Int
ChallengeContribution(pk *PublicKey) []*big.Int
MergeProofP(proofP *ProofP, pk *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 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"`
}
func (p *ProofU) MergeProofP(proofP *ProofP, pk *PublicKey) {
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 *PublicKey, context, nonce *big.Int) bool {
return p.VerifyWithChallenge(pk, createChallenge(context, nonce, p.ChallengeContribution(pk), false))
}
// correctResponseSizes checks the sizes of the elements in the ProofU proof.
func (p *ProofU) correctResponseSizes(pk *PublicKey) bool {
maximum := new(big.Int).Lsh(bigONE, pk.Params.LvPrimeCommit+1)
maximum.Sub(maximum, bigONE)
minimum := new(big.Int).Neg(maximum)
return p.VPrimeResponse.Cmp(minimum) >= 0 && p.VPrimeResponse.Cmp(maximum) <= 0
}
// VerifyWithChallenge verifies whether the proof is correct.
func (p *ProofU) VerifyWithChallenge(pk *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 *PublicKey) *big.Int {
// Reconstruct Ucommit
// U_commit = U^{-C} * S^{VPrimeResponse} * R_0^{SResponse}
Uc := modPow(p.U, new(big.Int).Neg(p.C), pk.N)
Sv := modPow(pk.S, p.VPrimeResponse, pk.N)
R0s := modPow(pk.R[0], p.SResponse, pk.N)
Ucommit := new(big.Int).Mul(Uc, Sv)
Ucommit.Mul(Ucommit, R0s).Mod(Ucommit, pk.N)
return Ucommit
}
// 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 *PublicKey) []*big.Int {
return []*big.Int{p.U, p.reconstructUcommit(pk)}
}
// ProofS represents a proof.
type ProofS struct {
C *big.Int `json:"c"`
EResponse *big.Int `json:"e_response"`
}
// Verify verifies the proof agains the given public key, signature, context,
// and nonce.
func (p *ProofS) Verify(pk *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 := 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"`
}
func (p *ProofD) MergeProofP(proofP *ProofP, pk *PublicKey) {
p.SecretKeyResponse().Add(p.SecretKeyResponse(), proofP.SResponse)
}
// correctResponseSizes checks the sizes of the elements in the ProofD proof.
func (p *ProofD) correctResponseSizes(pk *PublicKey) bool {
// Check range on the AResponses
maximum := new(big.Int).Lsh(bigONE, pk.Params.LmCommit+1)
maximum.Sub(maximum, bigONE)
minimum := new(big.Int).Neg(maximum)
for _, aResponse := range p.AResponses {
if aResponse.Cmp(minimum) < 0 || aResponse.Cmp(maximum) > 0 {
return false
}
}
// Check range EResponse
maximum.Lsh(bigONE, pk.Params.LeCommit+1)
maximum.Sub(maximum, bigONE)
minimum.Neg(maximum)
if p.EResponse.Cmp(minimum) < 0 || p.EResponse.Cmp(maximum) > 0 {
return false
}
return true
}
// reconstructZ reconstructs Z from the information in the proof and the
// provided public key.
func (p *ProofD) reconstructZ(pk *PublicKey) *big.Int {
// known = Z / ( prod_{disclosed} R_i^{a_i} * A^{2^{l_e - 1}} )
numerator := new(big.Int).Lsh(bigONE, 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 = intHashSha256(exp.Bytes())
}
numerator.Mul(numerator, new(big.Int).Exp(pk.R[i], exp, pk.N))
}
known := new(big.Int).ModInverse(numerator, pk.N)
known.Mul(pk.Z, known)
knownC := modPow(known, new(big.Int).Neg(p.C), pk.N)
Ae := modPow(p.A, p.EResponse, pk.N)
Sv := modPow(pk.S, p.VResponse, pk.N)
Rs := big.NewInt(1)
for i, response := range p.AResponses {
Rs.Mul(Rs, modPow(pk.R[i], response, pk.N))
}
Z := new(big.Int).Mul(knownC, Ae)
Z.Mul(Z, Rs).Mul(Z, Sv).Mod(Z, pk.N)
return Z
}
// Verify verifies the proof against the given public key, context, and nonce.
func (p *ProofD) Verify(pk *PublicKey, context, nonce1 *big.Int, issig bool) bool {
return p.VerifyWithChallenge(pk, createChallenge(context, nonce1, p.ChallengeContribution(pk), issig))
}
// Verify verifies the proof against the given public key and the provided
// reconstruted challenge.
func (p *ProofD) VerifyWithChallenge(pk *PublicKey, reconstructedChallenge *big.Int) bool {
return p.correctResponseSizes(pk) && p.C.Cmp(reconstructedChallenge) == 0
}
// ChallengeContribution returns the contribution of this proof to the
// challenge.
func (p *ProofD) ChallengeContribution(pk *PublicKey) []*big.Int {
return []*big.Int{p.A, p.reconstructZ(pk)}
}
// SecretKeyResponse returns the secret key response (as part of Proof
// interface).
func (p *ProofD) SecretKeyResponse() *big.Int {
return p.AResponses[0]
}
// 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"`
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
}