Skip to content

Commit

Permalink
passing JsonMaskArrayIndexNIVC
Browse files Browse the repository at this point in the history
  • Loading branch information
Autoparallel committed Oct 19, 2024
1 parent 1e073f4 commit b52a801
Show file tree
Hide file tree
Showing 3 changed files with 2,671 additions and 11 deletions.
20 changes: 10 additions & 10 deletions circuits/json/nivc/masker.circom
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,19 @@ template JsonMaskObjectNIVC(DATA_BYTES, MAX_STACK_HEIGHT, MAX_KEY_LENGTH) {
step_out[TOTAL_BYTES_ACROSS_NIVC - 1] <== step_in[TOTAL_BYTES_ACROSS_NIVC - 1] + 1;
}

template JsonMaskArrayIndexNIVC(TOTAL_BYTES, DATA_BYTES, MAX_STACK_HEIGHT) {
template JsonMaskArrayIndexNIVC(DATA_BYTES, MAX_STACK_HEIGHT) {
// ------------------------------------------------------------------------------------------------------------------ //
// ~~ Set sizes at compile time ~~
// Total number of variables in the parser for each byte of data
assert(MAX_STACK_HEIGHT >= 2);
var PER_ITERATION_DATA_LENGTH = MAX_STACK_HEIGHT * 2 + 2;
var TOTAL_BYTES_USED = DATA_BYTES * (PER_ITERATION_DATA_LENGTH + 1);
var TOTAL_BYTES_ACROSS_NIVC = DATA_BYTES * (PER_ITERATION_DATA_LENGTH + 1) + 1;
// ------------------------------------------------------------------------------------------------------------------ //

// ------------------------------------------------------------------------------------------------------------------ //
// ~ Unravel from previous NIVC step ~
// Read in from previous NIVC step (JsonParseNIVC)
signal input step_in[TOTAL_BYTES + 1]; // ADD 1 FOR CURRENT STACK POINTER
signal input step_in[TOTAL_BYTES_ACROSS_NIVC]; // ADD 1 FOR CURRENT STACK POINTER

// Grab the raw data bytes from the `step_in` variable
signal data[DATA_BYTES];
Expand Down Expand Up @@ -152,24 +152,24 @@ template JsonMaskArrayIndexNIVC(TOTAL_BYTES, DATA_BYTES, MAX_STACK_HEIGHT) {
component stackSelector[DATA_BYTES];
stackSelector[0] = ArraySelector(MAX_STACK_HEIGHT, 2);
stackSelector[0].in <== stack[0];
stackSelector[0].index <== step_in[TOTAL_BYTES];
stackSelector[0].index <== step_in[TOTAL_BYTES_ACROSS_NIVC - 1];

component nextStackSelector[DATA_BYTES];
nextStackSelector[0] = ArraySelector(MAX_STACK_HEIGHT, 2);
nextStackSelector[0].in <== stack[0];
nextStackSelector[0].index <== step_in[TOTAL_BYTES] + 1;
nextStackSelector[0].index <== step_in[TOTAL_BYTES_ACROSS_NIVC - 1] + 1;

parsing_array[0] <== InsideArrayIndexObject()(stackSelector[0].out, nextStackSelector[0].out, parsingData[0][0], parsingData[0][1], index);
mask[0] <== data[0] * parsing_array[0];

for(var data_idx = 1; data_idx < DATA_BYTES; data_idx++) {
stackSelector[data_idx] = ArraySelector(MAX_STACK_HEIGHT, 2);
stackSelector[data_idx].in <== stack[data_idx];
stackSelector[data_idx].index <== step_in[TOTAL_BYTES];
stackSelector[data_idx].index <== step_in[TOTAL_BYTES_ACROSS_NIVC - 1];

nextStackSelector[data_idx] = ArraySelector(MAX_STACK_HEIGHT, 2);
nextStackSelector[data_idx].in <== stack[data_idx];
nextStackSelector[data_idx].index <== step_in[TOTAL_BYTES] + 1;
nextStackSelector[data_idx].index <== step_in[TOTAL_BYTES_ACROSS_NIVC - 1] + 1;

parsing_array[data_idx] <== InsideArrayIndexObject()(stackSelector[data_idx].out, nextStackSelector[data_idx].out, parsingData[data_idx][0], parsingData[data_idx][1], index);

Expand All @@ -178,16 +178,16 @@ template JsonMaskArrayIndexNIVC(TOTAL_BYTES, DATA_BYTES, MAX_STACK_HEIGHT) {
}

// Write the `step_out` with masked data
signal output step_out[TOTAL_BYTES + 1];
signal output step_out[TOTAL_BYTES_ACROSS_NIVC];
for (var i = 0 ; i < DATA_BYTES ; i++) {
step_out[i] <== mask[i];
}
// Append the parser state back on `step_out`
for (var i = DATA_BYTES ; i < TOTAL_BYTES ; i++) {
for (var i = DATA_BYTES ; i < TOTAL_BYTES_ACROSS_NIVC - 1 ; i++) {
step_out[i] <== step_in[i];
}
// No need to pad as this is currently when TOTAL_BYTES == TOTAL_BYTES_USED
step_out[TOTAL_BYTES] <== step_in[TOTAL_BYTES] + 1;
step_out[TOTAL_BYTES_ACROSS_NIVC - 1] <== step_in[TOTAL_BYTES_ACROSS_NIVC - 1] + 1;
}

template ArraySelector(m, n) {
Expand Down
31 changes: 30 additions & 1 deletion circuits/test/json/nivc/masker_nivc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ let json_input = [123, 13, 10, 32, 32, 32, 34, 100, 97, 116, 97, 34, 58, 32, 123
let nivc_parse = readJsonFile<NIVCData>(join(__dirname, "..", "nivc/nivc_parse.json"));
let nivc_extract_key0 = readJsonFile<NIVCData>(join(__dirname, "..", "nivc/nivc_extract_key0.json"));
let nivc_extract_key1 = readJsonFile<NIVCData>(join(__dirname, "..", "nivc/nivc_extract_key1.json"));

let nivc_extract_arr = readJsonFile<NIVCData>(join(__dirname, "..", "nivc/nivc_extract_arr.json"));

describe("JsonParseNIVC", async () => {
let circuit: WitnessTester<["step_in"], ["step_out"]>;
Expand Down Expand Up @@ -105,4 +105,33 @@ describe("JsonMaskObjectNIVC", async () => {
let key1 = [105, 116, 101, 109, 115, 0, 0]; // "items"
let key1Len = 5;
generatePassCase({ step_in: nivc_extract_key0.step_out, key: key1, keyLen: key1Len }, { step_out: nivc_extract_key1.step_out }, "masking json object at depth 0");
});

describe("JsonMaskArrayIndexNIVC", async () => {
let circuit: WitnessTester<["step_in", "index"], ["step_out"]>;

let DATA_BYTES = 202;
let MAX_STACK_HEIGHT = 5;
let MAX_KEY_LENGTH = 7;

before(async () => {
circuit = await circomkit.WitnessTester(`JsonMaskArrayIndexNIVC`, {
file: "json/nivc/masker",
template: "JsonMaskArrayIndexNIVC",
params: [DATA_BYTES, MAX_STACK_HEIGHT],
});
console.log("#constraints:", await circuit.getConstraintCount());
});

function generatePassCase(input: any, expected: any, desc: string) {
const description = generateDescription(input);

it(`(valid) witness: ${description} ${desc}`, async () => {
console.log(JSON.stringify(await circuit.compute(input, ["step_out"])))
await circuit.expectPass(input, expected);
});
}

let index = 0;
generatePassCase({ step_in: nivc_extract_key1.step_out, index: index }, { step_out: nivc_extract_arr.step_out }, "masking json object at depth 0");
});
Loading

0 comments on commit b52a801

Please sign in to comment.