Skip to content

Commit

Permalink
bug: not extracting "profile"
Browse files Browse the repository at this point in the history
  • Loading branch information
Autoparallel committed Oct 19, 2024
1 parent ca23d2d commit b40464d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 23 deletions.
27 changes: 21 additions & 6 deletions circuits/json/nivc/masker.circom
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,26 @@ template JsonMaskObjectNIVC(DATA_BYTES, MAX_STACK_HEIGHT, MAX_KEY_LENGTH) {
stackSelector[data_idx].in <== stack[data_idx];
stackSelector[data_idx].index <== step_in[TOTAL_BYTES_ACROSS_NIVC - 1];

log("stackSelector[", data_idx, "].out[0] = ", stackSelector[data_idx].out[0]);
log("stackSelector[", data_idx, "].out[1] = ", stackSelector[data_idx].out[1]);

// Detect if we are parsing
parsing_key[data_idx] <== InsideKey()(stackSelector[data_idx].out, parsingData[data_idx][0], parsingData[data_idx][1]);
parsing_value[data_idx] <== InsideValueObject()(stackSelector[data_idx].out, stack[data_idx][1], parsingData[data_idx][0], parsingData[data_idx][1]);

log("parsing_key[", data_idx, "] = ", parsing_key[data_idx]);
log("parsing_value[", data_idx, "] = ", parsing_value[data_idx]);

// to get correct value, check:
// - key matches at current index and depth of key is as specified
// - whether next KV pair starts
// - whether key matched for a value (propogate key match until new KV pair of lower depth starts)
is_key_match[data_idx] <== KeyMatchAtIndex(DATA_BYTES, MAX_KEY_LENGTH, data_idx)(data, key, keyLen, parsing_key[data_idx]);
is_next_pair_at_depth[data_idx] <== NextKVPairAtDepth(MAX_STACK_HEIGHT)(stack[data_idx], data[data_idx], step_in[TOTAL_BYTES_ACROSS_NIVC - 1]);

log("is_key_match[", data_idx, "] = ", is_key_match[data_idx]);
log("is_next_pair_at_depth[", data_idx, "] = ", is_next_pair_at_depth[data_idx]);

is_key_match_for_value[data_idx+1] <== Mux1()([is_key_match_for_value[data_idx] * (1-is_next_pair_at_depth[data_idx]), is_key_match[data_idx] * (1-is_next_pair_at_depth[data_idx])], is_key_match[data_idx]);
is_value_match[data_idx] <== is_key_match_for_value[data_idx+1] * parsing_value[data_idx];

Expand Down Expand Up @@ -147,30 +157,35 @@ template JsonMaskArrayIndexNIVC(DATA_BYTES, MAX_STACK_HEIGHT) {
signal mask[DATA_BYTES];

signal parsing_array[DATA_BYTES];
signal or[DATA_BYTES];
signal or[DATA_BYTES]; // Maybe don't need

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

component nextStackSelector[DATA_BYTES];
nextStackSelector[0] = ArraySelector(MAX_STACK_HEIGHT, 2);
nextStackSelector[0] = ArraySelector(MAX_STACK_HEIGHT, 2);
nextStackSelector[0].in <== stack[0];
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] = ArraySelector(MAX_STACK_HEIGHT, 2);
stackSelector[data_idx].in <== stack[data_idx];
stackSelector[data_idx].index <== step_in[TOTAL_BYTES_ACROSS_NIVC - 1];

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

log("stackSelector[", data_idx, "].out[0] = ", stackSelector[data_idx].out[0]);
log("stackSelector[", data_idx, "].out[1] = ", stackSelector[data_idx].out[1]);
log("nextStackSelector[", data_idx, "].out[0] = ", nextStackSelector[data_idx].out[0]);
log("nextStackSelector[", data_idx, "].out[1] = ", nextStackSelector[data_idx].out[1]);

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

or[data_idx] <== OR()(parsing_array[data_idx], parsing_array[data_idx - 1]);
Expand Down
42 changes: 25 additions & 17 deletions circuits/test/json/nivc/masker_nivc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,26 @@ import { join } from "path";
// Transfer-Encoding: chunked
//
// {
// "data": {
// "items": [
// {
// "data": "Artist",
// "profile": {
// "name": "Taylor Swift"
// }
// }
// ]
// }
// "data": {
// "items": [
// {
// "data": "Artist",
// "profile": {
// "name": "Taylor Swift"
// }
// }
// ]
// }
// }

// Notes:
// - "data"'s object appears at byte 14
// - colon after "items" appears at byte 31
// - 0th index of arr appears at byte 47
// - byte 64 is `"` for the data inside the array obj
// - byte 81 is where `Artist",` ends
// - byte 100 is where `"profile"` starts

interface NIVCData {
step_out: number[];
}
Expand Down Expand Up @@ -93,18 +101,18 @@ describe("JsonMaskObjectNIVC", async () => {
const description = generateDescription(input);

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

let key0 = [100, 97, 116, 97, 0, 0, 0]; // "data"
let key0Len = 4;
generatePassCase({ step_in: nivc_parse.step_out, key: key0, keyLen: key0Len }, { step_out: nivc_extract_key0.step_out }, "masking json object at depth 0");
// let key0 = [100, 97, 116, 97, 0, 0, 0]; // "data"
// let key0Len = 4;
// generatePassCase({ step_in: nivc_parse.step_out, key: key0, keyLen: key0Len }, { step_out: nivc_extract_key0.step_out }, "masking json object at depth 0");

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");
// 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");

// Ran after doing arr masking
let key2 = [112, 114, 111, 102, 105, 108, 101]; // "profile"
Expand Down

0 comments on commit b40464d

Please sign in to comment.