From bce53f23a9ff8b321b9c46555e7eea54f08b5667 Mon Sep 17 00:00:00 2001 From: Deepak Maram Date: Fri, 2 Jun 2023 10:32:41 -0400 Subject: [PATCH] Make masked content serializable in Rust --- openid-zkp-auth/js/compressutils.js | 30 +++++++++++------ openid-zkp-auth/js/verify.js | 2 +- openid-zkp-auth/test/utils.test.js | 51 +++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 11 deletions(-) diff --git a/openid-zkp-auth/js/compressutils.js b/openid-zkp-auth/js/compressutils.js index 8ea9df825e..fc2bf2c37a 100644 --- a/openid-zkp-auth/js/compressutils.js +++ b/openid-zkp-auth/js/compressutils.js @@ -1,4 +1,8 @@ -const compressVector = (vector, targetNumber) => { +const compressVector = (vector) => { + return compressAny(vector, 61); +} + +const compressAny = (vector, targetNumber) => { let otherNumbers = []; let targetNumberIndices = []; let inRun = false; @@ -25,18 +29,22 @@ const compressVector = (vector, targetNumber) => { targetNumberIndices.push([startIdx, vector.length - 1]); } - return [ - otherNumbers, - targetNumberIndices - ]; + return { + 'arr': otherNumbers, + 'ind': targetNumberIndices + }; }; -const decompressVector = (compressedVector, targetNumber) => { - let decompressedVector = [...compressedVector[0]]; +const decompressVector = (compressedVector) => { + return decompressAny(compressedVector, 61); +} + +const decompressAny = (compressedVector, targetNumber) => { + let decompressedVector = [...compressedVector.arr]; 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; + for (let i = 0; i < compressedVector.ind.length; i++) { + let start = compressedVector.ind[i][0] + offset; + let end = compressedVector.ind[i][1] + offset; let length = end - start + 1; decompressedVector.splice(start, 0, ...Array(length).fill(targetNumber)); start += length; @@ -46,6 +54,8 @@ const decompressVector = (compressedVector, targetNumber) => { }; module.exports = { + compressAny: compressAny, + decompressAny: decompressAny, 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 0e61412ab3..2da67896c8 100644 --- a/openid-zkp-auth/js/verify.js +++ b/openid-zkp-auth/js/verify.js @@ -42,7 +42,7 @@ function checkMaskedContent ( expected_length ){ // Decompress masked_content - masked_content = compressutils.decompressVector(masked_content, 61); + masked_content = compressutils.decompressVector(masked_content); 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/utils.test.js b/openid-zkp-auth/test/utils.test.js index 3004ea1f8f..04632c8f30 100644 --- a/openid-zkp-auth/test/utils.test.js +++ b/openid-zkp-auth/test/utils.test.js @@ -8,6 +8,7 @@ const crypto = require("crypto"); const circuit = require("../js/circuitutils"); const utils = require("../js/utils"); const jwtutils = require("../js/jwtutils"); +const compressutils = require("../js/compressutils"); describe("Circuit Utilities", () => { it("Buffer to/from bit array works as expected", async () => { @@ -252,4 +253,54 @@ describe("JWT utils tests", () => { assert.deepEqual(decoded2, username); }) }) +}); + +describe("Compress utils tests", () => { + const compress = compressutils.compressAny; + const decompress = compressutils.decompressAny; + + function getLen(compressed) { + var len = compressed.arr.length; + for (let i = 0; i < compressed.ind.length; i++) { + var x = compressed.ind[i]; + len += x[1] - x[0] + 1; + } + return len; + } + + it ("targetNumber at the end", () => { + const input = [1, 2, 3, 4, 5, 6, 7, 8]; + const targetNumber = 8; + const compressed = compress(input, targetNumber); + assert.deepEqual(input.length, getLen(compressed)); + const decompressed = decompress(compressed, targetNumber); + assert.deepEqual(decompressed, input); + }); + + it ("targetNumber at the start", () => { + const input = [1, 2, 3, 4, 5, 6, 7, 8]; + const targetNumber = 1; + const compressed = compress(input, targetNumber); + assert.deepEqual(input.length, getLen(compressed)); + const decompressed = decompress(compressed, targetNumber); + assert.deepEqual(decompressed, input); + }); + + it ("multiple occurence of the targetNumber", () => { + const input = [ 4, 1, 2, 3, 4, 4, 4, 5, 6, 7, 8, 4]; + const targetNumber = 4; + const compressed = compress(input, targetNumber); + assert.deepEqual(input.length, getLen(compressed)); + const decompressed = decompress(compressed, targetNumber); + assert.deepEqual(decompressed, input); + }); + + it ("big input", () => { + const input = [101,121,74,104,98,71,99,105,79,105,74,83,85,122,73,49,78,105,73,115,73,109,116,112,90,67,73,54,73,106,89,119,79,68,78,107,90,68,85,53,79,68,69,50,78,122,78,109,78,106,89,120,90,109,82,108,79,87,82,104,90,84,89,48,78,109,73,50,90,106,65,122,79,68,66,104,77,68,69,48,78,87,77,105,76,67,74,48,101,88,65,105,79,105,74,75,86,49,81,105,102,81,46,61,121,74,112,99,51,77,105,79,105,74,111,100,72,82,119,99,122,111,118,76,50,70,106,89,50,57,49,98,110,82,122,76,109,100,118,98,50,100,115,90,83,53,106,98,50,48,105,76,67,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,67,74,104,100,87,81,105,79,105,73,53,78,68,89,51,77,122,69,122,78,84,73,121,78,122,89,116,99,71,115,49,90,50,120,106,90,122,104,106,99,87,56,122,79,71,53,107,89,106,77,53,97,68,100,113,77,68,107,122,90,110,66,122,99,71,104,49,99,51,85,117,89,88,66,119,99,121,53,110,98,50,57,110,98,71,86,49,99,50,86,121,89,50,57,117,100,71,86,117,100,67,53,106,98,50,48,105,76,67,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,128,0,0,0,0,0,0,0,0,21,168,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]; + const targetNumber = 61; + const compressed = compress(input, targetNumber); + assert.deepEqual(input.length, getLen(compressed)); + const decompressed = decompress(compressed, targetNumber); + assert.deepEqual(decompressed, input); + }); }); \ No newline at end of file