0xPolygonID / js-sdk

SDK to work with Polygon ID
https://0xpolygonid.github.io/js-sdk-tutorials/
Apache License 2.0
58 stars 36 forks source link

Error: empty JSON-LD query value, in ValueProof.validate when value is 0 #57

Closed nedgar closed 1 year ago

nedgar commented 1 year ago

With the fix for #53 in the latest patch release (1.0.0-beta.4), the filter for matching credential now works when the field value is 0. However, it fails later during ProofService.generateProof:

Error: empty JSON-LD query value at ValueProof.validate (/Users/nedgar/src/remix.run/blog-tutorial/node_modules/@0xpolygonid/js-sdk/src/circuits/models.ts:210:13) at AtomicQuerySigV2Inputs.inputsMarshal (/Users/nedgar/src/remix.run/blog-tutorial/node_modules/@0xpolygonid/js-sdk/src/circuits/atomic-query-sig-v2.ts:78:29) at ProofService.generateInputs (/Users/nedgar/src/remix.run/blog-tutorial/node_modules/@0xpolygonid/js-sdk/src/proof/proof-service.ts:368:38) at processTicksAndRejections (node:internal/process/task_queues:95:5) at ProofService.generateProof (/Users/nedgar/src/remix.run/blog-tutorial/node_modules/@0xpolygonid/js-sdk/src/proof/proof-service.ts:192:28) at AuthHandler.generateAuthorizationResponse (/Users/nedgar/src/remix.run/blog-tutorial/node_modules/@0xpolygonid/js-sdk/src/iden3comm/handlers/auth.ts:246:50) at generateAuthResponse (/Users/nedgar/src/remix.run/blog-tutorial/app/service/holder.server.ts:111:18) at generateProof (/Users/nedgar/src/remix.run/blog-tutorial/app/routes/verification.tsx:98:14) at action2 (/Users/nedgar/src/remix.run/blog-tutorial/app/routes/verification.tsx:114:9)

In ValueProof.validate: https://github.com/0xPolygonID/js-sdk/blob/0cc88261941baebfcfe43a5b635a28ce826c593a/src/circuits/models.ts#L209-L211

FYI @Kolezhniuk @vmidyllic.

Kolezhniuk commented 1 year ago

@nedgar Hello! Should be fixed in the v1.0.0-beta.5 version. Please update your dependency to the latest one

nedgar commented 1 year ago

@Kolezhniuk I can confirm that it gets past this point now. I tried a KYCCountryOfResidenceCredential with countryCode: 0, using query: {countryCode: {$nin: [4,364,408,728,760]}}. It found the credential OK and was able to marshal the input signals. However, when setting the value in WitnessCalculator._doCalculateWitness it failed with the following:

Error: Error: Assert Failed.
Error in template credentialAtomicQuerySigOffChain_383 line: 232

    at /Users/nedgar/src/remix.run/blog-tutorial/node_modules/@0xpolygonid/js-sdk/src/proof/witness_calculator.ts:148:17
    at Array.forEach (<anonymous>)
    at WitnessCalculator._doCalculateWitness (/Users/nedgar/src/remix.run/blog-tutorial/node_modules/@0xpolygonid/js-sdk/src/proof/witness_calculator.ts:123:10)
    at WitnessCalculator.calculateWTNSBin (/Users/nedgar/src/remix.run/blog-tutorial/node_modules/@0xpolygonid/js-sdk/src/proof/witness_calculator.ts:197:16)
    at NativeProver.generate (/Users/nedgar/src/remix.run/blog-tutorial/node_modules/@0xpolygonid/js-sdk/src/proof/prover.ts:45:61)
    at ProofService.generateProof (/Users/nedgar/src/remix.run/blog-tutorial/node_modules/@0xpolygonid/js-sdk/src/proof/proof-service.ts:199:36)
    at AuthHandler.generateAuthorizationResponse (/Users/nedgar/src/remix.run/blog-tutorial/node_modules/@0xpolygonid/js-sdk/src/iden3comm/handlers/auth.ts:246:50)
    at generateAuthResponse (/Users/nedgar/src/remix.run/blog-tutorial/app/service/holder.server.ts:111:18)
    at generateProof (/Users/nedgar/src/remix.run/blog-tutorial/app/routes/verification.tsx:101:12)
    at action2 (/Users/nedgar/src/remix.run/blog-tutorial/app/routes/verification.tsx:126:11)
    at Object.callRouteActionRR (/Users/nedgar/src/remix.run/blog-tutorial/node_modules/@remix-run/server-runtime/dist/data.js:24:16)
    at callLoaderOrAction (/Users/nedgar/src/remix.run/blog-tutorial/node_modules/@remix-run/router/router.ts:3125:14)
    at submit (/Users/nedgar/src/remix.run/blog-tutorial/node_modules/@remix-run/router/router.ts:2609:16)
    at queryImpl (/Users/nedgar/src/remix.run/blog-tutorial/node_modules/@remix-run/router/router.ts:2544:22)
    at Object.queryRoute (/Users/nedgar/src/remix.run/blog-tutorial/node_modules/@remix-run/router/router.ts:2494:18)
    at handleDataRequestRR (/Users/nedgar/src/remix.run/blog-tutorial/node_modules/@remix-run/server-runtime/dist/server.js:81:20) {stack: 'Error: Error: Assert Failed.
Error in templat…emix-run/server-runtime/dist/server.js:81:20)', message: 'Error: Assert Failed.
Error in template credentialAtomicQuerySigOffChain_383 line: 232
'}

This is a pretty cryptic error message, but under debug I can see that it's setting the last input signal, i.e. the last value field at index 63. This causes the query constraint query.out === 1 to fail at https://github.com/iden3/circuits/blob/8fffb6609ecad0b7bcda19bb908bdb544bdb3cf7/circuits/offchain/credentialAtomicQuerySigOffChain.circom#L232, presumably due to the 0s that pad out the remain value slots, so the comparison value of 0 is considered to actually be in the set.

This appears to be a fundamental limitation of the IN and NIN operators. Would it be possible to indicate the number of values given in the array? Or perhaps use a different value for "not specified", e.g. something higher than the BJJ Q value like 2^256-1?

nedgar commented 1 year ago

Feel free to close this issue and move the above to a new issue or discussion.

demonsh commented 1 year ago

Currently, this is a corner case that is impossible to fix. In the circuits value field is an array https://github.com/iden3/circuits/blob/master/circuits/offchain/credentialAtomicQueryMTPOffChain.circom signal input value[valueArraySize] - this an array of 64 elements.

When you create request {$nin: [4,364,408,728,760]}} like this before out inputs to the circuit they are padded with 0 and the final array is an array of 64 elements, something like this [4,364,408,728,760, 0, 0, 0,.... 0]

As you have nin operation it will fail as 0 is one of the elements of the array.

Workaround could be to provide all elements of the array with different value [[4,364,408,728,760, 760, 760, 760,.... 760]]

nedgar commented 1 year ago

Got it, that works, thanks. Just wondering if this could be fixed in the SDK by using a different placeholder value.

Or perhaps use a different value for "not specified", e.g. something higher than the BJJ Q value like 2^256-1?

github-actions[bot] commented 1 year ago

This issue is stale because it has been open for 14 days with no activity.

vmidyllic commented 1 year ago

work in progress in #63 But we are faced with an issue that the current mobile client uses the same principle for circuit inputs preparation. We will provide an upgrade of protocol in the next releaase

github-actions[bot] commented 1 year ago

This issue is stale because it has been open for 14 days with no activity.

github-actions[bot] commented 1 year ago

This issue was closed because it has been inactive for 7 days since being marked as stale.