Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup #1

Merged
merged 8 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: CI

on: [push]

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: 14
- name: Download circom
run: wget https://github.com/iden3/circom/releases/latest/download/circom-linux-amd64 -O /usr/local/bin/circom
- name: Update permission
run: chmod +x /usr/local/bin/circom
- name: Install yarn
run: npm install -g yarn
- name: Install dependencies
run: yarn
- name: Create build folder
run: mkdir build
- name: Run tests
run: yarn test
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,6 @@ dist

# TernJS port file
.tern-port

.vscode
build
48 changes: 48 additions & 0 deletions circuits/regex_helpers.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
pragma circom 2.0.3;

include "../node_modules/circomlib/circuits/comparators.circom";
include "../node_modules/circomlib/circuits/gates.circom";

template MultiOROld(n) {
signal input in[n];
signal output out;
component or1;
component or2;
component ors[2];
if (n==1) {
out <== in[0];
} else if (n==2) {
or1 = OR();
or1.a <== in[0];
or1.b <== in[1];
out <== or1.out;
} else {
or2 = OR();
var n1 = n\2;
var n2 = n-n\2;
ors[0] = MultiOR(n1);
ors[1] = MultiOR(n2);
var i;
for (i=0; i<n1; i++) ors[0].in[i] <== in[i];
for (i=0; i<n2; i++) ors[1].in[i] <== in[n1+i];
or2.a <== ors[0].out;
or2.b <== ors[1].out;
out <== or2.out;
}
}

template MultiOR(n) {
signal input in[n];
signal output out;

signal sums[n];
sums[0] <== in[0];
for (var i = 1; i < n; i++) {
sums[i] <== sums[i-1] + in[i];
}

component is_zero = IsZero();
is_zero.in <== sums[n-1];
out <== 1 - is_zero.out;
}

27 changes: 27 additions & 0 deletions compiler/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const generator = require('../compiler/gen')

const program = require("commander");

program
.version("0.0.1")
.description("A sample CLI program")

program
.command("compile <regex>")
.description("Compile a regular expression into circom circuits")
.action((regex) => {
generator.generateCircuit(regex)
});

program.on("command:*", () => {
console.error(
"Error: Invalid command. See --help for a list of available commands."
);
process.exit(1);
});

program.parse(process.argv);

if (!program.args.length) {
program.help();
}
220 changes: 220 additions & 0 deletions compiler/gen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
const fs = require("fs").promises;
const path = require("path")
const regexpTree = require('regexp-tree');
const assert = require("assert")
const lexical = require('./lexical')

