mirror of
https://github.com/arnaucube/circom.git
synced 2026-02-07 03:06:42 +01:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6b712f3587 | ||
|
|
26cad30222 | ||
|
|
f48de61ca9 | ||
|
|
89cea4755c | ||
|
|
9bf6ecc4f3 |
15
package-lock.json
generated
15
package-lock.json
generated
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "circom",
|
||||
"version": "0.5.14",
|
||||
"version": "0.5.16",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
@@ -576,9 +576,9 @@
|
||||
"dev": true
|
||||
},
|
||||
"fastfile": {
|
||||
"version": "0.0.7",
|
||||
"resolved": "https://registry.npmjs.org/fastfile/-/fastfile-0.0.7.tgz",
|
||||
"integrity": "sha512-Zk7sdqsV6DsN/rhjULDfCCowPiMDsziTMFicdkrKN80yybr/6YFf9H91ELXN85dVEf6EYkVR5EHkZNc0dMqZKA=="
|
||||
"version": "0.0.8",
|
||||
"resolved": "https://registry.npmjs.org/fastfile/-/fastfile-0.0.8.tgz",
|
||||
"integrity": "sha512-i78nuMJUoNZP5J1bPjecK07+ifHMJTBnJND/4QMUYp++DDltoAjQcIeFIWoUalt7n324fvvzWR55+WEPQUA7tQ=="
|
||||
},
|
||||
"ffiasm": {
|
||||
"version": "0.0.2",
|
||||
@@ -1167,6 +1167,13 @@
|
||||
"requires": {
|
||||
"fastfile": "0.0.7",
|
||||
"ffjavascript": "0.2.4"
|
||||
},
|
||||
"dependencies": {
|
||||
"fastfile": {
|
||||
"version": "0.0.7",
|
||||
"resolved": "https://registry.npmjs.org/fastfile/-/fastfile-0.0.7.tgz",
|
||||
"integrity": "sha512-Zk7sdqsV6DsN/rhjULDfCCowPiMDsziTMFicdkrKN80yybr/6YFf9H91ELXN85dVEf6EYkVR5EHkZNc0dMqZKA=="
|
||||
}
|
||||
}
|
||||
},
|
||||
"regexpp": {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "circom",
|
||||
"version": "0.5.14",
|
||||
"version": "0.5.16",
|
||||
"description": "Language to generate logic circuits",
|
||||
"main": "index.js",
|
||||
"directories": {
|
||||
@@ -31,7 +31,7 @@
|
||||
"dependencies": {
|
||||
"chai": "^4.2.0",
|
||||
"circom_runtime": "0.0.6",
|
||||
"fastfile": "0.0.7",
|
||||
"fastfile": "0.0.8",
|
||||
"ffiasm": "0.0.2",
|
||||
"ffjavascript": "0.2.4",
|
||||
"ffwasm": "0.0.7",
|
||||
|
||||
@@ -46,6 +46,19 @@ class _BigArray {
|
||||
if (idx >= this.length) this.length = idx+1;
|
||||
return true;
|
||||
}
|
||||
getKeys() {
|
||||
const newA = new BigArray();
|
||||
for (let i=0; i<this.arr.length; i++) {
|
||||
if (this.arr[i]) {
|
||||
for (let j=0; j<this.arr[i].length; j++) {
|
||||
if (typeof this.arr[i][j] !== "undefined") {
|
||||
newA.push(i*SUBARRAY_SIZE+j);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return newA;
|
||||
}
|
||||
}
|
||||
|
||||
class BigArray {
|
||||
|
||||
@@ -44,9 +44,12 @@ async function compile(srcFile, options) {
|
||||
ctx.mainComponent = options.mainComponent || "main";
|
||||
ctx.newThreadTemplates = options.newThreadTemplates;
|
||||
|
||||
if (ctx.verbose) console.time("Construction Phase");
|
||||
constructionPhase(ctx, srcFile);
|
||||
if (ctx.verbose) console.timeEnd("Construction Phase");
|
||||
|
||||
if (ctx.verbose) console.log("NConstraints Before: "+ctx.constraints.length);
|
||||
if (ctx.verbose) console.log("NSignals Before: "+ctx.signals.length);
|
||||
|
||||
if (ctx.error) {
|
||||
throw(ctx.error);
|
||||
@@ -57,10 +60,15 @@ async function compile(srcFile, options) {
|
||||
}
|
||||
|
||||
if (ctx.verbose) console.log("Classify Signals");
|
||||
if (ctx.verbose) console.time("Classify Signals");
|
||||
classifySignals(ctx);
|
||||
if (ctx.verbose) console.timeEnd("Classify Signals");
|
||||
|
||||
if (ctx.verbose) console.log("Reduce Constants");
|
||||
if (ctx.verbose) console.time("Reduce Constants");
|
||||
reduceConstants(ctx);
|
||||
if (ctx.verbose) console.timeEnd("Reduce Constants");
|
||||
|
||||
if (options.reduceConstraints) {
|
||||
|
||||
if (ctx.verbose) console.log("Reduce Constraints");
|
||||
@@ -73,13 +81,17 @@ async function compile(srcFile, options) {
|
||||
reduceConstrains(ctx);
|
||||
}
|
||||
*/
|
||||
if (ctx.verbose) console.time("Reduce Constraints");
|
||||
await reduceConstrains(ctx);
|
||||
if (ctx.verbose) console.timeEnd("Reduce Constraints");
|
||||
|
||||
reduceConstrains(ctx);
|
||||
}
|
||||
|
||||
if (ctx.verbose) console.log("NConstraints After: "+ctx.constraints.length);
|
||||
|
||||
if (ctx.verbose) console.time("Generate Witness Names");
|
||||
generateWitnessNames(ctx);
|
||||
if (ctx.verbose) console.timeEnd("Generate Witness Names");
|
||||
|
||||
if (ctx.error) {
|
||||
throw(ctx.error);
|
||||
@@ -87,16 +99,19 @@ async function compile(srcFile, options) {
|
||||
|
||||
if (options.cSourceWriteStream) {
|
||||
if (ctx.verbose) console.log("Generating c...");
|
||||
if (ctx.verbose) console.time("Generate C");
|
||||
ctx.builder = new BuilderC(options.prime);
|
||||
build(ctx);
|
||||
const rdStream = ctx.builder.build();
|
||||
rdStream.pipe(options.cSourceWriteStream);
|
||||
if (ctx.verbose) console.timeEnd("Generate C");
|
||||
|
||||
// await new Promise(fulfill => options.cSourceWriteStream.on("finish", fulfill));
|
||||
}
|
||||
|
||||
if ((options.wasmWriteStream)||(options.watWriteStream)) {
|
||||
if (ctx.verbose) console.log("Generating wasm...");
|
||||
if (ctx.verbose) console.time("Generating wasm");
|
||||
ctx.builder = new BuilderWasm(options.prime);
|
||||
build(ctx);
|
||||
if (options.wasmWriteStream) {
|
||||
@@ -107,6 +122,7 @@ async function compile(srcFile, options) {
|
||||
const rdStream = ctx.builder.build("wat");
|
||||
rdStream.pipe(options.watWriteStream);
|
||||
}
|
||||
if (ctx.verbose) console.timeEnd("Generate wasm");
|
||||
|
||||
// await new Promise(fulfill => options.wasmWriteStream.on("finish", fulfill));
|
||||
}
|
||||
@@ -115,12 +131,18 @@ async function compile(srcFile, options) {
|
||||
if (ctx.error) throw(ctx.error);
|
||||
|
||||
if (options.r1csFileName) {
|
||||
if (ctx.verbose) console.log("Generating r1cs...");
|
||||
if (ctx.verbose) console.time("Generating r1cs");
|
||||
await buildR1cs(ctx, options.r1csFileName);
|
||||
if (ctx.verbose) console.timeEnd("Generating r1cs");
|
||||
}
|
||||
|
||||
if (options.symWriteStream) {
|
||||
if (ctx.verbose) console.log("Generating syms...");
|
||||
if (ctx.verbose) console.time("Generating syms");
|
||||
const rdStream = buildSyms(ctx);
|
||||
rdStream.pipe(options.symWriteStream);
|
||||
if (ctx.verbose) console.timeEnd("Generating syms");
|
||||
|
||||
// await new Promise(fulfill => options.symWriteStream.on("finish", fulfill));
|
||||
}
|
||||
@@ -152,6 +174,7 @@ function classifySignals(ctx) {
|
||||
|
||||
// First classify the signals
|
||||
for (let s=0; s<ctx.signals.length; s++) {
|
||||
if ((ctx.verbose)&&(s%100000 == 0)) console.log(`classify signals: ${s}/${ctx.signals.length}`);
|
||||
const signal = ctx.signals[s];
|
||||
let tAll = ctx.stINTERNAL;
|
||||
let lSignal = signal;
|
||||
@@ -255,17 +278,19 @@ function reduceConstants(ctx) {
|
||||
ctx.constraints = newConstraints;
|
||||
}
|
||||
|
||||
function reduceConstrains(ctx) {
|
||||
const sig2constraint = {};
|
||||
let removedSignals = {};
|
||||
async function reduceConstrains(ctx) {
|
||||
const sig2constraint = new BigArray();
|
||||
let removedSignals = new BigArray();
|
||||
let nRemoved;
|
||||
let lIdx;
|
||||
|
||||
|
||||
let possibleConstraints = new Array(ctx.constraints.length);
|
||||
let possibleConstraints = new BigArray(ctx.constraints.length);
|
||||
let nextPossibleConstraints;
|
||||
for (let i=0; i<ctx.constraints.length; i++) {
|
||||
const insertedSig = {};
|
||||
if ((ctx.verbose)&&(i%100000 == 0)) console.log(`indexing constraints: ${i}/${ctx.constraints.length}`);
|
||||
|
||||
const insertedSig = { 0: true}; // Do not insert one.
|
||||
const c = ctx.constraints[i];
|
||||
for (let s in c.a.coefs) {
|
||||
if (!insertedSig[s]) {
|
||||
@@ -292,12 +317,13 @@ function reduceConstrains(ctx) {
|
||||
}
|
||||
|
||||
while (possibleConstraints.length >0) {
|
||||
nextPossibleConstraints = {};
|
||||
removedSignals = {};
|
||||
nextPossibleConstraints = new BigArray();
|
||||
removedSignals = new BigArray();
|
||||
nRemoved = 0;
|
||||
lIdx = {};
|
||||
lIdx = new BigArray();
|
||||
for (let i=0;i<possibleConstraints.length;i++) {
|
||||
if ((ctx.verbose)&&(i%10000 == 0)) {
|
||||
await Promise.resolve();
|
||||
console.log(`reducing constraints: ${i}/${possibleConstraints.length} reduced: ${nRemoved}`);
|
||||
}
|
||||
const c = ctx.constraints[possibleConstraints[i]];
|
||||
@@ -348,9 +374,13 @@ function reduceConstrains(ctx) {
|
||||
}
|
||||
}
|
||||
|
||||
nextPossibleConstraints = Object.keys(nextPossibleConstraints);
|
||||
nextPossibleConstraints = nextPossibleConstraints.getKeys();
|
||||
|
||||
for (let i=0; i<nextPossibleConstraints.length;i++) {
|
||||
if ((ctx.verbose)&&(i%10000 == 0)) {
|
||||
await Promise.resolve();
|
||||
console.log(`substituting constraints: ${i}/${nextPossibleConstraints.length}`);
|
||||
}
|
||||
const c = ctx.constraints[nextPossibleConstraints[i]];
|
||||
if (c) {
|
||||
const nc = {
|
||||
@@ -366,7 +396,11 @@ function reduceConstrains(ctx) {
|
||||
}
|
||||
}
|
||||
|
||||
for (let s in removedSignals) {
|
||||
const removedSignalsList = removedSignals.getKeys;
|
||||
|
||||
for (let i=0; i<removedSignalsList.length; i++) {
|
||||
if ((ctx.verbose )&&(i%100000 == 0)) console.log(`removing signals: ${i}/${removedSignalsList.length}`);
|
||||
const s = removedSignalsList[i];
|
||||
|
||||
let lSignal = ctx.signals[s];
|
||||
while (lSignal.e>=0) {
|
||||
@@ -377,11 +411,11 @@ function reduceConstrains(ctx) {
|
||||
}
|
||||
|
||||
possibleConstraints = nextPossibleConstraints;
|
||||
|
||||
}
|
||||
|
||||
let o=0;
|
||||
for (let i=0; i<ctx.constraints.length;i++) {
|
||||
if ((ctx.verbose)&&(i%100000 == 0)) console.log(`reordering constraints: ${i}/${ctx.constraints.length}`);
|
||||
if (ctx.constraints[i]) {
|
||||
if (!ctx.lc.isZero(ctx.constraints[i])) {
|
||||
ctx.constraints[o] = ctx.constraints[i];
|
||||
|
||||
@@ -123,6 +123,7 @@ class LCAlgebra {
|
||||
}
|
||||
|
||||
_signal2lc(a) {
|
||||
const self = this;
|
||||
if (a.t == "S") {
|
||||
const lc = {
|
||||
t: "LC",
|
||||
|
||||
@@ -4,11 +4,14 @@ const assert = require("assert");
|
||||
|
||||
module.exports.buildR1cs = buildR1cs;
|
||||
|
||||
|
||||
async function buildR1cs(ctx, fileName) {
|
||||
|
||||
const fd = await fastFile.createOverride(fileName);
|
||||
|
||||
|
||||
const buffBigInt = new Uint8Array(ctx.F.n8);
|
||||
|
||||
const type = "r1cs";
|
||||
const buff = new Uint8Array(4);
|
||||
for (let i=0; i<4; i++) buff[i] = type.charCodeAt(i);
|
||||
@@ -112,12 +115,9 @@ async function buildR1cs(ctx, fileName) {
|
||||
|
||||
async function writeBigInt(n, pos) {
|
||||
|
||||
const s = n.toString(16);
|
||||
const b = Buffer.from(s.padStart(n8*2, "0"), "hex");
|
||||
const buff = new Uint8Array(b.length);
|
||||
for (let i=0; i<b.length; i++) buff[i] = b[b.length-1-i];
|
||||
ctx.F.toRprLE(buffBigInt, 0, n);
|
||||
|
||||
await fd.write(buff, pos);
|
||||
await fd.write(buffBigInt, pos);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user