Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Compress masked_content in aux inputs
Browse files Browse the repository at this point in the history
mskd12 committed Jun 2, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent bbd602e commit a5d1d49
Showing 4 changed files with 59 additions and 3 deletions.
3 changes: 2 additions & 1 deletion openid-zkp-auth/js/circuitutils.js
Original file line number Diff line number Diff line change
@@ -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,
51 changes: 51 additions & 0 deletions openid-zkp-auth/js/compressutils.js
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 4 additions & 0 deletions openid-zkp-auth/js/verify.js
Original file line number Diff line number Diff line change
@@ -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");

4 changes: 2 additions & 2 deletions openid-zkp-auth/test/main.circom.test.js
Original file line number Diff line number Diff line change
@@ -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"],

0 comments on commit a5d1d49

Please sign in to comment.