Repository navigation
Expand file tree
/
Copy pathmodels.ts
More file actions
274 lines (260 loc) · 7 KB
/
Copy pathmodels.ts
File metadata and controls
274 lines (260 loc) · 7 KB
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
import { Signature } from '@iden3/js-crypto';
import { Claim, Claim as CoreClaim, Id } from '@iden3/js-iden3-core';
import { Hash, Proof } from '@iden3/js-merkletree';
import { Operators, QueryOperators } from './comparer';
/**
* TreeState is model for merkle tree roots
*
* @public
* @interface TreeState
*/
export interface TreeState {
state: Hash;
claimsRoot: Hash;
revocationRoot: Hash;
rootOfRoots: Hash;
}
/**
* claim non revocation status for circuit
* TreeState and Proof of inclusion / non-inclusion
*
* @public
* @interface ClaimNonRevStatus
*/
export interface ClaimNonRevStatus {
treeState: TreeState;
proof: Proof;
}
/**
* Query represents basic request to claim slot verification
*
* @public
* @class Query
*/
export class Query {
slotIndex!: number;
values!: bigint[];
operator!: number;
valueProof!: ValueProof;
/**
* Validates Query instance
*
*/
validate(): void {
if (
this.operator !== QueryOperators.$noop &&
this.operator !== QueryOperators.$sd &&
this.values?.some((v) => typeof v !== 'bigint')
)
throw new Error(CircuitError.EmptyQueryValue);
}
validateValueArraySize(maxArrSize: number): void {
switch (this.values.length) {
case 0: {
const zeroArrSizeOps = [Operators.NOOP, Operators.SD, Operators.NULLIFY];
if (!zeroArrSizeOps.includes(this.operator)) {
throw new Error(CircuitError.InvalidValuesArrSize);
}
return;
}
case 1: {
const oneArrSizeOps = [
Operators.EQ,
Operators.LT,
Operators.GT,
Operators.NE,
Operators.LTE,
Operators.GTE,
Operators.EXISTS
];
if (!oneArrSizeOps.includes(this.operator)) {
throw new Error(CircuitError.InvalidValuesArrSize);
}
return;
}
case 2: {
const twoArrSizeOps = [Operators.BETWEEN, Operators.NONBETWEEN];
if (!twoArrSizeOps.includes(this.operator)) {
throw new Error(CircuitError.InvalidValuesArrSize);
}
return;
}
default: {
const maxArrSizeOps = [Operators.IN, Operators.NIN];
if (!maxArrSizeOps.includes(this.operator)) {
throw new Error(CircuitError.InvalidOperationType);
}
if (!this.values.length || this.values.length > maxArrSize) {
throw new Error(CircuitError.InvalidValuesArrSize);
}
}
}
}
}
/**
* CircuitID is alias for circuit identifier
*
* @enum {number}
*/
export enum CircuitId {
// Auth is a type that must be used for authV2.circom
AuthV2 = 'authV2',
// StateTransition is a type that must be used for stateTransition.circom
StateTransition = 'stateTransition',
// AtomicQueryMTPV2 is a type for credentialAtomicQueryMTPV2.circom
AtomicQueryMTPV2 = 'credentialAtomicQueryMTPV2',
// AtomicQueryMTPV2OnChain is a type for credentialAtomicQueryMTPV2OnChain.circom
AtomicQueryMTPV2OnChain = 'credentialAtomicQueryMTPV2OnChain',
// AtomicQuerySig is a type for credentialAttrQuerySig.circom
AtomicQuerySigV2 = 'credentialAtomicQuerySigV2',
// AtomicQuerySigOnChain is a type for credentialAtomicQuerySigOnChain.circom
AtomicQuerySigV2OnChain = 'credentialAtomicQuerySigV2OnChain',
/**
* @beta
*/
// AtomicQueryV3CircuitID is a type for credentialAtomicQueryV3.circom
AtomicQueryV3 = 'credentialAtomicQueryV3-beta.1',
/**
* @beta
*/
// AtomicQueryV3OnChainCircuitID is a type for credentialAtomicQueryV3OnChain.circom
AtomicQueryV3OnChain = 'credentialAtomicQueryV3OnChain-beta.1',
/**
* @beta
*/
// LinkedMultiQuery is a type for linkedMultiQuery.circom
LinkedMultiQuery10 = 'linkedMultiQuery10-beta.1'
}
/**
* Claim structure for circuit inputs
*
* @public
* @class CircuitClaim
*/
export class CircuitClaim {
issuerId!: Id;
claim!: CoreClaim;
treeState!: TreeState;
proof!: Proof;
nonRevProof!: ClaimNonRevStatus; // Claim non revocation proof
signatureProof!: BJJSignatureProof;
}
/**
*
* Claim for circuit with non revocation proof and signature proof
* @public
* @interface ClaimWithSigProof
*/
export interface ClaimWithSigProof {
issuerID?: Id;
claim?: Claim;
nonRevProof?: MTProof;
signatureProof?: BJJSignatureProof;
}
/**
* Claim for circuit with non revocation proof and proof of merkle tree inclusion
*
* @public
* @interface ClaimWithMTPProof
*/
export interface ClaimWithMTPProof {
issuerID?: Id;
claim?: Claim;
incProof?: MTProof;
nonRevProof?: MTProof;
}
/**
* prepared bjj signature for circuits with auth bjj claim data
*
* @public
* @interface BJJSignatureProof
*/
export interface BJJSignatureProof {
signature: Signature;
issuerAuthClaim?: Claim;
issuerAuthIncProof: MTProof;
issuerAuthNonRevProof: MTProof;
}
/**
* prepared mtp with a tree state
*
* @public
* @interface MTProof
*/
export interface MTProof {
proof?: Proof;
treeState?: TreeState;
}
/**
* global identity state proof
*
* @public
* @interface GISTProof
*/
export interface GISTProof {
root: Hash;
proof: Proof;
}
/**
* List of errors of circuit inputs processing
*
* @enum {number}
*/
export enum CircuitError {
EmptyAuthClaimProof = 'empty auth claim mtp proof',
EmptyAuthClaimProofInTheNewState = 'empty auth claim mtp proof in the new state',
EmptyAuthClaimNonRevProof = 'empty auth claim non-revocation mtp proof',
EmptyChallengeSignature = 'empty challenge signature',
EmptyClaimSignature = 'empty claim signature',
EmptyClaimProof = 'empty claim mtp proof',
EmptyClaimNonRevProof = 'empty claim non-revocation mtp proof',
EmptyIssuerAuthClaimProof = 'empty issuer auth claim mtp proof',
EmptyIssuerAuthClaimNonRevProof = 'empty issuer auth claim non-revocation mtp proof',
EmptyJsonLDQueryProof = 'empty JSON-LD query mtp proof',
EmptyJsonLDQueryValue = 'empty JSON-LD query value',
EmptyJsonLDQueryPath = 'empty JSON-LD query path',
EmptyQueryValue = 'empty query value',
EmptyJsonLDQueryValues = 'empty JSON-LD query values',
EmptyId = 'empty Id',
EmptyChallenge = 'empty challenge',
EmptyGISTProof = 'empty GIST merkle tree proof',
EmptyTreeState = 'empty tree state',
EmptyRequestID = 'empty request ID',
InvalidProofType = 'invalid proof type',
InvalidValuesArrSize = 'invalid query Values array size',
InvalidOperationType = 'invalid operation type'
}
/**
* ValueProof represents a Merkle Proof for a value stored as MT
*
* @public
* @class ValueProof
*/
export class ValueProof {
path: bigint;
value: bigint;
mtp: Proof;
/**
* Creates an instance of ValueProof.
*/
constructor() {
this.path = BigInt(0);
this.value = BigInt(0);
this.mtp = new Proof();
}
/**
* validates instance of ValueProof
*
*/
validate(): void {
if (typeof this.path !== 'bigint') {
throw new Error(CircuitError.EmptyJsonLDQueryPath);
}
if (typeof this.value !== 'bigint') {
throw new Error(CircuitError.EmptyJsonLDQueryValue);
}
if (!this.mtp) {
throw new Error(CircuitError.EmptyJsonLDQueryProof);
}
}
}