Skip to content

Commit

Permalink
Merge pull request #29 from SneakY-NickY/js_to_ts
Browse files Browse the repository at this point in the history
WIP - JS to TS
  • Loading branch information
SoraSuegami authored Nov 1, 2023
2 parents fdf1d5a + d7e930a commit 1aed620
Show file tree
Hide file tree
Showing 2 changed files with 402 additions and 270 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
function genCircomAllstr(graph_json, template_name) {
const N = graph_json.length;
type Graph = {
type: string;
edges: Record<string, number>;
}[];

function genCircomAllstr(graph_json: Graph, template_name: string): string {
const N: number = graph_json.length;
// console.log(JSON.stringify(graph_json, null, 2));
// const graph = Array(N).fill({});
const rev_graph = [];
const to_init_graph = [];
let init_going_state = null;
const rev_graph: Record<number, Record<number, number[]>> = {};
const to_init_graph: number[][] = [];
let init_going_state: number | null = null;

for (let i = 0; i < N; i++) {
rev_graph.push({});
rev_graph[i] = {};
to_init_graph.push([]);
}
let accept_nodes = new Set();

let accept_nodes: Set<number> = new Set();
for (let i = 0; i < N; i++) {
for (let k in graph_json[i]["edges"]) {
const v = graph_json[i]["edges"][k];
rev_graph[v][i] = Array.from(JSON.parse(k)).map(c => c.charCodeAt());
const node = graph_json[i];
for (let k in node.edges) {
const v: number = node.edges[k];
rev_graph[v][i] = Array.from(JSON.parse(k)).map(c => (c as string).charCodeAt(0));
if (i === 0) {
const index = rev_graph[v][i].indexOf(94);
if (index !== -1) {
Expand All @@ -28,10 +36,11 @@ function genCircomAllstr(graph_json, template_name) {
}
}
}
if (graph_json[i]["type"] == "accept") {
if (node.type == "accept") {
accept_nodes.add(i);
}
}

if (init_going_state !== null) {
for (const [going_state, chars] of Object.entries(to_init_graph)) {
if (chars.length === 0) {
Expand All @@ -47,18 +56,18 @@ function genCircomAllstr(graph_json, template_name) {
if (accept_nodes[0] === null) {
throw new Error("accept node must not be 0");
}
accept_nodes = [...accept_nodes];
if (accept_nodes.length !== 1) {
accept_nodes.add([...accept_nodes][0]);
if (accept_nodes.size !== 1) {
throw new Error("the size of accept nodes must be one");
}

let eq_i = 0;
let lt_i = 0;
let and_i = 0;
let multi_or_i = 0;
let eq_i: number = 0;
let lt_i: number = 0;
let and_i: number = 0;
let multi_or_i: number = 0;

let lines = [];
lines.push("\tfor (var i = 0; i < num_bytes; i++) {");
let lines: string[] = [];
lines.push(`\tfor (var i = 0; i < num_bytes; i++) {`);

// const uppercase = new Set(Array.from("ABCDEFGHIJKLMNOPQRSTUVWXYZ").map(c => c.charCodeAt()));
// const lowercase = new Set(Array.from("abcdefghijklmnopqrstuvwxyz").map(c => c.charCodeAt()));
Expand All @@ -67,16 +76,15 @@ function genCircomAllstr(graph_json, template_name) {
// const symbols2 = new Set(["[", "\\", "]", "^", "_", "`"].map(c => c.charCodeAt()));
// const symbols3 = new Set(["{", "|", "}", "~"].map(c => c.charCodeAt()));
lines.push(`\t\tstate_changed[i] = MultiOR(${N - 1});`);

for (let i = 1; i < N; i++) {
const outputs = [];
const outputs: number[] = [];
// let is_negates = [];
for (let prev_i of Object.keys(rev_graph[i])) {
const k = rev_graph[i][prev_i];
k.sort((a, b) => {
Number(a) - Number(b);
});
const eq_outputs = [];
let vals = new Set(k);
k.sort((a, b) => Number(a) - Number(b));
const eq_outputs: [string, number][] = [];
let vals: Set<number> = new Set(k);
// let is_negate = false;
// if (vals.has(0xff)) {
// vals.delete(0xff);
Expand All @@ -102,9 +110,10 @@ function genCircomAllstr(graph_json, template_name) {
// }
// }
// }
const min_maxes = [];
let cur_min = k[0];
let cur_max = k[0];
const min_maxes: [number, number][] = [];
let cur_min: number = k[0];
let cur_max: number = k[0];

for (let idx = 1; idx < k.length; ++idx) {
if (cur_max + 1 === k[idx]) {
cur_max += 1;
Expand All @@ -116,6 +125,7 @@ function genCircomAllstr(graph_json, template_name) {
cur_max = k[idx];
}
}

if (cur_max - cur_min >= 16) {
min_maxes.push([cur_min, cur_max]);
}
Expand Down Expand Up @@ -165,16 +175,16 @@ function genCircomAllstr(graph_json, template_name) {
lines.push(`\t\tand[${and_i}][i].b <== lt[${lt_i + 1}][i].out;`);

eq_outputs.push(['and', and_i]);
lt_i += 2
and_i += 1
lt_i += 2;
and_i += 1;
}

for (let code of vals) {
lines.push(`\t\teq[${eq_i}][i] = IsEqual();`);
lines.push(`\t\teq[${eq_i}][i].in[0] <== in[i];`);
lines.push(`\t\teq[${eq_i}][i].in[1] <== ${code};`);
eq_outputs.push(['eq', eq_i]);
eq_i += 1
eq_i += 1;
}

lines.push(`\t\tand[${and_i}][i] = AND();`);
Expand All @@ -197,7 +207,7 @@ function genCircomAllstr(graph_json, template_name) {
// lines.push(`\t\tand[${and_i}][i].b <== multi_or[${multi_or_i}][i].out;`);
// }
lines.push(`\t\tand[${and_i}][i].b <== multi_or[${multi_or_i}][i].out;`);
multi_or_i += 1
multi_or_i += 1;
}

outputs.push(and_i);
Expand All @@ -212,15 +222,17 @@ function genCircomAllstr(graph_json, template_name) {
lines.push(`\t\tmulti_or[${multi_or_i}][i].in[${output_i}] <== and[${outputs[output_i]}][i].out;`);
}
lines.push(`\t\tstates[i+1][${i}] <== multi_or[${multi_or_i}][i].out;`);
multi_or_i += 1
multi_or_i += 1;
}

lines.push(`\t\tstate_changed[i].in[${i - 1}] <== states[i+1][${i}];`);
}

lines.push(`\t\tstates[i+1][0] <== 1 - state_changed[i].out;`);
lines.push("\t}");
lines.push(`\t}`);


const declarations = [];
const declarations: string[] = [];
declarations.push(`pragma circom 2.1.5;\n`);
declarations.push(`include "@zk-email/zk-regex-circom/circuits/regex_helpers.circom";\n`);
// declarations.push(`pragma circom 2.1.5;\ninclude "@zk-email/circuits/regexes/regex_helpers.circom";\n`);
Expand Down Expand Up @@ -249,7 +261,7 @@ function genCircomAllstr(graph_json, template_name) {
declarations.push(`\tcomponent state_changed[num_bytes];`);
declarations.push("");

const init_code = [];
const init_code: string[] = [];
init_code.push(`\tstates[0][0] <== 1;`);
init_code.push(`\tfor (var i = 1; i < ${N}; i++) {`);
init_code.push(`\t\tstates[0][i] <== 0;`);
Expand All @@ -258,7 +270,7 @@ function genCircomAllstr(graph_json, template_name) {

lines = declarations.concat(init_code).concat(lines);

const accept_node = accept_nodes[0];
const accept_node: number = accept_nodes[0];
const accept_lines = [""];
accept_lines.push("\tcomponent final_state_result = MultiOR(num_bytes+1);");
accept_lines.push("\tfor (var i = 0; i <= num_bytes; i++) {");
Expand All @@ -267,26 +279,27 @@ function genCircomAllstr(graph_json, template_name) {
accept_lines.push("\tout <== final_state_result.out;");

lines = lines.concat(accept_lines);
let string = lines.reduce((res, line) => res + line + "\n", "");
let string: string = lines.reduce((res, line) => res + line + "\n", "");
return string;
}


// Commented these two out as they're only used by the code that's also commented out

Set.prototype.isSuperset = function (subset) {
if (this.size === 0) {
return false;
}
for (var elem of subset) {
if (!this.has(elem)) {
return false;
}
}
return true;
}
// Set.prototype.isSuperset = function (subset) {
// if (this.size === 0) {
// return false;
// }
// for (var elem of subset) {
// if (!this.has(elem)) {
// return false;
// }
// }
// return true;
// }

Set.prototype.difference = function (setB) {
for (let elem of setB) {
this.delete(elem)
}
}
// Set.prototype.difference = function (setB) {
// for (let elem of setB) {
// this.delete(elem)
// }
// }
Loading

0 comments on commit 1aed620

Please sign in to comment.