Skip to content

Commit

Permalink
refactor: code reorganisation
Browse files Browse the repository at this point in the history
  • Loading branch information
lonerapier committed Aug 28, 2024
1 parent 1dbf8d4 commit 26031e3
Show file tree
Hide file tree
Showing 25 changed files with 243 additions and 193 deletions.
File renamed without changes.
File renamed without changes.
28 changes: 17 additions & 11 deletions circuits/interpreter.circom → circuits/json/interpreter.circom
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
pragma circom 2.1.9;

include "extract.circom";
include "parser.circom";
include "language.circom";
include "search.circom";
include "./utils/array.circom";
include "./parser/parser.circom";
include "./parser/language.circom";
include "../utils/search.circom";
include "../utils/array.circom";
include "circomlib/circuits/mux1.circom";
include "circomlib/circuits/gates.circom";
include "@zk-email/circuits/utils/functions.circom";
Expand Down Expand Up @@ -179,7 +178,9 @@ template NextKVPair(n) {
signal currentVal[2] <== topOfStack.value;

signal isNextPair <== IsEqualArray(2)([currentVal, [1, 0]]);
signal isComma <== IsEqual()([currByte, 44]); // `, -> 44`

component syntax = Syntax();
signal isComma <== IsEqual()([currByte, syntax.COMMA]); // `, -> 44`

out <== isNextPair*isComma ;
}
Expand Down Expand Up @@ -212,7 +213,8 @@ template NextKVPairAtDepth(n, depth) {
signal isNextPair <== IsEqualArray(2)([currentVal, [1, 0]]);

// `, -> 44`
signal isComma <== IsEqual()([currByte, 44]);
component syntax = Syntax();
signal isComma <== IsEqual()([currByte, syntax.COMMA]);
// pointer <= depth
signal atLessDepth <== LessEqThan(logMaxDepth)([pointer, depth]);
// current depth is less than key depth
Expand Down Expand Up @@ -243,11 +245,13 @@ template KeyMatch(dataLen, keyLen) {
signal input index;
signal input parsing_key;

component syntax = Syntax();

signal end_of_key <== IndexSelector(dataLen)(data, index + keyLen);
signal is_end_of_key_equal_to_quote <== IsEqual()([end_of_key, 34]);
signal is_end_of_key_equal_to_quote <== IsEqual()([end_of_key, syntax.QUOTE]);

signal start_of_key <== IndexSelector(dataLen)(data, index - 1);
signal is_start_of_key_equal_to_quote <== IsEqual()([start_of_key, 34]);
signal is_start_of_key_equal_to_quote <== IsEqual()([start_of_key, syntax.QUOTE]);

signal substring_match <== SubstringMatchWithIndex(dataLen, keyLen)(data, key, r, index);

Expand Down Expand Up @@ -287,13 +291,15 @@ template KeyMatchAtDepth(dataLen, n, keyLen, depth) {
topOfStack.stack <== stack;
signal pointer <== topOfStack.pointer;

component syntax = Syntax();

// end of key equals `"`
signal end_of_key <== IndexSelector(dataLen)(data, index + keyLen);
signal is_end_of_key_equal_to_quote <== IsEqual()([end_of_key, 34]);
signal is_end_of_key_equal_to_quote <== IsEqual()([end_of_key, syntax.QUOTE]);

// start of key equals `"`
signal start_of_key <== IndexSelector(dataLen)(data, index - 1);
signal is_start_of_key_equal_to_quote <== IsEqual()([start_of_key, 34]);
signal is_start_of_key_equal_to_quote <== IsEqual()([start_of_key, syntax.QUOTE]);

// key matches
signal substring_match <== SubstringMatchWithIndex(dataLen, keyLen)(data, key, r, index);
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ Tests for this module are located in the files: `circuits/test/parser/*.test.ts

pragma circom 2.1.9;

include "../utils/array.circom";
include "../utils/bytes.circom";
include "../utils/operators.circom";
include "../../utils/array.circom";
include "../../utils/bytes.circom";
include "../../utils/operators.circom";
include "language.circom";

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pragma circom 2.1.9;

include "../utils/bytes.circom";
include "../../utils/bytes.circom";
include "machine.circom";

template Parser(DATA_BYTES, MAX_STACK_HEIGHT) {
Expand Down
34 changes: 34 additions & 0 deletions circuits/test/common/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'mocha';
import { readFileSync } from "fs";
import { join } from "path";
import { Circomkit, WitnessTester } from "circomkit";

export const circomkit = new Circomkit({
Expand All @@ -21,4 +23,36 @@ export function generateDescription(input: any): string {
return Object.entries(input)
.map(([key, value]) => `${key} = ${stringifyValue(value)}`)
.join(", ");
}

export function readInputFile(filename: string, key: any[]): [number[], number[][], number[]] {
const valueStringPath = join(__dirname, "..", "..", "..", "examples", "json", "test", filename);

let input: number[] = [];
let output: number[] = [];

let data = readFileSync(valueStringPath, 'utf-8');

let keyUnicode: number[][] = [];
for (let i = 0; i < key.length; i++) {
keyUnicode[i] = [];
let key_string = key[i].toString();
for (let j = 0; j < key_string.length; j++) {
keyUnicode[i].push(key_string.charCodeAt(j));
}
}

const byteArray = [];
for (let i = 0; i < data.length; i++) {
byteArray.push(data.charCodeAt(i));
}
input = byteArray;

let jsonFile = JSON.parse(data);
let value: string = key.reduce((acc, key) => acc && acc[key], jsonFile).toString();
for (let i = 0; i < value.length; i++) {
output.push(value.charCodeAt(i));
}

return [input, keyUnicode, output];
}
Original file line number Diff line number Diff line change
@@ -1,45 +1,13 @@
import { circomkit, WitnessTester } from "../common";
import { readFileSync } from "fs";
import { circomkit, WitnessTester, readInputFile } from "../../common";
import { join } from "path";
import { spawn } from "child_process";

export function readInputFile(filename: string, key: any[]): [number[], number[][], number[]] {
const value_string_path = join(__dirname, "..", "..", "..", "json_examples", "test", filename);

let input: number[] = [];
let output: number[] = [];

let data = readFileSync(value_string_path, 'utf-8');

let keyUnicode: number[][] = [];
for (let i = 0; i < key.length; i++) {
keyUnicode[i] = [];
let key_string = key[i].toString();
for (let j = 0; j < key_string.length; j++) {
keyUnicode[i].push(key_string.charCodeAt(j));
}
}

const byteArray = [];
for (let i = 0; i < data.length; i++) {
byteArray.push(data.charCodeAt(i));
}
input = byteArray;

let jsonFile = JSON.parse(data);
let value: string = key.reduce((acc, key) => acc && acc[key], jsonFile).toString();
for (let i = 0; i < value.length; i++) {
output.push(value.charCodeAt(i));
}

return [input, keyUnicode, output];
}

function executeCodegen(input_file_name: string, output_filename: string) {
function executeCodegen(inputFilename: string, outputFilename: string) {
return new Promise((resolve, reject) => {
const input_path = join(__dirname, "..", "..", "..", "json_examples", "codegen", input_file_name);
const inputPath = join(__dirname, "..", "..", "..", "..", "examples", "json", "test", "codegen", inputFilename);

const codegen = spawn("cargo", ["run", "--bin", "codegen", "--", "--json-file", input_path, "--output-filename", output_filename]);
const codegen = spawn("cargo", ["run", "--bin", "codegen", "--", "--json-file", inputPath, "--output-filename", outputFilename]);

codegen.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { circomkit, WitnessTester, generateDescription } from "../common";
import { PoseidonModular } from "../common/poseidon";
import { readInputFile } from "./extractor.test";
import { circomkit, WitnessTester, generateDescription, readInputFile } from "../../common";
import { PoseidonModular } from "../../common/poseidon";

describe("Interpreter", async () => {
describe("InsideKey", async () => {
let circuit: WitnessTester<["stack", "parsing_string", "parsing_number"], ["out"]>;

before(async () => {
circuit = await circomkit.WitnessTester(`InsideKey`, {
file: "circuits/interpreter",
file: "circuits/json/interpreter",
template: "InsideKey",
params: [4],
});
Expand Down Expand Up @@ -47,7 +46,7 @@ describe("Interpreter", async () => {

before(async () => {
circuit = await circomkit.WitnessTester(`InsideValue`, {
file: "circuits/interpreter",
file: "circuits/json/interpreter",
template: "InsideValue",
params: [4],
});
Expand Down Expand Up @@ -89,7 +88,7 @@ describe("Interpreter", async () => {

it(`(valid) witness: ${description} ${desc}`, async () => {
circuit = await circomkit.WitnessTester(`InsideValueAtDepth`, {
file: "circuits/interpreter",
file: "circuits/json/interpreter",
template: "InsideValueAtDepth",
params: [4, depth],
});
Expand Down Expand Up @@ -126,7 +125,7 @@ describe("Interpreter", async () => {

it(`(valid) witness: ${description} ${desc}`, async () => {
circuit = await circomkit.WitnessTester(`InsideArrayIndex`, {
file: "circuits/interpreter",
file: "circuits/json/interpreter",
template: "InsideArrayIndex",
params: [4, index],
});
Expand Down Expand Up @@ -166,7 +165,7 @@ describe("Interpreter", async () => {

it(`(valid) witness: ${description} ${desc}`, async () => {
circuit = await circomkit.WitnessTester(`InsideArrayIndexAtDepth`, {
file: "circuits/interpreter",
file: "circuits/json/interpreter",
template: "InsideArrayIndexAtDepth",
params: [4, index, depth],
});
Expand Down Expand Up @@ -200,7 +199,7 @@ describe("Interpreter", async () => {

before(async () => {
circuit = await circomkit.WitnessTester(`NextKVPair`, {
file: "circuits/interpreter",
file: "circuits/json/interpreter",
template: "NextKVPair",
params: [4],
});
Expand Down Expand Up @@ -240,7 +239,7 @@ describe("Interpreter", async () => {

it(`(valid) witness: ${description} ${desc}`, async () => {
circuit = await circomkit.WitnessTester(`NextKVPairAtDepth`, {
file: "circuits/interpreter",
file: "circuits/json/interpreter",
template: "NextKVPairAtDepth",
params: [4, depth],
});
Expand Down Expand Up @@ -274,7 +273,7 @@ describe("Interpreter", async () => {

it(`(valid) witness: ${description} ${desc}`, async () => {
circuit = await circomkit.WitnessTester(`KeyMatch`, {
file: "circuits/interpreter",
file: "circuits/json/interpreter",
template: "KeyMatch",
params: [input.data.length, input.key.length],
});
Expand Down Expand Up @@ -315,7 +314,7 @@ describe("Interpreter", async () => {

it(`(valid) witness: ${description} ${desc}`, async () => {
circuit = await circomkit.WitnessTester(`KeyMatchAtDepth`, {
file: "circuits/interpreter",
file: "circuits/json/interpreter",
template: "KeyMatchAtDepth",
params: [input.data.length, 4, input.key.length, depth],
});
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { circomkit, WitnessTester, generateDescription } from "../common";
import { circomkit, WitnessTester, generateDescription } from "../../common";
import { Delimiters, WhiteSpace, Numbers, Escape, INITIAL_IN, INITIAL_OUT } from '.';


Expand All @@ -19,7 +19,7 @@ describe("StateUpdate", () => {

before(async () => {
circuit = await circomkit.WitnessTester(`StateUpdate`, {
file: "circuits/parser_json/machine",
file: "circuits/json/parser/machine",
template: "StateUpdate",
params: [4],
});
Expand Down Expand Up @@ -65,7 +65,7 @@ describe("StateUpdate", () => {
// init: stack == [[1, 0], [0, 0], [0, 0], [0, 0]]
// read: `"`
// expect: parsing_string --> 0
//
//
let in_key_to_exit = { ...INITIAL_IN };
in_key_to_exit.stack = [[1, 0], [0, 0], [0, 0], [0, 0]];
in_key_to_exit.parsing_string = 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { circomkit, WitnessTester, generateDescription } from "../common";
import { circomkit, WitnessTester, generateDescription } from "../../common";
import { Delimiters, WhiteSpace, Numbers, Escape, INITIAL_IN, INITIAL_OUT } from '.';

describe("GetTopOfStack", () => {
let circuit: WitnessTester<["stack"], ["value", "pointer"]>;
before(async () => {
circuit = await circomkit.WitnessTester(`GetTopOfStack`, {
file: "circuits/parser_json/machine",
file: "circuits/json/parser/machine",
template: "GetTopOfStack",
params: [4],
});
Expand Down Expand Up @@ -34,7 +34,7 @@ describe("StateUpdate :: RewriteStack", () => {
>;
before(async () => {
circuit = await circomkit.WitnessTester(`GetTopOfStack`, {
file: "circuits/parser_json/machine",
file: "circuits/json/parser/machine",
template: "StateUpdate",
params: [4],
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { circomkit, WitnessTester, generateDescription } from "../common";
import { circomkit, WitnessTester, generateDescription } from "../../common";
import { Delimiters, WhiteSpace, Numbers, Escape, INITIAL_IN, INITIAL_OUT } from '.';

describe("StateUpdate :: Values", () => {
Expand All @@ -8,7 +8,7 @@ describe("StateUpdate :: Values", () => {
>;
before(async () => {
circuit = await circomkit.WitnessTester(`GetTopOfStack`, {
file: "circuits/parser_json/machine",
file: "circuits/json/parser/machine",
template: "StateUpdate",
params: [4],
});
Expand Down
Loading

0 comments on commit 26031e3

Please sign in to comment.