-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlinkedMultiQuery.go
212 lines (177 loc) · 6.45 KB
/
linkedMultiQuery.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
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"
)
const LinkedMultiQueryLength = 10
// LinkedMultiQueryInputs type represent linkedMultiQuery10.circom inputs
type LinkedMultiQueryInputs struct {
BaseConfig
LinkNonce *big.Int
Claim *core.Claim
Query []*Query
}
// linkedMultiQueryCircuitInputs type reflect linkedMultiQuery10.circom private inputs required by prover
type linkedMultiQueryCircuitInputs struct {
LinkNonce string `json:"linkNonce"`
IssuerClaim *core.Claim `json:"issuerClaim"`
ClaimSchema string `json:"claimSchema"`
ClaimPathMtp [][]string `json:"claimPathMtp"`
ClaimPathMtpNoAux []string `json:"claimPathMtpNoAux"` // 1 if aux node is empty, 0 if non-empty or for inclusion proofs
ClaimPathMtpAuxHi []*merkletree.Hash `json:"claimPathMtpAuxHi"` // 0 for inclusion proof
ClaimPathMtpAuxHv []*merkletree.Hash `json:"claimPathMtpAuxHv"` // 0 for inclusion proof
ClaimPathKey []string `json:"claimPathKey"` // hash of path in merklized json-ld document
ClaimPathValue []string `json:"claimPathValue"` // value in this path in merklized json-ld document
SlotIndex []int `json:"slotIndex"`
Operator []int `json:"operator"`
Value [][]string `json:"value"`
ActualValueArraySize []int `json:"valueArraySize"`
}
func (l LinkedMultiQueryInputs) Validate() error {
if l.LinkNonce == nil {
return errors.New(ErrorEmptyLinkNonce)
}
if l.Claim == nil {
return errors.New(ErrorEmptyClaim)
}
if len(l.Query) == 0 {
return errors.New(ErrorEmptyQueries)
}
if len(l.Query) > LinkedMultiQueryLength {
return errors.New(ErrorTooManyQueries)
}
for _, q := range l.Query {
if q == nil {
continue
}
if err := q.ValidateValueArraySize(l.GetValueArrSize()); err != nil {
return err
}
}
return nil
}
// InputsMarshal returns Circom private inputs for linkedMultiQuery10.circom
func (l LinkedMultiQueryInputs) InputsMarshal() ([]byte, error) {
if err := l.Validate(); err != nil {
return nil, err
}
s := linkedMultiQueryCircuitInputs{}
s.LinkNonce = l.LinkNonce.String()
s.IssuerClaim = l.Claim
s.ClaimSchema = l.Claim.GetSchemaHash().BigInt().String()
s.ClaimPathMtp = make([][]string, LinkedMultiQueryLength)
s.ClaimPathMtpNoAux = make([]string, LinkedMultiQueryLength)
s.ClaimPathMtpAuxHi = make([]*merkletree.Hash, LinkedMultiQueryLength)
s.ClaimPathMtpAuxHv = make([]*merkletree.Hash, LinkedMultiQueryLength)
s.ClaimPathKey = make([]string, LinkedMultiQueryLength)
s.ClaimPathValue = make([]string, LinkedMultiQueryLength)
s.SlotIndex = make([]int, LinkedMultiQueryLength)
s.Operator = make([]int, LinkedMultiQueryLength)
s.Value = make([][]string, LinkedMultiQueryLength)
s.ActualValueArraySize = make([]int, LinkedMultiQueryLength)
for i := 0; i < LinkedMultiQueryLength; i++ {
if i >= len(l.Query) || l.Query[i] == nil {
s.ClaimPathMtp[i] = PrepareSiblingsStr([]*merkletree.Hash{}, l.GetMTLevelsClaim())
s.ClaimPathMtpNoAux[i] = "0"
s.ClaimPathMtpAuxHi[i] = &merkletree.HashZero
s.ClaimPathMtpAuxHv[i] = &merkletree.HashZero
s.ClaimPathKey[i] = "0"
s.ClaimPathValue[i] = "0"
s.SlotIndex[i] = 0
s.Operator[i] = 0
values, err := PrepareCircuitArrayValues(make([]*big.Int, 0), l.GetValueArrSize())
if err != nil {
return nil, err
}
s.Value[i] = bigIntArrayToStringArray(values)
s.ActualValueArraySize[i] = 0
continue
}
valueProof := l.Query[i].ValueProof
if valueProof == nil {
valueProof = &ValueProof{}
valueProof.Path = big.NewInt(0)
valueProof.Value = big.NewInt(0)
valueProof.MTP = &merkletree.Proof{}
}
s.ClaimPathMtp[i] = PrepareSiblingsStr(valueProof.MTP.AllSiblings(),
l.GetMTLevelsClaim())
nodAuxJSONLD := GetNodeAuxValue(valueProof.MTP)
s.ClaimPathMtpNoAux[i] = nodAuxJSONLD.noAux
s.ClaimPathMtpAuxHi[i] = nodAuxJSONLD.key
s.ClaimPathMtpAuxHv[i] = nodAuxJSONLD.value
s.ClaimPathKey[i] = valueProof.Path.String()
s.ClaimPathValue[i] = valueProof.Value.String()
s.SlotIndex[i] = l.Query[i].SlotIndex
s.Operator[i] = l.Query[i].Operator
s.ActualValueArraySize[i] = len(l.Query[i].Values)
values, err := PrepareCircuitArrayValues(l.Query[i].Values, l.GetValueArrSize())
if err != nil {
return nil, err
}
s.Value[i] = bigIntArrayToStringArray(values)
}
return json.Marshal(s)
}
// LinkedMultiQueryPubSignals linkedMultiQuery10.circom public signals
type LinkedMultiQueryPubSignals struct {
LinkID *big.Int `json:"linkID"`
Merklized int `json:"merklized"`
OperatorOutput []*big.Int `json:"operatorOutput"`
CircuitQueryHash []*big.Int `json:"circuitQueryHash"`
}
// PubSignalsUnmarshal unmarshal linkedMultiQuery10.circom public inputs to LinkedMultiQueryPubSignals
func (lo *LinkedMultiQueryPubSignals) PubSignalsUnmarshal(data []byte) error {
// expected order:
// linkID
// merklized
// operatorOutput
// circuitQueryHash
outputsLength := LinkedMultiQueryLength*2 + 2
var sVals []string
err := json.Unmarshal(data, &sVals)
if err != nil {
return err
}
if len(sVals) != outputsLength {
return fmt.Errorf("invalid number of Output values expected {%d} go {%d} ", outputsLength, len(sVals))
}
var ok bool
fieldIdx := 0
// - linkID
if lo.LinkID, ok = big.NewInt(0).SetString(sVals[fieldIdx], 10); !ok {
return fmt.Errorf("invalid link ID value: '%s'", sVals[fieldIdx])
}
fieldIdx++
// -- merklized
if lo.Merklized, err = strconv.Atoi(sVals[fieldIdx]); err != nil {
return err
}
fieldIdx++
// -- operatorOutput
lo.OperatorOutput = make([]*big.Int, LinkedMultiQueryLength)
for i := 0; i < LinkedMultiQueryLength; i++ {
if lo.OperatorOutput[i], ok = big.NewInt(0).SetString(sVals[fieldIdx], 10); !ok {
return fmt.Errorf("invalid operator output value: '%s'", sVals[fieldIdx])
}
fieldIdx++
}
// -- circuitQueryHash
lo.CircuitQueryHash = make([]*big.Int, LinkedMultiQueryLength)
for i := 0; i < LinkedMultiQueryLength; i++ {
if lo.CircuitQueryHash[i], ok = big.NewInt(0).SetString(sVals[fieldIdx], 10); !ok {
return fmt.Errorf("invalid query hash value: '%s'", sVals[fieldIdx])
}
fieldIdx++
}
return nil
}
// GetObjMap returns LinkedMultiQueryPubSignals as a map
func (l LinkedMultiQueryPubSignals) GetObjMap() map[string]interface{} {
return toMap(l)
}