Skip to content

Commit 198cfc5

Browse files
circuit v3 beta.1 changes (#181)
* add v3 beta 1 --------- Co-authored-by: vmidyllic <[email protected]>
1 parent e5baf6a commit 198cfc5

36 files changed

+523
-391
lines changed

.mocharc.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
],
55
"spec": "tests/**/*.test.ts",
66
"require": "ts-node/register",
7-
"timeout": "240000"
7+
"timeout": "300000",
8+
"maxDiffSize": "10000"
89
}

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@0xpolygonid/js-sdk",
3-
"version": "1.8.3",
3+
"version": "1.9.0",
44
"description": "SDK to work with Polygon ID",
55
"main": "dist/node/cjs/index.js",
66
"module": "dist/node/esm/index.js",

src/circuits/atomic-query-v3-on-chain.ts

+13-38
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
BaseConfig,
44
bigIntArrayToStringArray,
55
prepareSiblingsStr,
6-
existenceToInt,
76
getNodeAuxValue,
87
prepareCircuitArrayValues
98
} from './common';
@@ -46,7 +45,7 @@ export class AtomicQueryV3OnChainInputs extends BaseConfig {
4645
linkNonce!: bigint;
4746
verifierID?: Id;
4847
nullifierSessionID!: bigint;
49-
authEnabled!: number;
48+
isBJJAuthEnabled!: number;
5049

5150
validate(): void {
5251
if (!this.requestID) {
@@ -61,6 +60,8 @@ export class AtomicQueryV3OnChainInputs extends BaseConfig {
6160
throw new Error(CircuitError.EmptyQueryValue);
6261
}
6362

63+
this.query.validateValueArraySize(this.getValueArrSize());
64+
6465
if (!this.proofType) {
6566
throw new Error(CircuitError.InvalidProofType);
6667
}
@@ -69,7 +70,7 @@ export class AtomicQueryV3OnChainInputs extends BaseConfig {
6970
throw new Error(CircuitError.EmptyChallenge);
7071
}
7172

72-
if (this.authEnabled === 1) {
73+
if (this.isBJJAuthEnabled === 1) {
7374
if (!this.authClaimIncMtp) {
7475
throw new Error(CircuitError.EmptyAuthClaimProof);
7576
}
@@ -214,7 +215,7 @@ export class AtomicQueryV3OnChainInputs extends BaseConfig {
214215
};
215216

216217
s.challenge = this.challenge?.toString();
217-
if (this.authEnabled === 1) {
218+
if (this.isBJJAuthEnabled === 1) {
218219
s.authClaim = this.authClaim?.marshalJson();
219220
s.userClaimsTreeRoot = this.treeState.claimsRoot?.bigInt().toString();
220221
s.userRevTreeRoot = this.treeState.revocationRoot?.bigInt().toString();
@@ -306,22 +307,22 @@ export class AtomicQueryV3OnChainInputs extends BaseConfig {
306307
s.issuerClaimNonRevMtpAuxHv = nodeAuxNonRev.value.bigInt().toString();
307308
s.issuerClaimNonRevMtpNoAux = nodeAuxNonRev.noAux;
308309

309-
s.claimPathNotExists = existenceToInt(valueProof.mtp.existence);
310310
const nodAuxJSONLD = getNodeAuxValue(valueProof.mtp);
311311
s.claimPathMtpNoAux = nodAuxJSONLD.noAux;
312312
s.claimPathMtpAuxHi = nodAuxJSONLD.key.bigInt().toString();
313313
s.claimPathMtpAuxHv = nodAuxJSONLD.value.bigInt().toString();
314314

315315
s.claimPathKey = valueProof.path.toString();
316316

317+
s.valueArraySize = this.query.values.length;
317318
const values = prepareCircuitArrayValues(this.query.values, this.getValueArrSize());
318319
s.value = bigIntArrayToStringArray(values);
319320

320321
s.linkNonce = this.linkNonce.toString();
321322
s.verifierID = this.verifierID?.bigInt().toString() ?? '0';
322323
s.nullifierSessionID = this.nullifierSessionID.toString();
323324

324-
s.authEnabled = this.authEnabled.toString();
325+
s.isBJJAuthEnabled = this.isBJJAuthEnabled.toString();
325326

326327
return byteEncoder.encode(JSON.stringify(s));
327328
}
@@ -367,7 +368,6 @@ interface AtomicQueryV3OnChainCircuitInputs {
367368
isRevocationChecked: number;
368369
// Query
369370
// JSON path
370-
claimPathNotExists: number; // 0 for inclusion, 1 for non-inclusion
371371
claimPathMtp: string[];
372372
claimPathMtpNoAux: string; // 1 if aux node is empty, 0 if non-empty or for inclusion proofs
373373
claimPathMtpAuxHi: string; // 0 for inclusion proof
@@ -379,6 +379,7 @@ interface AtomicQueryV3OnChainCircuitInputs {
379379
slotIndex: number;
380380
timestamp: number;
381381
value: string[];
382+
valueArraySize: number;
382383

383384
issuerClaimMtp: string[];
384385
issuerClaimClaimsTreeRoot: string;
@@ -412,7 +413,7 @@ interface AtomicQueryV3OnChainCircuitInputs {
412413
linkNonce: string;
413414
verifierID: string;
414415
nullifierSessionID: string;
415-
authEnabled: string;
416+
isBJJAuthEnabled: string;
416417
}
417418

418419
/**
@@ -426,23 +427,18 @@ export class AtomicQueryV3OnChainPubSignals extends BaseConfig {
426427
issuerState!: Hash;
427428
issuerClaimNonRevState!: Hash;
428429
timestamp!: number;
429-
merklized!: number;
430-
isRevocationChecked!: number;
431430
circuitQueryHash!: bigint;
432431
challenge!: bigint;
433432
gistRoot!: Hash;
434433
proofType!: number;
435434
linkID!: bigint;
436435
nullifier!: bigint;
437436
operatorOutput!: bigint;
438-
verifierID!: Id;
439-
nullifierSessionID!: bigint;
440-
authEnabled!: number;
437+
isBJJAuthEnabled!: number;
441438

442439
// PubSignalsUnmarshal unmarshal credentialAtomicQueryV3.circom public signals
443440
pubSignalsUnmarshal(data: Uint8Array): AtomicQueryV3OnChainPubSignals {
444441
// expected order:
445-
// merklized
446442
// userID
447443
// circuitQueryHash
448444
// issuerState
@@ -454,21 +450,14 @@ export class AtomicQueryV3OnChainPubSignals extends BaseConfig {
454450
// challenge
455451
// gistRoot
456452
// issuerID
457-
// isRevocationChecked
458453
// issuerClaimNonRevState
459454
// timestamp
460-
// verifierID
461-
// nullifierSessionID
462-
// authEnabled
455+
// isBJJAuthEnabled
463456

464457
const sVals: string[] = JSON.parse(byteDecoder.decode(data));
465458

466459
let fieldIdx = 0;
467460

468-
// -- merklized
469-
this.merklized = parseInt(sVals[fieldIdx]);
470-
fieldIdx++;
471-
472461
// - userID
473462
this.userID = Id.fromBigInt(BigInt(sVals[fieldIdx]));
474463
fieldIdx++;
@@ -513,10 +502,6 @@ export class AtomicQueryV3OnChainPubSignals extends BaseConfig {
513502
this.issuerID = Id.fromBigInt(BigInt(sVals[fieldIdx]));
514503
fieldIdx++;
515504

516-
// - isRevocationChecked
517-
this.isRevocationChecked = parseInt(sVals[fieldIdx]);
518-
fieldIdx++;
519-
520505
// - issuerClaimNonRevState
521506
this.issuerClaimNonRevState = Hash.fromString(sVals[fieldIdx]);
522507
fieldIdx++;
@@ -525,18 +510,8 @@ export class AtomicQueryV3OnChainPubSignals extends BaseConfig {
525510
this.timestamp = parseInt(sVals[fieldIdx]);
526511
fieldIdx++;
527512

528-
// - verifierID
529-
if (sVals[fieldIdx] !== '0') {
530-
this.verifierID = Id.fromBigInt(BigInt(sVals[fieldIdx]));
531-
}
532-
fieldIdx++;
533-
534-
// - nullifierSessionID
535-
this.nullifierSessionID = BigInt(sVals[fieldIdx]);
536-
fieldIdx++;
537-
538-
// - authEnabled
539-
this.authEnabled = parseInt(sVals[fieldIdx]);
513+
// - isBJJAuthEnabled
514+
this.isBJJAuthEnabled = parseInt(sVals[fieldIdx]);
540515

541516
return this;
542517
}

src/circuits/atomic-query-v3.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
BaseConfig,
44
bigIntArrayToStringArray,
55
prepareSiblingsStr,
6-
existenceToInt,
76
getNodeAuxValue,
87
prepareCircuitArrayValues
98
} from './common';
@@ -55,6 +54,8 @@ export class AtomicQueryV3Inputs extends BaseConfig {
5554
throw new Error(CircuitError.EmptyQueryValue);
5655
}
5756

57+
this.query.validateValueArraySize(this.getValueArrSize());
58+
5859
if (!this.proofType) {
5960
throw new Error(CircuitError.InvalidProofType);
6061
}
@@ -221,14 +222,13 @@ export class AtomicQueryV3Inputs extends BaseConfig {
221222
s.issuerClaimNonRevMtpAuxHv = nodeAuxNonRev.value.bigInt().toString();
222223
s.issuerClaimNonRevMtpNoAux = nodeAuxNonRev.noAux;
223224

224-
s.claimPathNotExists = existenceToInt(valueProof.mtp.existence);
225225
const nodAuxJSONLD = getNodeAuxValue(valueProof.mtp);
226226
s.claimPathMtpNoAux = nodAuxJSONLD.noAux;
227227
s.claimPathMtpAuxHi = nodAuxJSONLD.key.bigInt().toString();
228228
s.claimPathMtpAuxHv = nodAuxJSONLD.value.bigInt().toString();
229229

230230
s.claimPathKey = valueProof.path.toString();
231-
231+
s.valueArraySize = this.query.values.length;
232232
const values = prepareCircuitArrayValues(this.query.values, this.getValueArrSize());
233233
s.value = bigIntArrayToStringArray(values);
234234

@@ -280,7 +280,6 @@ interface AtomicQueryV3CircuitInputs {
280280
isRevocationChecked: number;
281281
// Query
282282
// JSON path
283-
claimPathNotExists: number; // 0 for inclusion, 1 for non-inclusion
284283
claimPathMtp: string[];
285284
claimPathMtpNoAux: string; // 1 if aux node is empty, 0 if non-empty or for inclusion proofs
286285
claimPathMtpAuxHi: string; // 0 for inclusion proof
@@ -292,6 +291,7 @@ interface AtomicQueryV3CircuitInputs {
292291
slotIndex: number;
293292
timestamp: number;
294293
value: string[];
294+
valueArraySize: number;
295295

296296
issuerClaimMtp: string[];
297297
issuerClaimClaimsTreeRoot: string;
@@ -320,10 +320,10 @@ export class AtomicQueryV3PubSignals extends BaseConfig {
320320
slotIndex!: number;
321321
operator!: number;
322322
value: bigint[] = [];
323+
valueArraySize!: number;
323324
timestamp!: number;
324325
merklized!: number;
325326
claimPathKey!: bigint;
326-
claimPathNotExists!: number;
327327
isRevocationChecked!: number;
328328
proofType!: number;
329329
linkID!: bigint;
@@ -348,11 +348,11 @@ export class AtomicQueryV3PubSignals extends BaseConfig {
348348
// issuerClaimNonRevState
349349
// timestamp
350350
// claimSchema
351-
// claimPathNotExists
352351
// claimPathKey
353352
// slotIndex
354353
// operator
355354
// value
355+
// valueArraySize
356356
// verifierID
357357
// nullifierSessionID
358358

@@ -425,10 +425,6 @@ export class AtomicQueryV3PubSignals extends BaseConfig {
425425
this.claimSchema = SchemaHash.newSchemaHashFromInt(BigInt(sVals[fieldIdx]));
426426
fieldIdx++;
427427

428-
// - ClaimPathNotExists
429-
this.claimPathNotExists = parseInt(sVals[fieldIdx]);
430-
fieldIdx++;
431-
432428
// - ClaimPathKey
433429
this.claimPathKey = BigInt(sVals[fieldIdx]);
434430
fieldIdx++;
@@ -447,6 +443,10 @@ export class AtomicQueryV3PubSignals extends BaseConfig {
447443
fieldIdx++;
448444
}
449445

446+
// - valueArraySize
447+
this.valueArraySize = parseInt(sVals[fieldIdx]);
448+
fieldIdx++;
449+
450450
// - verifierID
451451
if (sVals[fieldIdx] !== '0') {
452452
this.verifierID = Id.fromBigInt(BigInt(sVals[fieldIdx]));

src/circuits/common.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const ErrorEmptyIssuerAuthClaimNonRevProof =
2727
*/
2828
export class BaseConfig {
2929
mtLevel!: number; // Max levels of MT
30-
valueArraySize!: number; // Size if( value array in identity circuit)s
30+
maxValueArraySize!: number; // Size if( value array in identity circuit)s
3131
mtLevelOnChain!: number;
3232
mtLevelClaim!: number; // Max level of JSONLD claim
3333

@@ -54,7 +54,7 @@ export class BaseConfig {
5454
* @returns number
5555
*/
5656
getValueArrSize(): number {
57-
return this.valueArraySize ? this.valueArraySize : defaultValueArraySize;
57+
return this.maxValueArraySize ? this.maxValueArraySize : defaultValueArraySize;
5858
}
5959

6060
/**

src/circuits/comparer.ts

+24-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ export enum Operators {
3030
LTE = 7,
3131
GTE = 8,
3232
BETWEEN = 9,
33+
NONBETWEEN = 10,
34+
EXISTS = 11,
3335
SD = 16,
3436
NULLIFY = 17
3537
}
@@ -46,14 +48,19 @@ export const QueryOperators = {
4648
$lte: Operators.LTE,
4749
$gte: Operators.GTE,
4850
$between: Operators.BETWEEN,
51+
$nonbetween: Operators.NONBETWEEN,
52+
$exists: Operators.EXISTS,
4953
$sd: Operators.SD,
5054
$nullify: Operators.NULLIFY
5155
};
5256

5357
const allOperations = Object.values(QueryOperators);
5458

5559
export const availableTypesOperators: Map<string, Operators[]> = new Map([
56-
[XSDNS.Boolean, [QueryOperators.$eq, QueryOperators.$ne, QueryOperators.$sd]],
60+
[
61+
XSDNS.Boolean,
62+
[QueryOperators.$eq, QueryOperators.$ne, QueryOperators.$sd, QueryOperators.$exists]
63+
],
5764
[XSDNS.Integer, allOperations],
5865
[XSDNS.NonNegativeInteger, allOperations],
5966
[XSDNS.PositiveInteger, allOperations],
@@ -64,7 +71,8 @@ export const availableTypesOperators: Map<string, Operators[]> = new Map([
6471
QueryOperators.$ne,
6572
QueryOperators.$in,
6673
QueryOperators.$nin,
67-
QueryOperators.$sd
74+
QueryOperators.$sd,
75+
QueryOperators.$exists
6876
]
6977
],
7078
[
@@ -74,7 +82,8 @@ export const availableTypesOperators: Map<string, Operators[]> = new Map([
7482
QueryOperators.$ne,
7583
QueryOperators.$in,
7684
QueryOperators.$nin,
77-
QueryOperators.$sd
85+
QueryOperators.$sd,
86+
QueryOperators.$exists
7887
]
7988
],
8089
[XSDNS.DateTime, allOperations]
@@ -168,6 +177,16 @@ export class Vector implements IComparer {
168177
return this.y.includes(this.x);
169178
case Operators.NIN:
170179
return !this.y.includes(this.x);
180+
case Operators.BETWEEN:
181+
if (this.y.length !== 2) {
182+
return false;
183+
}
184+
return this.x >= this.y[0] && this.x <= this.y[1];
185+
case Operators.NONBETWEEN:
186+
if (this.y.length !== 2) {
187+
return false;
188+
}
189+
return this.x < this.y[0] || this.x > this.y[1];
171190
default:
172191
throw new Error('unknown compare type for vector');
173192
}
@@ -194,6 +213,8 @@ export const factoryComparer = (x: bigint, y: bigint[], operator: Operators): IC
194213
return new Scalar(x, y[0]);
195214
case Operators.IN:
196215
case Operators.NIN:
216+
case Operators.BETWEEN:
217+
case Operators.NONBETWEEN:
197218
return new Vector(x, y);
198219
default:
199220
throw new Error('unknown compare type');

0 commit comments

Comments
 (0)