Skip to content

Commit

Permalink
update js-sdk to 1.9.0 version & update tests with beta.1 circuits
Browse files Browse the repository at this point in the history
  • Loading branch information
volodymyr-basiuk committed Mar 20, 2024
1 parent aa6f31b commit 3b32b74
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 110 deletions.
68 changes: 37 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
},
"dependencies": {
"@iden3/js-crypto": "1.0.3",
"@0xpolygonid/js-sdk": "1.7.4",
"@0xpolygonid/js-sdk": "1.9.0",
"@iden3/js-iden3-core": "1.2.1",
"@iden3/js-jsonld-merklization": "1.1.2",
"@iden3/js-jwz": "1.2.1",
"@iden3/js-jwz": "1.3.0",
"@iden3/js-merkletree": "1.1.2",
"did-resolver": "^4.1.0",
"ethers": "^5.4.0",
Expand Down
2 changes: 0 additions & 2 deletions src/circuits/atomicV3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export class AtomicQueryV3PubSignalsVerifier
timestamp: this.pubSignals.timestamp,
merklized: this.pubSignals.merklized,
claimPathKey: this.pubSignals.claimPathKey,
claimPathNotExists: this.pubSignals.claimPathNotExists,
valueArraySize: valuesSize,
isRevocationChecked: this.pubSignals.isRevocationChecked,
operatorOutput: this.pubSignals.operatorOutput
Expand All @@ -77,7 +76,6 @@ export class AtomicQueryV3PubSignalsVerifier
}

const nSessionId = BigInt((params?.nullifierSessionId as string) ?? 0);

if (nSessionId !== 0n) {
if (BigInt(nullifier ?? 0) === 0n) {
throw new Error('nullifier should be provided for nullification and should not be 0');
Expand Down
87 changes: 64 additions & 23 deletions src/circuits/linkedMultiQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ import {
LinkedMultiQueryPubSignals,
byteEncoder,
cacheLoader,
createSchemaHash,
parseQueriesMetadata
parseQueriesMetadata,
calculateQueryHashV3,
calculateCoreSchemaHash,
QueryMetadata,
LinkedMultiQueryInputs,
Operators,
fieldValueFromVerifiablePresentation
} from '@0xpolygonid/js-sdk';
import { poseidon } from '@iden3/js-crypto';

Expand All @@ -22,16 +27,19 @@ export class LinkedMultiQueryVerifier implements PubSignalsVerifier {

constructor(pubSignals: string[]) {
this.pubSignals = this.pubSignals.pubSignalsUnmarshal(
byteEncoder.encode(JSON.stringify(pubSignals)),
10
byteEncoder.encode(JSON.stringify(pubSignals))
);
}

verifyIdOwnership(): Promise<void> {
return Promise.resolve();
}

async verifyQuery(query: Query, schemaLoader?: DocumentLoader): Promise<BaseConfig> {
async verifyQuery(
query: Query,
schemaLoader?: DocumentLoader,
verifiablePresentation?: JSON
): Promise<BaseConfig> {
let schema: JSONObject;
const ldOpts = { documentLoader: schemaLoader ?? cacheLoader() };
try {
Expand All @@ -42,7 +50,7 @@ export class LinkedMultiQueryVerifier implements PubSignalsVerifier {
const ldContextJSON = JSON.stringify(schema);
const credentialSubject = query.credentialSubject as JSONObject;
const schemaId: string = await Path.getTypeIDFromContext(ldContextJSON, query.type, ldOpts);
const schemaHash = createSchemaHash(byteEncoder.encode(schemaId));
const schemaHash = calculateCoreSchemaHash(byteEncoder.encode(schemaId));

const queriesMetadata = await parseQueriesMetadata(
query.type,
Expand All @@ -51,24 +59,57 @@ export class LinkedMultiQueryVerifier implements PubSignalsVerifier {
ldOpts
);

const queryHashes = queriesMetadata.map((queryMeta) => {
const valueHash = poseidon.spongeHashX(queryMeta.values, 6);
return poseidon.hash([
schemaHash.bigInt(),
BigInt(queryMeta.slotIndex),
BigInt(queryMeta.operator),
BigInt(queryMeta.claimPathKey),
queryMeta.merklizedSchema ? 0n : 1n,
valueHash
]);
});
const request: { queryHash: bigint; queryMeta: QueryMetadata }[] = [];
const merklized = queriesMetadata[0]?.merklizedSchema ? 1 : 0;
for (let i = 0; i < LinkedMultiQueryInputs.queryCount; i++) {
const queryMeta = queriesMetadata[i];
const values = queryMeta?.values ?? [];
const valArrSize = values.length;

const circuitQueryHashes = this.pubSignals.circuitQueryHash
.filter((i) => i !== 0n)
.sort(this.bigIntCompare);
queryHashes.sort(this.bigIntCompare);
if (!queryHashes.every((queryHash, i) => queryHash === circuitQueryHashes[i])) {
throw new Error('query hashes do not match');
const queryHash = calculateQueryHashV3(
values,
schemaHash,
queryMeta?.slotIndex ?? 0,
queryMeta?.operator ?? 0,
queryMeta?.claimPathKey.toString() ?? 0,
valArrSize,
merklized,
0,
0,
0
);
request.push({ queryHash, queryMeta });
}

const queryHashCompare = (a: { queryHash: bigint }, b: { queryHash: bigint }): number => {
if (a.queryHash < b.queryHash) return -1;
if (a.queryHash > b.queryHash) return 1;
return 0;
};

const pubSignalsMeta = this.pubSignals.circuitQueryHash.map((queryHash, index) => ({
queryHash,
operatorOutput: this.pubSignals.operatorOutput[index]
}));

pubSignalsMeta.sort(queryHashCompare);
request.sort(queryHashCompare);

for (let i = 0; i < LinkedMultiQueryInputs.queryCount; i++) {
if (request[i].queryHash != pubSignalsMeta[i].queryHash) {
throw new Error('query hashes do not match');
}

if (request[i].queryMeta?.operator === Operators.SD) {
const disclosedValue = await fieldValueFromVerifiablePresentation(
request[i].queryMeta.fieldName,
verifiablePresentation,
schemaLoader
);
if (disclosedValue != pubSignalsMeta[i].operatorOutput) {
throw new Error('disclosed value is not in the proof outputs');
}
}
}

return this.pubSignals as unknown as BaseConfig;
Expand Down
Loading

0 comments on commit 3b32b74

Please sign in to comment.