-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsybilCredentialAtomicSig.go
285 lines (222 loc) · 9.79 KB
/
sybilCredentialAtomicSig.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
package circuits
import (
"encoding/json"
"fmt"
"math/big"
"strconv"
core "github.com/iden3/go-iden3-core/v2"
"github.com/iden3/go-merkletree-sql/v2"
"github.com/pkg/errors"
)
// Deprecated: trusted setup is not done for that circuits. In future released
// new circuit with sybil support will be added.
type SybilAtomicSigInputs struct {
BaseConfig
ID *core.ID
ProfileNonce *big.Int
ClaimSubjectProfileNonce *big.Int
IssuerClaim ClaimWithSigProof
StateCommitmentClaim ClaimWithMTPProof
GISTProof GISTProof
CRS *big.Int
RequestID *big.Int
Timestamp int64
}
type sybilAtomicSigCircuitInputs struct {
IssuerAuthClaim *core.Claim `json:"issuerAuthClaim"`
IssuerAuthClaimMtp []*merkletree.Hash `json:"issuerAuthClaimMtp"`
IssuerAuthClaimsRoot *merkletree.Hash `json:"issuerAuthClaimsRoot"`
IssuerAuthRevRoot *merkletree.Hash `json:"issuerAuthRevRoot"`
IssuerAuthRootsRoot *merkletree.Hash `json:"issuerAuthRootsRoot"`
IssuerAuthClaimNonRevMtp []*merkletree.Hash `json:"issuerAuthClaimNonRevMtp"`
IssuerAuthClaimNonRevMtpAuxHi *merkletree.Hash `json:"issuerAuthClaimNonRevMtpAuxHi"`
IssuerAuthClaimNonRevMtpAuxHv *merkletree.Hash `json:"issuerAuthClaimNonRevMtpAuxHv"`
IssuerAuthClaimNonRevMtpNoAux string `json:"issuerAuthClaimNonRevMtpNoAux"`
IssuerClaim *core.Claim `json:"issuerClaim"`
IssuerClaimNonRevClaimsRoot *merkletree.Hash `json:"issuerClaimNonRevClaimsRoot"`
IssuerClaimNonRevRevRoot *merkletree.Hash `json:"issuerClaimNonRevRevRoot"`
IssuerClaimNonRevRootsRoot *merkletree.Hash `json:"issuerClaimNonRevRootsRoot"`
IssuerClaimNonRevState *merkletree.Hash `json:"issuerClaimNonRevState"`
IssuerClaimNonRevMtp []*merkletree.Hash `json:"issuerClaimNonRevMtp"`
IssuerClaimNonRevMtpAuxHi *merkletree.Hash `json:"issuerClaimNonRevMtpAuxHi"`
IssuerClaimNonRevMtpAuxHv *merkletree.Hash `json:"issuerClaimNonRevMtpAuxHv"`
IssuerClaimNonRevMtpNoAux string `json:"issuerClaimNonRevMtpNoAux"`
IssuerClaimSignatureR8X string `json:"issuerClaimSignatureR8x"`
IssuerClaimSignatureR8Y string `json:"issuerClaimSignatureR8y"`
IssuerClaimSignatureS string `json:"issuerClaimSignatureS"`
ClaimSchema string `json:"claimSchema"`
StateCommitmentClaim *core.Claim `json:"stateCommitmentClaim"`
StateCommitmentClaimMtp []*merkletree.Hash `json:"stateCommitmentClaimMtp"`
StateCommitmentClaimClaimsRoot *merkletree.Hash `json:"stateCommitmentClaimClaimsRoot"`
StateCommitmentClaimRevRoot *merkletree.Hash `json:"stateCommitmentClaimRevRoot"`
StateCommitmentClaimRootsRoot *merkletree.Hash `json:"stateCommitmentClaimRootsRoot"`
StateCommitmentClaimIdenState *merkletree.Hash `json:"stateCommitmentClaimIdenState"`
GistRoot *merkletree.Hash `json:"gistRoot"`
GistMtp []*merkletree.Hash `json:"gistMtp"`
GistMtpAuxHi *merkletree.Hash `json:"gistMtpAuxHi"`
GistMtpAuxHv *merkletree.Hash `json:"gistMtpAuxHv"`
GistMtpNoAux string `json:"gistMtpNoAux"`
CRS string `json:"crs"`
// user data
UserGenesisID string `json:"userGenesisID"`
ProfileNonce string `json:"profileNonce"`
ClaimSubjectProfileNonce string `json:"claimSubjectProfileNonce"`
RequestID *big.Int `json:"requestID"`
IssuerID string `json:"issuerID"`
Timestamp int64 `json:"timestamp"`
}
func (s SybilAtomicSigInputs) Validate() error {
if s.ID == nil {
return errors.New(ErrorEmptyID)
}
if s.GISTProof.Proof == nil {
return errors.New(ErrorEmptyGISTProof)
}
if s.IssuerClaim.Claim == nil {
return errors.New(ErrorEmptyGISTProof)
}
if s.StateCommitmentClaim.Claim == nil {
return errors.New(ErrorEmptyGISTProof)
}
return nil
}
func (s SybilAtomicSigInputs) InputsMarshal() ([]byte, error) {
if err := s.Validate(); err != nil {
return nil, err
}
sigInputs := sybilAtomicSigCircuitInputs{
IssuerAuthClaim: s.IssuerClaim.SignatureProof.IssuerAuthClaim,
IssuerAuthClaimMtp: CircomSiblings(s.IssuerClaim.SignatureProof.IssuerAuthIncProof.Proof,
s.GetMTLevel()),
IssuerAuthClaimNonRevMtp: CircomSiblings(s.IssuerClaim.SignatureProof.IssuerAuthNonRevProof.Proof,
s.GetMTLevel()),
IssuerAuthRevRoot: s.IssuerClaim.SignatureProof.IssuerAuthIncProof.TreeState.RevocationRoot,
IssuerAuthClaimsRoot: s.IssuerClaim.SignatureProof.IssuerAuthIncProof.TreeState.ClaimsRoot,
IssuerAuthRootsRoot: s.IssuerClaim.SignatureProof.IssuerAuthIncProof.TreeState.RootOfRoots,
IssuerClaimSignatureR8X: s.IssuerClaim.SignatureProof.Signature.R8.X.String(),
IssuerClaimSignatureR8Y: s.IssuerClaim.SignatureProof.Signature.R8.Y.String(),
IssuerClaimSignatureS: s.IssuerClaim.SignatureProof.Signature.S.String(),
IssuerClaim: s.IssuerClaim.Claim,
IssuerClaimNonRevMtp: CircomSiblings(s.IssuerClaim.NonRevProof.Proof, s.GetMTLevel()),
IssuerClaimNonRevClaimsRoot: s.IssuerClaim.NonRevProof.TreeState.ClaimsRoot,
IssuerClaimNonRevRevRoot: s.IssuerClaim.NonRevProof.TreeState.RevocationRoot,
IssuerClaimNonRevRootsRoot: s.IssuerClaim.NonRevProof.TreeState.RootOfRoots,
IssuerClaimNonRevState: s.IssuerClaim.NonRevProof.TreeState.State,
ClaimSchema: s.IssuerClaim.Claim.GetSchemaHash().BigInt().String(),
StateCommitmentClaim: s.StateCommitmentClaim.Claim,
StateCommitmentClaimMtp: CircomSiblings(s.StateCommitmentClaim.IncProof.Proof, s.GetMTLevel()),
StateCommitmentClaimClaimsRoot: s.StateCommitmentClaim.IncProof.TreeState.ClaimsRoot,
StateCommitmentClaimRevRoot: s.StateCommitmentClaim.IncProof.TreeState.RevocationRoot,
StateCommitmentClaimRootsRoot: s.StateCommitmentClaim.IncProof.TreeState.RootOfRoots,
StateCommitmentClaimIdenState: s.StateCommitmentClaim.IncProof.TreeState.State,
GistRoot: s.GISTProof.Root,
GistMtp: CircomSiblings(s.GISTProof.Proof, s.GetMTLevelOnChain()),
CRS: s.CRS.String(),
UserGenesisID: s.ID.BigInt().String(),
ProfileNonce: s.ProfileNonce.String(),
ClaimSubjectProfileNonce: s.ClaimSubjectProfileNonce.String(),
}
nodeAuxAuth := GetNodeAuxValue(s.IssuerClaim.NonRevProof.Proof)
sigInputs.IssuerClaimNonRevMtpNoAux = nodeAuxAuth.noAux
sigInputs.IssuerClaimNonRevMtpAuxHi = nodeAuxAuth.key
sigInputs.IssuerClaimNonRevMtpAuxHv = nodeAuxAuth.value
gistNodeAux := GetNodeAuxValue(s.GISTProof.Proof)
sigInputs.GistMtpAuxHi = gistNodeAux.key
sigInputs.GistMtpAuxHv = gistNodeAux.value
sigInputs.GistMtpNoAux = gistNodeAux.noAux
issuerAuthAux := GetNodeAuxValue(s.IssuerClaim.SignatureProof.IssuerAuthNonRevProof.Proof)
sigInputs.IssuerAuthClaimNonRevMtpNoAux = issuerAuthAux.noAux
sigInputs.IssuerAuthClaimNonRevMtpAuxHi = issuerAuthAux.key
sigInputs.IssuerAuthClaimNonRevMtpAuxHv = issuerAuthAux.value
sigInputs.RequestID = s.RequestID
sigInputs.IssuerID = s.IssuerClaim.IssuerID.BigInt().String()
sigInputs.Timestamp = s.Timestamp
return json.Marshal(sigInputs)
}
// Deprecated: trusted setup is not done for that circuits. In future released
// new circuit with sybil support will be added.
type SybilAtomicSigPubSignals struct {
BaseConfig
SybilID *big.Int `json:"sybilID"`
UserID *core.ID `json:"userID"`
RequestID *big.Int `json:"requestID"`
IssuerID *core.ID `json:"issuerID"`
Timestamp int64 `json:"timestamp"`
IssuerClaimNonRevState *merkletree.Hash `json:"issuerClaimNonRevState"`
ClaimSchema core.SchemaHash `json:"claimSchema"`
CRS *big.Int `json:"crs"`
GISTRoot *merkletree.Hash `json:"gistRoot"`
IssuerAuthState *merkletree.Hash `json:"issuerAuthState"`
}
func (s *SybilAtomicSigPubSignals) PubSignalsUnmarshal(data []byte) error {
var sVals []string
err := json.Unmarshal(data, &sVals)
if err != nil {
return err
}
if len(sVals) != 10 {
return fmt.Errorf("invalid number of Output values expected {%d} got {%d} ", 10, len(sVals))
}
// expected order:
// 0 - issuerAuthState
// 1 - sybilID
// 2 - userID
// 3 - issuerClaimNonRevState
// 4 - claimSchema
// 5 - gistRoot
// 6 - crs
// 7 - requestID
// 8 - issuerID
// 9 - timestamp
fieldIdx := 0
if s.IssuerAuthState, err = merkletree.NewHashFromString(sVals[fieldIdx]); err != nil {
return fmt.Errorf("invalid IssuerAuthState value: '%s'", sVals[fieldIdx])
}
fieldIdx++
var ok bool
if s.SybilID, ok = big.NewInt(0).SetString(sVals[fieldIdx], 10); !ok {
return fmt.Errorf("invalid SybilID value: '%s'", sVals[fieldIdx])
}
fieldIdx++
if s.UserID, err = idFromIntStr(sVals[fieldIdx]); err != nil {
return err
}
if err != nil {
return fmt.Errorf("invalid UserID value: '%s'", sVals[fieldIdx])
}
fieldIdx++
if s.IssuerClaimNonRevState, err = merkletree.NewHashFromString(sVals[fieldIdx]); err != nil {
return fmt.Errorf("invalid IssuerClaimNonRevState value: '%s'", sVals[fieldIdx])
}
fieldIdx++
var schemaInt *big.Int
if schemaInt, ok = big.NewInt(0).SetString(sVals[fieldIdx], 10); !ok {
return fmt.Errorf("invalid ClaimSchema value: '%s'", sVals[fieldIdx])
}
s.ClaimSchema = core.NewSchemaHashFromInt(schemaInt)
fieldIdx++
if s.GISTRoot, err = merkletree.NewHashFromString(sVals[fieldIdx]); err != nil {
return fmt.Errorf("invalid GISTRoot value: '%s'", sVals[fieldIdx])
}
fieldIdx++
if s.CRS, ok = big.NewInt(0).SetString(sVals[fieldIdx], 10); !ok {
return fmt.Errorf("invalid schema value: '%s'", sVals[fieldIdx])
}
fieldIdx++
if s.RequestID, ok = big.NewInt(0).SetString(sVals[fieldIdx], 10); !ok {
return fmt.Errorf("invalid requestID value: '%s'", sVals[fieldIdx])
}
fieldIdx++
if s.IssuerID, err = idFromIntStr(sVals[fieldIdx]); err != nil {
return fmt.Errorf("invalid IssuerID value: '%s'", sVals[fieldIdx])
}
fieldIdx++
s.Timestamp, err = strconv.ParseInt(sVals[fieldIdx], 10, 64)
if err != nil {
return fmt.Errorf("invalid Timestamp value: '%s'", sVals[fieldIdx])
}
return nil
}
func (s SybilAtomicSigPubSignals) GetObjMap() map[string]interface{} {
return toMap(s)
}