Skip to content

Commit

Permalink
fix: extract circuit (#32)
Browse files Browse the repository at this point in the history
* complete full test

* remove output value

* update version

* change max defaults for http

* change input signal name

* fix tests

* increase http start line default length

* reverting name change

* changing witcalc in CI
  • Loading branch information
lonerapier authored Nov 5, 2024
1 parent bb86471 commit e50209a
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/artifacts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ jobs:
- name: Setup circom-witnesscalc
run: |
cd .. && git clone https://github.com/iden3/circom-witnesscalc.git
cd .. && git clone https://github.com/pluto/circom-witnesscalc.git
cd circom-witnesscalc
cargo install --path .
echo $(which build-circuit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ pragma circom 2.1.9;

include "../../circuits/http/nivc/parse_and_lock_start_line.circom";

component main { public [step_in] } = ParseAndLockStartLine(1024, 10, 20, 20, 20);
component main { public [step_in] } = ParseAndLockStartLine(1024, 10, 50, 200, 50);
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ pragma circom 2.1.9;

include "../../circuits/http/nivc/parse_and_lock_start_line.circom";

component main { public [step_in] } = ParseAndLockStartLine(256, 10, 20, 20, 20);
component main { public [step_in] } = ParseAndLockStartLine(256, 10, 50, 200, 50);
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ pragma circom 2.1.9;

include "../../circuits/http/nivc/parse_and_lock_start_line.circom";

component main { public [step_in] } = ParseAndLockStartLine(512, 10, 20, 20, 20);
component main { public [step_in] } = ParseAndLockStartLine(512, 10, 50, 200, 50);
2 changes: 1 addition & 1 deletion circuits/json/nivc/extractor.circom
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ template MaskExtractFinal(DATA_BYTES, MAX_STACK_HEIGHT, MAX_VALUE_LENGTH) {
value_starting_index[i] <== value_starting_index[i-1] + i * (1-is_zero_mask[i]) * is_prev_starting_index[i];
}
// TODO: Clear step out?
signal output value[MAX_VALUE_LENGTH] <== SelectSubArray(DATA_BYTES, MAX_VALUE_LENGTH)(data, value_starting_index[DATA_BYTES-1], MAX_VALUE_LENGTH);
signal value[MAX_VALUE_LENGTH] <== SelectSubArray(DATA_BYTES, MAX_VALUE_LENGTH)(data, value_starting_index[DATA_BYTES-1], MAX_VALUE_LENGTH);
for (var i = 0 ; i < MAX_VALUE_LENGTH ; i++) {
// log(i, value[i]);
step_out[i] <== value[i];
Expand Down
64 changes: 63 additions & 1 deletion circuits/test/full/full.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ describe("NIVC_FULL", async () => {
let httpParseAndLockStartLineCircuit: WitnessTester<["step_in", "beginning", "beginning_length", "middle", "middle_length", "final", "final_length"], ["step_out"]>;
let lockHeaderCircuit: WitnessTester<["step_in", "header", "headerNameLength", "value", "headerValueLength"], ["step_out"]>;
let bodyMaskCircuit: WitnessTester<["step_in"], ["step_out"]>;
let parse_circuit: WitnessTester<["step_in"], ["step_out"]>;
let json_mask_object_circuit: WitnessTester<["step_in", "key", "keyLen"], ["step_out"]>;
let json_mask_arr_circuit: WitnessTester<["step_in", "index"], ["step_out"]>;
let extract_value_circuit: WitnessTester<["step_in"], ["step_out"]>;

const DATA_BYTES = 320;
const MAX_STACK_HEIGHT = 5;
Expand All @@ -58,6 +62,9 @@ describe("NIVC_FULL", async () => {
const middle = [50, 48, 48]; // 200
const final = [79, 75]; // OK

const MAX_KEY_LENGTH = 8;
const MAX_VALUE_LENGTH = 35;

before(async () => {
aesCircuit = await circomkit.WitnessTester("AESGCTRFOLD", {
file: "aes-gcm/nivc/aes-gctr-nivc",
Expand Down Expand Up @@ -85,6 +92,34 @@ describe("NIVC_FULL", async () => {
params: [DATA_BYTES, MAX_STACK_HEIGHT],
});
console.log("#constraints (HTTP-BODY-MASK):", await bodyMaskCircuit.getConstraintCount());

parse_circuit = await circomkit.WitnessTester(`JsonParseNIVC`, {
file: "json/nivc/parse",
template: "JsonParseNIVC",
params: [DATA_BYTES, MAX_STACK_HEIGHT],
});
console.log("#constraints (JSON-PARSE):", await parse_circuit.getConstraintCount());

json_mask_arr_circuit = await circomkit.WitnessTester(`JsonMaskArrayIndexNIVC`, {
file: "json/nivc/masker",
template: "JsonMaskArrayIndexNIVC",
params: [DATA_BYTES, MAX_STACK_HEIGHT],
});
console.log("#constraints (JSON-MASK-ARRAY-INDEX):", await json_mask_arr_circuit.getConstraintCount());

json_mask_object_circuit = await circomkit.WitnessTester(`JsonMaskObjectNIVC`, {
file: "json/nivc/masker",
template: "JsonMaskObjectNIVC",
params: [DATA_BYTES, MAX_STACK_HEIGHT, MAX_KEY_LENGTH],
});
console.log("#constraints (JSON-MASK-OBJECT):", await json_mask_object_circuit.getConstraintCount());

extract_value_circuit = await circomkit.WitnessTester(`JsonMaskExtractFinal`, {
file: "json/nivc/extractor",
template: "MaskExtractFinal",
params: [DATA_BYTES, MAX_STACK_HEIGHT, MAX_VALUE_LENGTH],
});
console.log("#constraints (JSON-MASK-EXTRACT-FINAL):", await extract_value_circuit.getConstraintCount());
});


Expand Down Expand Up @@ -127,6 +162,33 @@ describe("NIVC_FULL", async () => {
let maskedInput = extendedJsonInput.fill(0, 0, idx);
maskedInput = maskedInput.fill(0, 320);

bodyMaskOut === maskedInput;


let key0 = [100, 97, 116, 97, 0, 0, 0, 0]; // "data"
let key0Len = 4;
let key1 = [105, 116, 101, 109, 115, 0, 0, 0]; // "items"
let key1Len = 5;
let key2 = [112, 114, 111, 102, 105, 108, 101, 0]; // "profile"
let key2Len = 7;
let key3 = [110, 97, 109, 101, 0, 0, 0, 0]; // "name"
let key3Len = 4;

let value = toByte("\"Taylor Swift\"");

let json_parse = await parse_circuit.compute({ step_in: bodyMaskOut }, ["step_out"]);

let json_extract_key0 = await json_mask_object_circuit.compute({ step_in: json_parse.step_out, key: key0, keyLen: key0Len }, ["step_out"]);

let json_num = json_extract_key0.step_out as number[];
console.log("json_extract_key0", json_num);
let json_extract_key1 = await json_mask_object_circuit.compute({ step_in: json_extract_key0.step_out, key: key1, keyLen: key1Len }, ["step_out"]);

let json_extract_arr = await json_mask_arr_circuit.compute({ step_in: json_extract_key1.step_out, index: 0 }, ["step_out"]);

let json_extract_key2 = await json_mask_object_circuit.compute({ step_in: json_extract_arr.step_out, key: key2, keyLen: key2Len }, ["step_out"]);

let json_extract_key3 = await json_mask_object_circuit.compute({ step_in: json_extract_key2.step_out, key: key3, keyLen: key3Len }, ["step_out"]);

await extract_value_circuit.expectPass({ step_in: json_extract_key3.step_out }, { step_out: value });
});
});
2 changes: 1 addition & 1 deletion circuits/test/json/nivc/masker_nivc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe("NIVC Extract", async () => {
let key1Len = 5;
let key2 = [112, 114, 111, 102, 105, 108, 101, 0]; // "profile"
let key2Len = 7;
let key3 = [110, 97, 109, 101, 0, 0, 0, 0]; "name"
let key3 = [110, 97, 109, 101, 0, 0, 0, 0]; // "name"
let key3Len = 4;

let value = toByte("\"Taylor Swift\"");
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "web-prover-circuits",
"description": "ZK Circuits for WebProofs",
"version": "0.2.6",
"version": "0.2.7",
"license": "Apache-2.0",
"repository": {
"type": "git",
Expand Down

0 comments on commit e50209a

Please sign in to comment.