mirror of
https://github.com/arnaucube/circom.git
synced 2026-02-06 18:56:40 +01:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0d5077106a | ||
|
|
b68c9fa5d2 | ||
|
|
94cb37bd9e | ||
|
|
9f15dd2e29 |
8
package-lock.json
generated
8
package-lock.json
generated
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "circom",
|
||||
"version": "0.5.32",
|
||||
"version": "0.5.34",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
@@ -208,9 +208,9 @@
|
||||
"integrity": "sha1-V00xLt2Iu13YkS6Sht1sCu1KrII="
|
||||
},
|
||||
"circom_runtime": {
|
||||
"version": "0.1.5",
|
||||
"resolved": "https://registry.npmjs.org/circom_runtime/-/circom_runtime-0.1.5.tgz",
|
||||
"integrity": "sha512-BT3d9VCrH/rBRbThDXG731JwezKyskxyE46nACO6Tt/jaorn27LDxFDORdAAjyD0RAoBt+6FpaTp3qlYSx7Pjg==",
|
||||
"version": "0.1.9",
|
||||
"resolved": "https://registry.npmjs.org/circom_runtime/-/circom_runtime-0.1.9.tgz",
|
||||
"integrity": "sha512-eh34quaGpeEWXthnhmC9zpoBL/5zJ0mGDbPT/plb/xVmFaKxJDcLuCr2sma5s3il8AYlEIb/nbqytojyI3TWDQ==",
|
||||
"requires": {
|
||||
"ffjavascript": "0.2.10",
|
||||
"fnv-plus": "^1.3.1"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "circom",
|
||||
"version": "0.5.32",
|
||||
"version": "0.5.34",
|
||||
"description": "Language to generate logic circuits",
|
||||
"main": "index.js",
|
||||
"directories": {
|
||||
@@ -30,7 +30,7 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"chai": "^4.2.0",
|
||||
"circom_runtime": "0.1.5",
|
||||
"circom_runtime": "0.1.9",
|
||||
"fastfile": "0.0.18",
|
||||
"ffiasm": "0.1.1",
|
||||
"ffjavascript": "0.2.22",
|
||||
|
||||
@@ -59,8 +59,9 @@ class CodeBuilderC {
|
||||
this.ops.push({op: "SETSIGNAL", component, signal, value});
|
||||
}
|
||||
|
||||
getSignal(dLabel, component, signal) {
|
||||
this.ops.push({op: "GETSIGNAL", dLabel, component, signal});
|
||||
getSignal(dLabel, component, signal, n) {
|
||||
if (typeof(n) == "undefined") n=1;
|
||||
this.ops.push({op: "GETSIGNAL", dLabel, component, signal, n});
|
||||
}
|
||||
|
||||
copyN(dLabel, offset, src, n) {
|
||||
@@ -181,7 +182,7 @@ class CodeBuilderC {
|
||||
} else if (o.op == "SETSIGNAL") {
|
||||
code.push(`ctx->setSignal(__cIdx, ${ref2src(o.component)}, ${ref2src(o.signal)}, ${ref2src(o.value)});`);
|
||||
} else if (o.op == "GETSIGNAL") {
|
||||
code.push(`ctx->getSignal(__cIdx, ${ref2src(o.component)}, ${ref2src(o.signal)}, ${o.dLabel});`);
|
||||
code.push(`ctx->multiGetSignal(__cIdx, ${ref2src(o.component)}, ${ref2src(o.signal)}, ${o.dLabel}, ${o.n});`);
|
||||
} else if (o.op == "COPYN") {
|
||||
const oS = ref2src(o.offset);
|
||||
const dLabel = (oS != "0") ? (o.dLabel + "+" + oS) : o.dLabel;
|
||||
@@ -432,6 +433,7 @@ class BuilderC {
|
||||
`#define NOutputs ${this.header.NOutputs}`,
|
||||
`#define NInputs ${this.header.NInputs}`,
|
||||
`#define NVars ${this.header.NVars}`,
|
||||
`#define NPublic ${this.header.NPublic}`,
|
||||
`#define __P__ "${this.header.P.toString()}"`,
|
||||
""
|
||||
);
|
||||
@@ -661,7 +663,7 @@ class BuilderC {
|
||||
|
||||
async _buildCircuitVar(fdData) {
|
||||
|
||||
const buff = new Uint8Array(72);
|
||||
const buff = new Uint8Array(76);
|
||||
const buffV = new DataView(buff.buffer);
|
||||
|
||||
utils.setUint64(buffV, 0, this.pWit2Sig, true);
|
||||
@@ -677,6 +679,7 @@ class BuilderC {
|
||||
buffV.setUint32(60, this.header.NInputs, true);
|
||||
buffV.setUint32(64, this.header.NVars, true);
|
||||
buffV.setUint32(68, this.nCets, true);
|
||||
buffV.setUint32(72, this.header.NPublic, true);
|
||||
|
||||
fdData.pos = 0;
|
||||
|
||||
@@ -696,7 +699,7 @@ class BuilderC {
|
||||
|
||||
async build(fdCode, fdData) {
|
||||
const encoder = new TextEncoder("utf-8");
|
||||
fdData.pos = 72;
|
||||
fdData.pos = 76;
|
||||
while (fdData.pos % 8) fdData.pos++;
|
||||
|
||||
const code=new BigArray();
|
||||
|
||||
@@ -412,6 +412,42 @@ module.exports = function buildRuntime(module, builder) {
|
||||
|
||||
}
|
||||
|
||||
function buildMultiGetSignal() {
|
||||
const f = module.addFunction("multiGetSignal");
|
||||
f.addParam("cIdx", "i32");
|
||||
f.addParam("pR", "i32");
|
||||
f.addParam("component", "i32");
|
||||
f.addParam("signal", "i32");
|
||||
f.addParam("n", "i32");
|
||||
f.addLocal("i", "i32");
|
||||
|
||||
const c = f.getCodeBuilder();
|
||||
|
||||
f.addCode(
|
||||
c.setLocal("i", c.i32_const(0)),
|
||||
c.block(c.loop(
|
||||
c.br_if(1, c.i32_eq(c.getLocal("i"), c.getLocal("n"))),
|
||||
c.call(
|
||||
"getSignal",
|
||||
c.getLocal("cIdx"),
|
||||
c.i32_add(
|
||||
c.getLocal("pR"),
|
||||
c.i32_mul(
|
||||
c.getLocal("i"),
|
||||
c.i32_const(builder.sizeFr)
|
||||
)
|
||||
),
|
||||
c.getLocal("component"),
|
||||
c.i32_add(
|
||||
c.getLocal("signal"),
|
||||
c.getLocal("i")
|
||||
)
|
||||
),
|
||||
c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))),
|
||||
c.br(0)
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
function buildSetSignal() {
|
||||
const f = module.addFunction("setSignal");
|
||||
@@ -657,6 +693,15 @@ module.exports = function buildRuntime(module, builder) {
|
||||
f.addCode(c.i32_const(builder.header.NVars));
|
||||
}
|
||||
|
||||
function buildGetNPublic() {
|
||||
const f = module.addFunction("getNPublic");
|
||||
f.setReturnType("i32");
|
||||
|
||||
const c = f.getCodeBuilder();
|
||||
|
||||
f.addCode(c.i32_const(builder.header.NPublic));
|
||||
}
|
||||
|
||||
function buildGetFrLen() {
|
||||
const f = module.addFunction("getFrLen");
|
||||
f.setReturnType("i32");
|
||||
@@ -847,6 +892,7 @@ module.exports = function buildRuntime(module, builder) {
|
||||
|
||||
buildGetSignal();
|
||||
buildSetSignal();
|
||||
buildMultiGetSignal();
|
||||
|
||||
buildComponentStarted();
|
||||
buildComponentFinished();
|
||||
@@ -855,6 +901,7 @@ module.exports = function buildRuntime(module, builder) {
|
||||
buildCheckAssert();
|
||||
|
||||
buildGetNVars();
|
||||
buildGetNPublic();
|
||||
buildGetFrLen();
|
||||
buildGetPWitness();
|
||||
buildGetPRawPrime();
|
||||
@@ -864,9 +911,11 @@ module.exports = function buildRuntime(module, builder) {
|
||||
|
||||
module.exportFunction("init");
|
||||
module.exportFunction("getNVars");
|
||||
module.exportFunction("getNPublic");
|
||||
module.exportFunction("getFrLen");
|
||||
module.exportFunction("getSignalOffset32");
|
||||
module.exportFunction("setSignal");
|
||||
module.exportFunction("multiGetSignal");
|
||||
module.exportFunction("getPWitness");
|
||||
module.exportFunction("Fr_toInt");
|
||||
module.exportFunction("getPRawPrime");
|
||||
|
||||
@@ -62,8 +62,9 @@ class CodeBuilderWasm {
|
||||
this.ops.push({op: "SETSIGNAL", component, signal, value});
|
||||
}
|
||||
|
||||
getSignal(dLabel, component, signal) {
|
||||
this.ops.push({op: "GETSIGNAL", dLabel, component, signal});
|
||||
getSignal(dLabel, component, signal, n) {
|
||||
if (typeof n == "undefined") n=1;
|
||||
this.ops.push({op: "GETSIGNAL", dLabel, component, signal, n});
|
||||
}
|
||||
|
||||
copyN(dLabel, offset, src, n) {
|
||||
@@ -251,11 +252,12 @@ class CodeBuilderWasm {
|
||||
} else if (o.op == "GETSIGNAL") {
|
||||
code.push(
|
||||
c.call(
|
||||
"getSignal",
|
||||
"multiGetSignal",
|
||||
c.getLocal("cIdx"),
|
||||
this.fnBuilder._getPtr(c, o.dLabel),
|
||||
this.fnBuilder._deRefInt(c, o.component),
|
||||
this.fnBuilder._deRefInt(c, o.signal)
|
||||
this.fnBuilder._deRefInt(c, o.signal),
|
||||
c.i32_const(o.n)
|
||||
)
|
||||
);
|
||||
} else if (o.op == "COPYN") {
|
||||
@@ -779,20 +781,21 @@ class BuilderWasm {
|
||||
|
||||
_buildHeader(module) {
|
||||
|
||||
this.pCircuit = module.alloc(48);
|
||||
this.pCircuit = module.alloc(52);
|
||||
|
||||
this.pNSignals = this.pCircuit;
|
||||
this.pNComponents = this.pCircuit + 4;
|
||||
this.pNInputs = this.pCircuit + 8;
|
||||
this.pNOutputs = this.pCircuit + 12;
|
||||
this.pNVars = this.pCircuit + 16;
|
||||
this.ppWit2sig = this.pCircuit + 20;
|
||||
this.ppComponents = this.pCircuit + 24;
|
||||
this.ppMapIsInput = this.pCircuit + 28;
|
||||
this.ppConstants = this.pCircuit + 32;
|
||||
this.ppSignals = this.pCircuit + 36;
|
||||
this.ppInputSignalsToTrigger = this.pCircuit + 40;
|
||||
this.ppSignalsAssigned = this.pCircuit + 44;
|
||||
this.pNPublic = this.pCircuit + 20;
|
||||
this.ppWit2sig = this.pCircuit + 24;
|
||||
this.ppComponents = this.pCircuit + 28;
|
||||
this.ppMapIsInput = this.pCircuit + 32;
|
||||
this.ppConstants = this.pCircuit + 36;
|
||||
this.ppSignals = this.pCircuit + 40;
|
||||
this.ppInputSignalsToTrigger = this.pCircuit + 44;
|
||||
this.ppSignalsAssigned = this.pCircuit + 48;
|
||||
}
|
||||
|
||||
_buildSizes(module) {
|
||||
@@ -983,6 +986,7 @@ class BuilderWasm {
|
||||
module.addData(this.pNInputs, intToBytes32(this.header.NInputs));
|
||||
module.addData(this.pNOutputs, intToBytes32(this.header.NOutputs));
|
||||
module.addData(this.pNVars, intToBytes32(this.header.NVars));
|
||||
module.addData(this.pNPublic, intToBytes32(this.header.NPublic));
|
||||
module.addData(this.ppWit2sig, intToBytes32(this.pWit2sig));
|
||||
module.addData(this.ppComponents, intToBytes32(this.pComponents));
|
||||
module.addData(this.ppMapIsInput, intToBytes32(this.pMapIsInput));
|
||||
|
||||
@@ -196,6 +196,7 @@ function buildHeader(ctx) {
|
||||
NInputs: ctx.components[ ctx.getComponentIdx("main") ].nInSignals,
|
||||
NOutputs: ctx.totals[ ctx.stOUTPUT ],
|
||||
NVars: ctx.totals[ctx.stONE] + ctx.totals[ctx.stOUTPUT] + ctx.totals[ctx.stPUBINPUT] + ctx.totals[ctx.stPRVINPUT] + ctx.totals[ctx.stINTERNAL],
|
||||
NPublic: ctx.totals[ctx.stOUTPUT] + ctx.totals[ctx.stPUBINPUT],
|
||||
P: ctx.F.p
|
||||
});
|
||||
}
|
||||
|
||||
@@ -888,7 +888,8 @@ function execOpOp(ctx, ast, op, lr) {
|
||||
right = {t:"N", v: ctx.F.one};
|
||||
}
|
||||
|
||||
if (!right) return ctx.throwError(ast, "adding a no number");
|
||||
if (!right) return ctx.throwError(ast, "right operand is not initialized");
|
||||
if (!resBefore) return ctx.throwError(ast, "left operand is not initialized");
|
||||
|
||||
const resAfter = ctx.lc[op](resBefore, right);
|
||||
left.v[o] = resAfter;
|
||||
|
||||
@@ -284,6 +284,32 @@ function genDeclareComponent(ctx, ast) {
|
||||
}
|
||||
|
||||
function genDeclareSignal(ctx, ast) {
|
||||
|
||||
const v = ctx.refs[ast.refId];
|
||||
|
||||
let sizes;
|
||||
if (ast.name.selectors.length>0) {
|
||||
sizes=[];
|
||||
for (let i=0; i< ast.name.selectors.length; i++) {
|
||||
const sizeRef = gen(ctx, ast.name.selectors[i]);
|
||||
const size = ctx.refs[sizeRef];
|
||||
if (size.sizes[0] != 1) return ctx.throwError(ast, "A selector cannot be an array");
|
||||
if (size.used) return ctx.throwError(ast, "Signal size variables not allowed");
|
||||
sizes.push(Scalar.toNumber(size.value[0]));
|
||||
}
|
||||
sizes = utils.accSizes(sizes);
|
||||
} else {
|
||||
sizes = [1,0];
|
||||
}
|
||||
|
||||
if ((!v.sizes)&&(sizes)) {
|
||||
v.sizes = sizes;
|
||||
}
|
||||
|
||||
if (v.sizes) {
|
||||
if (!utils.sameSizes(v.sizes, sizes)) return ctx.throwError(ast, "Redefined a signal with different sized");
|
||||
}
|
||||
|
||||
return ast.refId;
|
||||
}
|
||||
|
||||
@@ -414,12 +440,13 @@ function genVariable(ctx, ast) {
|
||||
vOffset = genGetSignalOffset(ctx, -1, ast.name);
|
||||
}
|
||||
|
||||
const resRef = newRef(ctx, "BIGINT", "_sigValue");
|
||||
const sizes = v.sizes.slice(l);
|
||||
|
||||
const resRef = newRef(ctx, "BIGINT", "_sigValue", null, sizes);
|
||||
const res = ctx.refs[resRef];
|
||||
instantiateRef(ctx, resRef);
|
||||
ctx.codeBuilder.getSignal(res.label, ["CC"], toRefA_Int1(ctx, ast, vOffset));
|
||||
ctx.codeBuilder.getSignal(res.label, ["CC"], toRefA_Int1(ctx, ast, vOffset), sizes[0]);
|
||||
return resRef;
|
||||
|
||||
} else if (v.type == "BIGINT") {
|
||||
const refOffset = genGetOffset(ctx, 0, v.sizes, ast.selectors );
|
||||
const offset = ctx.refs[refOffset];
|
||||
@@ -486,13 +513,18 @@ function genPin(ctx, ast) {
|
||||
vsIdx = genGetSignalOffset(ctx, vcIdx, ast.pin.name);
|
||||
}
|
||||
|
||||
const resRef = newRef(ctx, "BIGINT", "_sigValue");
|
||||
const l = ast.selectors ? ast.selectors.length : 0;
|
||||
|
||||
const sizes = [1,0]; // TODO, Treat array
|
||||
|
||||
const resRef = newRef(ctx, "BIGINT", "_sigValue", null, sizes);
|
||||
const res = ctx.refs[resRef];
|
||||
instantiateRef(ctx, resRef);
|
||||
ctx.codeBuilder.getSignal(
|
||||
res.label,
|
||||
toRefA_Int1(ctx, ast.component, vcIdx),
|
||||
toRefA_Int1(ctx, ast.pin, vsIdx)
|
||||
toRefA_Int1(ctx, ast.pin, vsIdx),
|
||||
sizes[0]
|
||||
);
|
||||
return resRef;
|
||||
}
|
||||
@@ -951,7 +983,7 @@ function genLoop(ctx, ast) {
|
||||
if (ctx.error) return;
|
||||
|
||||
const cond = ctx.refs[condRef];
|
||||
if (!utils.sameSizes(cond.sizes, [1,0])) return ctx.throwError(ast.condition, "Operation cannot be done on an array");
|
||||
if (!utils.sameSizes(cond.sizes, [1,0])) return ctx.throwError(ast.condition, "genLoop: Operation cannot be done on an array");
|
||||
|
||||
if (cond.used) {
|
||||
inLoop = true;
|
||||
@@ -1028,7 +1060,7 @@ function genIf(ctx, ast) {
|
||||
const condRef = gen(ctx, ast.condition);
|
||||
if (ctx.error) return;
|
||||
const cond = ctx.refs[condRef];
|
||||
if (!utils.sameSizes(cond.sizes, [1,0])) return ctx.throwError(ast.condition, "Operation cannot be done on an array");
|
||||
if (!utils.sameSizes(cond.sizes, [1,0])) return ctx.throwError(ast.condition, "genIf: Operation cannot be done on an array");
|
||||
|
||||
if (cond.used) {
|
||||
let thenCode, elseCode;
|
||||
@@ -1148,7 +1180,12 @@ function genOp(ctx, ast, op, nOps, adjustBool) {
|
||||
valRefs.push(ref);
|
||||
vals.push(v);
|
||||
|
||||
if (!utils.sameSizes(v.sizes, [1,0])) return ctx.throwError(ast, "Operation cannot be done on an array");
|
||||
if (!utils.sameSizes(v.sizes, [1,0])) {
|
||||
console.log("xx");
|
||||
console.log(i);
|
||||
console.log(v);
|
||||
return ctx.throwError(ast, "genOp2: Operation cannot be done on an array");
|
||||
}
|
||||
if ( (!v.used)
|
||||
&&( (!utils.isDefined(v.value))
|
||||
||(!utils.isDefined(v.value[0]))))
|
||||
@@ -1184,7 +1221,7 @@ function genTerCon(ctx, ast) {
|
||||
const condRef = gen(ctx, ast.values[0]);
|
||||
if (ctx.error) return;
|
||||
const cond = ctx.refs[condRef];
|
||||
if (!utils.sameSizes(cond.sizes, [1,0])) return ctx.throwError(ast.condition, "Operation cannot be done on an array");
|
||||
if (!utils.sameSizes(cond.sizes, [1,0])) return ctx.throwError(ast.condition, "genTer: Operation cannot be done on an array");
|
||||
|
||||
if (cond.used) {
|
||||
let thenCode, elseCode;
|
||||
|
||||
@@ -46,7 +46,7 @@ async function doTest(tester, circuit, testVectors) {
|
||||
describe("basic cases", function () {
|
||||
this.timeout(100000);
|
||||
|
||||
for (let i=0; i<basicCases.length; i++) {
|
||||
for (let i=0; i< basicCases.length; i++) {
|
||||
it("c/c++ " + basicCases[i].name, async () => {
|
||||
await doTest(c_tester, basicCases[i].circuit, basicCases[i].tv);
|
||||
});
|
||||
|
||||
@@ -14,6 +14,17 @@
|
||||
}]
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "fnarray",
|
||||
"circuit": "fnarray.circom",
|
||||
"tv": [
|
||||
[{
|
||||
"in": [1, 8, 25]
|
||||
}, {
|
||||
"out": [2, 16, 50]
|
||||
}]
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "add",
|
||||
"circuit": "add.circom",
|
||||
|
||||
21
test/circuits/fnarray.circom
Normal file
21
test/circuits/fnarray.circom
Normal file
@@ -0,0 +1,21 @@
|
||||
|
||||
|
||||
|
||||
|
||||
function calc(h) {
|
||||
var res[3];
|
||||
for (var i=0; i<3; i++) res[i] = h[i]*2;
|
||||
return res;
|
||||
}
|
||||
|
||||
template Test() {
|
||||
signal input in[3];
|
||||
signal output out[3];
|
||||
|
||||
var cout[3] = calc(in);
|
||||
for (var i=0; i<3; i++) out[i] <-- cout[i];
|
||||
|
||||
for (var i=0; i<3; i++) out[i] === in[i]*2;
|
||||
}
|
||||
|
||||
component main = Test();
|
||||
Reference in New Issue
Block a user