async function generateCircuit(regex, circuitLibPath) {
const ast = regexpTree.parse(`/${regex}/`);
regexpTree.traverse(ast, {
'*': function({node}) {
if (node.type === "CharacterClass") {
throw new Error('CharacterClass not supported')
}
},
});

const graph_json = lexical.compile(regex)
const N = graph_json.length;

// Outgoing nodes
const graph = Array.from({
length: N
}, () => ({}));
// Incoming Nodes
const rev_graph = Array.from({
length: N
}, () => []);
const accept_nodes = new Set();

for (let i = 0; i < N; i++) {
for (let k in graph_json[i]["edges"]) {
//assert len(k) == 1
//assert ord(k) < 128
const v = graph_json[i]["edges"][k];
graph[i][k] = v;
rev_graph[v].push([k, i]);
}
if (graph_json[i]["type"] === "accept") {
accept_nodes.add(i);
}
}

assert.strictEqual(accept_nodes.size, 1);

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

let lines = [];
lines.push("for (var i = 0; i < num_bytes; i++) {");

assert.strictEqual(accept_nodes.has(0), false);

for (let i = 1; i < N; i++) {
const outputs = [];
for (let [k, prev_i] of rev_graph[i]) {
let vals = new Set(JSON.parse(k));
const eq_outputs = [];

const uppercase = new Set("ABCDEFGHIJKLMNOPQRSTUVWXYZ".split(""));
const lowercase = new Set("abcdefghijklmnopqrstuvwxyz".split(""));
const digits = new Set("0123456789".split(""));

if (new Set([...uppercase].filter((x) => vals.has(x))).size === uppercase.size) {
vals = new Set([...vals].filter((x) => !uppercase.has(x)));
lines.push(`\t//UPPERCASE`);
lines.push(`\tlt[${lt_i}][i] = LessThan(8);`);
lines.push(`\tlt[${lt_i}][i].in[0] <== 64;`);
lines.push(`\tlt[${lt_i}][i].in[1] <== in[i];`);

lines.push(`\tlt[${lt_i + 1}][i] = LessThan(8);`);
lines.push(`\tlt[${lt_i + 1}][i].in[0] <== in[i];`);
lines.push(`\tlt[${lt_i + 1}][i].in[1] <== 91;`);

lines.push(`\tand[${and_i}][i] = AND();`);
lines.push(`\tand[${and_i}][i].a <== lt[${lt_i}][i].out;`);
lines.push(`\tand[${and_i}][i].b <== lt[${lt_i + 1}][i].out;`);

eq_outputs.push(["and", and_i]);
lt_i += 2;
and_i += 1;
}
if (new Set([...lowercase].filter((x) => vals.has(x))).size === lowercase.size) {
vals = new Set([...vals].filter((x) => !lowercase.has(x)));
lines.push(`\t//lowercase`);
lines.push(`\tlt[${lt_i}][i] = LessThan(8);`);
lines.push(`\tlt[${lt_i}][i].in[0] <== 96;`);
lines.push(`\tlt[${lt_i}][i].in[1] <== in[i];`);

lines.push(`\tlt[${lt_i + 1}][i] = LessThan(8);`);
lines.push(`\tlt[${lt_i + 1}][i].in[0] <== in[i];`);
lines.push(`\tlt[${lt_i + 1}][i].in[1] <== 123;`);

lines.push(`\tand[${and_i}][i] = AND();`);
lines.push(`\tand[${and_i}][i].a <== lt[${lt_i}][i].out;`);
lines.push(`\tand[${and_i}][i].b <== lt[${lt_i + 1}][i].out;`);

eq_outputs.push(["and", and_i]);
lt_i += 2;
and_i += 1;
}
if (new Set([...digits].filter((x) => vals.has(x))).size === digits.size) {
vals = new Set([...vals].filter((x) => !digits.has(x)));
lines.push(`\t//digits`);
lines.push(`\tlt[${lt_i}][i] = LessThan(8);`);
lines.push(`\tlt[${lt_i}][i].in[0] <== 47;`);
lines.push(`\tlt[${lt_i}][i].in[1] <== in[i];`);

lines.push(`\tlt[${lt_i + 1}][i] = LessThan(8);`);
lines.push(`\tlt[${lt_i + 1}][i].in[0] <== in[i];`);
lines.push(`\tlt[${lt_i + 1}][i].in[1] <== 58;`);

lines.push(`\tand[${and_i}][i] = AND();`);
lines.push(`\tand[${and_i}][i].a <== lt[${lt_i}][i].out;`);
lines.push(`\tand[${and_i}][i].b <== lt[${lt_i + 1}][i].out;`);

eq_outputs.push(["and", and_i]);
lt_i += 2;
and_i += 1;
}
for (let c of vals) {
assert.strictEqual(c.length, 1);
lines.push(`\t//${c}`);
lines.push(`\teq[${eq_i}][i] = IsEqual();`);
lines.push(`\teq[${eq_i}][i].in[0] <== in[i];`);
lines.push(`\teq[${eq_i}][i].in[1] <== ${c.charCodeAt(0)};`);
eq_outputs.push(["eq", eq_i]);
eq_i += 1;
}

lines.push(`\tand[${and_i}][i] = AND();`);
lines.push(`\tand[${and_i}][i].a <== states[i][${prev_i}];`);

if (eq_outputs.length === 1) {
lines.push(`\tand[${and_i}][i].b <== ${eq_outputs[0][0]}[${eq_outputs[0][1]}][i].out;`);
} else if (eq_outputs.length > 1) {
lines.push(`\tmulti_or[${multi_or_i}][i] = MultiOR(${eq_outputs.length});`);
for (let output_i = 0; output_i < eq_outputs.length; output_i++) {
lines.push(`\tmulti_or[${multi_or_i}][i].in[${output_i}] <== ${eq_outputs[output_i][0]}[${eq_outputs[output_i][1]}][i].out;`);
}
lines.push(`\tand[${and_i}][i].b <== multi_or[${multi_or_i}][i].out;`);
multi_or_i += 1;
}
outputs.push(and_i);
and_i += 1;
}

if (outputs.length === 1) {
lines.push(`\tstates[i+1][${i}] <== and[${outputs[0]}][i].out;`);
} else if (outputs.length > 1) {
lines.push(`\tmulti_or[${multi_or_i}][i] = MultiOR(${outputs.length});`);
for (let output_i = 0; output_i < outputs.length; output_i++) {
lines.push(`\tmulti_or[${multi_or_i}][i].in[${output_i}] <== and[${outputs[output_i]}][i].out;`);
}
lines.push(`\tstates[i+1][${i}] <== multi_or[${multi_or_i}][i].out;`);
multi_or_i += 1;
}
}

lines.push("}");

let declarations = [];

if (eq_i > 0) {
declarations.push(`component eq[${eq_i}][num_bytes];`);
}
if (lt_i > 0) {
declarations.push(`component lt[${lt_i}][num_bytes];`);
}
if (and_i > 0) {
declarations.push(`component and[${and_i}][num_bytes];`);
}
if (multi_or_i > 0) {
declarations.push(`component multi_or[${multi_or_i}][num_bytes];`);
}
declarations.push(`signal states[num_bytes+1][${N}];`);
declarations.push("");

let init_code = [];

init_code.push("for (var i = 0; i < num_bytes; i++) {");
init_code.push("\tstates[i][0] <== 1;");
init_code.push("}");

init_code.push(`for (var i = 1; i < ${N}; i++) {`);
init_code.push("\tstates[0][i] <== 0;");
init_code.push("}");

init_code.push("");

const reveal_code = [];
reveal_code.push("signal output reveal[num_bytes];");
reveal_code.push("for (var i = 0; i < num_bytes; i++) {");
reveal_code.push("\treveal[i] <== in[i] * states[i+1][1];");
reveal_code.push("}");
reveal_code.push("");

lines = [...declarations, ...init_code, ...lines, ...reveal_code];

try {
let tpl = await (await fs.readFile(`${__dirname}/tpl.circom`)).toString()
tpl = tpl.replace('TEMPLATE_NAME_PLACEHOLDER', 'Regex')
tpl = tpl.replace('COMPILED_CONTENT_PLACEHOLDER', lines.join('\n\t'))
tpl = tpl.replace(/CIRCUIT_FOLDER/g, circuitLibPath || `../circuits`)
tpl = tpl.replace(/\t/g, ' '.repeat(4))

const outputPath = `${__dirname}/../build/compiled.circom`;
await fs.writeFile(outputPath, tpl);
process.env.DEBUG && console.log(`Circuit compiled to ${path.normalize(outputPath)}`);
} catch (error) {
console.log(error)
}
}


module.exports = {
generateCircuit,
...lexical
}
Loading