From b40464d8b7bed56e3823dc5e7e03dfb3d3862bf1 Mon Sep 17 00:00:00 2001 From: Colin Roberts Date: Fri, 18 Oct 2024 20:57:10 -0600 Subject: [PATCH] bug: not extracting `"profile"` --- circuits/json/nivc/masker.circom | 27 ++++++++++--- circuits/test/json/nivc/masker_nivc.test.ts | 42 ++++++++++++--------- 2 files changed, 46 insertions(+), 23 deletions(-) diff --git a/circuits/json/nivc/masker.circom b/circuits/json/nivc/masker.circom index 3bfe81d..f9d31e2 100644 --- a/circuits/json/nivc/masker.circom +++ b/circuits/json/nivc/masker.circom @@ -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]; @@ -147,15 +157,15 @@ 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; @@ -163,14 +173,19 @@ template JsonMaskArrayIndexNIVC(DATA_BYTES, MAX_STACK_HEIGHT) { 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]); diff --git a/circuits/test/json/nivc/masker_nivc.test.ts b/circuits/test/json/nivc/masker_nivc.test.ts index 754d7fb..0f42c88 100644 --- a/circuits/test/json/nivc/masker_nivc.test.ts +++ b/circuits/test/json/nivc/masker_nivc.test.ts @@ -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[]; } @@ -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"