mirror of
https://github.com/arnaucube/circom.git
synced 2026-02-06 18:56:40 +01:00
Stars specifying wasm
This commit is contained in:
3
cli.js
3
cli.js
@@ -33,6 +33,7 @@ const argv = require("yargs")
|
|||||||
.usage("circom -s [input source circuit file] -o [output definition circuit file]")
|
.usage("circom -s [input source circuit file] -o [output definition circuit file]")
|
||||||
.alias("s", "source")
|
.alias("s", "source")
|
||||||
.alias("o", "output")
|
.alias("o", "output")
|
||||||
|
.alias("c", "cfile")
|
||||||
.require(["s","o"])
|
.require(["s","o"])
|
||||||
.help("h")
|
.help("h")
|
||||||
.alias("h", "help")
|
.alias("h", "help")
|
||||||
@@ -45,7 +46,7 @@ const argv = require("yargs")
|
|||||||
|
|
||||||
const fullFileName = path.resolve(process.cwd(), argv.source);
|
const fullFileName = path.resolve(process.cwd(), argv.source);
|
||||||
|
|
||||||
compiler(fullFileName).then( (cir) => {
|
compiler(fullFileName, argv.cfile).then( (cir) => {
|
||||||
fs.writeFileSync(argv.output, JSON.stringify(cir, null, 1), "utf8");
|
fs.writeFileSync(argv.output, JSON.stringify(cir, null, 1), "utf8");
|
||||||
}, (err) => {
|
}, (err) => {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
|
|||||||
83
out.c
Normal file
83
out.c
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
// File: ../../circuits/sha256/bitify.circom
|
||||||
|
function Num2Bits(ctx)
|
||||||
|
{
|
||||||
|
ctx.setVar("lc1", [], "0");
|
||||||
|
for (ctx.setVar("i", [], "0");bigInt(ctx.getVar("i",[])).lt(bigInt(ctx.getVar("n",[]))) ? 1 : 0;(ctx.setVar("i", [], bigInt(ctx.getVar("i",[])).add(bigInt("1")).mod(__P__))).add(__P__).sub(bigInt(1)).mod(__P__))
|
||||||
|
{
|
||||||
|
ctx.setSignal("out", [ctx.getVar("i",[])], bigInt(bigInt(ctx.getVar("i",[])).greater(bigInt(256)) ? 0 : bigInt(ctx.getSignal("in", [])).shr(bigInt(ctx.getVar("i",[]))).and(__MASK__)).and(bigInt("1")).and(__MASK__));
|
||||||
|
ctx.assert(bigInt(ctx.getSignal("out", [ctx.getVar("i",[])])).mul(bigInt(bigInt(ctx.getSignal("out", [ctx.getVar("i",[])])).add(__P__).sub(bigInt("1")).mod(__P__))).mod(__P__), "0");
|
||||||
|
ctx.setVar("lc1", [], bigInt(ctx.getVar("lc1",[])).add(bigInt(bigInt(ctx.getSignal("out", [ctx.getVar("i",[])])).mul(bigInt(bigInt("2").modPow(bigInt(ctx.getVar("i",[])), __P__))).mod(__P__))).mod(__P__));
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.assert(ctx.getVar("lc1",[]), ctx.getSignal("in", []));
|
||||||
|
}
|
||||||
|
|
||||||
|
function Bits2Num(ctx)
|
||||||
|
{
|
||||||
|
ctx.setVar("lc1", [], "0");
|
||||||
|
for (ctx.setVar("i", [], "0");bigInt(ctx.getVar("i",[])).lt(bigInt(ctx.getVar("n",[]))) ? 1 : 0;(ctx.setVar("i", [], bigInt(ctx.getVar("i",[])).add(bigInt("1")).mod(__P__))).add(__P__).sub(bigInt(1)).mod(__P__))
|
||||||
|
{
|
||||||
|
ctx.setVar("lc1", [], bigInt(ctx.getVar("lc1",[])).add(bigInt(bigInt(ctx.getSignal("in", [ctx.getVar("i",[])])).mul(bigInt(bigInt("2").modPow(bigInt(ctx.getVar("i",[])), __P__))).mod(__P__))).mod(__P__));
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.setSignal("out", [], ctx.getVar("lc1",[]));
|
||||||
|
ctx.assert(ctx.getSignal("out", []), ctx.getVar("lc1",[]));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// File: ../../circuits/sha256/binsum.circom
|
||||||
|
function nbits(ctx) {
|
||||||
|
ctx.setVar("n", [], "1");
|
||||||
|
ctx.setVar("r", [], "0");
|
||||||
|
while (bigInt(bigInt(ctx.getVar("n",[])).add(__P__).sub(bigInt("1")).mod(__P__)).lt(bigInt(ctx.getVar("a",[]))) ? 1 : 0) {
|
||||||
|
(ctx.setVar("r", [], bigInt(ctx.getVar("r",[])).add(bigInt("1")).mod(__P__))).add(__P__).sub(bigInt(1)).mod(__P__);
|
||||||
|
ctx.setVar("n", [], bigInt(ctx.getVar("n",[])).mul(bigInt("2")).mod(__P__));
|
||||||
|
}
|
||||||
|
|
||||||
|
return ctx.getVar("r",[]);;
|
||||||
|
}
|
||||||
|
function BinSum(ctx)
|
||||||
|
{
|
||||||
|
ctx.setVar("nout", [], ctx.callFunction("nbits", [bigInt(bigInt(bigInt("2").modPow(bigInt(ctx.getVar("n",[])), __P__)).add(__P__).sub(bigInt("1")).mod(__P__)).mul(bigInt(ctx.getVar("ops",[]))).mod(__P__)]));
|
||||||
|
ctx.setVar("lin", [], "0");
|
||||||
|
ctx.setVar("lout", [], "0");
|
||||||
|
for (ctx.setVar("k", [], "0");bigInt(ctx.getVar("k",[])).lt(bigInt(ctx.getVar("n",[]))) ? 1 : 0;(ctx.setVar("k", [], bigInt(ctx.getVar("k",[])).add(bigInt("1")).mod(__P__))).add(__P__).sub(bigInt(1)).mod(__P__))
|
||||||
|
{
|
||||||
|
for (ctx.setVar("j", [], "0");bigInt(ctx.getVar("j",[])).lt(bigInt(ctx.getVar("ops",[]))) ? 1 : 0;(ctx.setVar("j", [], bigInt(ctx.getVar("j",[])).add(bigInt("1")).mod(__P__))).add(__P__).sub(bigInt(1)).mod(__P__))
|
||||||
|
{
|
||||||
|
ctx.setVar("lin", [], bigInt(ctx.getVar("lin",[])).add(bigInt(bigInt(ctx.getSignal("in", [ctx.getVar("j",[]),ctx.getVar("k",[])])).mul(bigInt(bigInt("2").modPow(bigInt(ctx.getVar("k",[])), __P__))).mod(__P__))).mod(__P__));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
for (ctx.setVar("k", [], "0");bigInt(ctx.getVar("k",[])).lt(bigInt(ctx.getVar("nout",[]))) ? 1 : 0;(ctx.setVar("k", [], bigInt(ctx.getVar("k",[])).add(bigInt("1")).mod(__P__))).add(__P__).sub(bigInt(1)).mod(__P__))
|
||||||
|
{
|
||||||
|
ctx.setSignal("out", [ctx.getVar("k",[])], bigInt(bigInt(ctx.getVar("k",[])).greater(bigInt(256)) ? 0 : bigInt(ctx.getVar("lin",[])).shr(bigInt(ctx.getVar("k",[]))).and(__MASK__)).and(bigInt("1")).and(__MASK__));
|
||||||
|
ctx.assert(bigInt(ctx.getSignal("out", [ctx.getVar("k",[])])).mul(bigInt(bigInt(ctx.getSignal("out", [ctx.getVar("k",[])])).add(__P__).sub(bigInt("1")).mod(__P__))).mod(__P__), "0");
|
||||||
|
ctx.setVar("lout", [], bigInt(ctx.getVar("lout",[])).add(bigInt(bigInt(ctx.getSignal("out", [ctx.getVar("k",[])])).mul(bigInt(bigInt("2").modPow(bigInt(ctx.getVar("k",[])), __P__))).mod(__P__))).mod(__P__));
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.assert(ctx.getVar("lin",[]), ctx.getVar("lout",[]));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function A(ctx)
|
||||||
|
{
|
||||||
|
ctx.setPin("n2ba", [], "in", [], ctx.getSignal("a", []));
|
||||||
|
ctx.assert(ctx.getPin("n2ba", [], "in", []), ctx.getSignal("a", []));
|
||||||
|
ctx.setPin("n2bb", [], "in", [], ctx.getSignal("b", []));
|
||||||
|
ctx.assert(ctx.getPin("n2bb", [], "in", []), ctx.getSignal("b", []));
|
||||||
|
for (ctx.setVar("i", [], "0");bigInt(ctx.getVar("i",[])).lt(bigInt("32")) ? 1 : 0;(ctx.setVar("i", [], bigInt(ctx.getVar("i",[])).add(bigInt("1")).mod(__P__))).add(__P__).sub(bigInt(1)).mod(__P__))
|
||||||
|
{
|
||||||
|
ctx.setPin("sum", [], "in", ["0",ctx.getVar("i",[])], ctx.getPin("n2ba", [], "out", [ctx.getVar("i",[])]));
|
||||||
|
ctx.assert(ctx.getPin("sum", [], "in", ["0",ctx.getVar("i",[])]), ctx.getPin("n2ba", [], "out", [ctx.getVar("i",[])]));
|
||||||
|
ctx.setPin("sum", [], "in", ["1",ctx.getVar("i",[])], ctx.getPin("n2bb", [], "out", [ctx.getVar("i",[])]));
|
||||||
|
ctx.assert(ctx.getPin("sum", [], "in", ["1",ctx.getVar("i",[])]), ctx.getPin("n2bb", [], "out", [ctx.getVar("i",[])]));
|
||||||
|
ctx.setPin("b2n", [], "in", [ctx.getVar("i",[])], ctx.getPin("sum", [], "out", [ctx.getVar("i",[])]));
|
||||||
|
ctx.assert(ctx.getPin("b2n", [], "in", [ctx.getVar("i",[])]), ctx.getPin("sum", [], "out", [ctx.getVar("i",[])]));
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.setSignal("out", [], ctx.getPin("b2n", [], "out", []));
|
||||||
|
ctx.assert(ctx.getSignal("out", []), ctx.getPin("b2n", [], "out", []));
|
||||||
|
}
|
||||||
|
|
||||||
@@ -23,17 +23,20 @@ const bigInt = require("big-integer");
|
|||||||
const __P__ = new bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
const __P__ = new bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
||||||
const __MASK__ = new bigInt(2).pow(253).minus(1);
|
const __MASK__ = new bigInt(2).pow(253).minus(1);
|
||||||
const assert = require("assert");
|
const assert = require("assert");
|
||||||
const gen = require("./gencode");
|
const genCode = require("./gencode");
|
||||||
const exec = require("./exec");
|
const exec = require("./exec");
|
||||||
const lc = require("./lcalgebra");
|
const lc = require("./lcalgebra");
|
||||||
|
|
||||||
|
const util = require("util");
|
||||||
|
const fs_writeFile = util.promisify(fs.writeFile)
|
||||||
|
|
||||||
module.exports = compile;
|
module.exports = compile;
|
||||||
|
|
||||||
const parser = require("../parser/jaz.js").parser;
|
const parser = require("../parser/jaz.js").parser;
|
||||||
|
|
||||||
const timeout = ms => new Promise(res => setTimeout(res, ms))
|
const timeout = ms => new Promise(res => setTimeout(res, ms))
|
||||||
|
|
||||||
async function compile(srcFile) {
|
async function compile(srcFile, cFile) {
|
||||||
const fullFileName = srcFile;
|
const fullFileName = srcFile;
|
||||||
const fullFilePath = path.dirname(fullFileName);
|
const fullFilePath = path.dirname(fullFileName);
|
||||||
|
|
||||||
@@ -82,9 +85,13 @@ async function compile(srcFile) {
|
|||||||
|
|
||||||
ctx.scopes = [{}];
|
ctx.scopes = [{}];
|
||||||
|
|
||||||
const mainCode = gen(ctx,ast);
|
const mainCode = genCode(ctx,ast);
|
||||||
if (ctx.error) throw(ctx.error);
|
if (ctx.error) throw(ctx.error);
|
||||||
|
|
||||||
|
if (cFile) {
|
||||||
|
await fs_writeFile(cFile, mainCode);
|
||||||
|
}
|
||||||
|
|
||||||
const def = buildCircuitDef(ctx, mainCode);
|
const def = buildCircuitDef(ctx, mainCode);
|
||||||
|
|
||||||
return def;
|
return def;
|
||||||
@@ -399,4 +406,9 @@ function buildConstraints(ctx) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function generateCCode(ctx) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,7 @@
|
|||||||
|
|
||||||
const bigInt = require("big-integer");
|
const bigInt = require("big-integer");
|
||||||
|
|
||||||
module.exports = gen;
|
module.exports = genCode;
|
||||||
|
|
||||||
function ident(text) {
|
function ident(text) {
|
||||||
let lines = text.split("\n");
|
let lines = text.split("\n");
|
||||||
@@ -170,8 +170,9 @@ function genBlock(ctx, ast) {
|
|||||||
return "{\n"+ident(body)+"}\n";
|
return "{\n"+ident(body)+"}\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function genTemplateDef(ctx, ast) {
|
function genTemplateDef(ctx, ast) {
|
||||||
let S = "function(ctx) ";
|
let S = `function ${ast.name}(ctx)\n`;
|
||||||
|
|
||||||
const newScope = {};
|
const newScope = {};
|
||||||
for (let i=0; i< ast.params.length; i++) {
|
for (let i=0; i< ast.params.length; i++) {
|
||||||
@@ -180,6 +181,7 @@ function genTemplateDef(ctx, ast) {
|
|||||||
|
|
||||||
ctx.scopes.push(newScope);
|
ctx.scopes.push(newScope);
|
||||||
S += genBlock(ctx, ast.block);
|
S += genBlock(ctx, ast.block);
|
||||||
|
S += "\n";
|
||||||
ctx.scopes.pop();
|
ctx.scopes.pop();
|
||||||
|
|
||||||
// const scope = ctx.scopes[ctx.scopes.length-1];
|
// const scope = ctx.scopes[ctx.scopes.length-1];
|
||||||
@@ -190,11 +192,11 @@ function genTemplateDef(ctx, ast) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
ctx.templates[ast.name] = S;
|
ctx.templates[ast.name] = S;
|
||||||
return "";
|
return S;
|
||||||
}
|
}
|
||||||
|
|
||||||
function genFunctionDef(ctx, ast) {
|
function genFunctionDef(ctx, ast) {
|
||||||
let S = "function(ctx) ";
|
let S = `function ${ast.name}(ctx) `;
|
||||||
|
|
||||||
const newScope = {};
|
const newScope = {};
|
||||||
const params = [];
|
const params = [];
|
||||||
@@ -216,7 +218,7 @@ function genFunctionDef(ctx, ast) {
|
|||||||
|
|
||||||
ctx.functions[ast.name] = S;
|
ctx.functions[ast.name] = S;
|
||||||
ctx.functionParams[ast.name] = params;
|
ctx.functionParams[ast.name] = params;
|
||||||
return "";
|
return S;
|
||||||
}
|
}
|
||||||
|
|
||||||
function genFor(ctx, ast) {
|
function genFor(ctx, ast) {
|
||||||
@@ -228,7 +230,7 @@ function genFor(ctx, ast) {
|
|||||||
if (ctx.error) return;
|
if (ctx.error) return;
|
||||||
const body = gen(ctx, ast.body);
|
const body = gen(ctx, ast.body);
|
||||||
if (ctx.error) return;
|
if (ctx.error) return;
|
||||||
return `for (${init};${condition};${step}) { \n${body}\n }\n`;
|
return `for (${init};${condition};${step})\n${body}\n`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function genWhile(ctx, ast) {
|
function genWhile(ctx, ast) {
|
||||||
@@ -236,7 +238,7 @@ function genWhile(ctx, ast) {
|
|||||||
if (ctx.error) return;
|
if (ctx.error) return;
|
||||||
const body = gen(ctx, ast.body);
|
const body = gen(ctx, ast.body);
|
||||||
if (ctx.error) return;
|
if (ctx.error) return;
|
||||||
return `while (${condition}) {\n${body}\n}\n`;
|
return `while (${condition}) ${body}\n`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function genIf(ctx, ast) {
|
function genIf(ctx, ast) {
|
||||||
@@ -246,7 +248,7 @@ function genIf(ctx, ast) {
|
|||||||
if (ctx.error) return;
|
if (ctx.error) return;
|
||||||
const elseBody = gen(ctx, ast.else);
|
const elseBody = gen(ctx, ast.else);
|
||||||
if (ctx.error) return;
|
if (ctx.error) return;
|
||||||
return `if (${condition}) {\n${thenBody}\n} else {\n${elseBody}\n}\n`;
|
return `if (${condition}) ${thenBody} else ${elseBody}\n`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -535,7 +537,21 @@ function genTerCon(ctx, ast) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function genInclude(ctx, ast) {
|
function genInclude(ctx, ast) {
|
||||||
return ast.block ? gen(ctx, ast.block) : "";
|
let body = genCode(ctx, ast.block);
|
||||||
|
return `// File: ${ast.file}\n` +body+"\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
function genCode(ctx, ast) {
|
||||||
|
let body = "";
|
||||||
|
for (let i=0; i<ast.statements.length; i++) {
|
||||||
|
const l = gen(ctx, ast.statements[i]);
|
||||||
|
if (ctx.error) return;
|
||||||
|
if (l) {
|
||||||
|
body += l;
|
||||||
|
if (body[body.length-1] != "\n") body += ";\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return body;
|
||||||
}
|
}
|
||||||
|
|
||||||
function genArray(ctx, ast) {
|
function genArray(ctx, ast) {
|
||||||
|
|||||||
36
wasm/AddSum.cpp
Normal file
36
wasm/AddSum.cpp
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
|
||||||
|
class Value {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class Var : Value {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class Ctx {
|
||||||
|
char *currentComponent;
|
||||||
|
|
||||||
|
Var getParam(char *);
|
||||||
|
Var getSignal(char *, ...);
|
||||||
|
|
||||||
|
Var newVar(char *);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void Num2Bits::Num2Bits(ctx) {
|
||||||
|
Var n = ctx.getParam("n");
|
||||||
|
Var in = ctx.getSignal("in");
|
||||||
|
Var out = ctx.getSignal("out");
|
||||||
|
(Var lc1 = ctx.newVar()) = _0x0;
|
||||||
|
|
||||||
|
for ((Var i = ctx.newVar()) = _0x0 ; i<n; i++ ) {
|
||||||
|
out[i] = (in >> i) & _0x1;
|
||||||
|
assert(out[i]*(out[i] - _0x1), _0x0);
|
||||||
|
lc1 += out[i] * (2**i);
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(lc1, in);
|
||||||
|
}
|
||||||
0
wasm/context.h
Normal file
0
wasm/context.h
Normal file
Reference in New Issue
Block a user