Skip to content

Commit

Permalink
Make masked content serializable in Rust
Browse files Browse the repository at this point in the history
  • Loading branch information
mskd12 committed Jun 2, 2023
1 parent a5d1d49 commit bce53f2
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 11 deletions.
30 changes: 20 additions & 10 deletions openid-zkp-auth/js/compressutils.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -46,6 +54,8 @@ const decompressVector = (compressedVector, targetNumber) => {
};

module.exports = {
compressAny: compressAny,
decompressAny: decompressAny,
compressVector: compressVector,
decompressVector: decompressVector
}
2 changes: 1 addition & 1 deletion openid-zkp-auth/js/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
51 changes: 51 additions & 0 deletions openid-zkp-auth/test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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);
});
});

0 comments on commit bce53f2

Please sign in to comment.