diff --git a/openid-zkp-auth/js/circuitutils.js b/openid-zkp-auth/js/circuitutils.js index 2c3e76433e..2f90f67dc6 100644 --- a/openid-zkp-auth/js/circuitutils.js +++ b/openid-zkp-auth/js/circuitutils.js @@ -3,6 +3,7 @@ const {toBigIntBE, toBufferBE} = require('bigint-buffer'); const utils = require('./utils'); const jwtutils = require('./jwtutils'); +const compressutils = require('./compressutils'); const constants = require('./constants'); const claimsToReveal = constants.claimsToReveal; @@ -219,7 +220,7 @@ async function genJwtProofUAInputs( ], poseidon); const auxiliary_inputs = { - "masked_content": masked_content, + "masked_content": compressutils.compressVector(masked_content), "jwt_sha2_hash": jwt_sha2_hash.map(e => e.toString()), "payload_start_index": inputs.payload_start_index, "payload_len": inputs.payload_len, diff --git a/openid-zkp-auth/js/compressutils.js b/openid-zkp-auth/js/compressutils.js new file mode 100644 index 0000000000..8ea9df825e --- /dev/null +++ b/openid-zkp-auth/js/compressutils.js @@ -0,0 +1,51 @@ +const compressVector = (vector, targetNumber) => { + let otherNumbers = []; + let targetNumberIndices = []; + let inRun = false; + let startIdx = 0; + let targetCount = 0; + + for (let i = 0; i < vector.length; i++) { + if (vector[i] === targetNumber) { + if (!inRun) { + inRun = true; + startIdx = i; + } + targetCount++; + } else { + if (inRun) { + inRun = false; + targetNumberIndices.push([startIdx, i - 1]); + } + otherNumbers.push(vector[i]); + } + } + + if (inRun) { + targetNumberIndices.push([startIdx, vector.length - 1]); + } + + return [ + otherNumbers, + targetNumberIndices + ]; +}; + +const decompressVector = (compressedVector, targetNumber) => { + let decompressedVector = [...compressedVector[0]]; + let offset = 0; + for (let i = 0; i < compressedVector[1].length; i++) { + let start = compressedVector[1][i][0] + offset; + let end = compressedVector[1][i][1] + offset; + let length = end - start + 1; + decompressedVector.splice(start, 0, ...Array(length).fill(targetNumber)); + start += length; + offset += end - start + 1; + } + return decompressedVector; +}; + +module.exports = { + compressVector: compressVector, + decompressVector: decompressVector +} \ No newline at end of file diff --git a/openid-zkp-auth/js/verify.js b/openid-zkp-auth/js/verify.js index 35f16ef60a..0e61412ab3 100644 --- a/openid-zkp-auth/js/verify.js +++ b/openid-zkp-auth/js/verify.js @@ -1,6 +1,7 @@ const jwt = require("jsonwebtoken"); const jwkToPem = require("jwk-to-pem"); +const compressutils = require("./compressutils"); const jwtutils = require("./jwtutils"); const { toBigIntBE } = require("bigint-buffer"); @@ -40,6 +41,9 @@ function checkMaskedContent ( expected_payload_len, expected_length ){ + // Decompress masked_content + masked_content = compressutils.decompressVector(masked_content, 61); + if (masked_content.length != expected_length) throw new Error("Invalid length"); if (num_sha2_blocks * 64 > masked_content.length) throw new Error("Invalid last block"); diff --git a/openid-zkp-auth/test/main.circom.test.js b/openid-zkp-auth/test/main.circom.test.js index e8a033a5fc..b7612161d3 100644 --- a/openid-zkp-auth/test/main.circom.test.js +++ b/openid-zkp-auth/test/main.circom.test.js @@ -11,6 +11,7 @@ const utils = require("../js/utils"); const verify = require('../js/verify'); const testutils = require("./testutils"); +const compressutils = require("../js/compressutils"); async function genCircuit( maxContentLen = constants.maxContentLen, @@ -53,9 +54,8 @@ async function genProof( const w = await circuit.calculateWitness(inputs, true); await circuit.checkConstraints(w); - const masked_content = utils.applyMask(inputs["content"], inputs["mask"]); verify.checkMaskedContent( - masked_content, + auxiliary_inputs["masked_content"], inputs["num_sha2_blocks"], inputs["payload_start_index"], inputs["payload_len"],