-
Notifications
You must be signed in to change notification settings - Fork 5
/
credentialAtomicQueryMTP.go
241 lines (204 loc) · 9.31 KB
/
credentialAtomicQueryMTP.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
package circuits
import (
"encoding/json"
"fmt"
"math/big"
"strconv"
core "github.com/iden3/go-iden3-core/v2"
"github.com/iden3/go-iden3-crypto/babyjub"
"github.com/iden3/go-merkletree-sql/v2"
"github.com/pkg/errors"
)
// AtomicQueryMTPInputs ZK private inputs for credentialAtomicQueryMTP.circom
// Deprecated: use AtomicQueryMTPV2Inputs instead
type AtomicQueryMTPInputs struct {
BaseConfig
// auth
ID *core.ID
AuthClaim ClaimWithMTPProof
Challenge *big.Int
Signature *babyjub.Signature
Claim ClaimWithMTPProof // claim issued for user
CurrentTimeStamp int64
// query
Query
}
// stateTransitionInputsInternal type represents credentialAtomicQueryMTP.circom private inputs required by prover
type atomicQueryMTPCircuitInputs struct {
UserAuthClaim *core.Claim `json:"userAuthClaim"`
UserAuthClaimMtp []string `json:"userAuthClaimMtp"`
UserAuthClaimNonRevMtp []string `json:"userAuthClaimNonRevMtp"`
UserAuthClaimNonRevMtpAuxHi *merkletree.Hash `json:"userAuthClaimNonRevMtpAuxHi"`
UserAuthClaimNonRevMtpAuxHv *merkletree.Hash `json:"userAuthClaimNonRevMtpAuxHv"`
UserAuthClaimNonRevMtpNoAux string `json:"userAuthClaimNonRevMtpNoAux"`
UserClaimsTreeRoot *merkletree.Hash `json:"userClaimsTreeRoot"`
UserState *merkletree.Hash `json:"userState"`
UserRevTreeRoot *merkletree.Hash `json:"userRevTreeRoot"`
UserRootsTreeRoot *merkletree.Hash `json:"userRootsTreeRoot"`
UserID string `json:"userID"`
Challenge string `json:"challenge"`
ChallengeSignatureR8X string `json:"challengeSignatureR8x"`
ChallengeSignatureR8Y string `json:"challengeSignatureR8y"`
ChallengeSignatureS string `json:"challengeSignatureS"`
IssuerClaim *core.Claim `json:"issuerClaim"`
IssuerClaimClaimsTreeRoot *merkletree.Hash `json:"issuerClaimClaimsTreeRoot"`
IssuerClaimIdenState *merkletree.Hash `json:"issuerClaimIdenState"`
IssuerClaimMtp []string `json:"issuerClaimMtp"`
IssuerClaimRevTreeRoot *merkletree.Hash `json:"issuerClaimRevTreeRoot"`
IssuerClaimRootsTreeRoot *merkletree.Hash `json:"issuerClaimRootsTreeRoot"`
IssuerClaimNonRevClaimsTreeRoot *merkletree.Hash `json:"issuerClaimNonRevClaimsTreeRoot"`
IssuerClaimNonRevRevTreeRoot *merkletree.Hash `json:"issuerClaimNonRevRevTreeRoot"`
IssuerClaimNonRevRootsTreeRoot *merkletree.Hash `json:"issuerClaimNonRevRootsTreeRoot"`
IssuerClaimNonRevState *merkletree.Hash `json:"issuerClaimNonRevState"`
IssuerClaimNonRevMtp []string `json:"issuerClaimNonRevMtp"`
IssuerClaimNonRevMtpAuxHi *merkletree.Hash `json:"issuerClaimNonRevMtpAuxHi"`
IssuerClaimNonRevMtpAuxHv *merkletree.Hash `json:"issuerClaimNonRevMtpAuxHv"`
IssuerClaimNonRevMtpNoAux string `json:"issuerClaimNonRevMtpNoAux"`
ClaimSchema string `json:"claimSchema"`
IssuerID string `json:"issuerID"`
Operator int `json:"operator"`
SlotIndex int `json:"slotIndex"`
Timestamp int64 `json:"timestamp,string"`
Value []string `json:"value"`
}
// InputsMarshal returns Circom private inputs for credentialAtomicQueryMTP.circom
func (a AtomicQueryMTPInputs) InputsMarshal() ([]byte, error) {
if a.AuthClaim.IncProof.Proof == nil {
return nil, errors.New(ErrorEmptyAuthClaimProof)
}
if a.AuthClaim.NonRevProof.Proof == nil {
return nil, errors.New(ErrorEmptyAuthClaimNonRevProof)
}
if a.Claim.IncProof.Proof == nil {
return nil, errors.New(ErrorEmptyClaimProof)
}
if a.Claim.NonRevProof.Proof == nil {
return nil, errors.New(ErrorEmptyClaimNonRevProof)
}
if a.Signature == nil {
return nil, errors.New(ErrorEmptyChallengeSignature)
}
s := atomicQueryMTPCircuitInputs{
UserAuthClaim: a.AuthClaim.Claim,
UserAuthClaimMtp: PrepareSiblingsStr(a.AuthClaim.IncProof.Proof.AllSiblings(),
a.GetMTLevel()),
UserAuthClaimNonRevMtp: PrepareSiblingsStr(a.AuthClaim.NonRevProof.Proof.AllSiblings(),
a.GetMTLevel()),
Challenge: a.Challenge.String(),
ChallengeSignatureR8X: a.Signature.R8.X.String(),
ChallengeSignatureR8Y: a.Signature.R8.Y.String(),
ChallengeSignatureS: a.Signature.S.String(),
IssuerClaim: a.Claim.Claim,
IssuerClaimClaimsTreeRoot: a.Claim.IncProof.TreeState.ClaimsRoot,
IssuerClaimIdenState: a.Claim.IncProof.TreeState.State,
IssuerClaimMtp: PrepareSiblingsStr(a.Claim.IncProof.Proof.AllSiblings(), a.GetMTLevel()),
IssuerClaimRevTreeRoot: a.Claim.IncProof.TreeState.RevocationRoot,
IssuerClaimRootsTreeRoot: a.Claim.IncProof.TreeState.RootOfRoots,
IssuerClaimNonRevClaimsTreeRoot: a.Claim.NonRevProof.TreeState.ClaimsRoot,
IssuerClaimNonRevRevTreeRoot: a.Claim.NonRevProof.TreeState.RevocationRoot,
IssuerClaimNonRevRootsTreeRoot: a.Claim.NonRevProof.TreeState.RootOfRoots,
IssuerClaimNonRevState: a.Claim.NonRevProof.TreeState.State,
IssuerClaimNonRevMtp: PrepareSiblingsStr(a.Claim.NonRevProof.Proof.AllSiblings(),
a.GetMTLevel()),
ClaimSchema: a.Claim.Claim.GetSchemaHash().BigInt().String(),
UserClaimsTreeRoot: a.AuthClaim.IncProof.TreeState.ClaimsRoot,
UserState: a.AuthClaim.IncProof.TreeState.State,
UserRevTreeRoot: a.AuthClaim.IncProof.TreeState.RevocationRoot,
UserRootsTreeRoot: a.AuthClaim.IncProof.TreeState.RootOfRoots,
UserID: a.ID.BigInt().String(),
IssuerID: a.Claim.IssuerID.BigInt().String(),
Operator: a.Operator,
SlotIndex: a.SlotIndex,
Timestamp: a.CurrentTimeStamp,
}
values, err := PrepareCircuitArrayValues(a.Values, a.GetValueArrSize())
if err != nil {
return nil, err
}
s.Value = bigIntArrayToStringArray(values)
nodeAuxAuth := GetNodeAuxValue(a.AuthClaim.NonRevProof.Proof)
s.UserAuthClaimNonRevMtpAuxHi = nodeAuxAuth.key
s.UserAuthClaimNonRevMtpAuxHv = nodeAuxAuth.value
s.UserAuthClaimNonRevMtpNoAux = nodeAuxAuth.noAux
nodeAux := GetNodeAuxValue(a.Claim.NonRevProof.Proof)
s.IssuerClaimNonRevMtpAuxHi = nodeAux.key
s.IssuerClaimNonRevMtpAuxHv = nodeAux.value
s.IssuerClaimNonRevMtpNoAux = nodeAux.noAux
return json.Marshal(s)
}
// AtomicQueryMTPPubSignals public signals
// Deprecated: use AtomicQueryMTPV2PubSignals instead
type AtomicQueryMTPPubSignals struct {
BaseConfig
UserID *core.ID `json:"userID"`
UserState *merkletree.Hash `json:"userState"`
Challenge *big.Int `json:"challenge"`
ClaimSchema core.SchemaHash `json:"claimSchema"`
IssuerClaimIdenState *merkletree.Hash `json:"issuerClaimIdenState"`
IssuerClaimNonRevState *merkletree.Hash `json:"issuerClaimNonRevState"`
IssuerID *core.ID `json:"issuerID"`
SlotIndex int `json:"slotIndex"`
Values []*big.Int `json:"values"`
Operator int `json:"operator"`
Timestamp int64 `json:"timestamp"`
}
// PubSignalsUnmarshal unmarshal credentialAtomicQueryMTP.circom public signals array to AtomicQueryMTPPubSignals
func (ao *AtomicQueryMTPPubSignals) PubSignalsUnmarshal(data []byte) error {
// 10 is a number of fields in AtomicQueryMTPPubSignals before values, values is last element in the proof and
// it is length could be different base on the circuit configuration. The length could be modified by set value
// in ValueArraySize
const fieldLength = 10
var sVals []string
err := json.Unmarshal(data, &sVals)
if err != nil {
return err
}
if len(sVals) != fieldLength+ao.GetValueArrSize() {
return fmt.Errorf("invalid number of Output values expected {%d} go {%d} ", fieldLength+ao.GetValueArrSize(), len(sVals))
}
if ao.UserID, err = idFromIntStr(sVals[0]); err != nil {
return err
}
if ao.UserState, err = merkletree.NewHashFromString(sVals[1]); err != nil {
return err
}
var ok bool
if ao.Challenge, ok = big.NewInt(0).SetString(sVals[2], 10); !ok {
return fmt.Errorf("invalid challenge value: '%s'", sVals[0])
}
if ao.IssuerClaimIdenState, err = merkletree.NewHashFromString(sVals[3]); err != nil {
return err
}
if ao.IssuerID, err = idFromIntStr(sVals[4]); err != nil {
return err
}
if ao.IssuerClaimNonRevState, err = merkletree.NewHashFromString(sVals[5]); err != nil {
return err
}
if ao.Timestamp, err = strconv.ParseInt(sVals[6], 10, 64); err != nil {
return err
}
var schemaInt *big.Int
if schemaInt, ok = big.NewInt(0).SetString(sVals[7], 10); !ok {
return fmt.Errorf("invalid schema value: '%s'", sVals[0])
}
ao.ClaimSchema = core.NewSchemaHashFromInt(schemaInt)
if ao.SlotIndex, err = strconv.Atoi(sVals[8]); err != nil {
return err
}
if ao.Operator, err = strconv.Atoi(sVals[9]); err != nil {
return err
}
for i, v := range sVals[fieldLength : fieldLength+ao.GetValueArrSize()] {
bi, ok := big.NewInt(0).SetString(v, 10)
if !ok {
return fmt.Errorf("invalid value in index: %d", i)
}
ao.Values = append(ao.Values, bi)
}
return nil
}
// GetObjMap returns struct field as a map
func (ao AtomicQueryMTPPubSignals) GetObjMap() map[string]interface{} {
return toMap(ao)
}