arnaucube
arnaucube/circomPublic Self-hosted repository
circom/src/compiler.js
src/compiler.jsRaw
  1/*
  2    Copyright 2018 0KIMS association.
  3
  4    This file is part of circom (Zero Knowledge Circuit Compiler).
  5
  6    circom is a free software: you can redistribute it and/or modify it
  7    under the terms of the GNU General Public License as published by
  8    the Free Software Foundation, either version 3 of the License, or
  9    (at your option) any later version.
 10
 11    circom is distributed in the hope that it will be useful, but WITHOUT
 12    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 13    or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
 14    License for more details.
 15
 16    You should have received a copy of the GNU General Public License
 17    along with circom. If not, see <https://www.gnu.org/licenses/>.
 18*/
 19
 20const Scalar = require("ffjavascript").Scalar;
 21const sONE = 0;
 22const build = require("./build");
 23const BuilderC = require("../ports/c/builder.js");
 24const BuilderWasm = require("../ports/wasm/builder.js");
 25const constructionPhase = require("./construction_phase");
 26const Ctx = require("./ctx");
 27const utils = require("./utils");
 28const buildR1cs = require("./r1csfile").buildR1cs;
 29const BigArray = require("./bigarray");
 30const buildSyms = require("./buildsyms");
 31const {performance} = require("perf_hooks");
 32
 33module.exports = compile;
 34const measures = {};
 35
 36function ms2String(v) {
 37    v = Math.floor(v);
 38    const ms = v % 1000;
 39    v = Math.floor(v/1000);
 40    const secs = v % 60;
 41    v = Math.floor(v/60);
 42    const mins = v % 60;
 43    v = Math.floor(v/60);
 44    const hours = v % 24;
 45    const days = Math.floor(v/24);
 46    let S = "";
 47    if (days) S = S + days + "D ";
 48    if ((S!="")||(hours)) S = S + hours.toString().padStart(2, "0") + ":";
 49    if ((S!="")||(mins)) S = S + mins.toString().padStart(2, "0") + ":";
 50    if ((S!="")||(secs)) S = S + secs.toString().padStart(2, "0");
 51    S+=".";
 52    S = S + ms.toString().padStart(3, "0");
 53    return S;
 54}
 55
 56async function compile(srcFile, options) {
 57    options.prime = options.prime || Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
 58    if (!options) {
 59        options = {};
 60    }
 61    if (typeof options.reduceConstraints === "undefined") {
 62        options.reduceConstraints = true;
 63    }
 64    const ctx = new Ctx(options.prime);
 65    ctx.verbose= options.verbose || false;
 66    ctx.mainComponent = options.mainComponent || "main";
 67    ctx.newThreadTemplates = options.newThreadTemplates;
 68
 69    measures.constructionPhase = -performance.now();
 70    constructionPhase(ctx, srcFile);
 71    measures.constructionPhase += performance.now();
 72
 73    if (ctx.verbose) console.log("NConstraints Before: "+ctx.constraints.length);
 74    if (ctx.verbose) console.log("NSignals Before: "+ctx.signals.length);
 75
 76    if (ctx.error) {
 77        throw(ctx.error);
 78    }
 79
 80    if (ctx.getComponentIdx(ctx.mainComponent)<0) {
 81        throw new Error("A main component must be defined");
 82    }
 83
 84    if (ctx.verbose) console.log("Classify Signals");
 85    measures.classifySignals = -performance.now();
 86    classifySignals(ctx);
 87    measures.classifySignals += performance.now();
 88
 89    if (ctx.verbose) console.log("Reduce Constants");
 90    measures.reduceConstants = -performance.now();
 91    reduceConstants(ctx);
 92    measures.reduceConstants += performance.now();
 93
 94    if (options.reduceConstraints) {
 95
 96        if (ctx.verbose) console.log("Reduce Constraints");
 97        // Repeat while reductions are performed
 98/*
 99        let oldNConstrains = -1;
100        while (ctx.constraints.length != oldNConstrains) {
101            if (ctx.verbose) console.log("Reducing constraints: "+ctx.constraints.length);
102            oldNConstrains = ctx.constraints.length;
103            reduceConstrains(ctx);
104        }
105*/
106        measures.reduceConstraints = -performance.now();
107        await reduceConstrains(ctx);
108        measures.reduceConstraints += performance.now();
109
110    }
111
112    if (ctx.verbose) console.log("NConstraints After: "+ctx.constraints.length);
113
114    measures.generateWitnessNames = -performance.now();
115    generateWitnessNames(ctx);
116    measures.generateWitnessNames += performance.now();
117
118    if (ctx.error) {
119        throw(ctx.error);
120    }
121
122    if (options.r1csFileName) {
123        measures.generateR1cs = -performance.now();
124        await buildR1cs(ctx, options.r1csFileName);
125        measures.generateR1cs += performance.now();
126    }
127
128    if (ctx.error) throw(ctx.error);
129
130    delete ctx.constraints;  // Liberate memory.
131
132    if (options.cSourceFile) {
133        if (ctx.verbose) console.log("Generating c...");
134        measures.generateC = -performance.now();
135        ctx.builder = new BuilderC(options.prime, ctx.verbose);
136        build(ctx);
137        await ctx.builder.build(options.cSourceFile, options.dataFile);
138        measures.generateC += performance.now();
139    }
140
141    if (ctx.error) throw(ctx.error);
142
143    if ((options.wasmFile)||(options.watFile)) {
144        if (ctx.verbose) console.log("Generating wasm...");
145        measures.generateWasm = -performance.now();
146        ctx.builder = new BuilderWasm(options.prime);
147        build(ctx);
148        if (options.wasmFile) {
149            await ctx.builder.build(options.wasmFile, "wasm");
150        }
151        if (options.watFile) {
152            await ctx.builder.build(options.watFile, "wat");
153        }
154        measures.generateWasm += performance.now();
155    }
156
157    // const mainCode = gen(ctx,ast);
158    if (ctx.error) throw(ctx.error);
159
160    if (options.symWriteStream) {
161        measures.generateSyms = -performance.now();
162        const rdStream = buildSyms(ctx);
163        rdStream.pipe(options.symWriteStream);
164        measures.generateSyms += performance.now();
165
166        await new Promise(fulfill => options.symWriteStream.on("finish", fulfill));
167    }
168
169//    const def = buildCircuitDef(ctx, mainCode);
170
171    if (ctx.verbose) {
172        for (let [mStr, mValue] of Object.entries(measures)) {
173            console.log(mStr + ": " + ms2String(mValue));
174        }
175    }
176}
177
178
179
180function classifySignals(ctx) {
181
182    const ERROR = 0xFFFF;
183
184    function priorize(t1, t2) {
185        if ((t1 == ERROR) || (t2==ERROR)) return ERROR;
186        if (t1 == ctx.stINTERNAL) {
187            return t2;
188        } else if (t2==ctx.stINTERNAL) {
189            return t1;
190        }
191        if ((t1 == ctx.stONE) || (t2 == ctx.stONE)) return ctx.stONE;
192        if ((t1 == ctx.stOUTPUT) || (t2 == ctx.stOUTPUT)) return ctx.stOUTPUT;
193        if ((t1 == ctx.stCONSTANT) || (t2 == ctx.stCONSTANT)) return ctx.stCONSTANT;
194        if ((t1 == ctx.stDISCARDED) || (t2 == ctx.stDISCARDED)) return ctx.stDISCARDED;
195        if (t1!=t2) return ERROR;
196        return t1;
197    }
198
199    // First classify the signals
200    for (let s=0; s<ctx.signals.length; s++) {
201        if ((ctx.verbose)&&(s%100000 == 0)) console.log(`classify signals: ${s}/${ctx.signals.length}`);
202        const signal = ctx.signals[s];
203        let tAll = ctx.stINTERNAL;
204        let lSignal = signal;
205        let end = false;
206        while (!end) {
207            let t = lSignal.c || ctx.stINTERNAL;
208            if (s == 0) {
209                t = ctx.stONE;
210            } else if (lSignal.o & ctx.MAIN) {
211                if (lSignal.o & ctx.IN) {
212                    if (lSignal.o & ctx.PRV) {
213                        t = ctx.stPRVINPUT;
214                    } else {
215                        t = ctx.stPUBINPUT;
216                    }
217                } else if (lSignal.o & ctx.OUT) {
218                    t = ctx.stOUTPUT;
219                }
220            } else if (utils.isDefined(lSignal.v)) {
221                t = ctx.stCONSTANT;
222            }
223            tAll = priorize(t,tAll);
224            if (lSignal.e>=0) {
225                lSignal = ctx.signals[lSignal.e];
226            } else {
227                end=true;
228            }
229        }
230        if (tAll == ERROR) {
231            throw new Error("Incompatible types in signal: " + s);
232        }
233        lSignal.c = tAll;
234    }
235}
236
237
238function generateWitnessNames(ctx) {
239    const totals = {};
240    totals[ctx.stONE] = 0;
241    totals[ctx.stOUTPUT] = 0;
242    totals[ctx.stPUBINPUT] = 0;
243    totals[ctx.stPRVINPUT] = 0;
244    totals[ctx.stINTERNAL] = 0;
245    totals[ctx.stDISCARDED] = 0;
246    totals[ctx.stCONSTANT] = 0;
247    const ids = {};
248
249    // First classify the signals
250    for (let s=0; s<ctx.signals.length; s++) {
251
252        if ((ctx.verbose)&&(s%10000 == 0)) console.log("generate witness (counting): ", s);
253
254        const signal = ctx.signals[s];
255        let lSignal = signal;
256        while (lSignal.e>=0) lSignal = ctx.signals[lSignal.e];
257
258        if (!( lSignal.o & ctx.COUNTED) ) {
259            lSignal.o |= ctx.COUNTED;
260            totals[lSignal.c] ++;
261        }
262    }
263
264    ids[ctx.stONE] = 0;
265    ids[ctx.stOUTPUT] = 1;
266    ids[ctx.stPUBINPUT] = ids[ctx.stOUTPUT] + totals[ctx.stOUTPUT];
267    ids[ctx.stPRVINPUT] = ids[ctx.stPUBINPUT] + totals[ctx.stPUBINPUT];
268    ids[ctx.stINTERNAL] = ids[ctx.stPRVINPUT] + totals[ctx.stPRVINPUT];
269    ids[ctx.stDISCARDED] = ids[ctx.stINTERNAL] + totals[ctx.stINTERNAL];
270    ids[ctx.stCONSTANT] = ids[ctx.stDISCARDED] + totals[ctx.stDISCARDED];
271    const nSignals = ids[ctx.stCONSTANT] + totals[ctx.stCONSTANT];
272
273    for (let s=0; s<ctx.signals.length; s++) {
274
275        if ((ctx.verbose)&&(s%10000 == 0)) console.log("seting id: ", s);
276
277        const signal = ctx.signals[s];
278        let lSignal = signal;
279        while (lSignal.e>=0) {
280            lSignal = ctx.signals[lSignal.e];
281        }
282        if ( typeof(lSignal.id) === "undefined" ) {
283            lSignal.id = ids[lSignal.c] ++;
284        }
285
286        signal.id = lSignal.id;
287    }
288
289    ctx.totals = totals;
290}
291
292function reduceConstants(ctx) {
293    const newConstraints = new BigArray();
294    for (let i=0; i<ctx.constraints.length; i++) {
295        if ((ctx.verbose)&&(i%10000 == 0)) console.log("reducing constants: ", i);
296        const c = ctx.lc.canonize(ctx, ctx.constraints[i]);
297        if (!ctx.lc.isZero(c)) {
298            newConstraints.push(c);
299        }
300        delete ctx.constraints[i];
301    }
302    ctx.constraints = newConstraints;
303}
304
305async function reduceConstrains(ctx) {
306    const sig2constraint = new BigArray();
307    let removedSignals = new BigArray();
308    let nRemoved;
309    let lIdx;
310
311
312    let possibleConstraints = new BigArray(ctx.constraints.length);
313    let nextPossibleConstraints;
314    for (let i=0; i<ctx.constraints.length; i++) {
315        if ((ctx.verbose)&&(i%100000 == 0)) console.log(`indexing constraints: ${i}/${ctx.constraints.length}`);
316
317        const insertedSig = { 0: true};  // Do not insert one.
318        const c = ctx.constraints[i];
319        for (let s in c.a.coefs) {
320            if (!insertedSig[s]) {
321                if (!sig2constraint[s]) sig2constraint[s] = {};
322                sig2constraint[s][i] = true;
323                insertedSig[s] = true;
324            }
325        }
326        for (let s in c.b.coefs) {
327            if (!insertedSig[s]) {
328                if (!sig2constraint[s]) sig2constraint[s] = {};
329                sig2constraint[s][i] = true;
330                insertedSig[s] = true;
331            }
332        }
333        for (let s in c.c.coefs) {
334            if (!insertedSig[s]) {
335                if (!sig2constraint[s]) sig2constraint[s] = {};
336                sig2constraint[s][i] = true;
337                insertedSig[s] = true;
338            }
339        }
340        possibleConstraints[i] = ctx.constraints.length - i -1;
341    }
342
343    let totalRemoved = 0;
344    while (possibleConstraints.length >0) {
345        if (possibleConstraints.length>1<<20) {
346            nextPossibleConstraints = new BigArray();
347        } else {
348            nextPossibleConstraints = {};
349        }
350        removedSignals = {};
351        nRemoved = 0;
352        lIdx = {};
353        for (let i=0;i<possibleConstraints.length;i++) {
354            if ((ctx.verbose)&&(i%10000 == 0)) {
355                await Promise.resolve();
356                console.log(`reducing constraints: ${i}/${possibleConstraints.length}   reduced: ${nRemoved}`);
357            }
358
359            const c = ctx.constraints[possibleConstraints[i]];
360            if (!c) continue;
361
362            // Limit of number of lelements removed per step
363            if (nRemoved>5000000) {
364                nextPossibleConstraints[possibleConstraints[i]] = true;
365                continue;
366            }
367
368            // Swap a and b if b has more variables.
369            if (Object.keys(c.b).length > Object.keys(c.a).length) {
370                const aux = c.a;
371                c.a=c.b;
372                c.b=aux;
373            }
374
375            // Mov to C if possible.
376            if (isConstant(c.a)) {
377                const ct = {t: "N", v: c.a.coefs[sONE]};
378                c.c = ctx.lc.add(ctx.lc.mul(c.b, ct), c.c);
379                c.a = { t: "LC", coefs: {} };
380                c.b = { t: "LC", coefs: {} };
381            }
382            if (isConstant(c.b)) {
383                const ct = {t: "N", v: c.b.coefs[sONE]};
384                c.c = ctx.lc.add(ctx.lc.mul(c.a, ct), c.c);
385                c.a = { t: "LC", coefs: {} };
386                c.b = { t: "LC", coefs: {} };
387            }
388
389            if (ctx.lc.isZero(c.a) || ctx.lc.isZero(c.b)) {
390                const freeC = substituteRemoved(c.c);
391                const isolatedSignal = getFirstInternalSignal(ctx, freeC);
392                if (isolatedSignal) {
393                    removedSignals[isolatedSignal] = isolateSignal(freeC, isolatedSignal);
394                    if (lIdx[isolatedSignal]) {
395                        const sigs = Object.keys(lIdx[isolatedSignal]);
396
397                        for (let k=0; k<sigs.length; k++) {
398                            const s = sigs[k];
399                            const oldLC = removedSignals[s];
400                            removedSignals[s] = substitute(removedSignals[s], isolatedSignal, removedSignals[isolatedSignal]);
401                            if (oldLC !== removedSignals[s]) addTolIdx(removedSignals[s], s);
402                        }
403                    }
404
405                    addTolIdx(removedSignals[isolatedSignal], isolatedSignal);
406                    ctx.constraints[possibleConstraints[i]] = null;
407                    nRemoved ++;
408
409                    delete lIdx[isolatedSignal];
410
411                    const cts = Object.keys(sig2constraint[isolatedSignal]);
412                    for (let k=0; k<cts.length; k++) {
413                        if (ctx.constraints[cts[k]]) nextPossibleConstraints[cts[k]] = true;
414                    }
415                }
416            }
417        }
418
419        if (nextPossibleConstraints.getKeys) {
420            nextPossibleConstraints = nextPossibleConstraints.getKeys();
421        } else {
422            nextPossibleConstraints = Object.keys(nextPossibleConstraints);
423        }
424
425        for (let i=0; i<nextPossibleConstraints.length;i++) {
426            if ((ctx.verbose)&&(i%10000 == 0)) {
427                await Promise.resolve();
428                console.log(`substituting constraints: ${i}/${nextPossibleConstraints.length}`);
429            }
430            const c = ctx.constraints[nextPossibleConstraints[i]];
431            if (c) {
432                const nc = {
433                    a: substituteRemoved(c.a, nextPossibleConstraints[i]),
434                    b: substituteRemoved(c.b, nextPossibleConstraints[i]),
435                    c: substituteRemoved(c.c, nextPossibleConstraints[i])
436                };
437                if (ctx.lc.isZero(nc)) {
438                    delete ctx.constraints[nextPossibleConstraints[i]];
439                } else {
440                    ctx.constraints[nextPossibleConstraints[i]] = nc;
441                }
442            }
443        }
444
445        const removedSignalsList = Object.keys(removedSignals);
446
447        for (let i=0; i<removedSignalsList.length; i++) {
448            if ((ctx.verbose    )&&(i%100000 == 0)) console.log(`removing signals: ${i}/${removedSignalsList.length}`);
449            const s = removedSignalsList[i];
450
451            let lSignal = ctx.signals[s];
452            while (lSignal.e>=0) {
453                lSignal = ctx.signals[lSignal.e];
454            }
455
456            sig2constraint[s] = null;
457            lSignal.c = ctx.stDISCARDED;
458        }
459
460/*
461        possibleConstraints = new BigArray();
462        // Reverse
463        for (let i=0; i<nextPossibleConstraints.length; i++) {
464            possibleConstraints[i] = nextPossibleConstraints[nextPossibleConstraints.length -1 -i];
465        }
466*/
467        possibleConstraints = nextPossibleConstraints;
468
469        totalRemoved += nRemoved;
470        if (ctx.verbose) console.log(`Removed: ${totalRemoved} TotalConstraints: ${ctx.constraints.length}` );
471    }
472
473
474    let o=0;
475    for (let i=0; i<ctx.constraints.length;i++) {
476        if ((ctx.verbose)&&(i%100000 == 0)) console.log(`reordering constraints: ${i}/${ctx.constraints.length}`);
477        if (ctx.constraints[i]) {
478            if (!ctx.lc.isZero(ctx.constraints[i])) {
479                ctx.constraints[o] = ctx.constraints[i];
480                o++;
481            }
482        }
483    }
484
485    ctx.constraints.length = o;
486
487    function getFirstInternalSignal(ctx, l) {
488        for (let k in l.coefs) {
489            k = Number(k);
490            const signal = ctx.signals[k];
491            if ((signal.c == ctx.stINTERNAL)&&(!ctx.F.isZero(l.coefs[k])) &&(!removedSignals[k])) return k;
492        }
493        return null;
494    }
495
496    function isolateSignal(lc, s) {
497        const eq = {
498            t: "LC",
499            coefs: {}
500        };
501        const invCoef = ctx.F.inv(lc.coefs[s]);
502        for (const k in lc.coefs) {
503            if (k != s) {
504                const v = ctx.F.mul( ctx.F.neg(lc.coefs[k]), invCoef);
505                if (!ctx.F.isZero(v)) {
506                    eq.coefs[k] = v;
507                }
508            }
509        }
510        return eq;
511    }
512
513    function substituteRemoved(lc, idxConstraint) {
514        const newLc = ctx.lc._clone(lc);
515        for (let k in lc.coefs) {
516            if (removedSignals[k]) {
517                delete newLc.coefs[k];
518                for (let k2 in removedSignals[k].coefs) {
519                    const newP = ctx.F.mul(removedSignals[k].coefs[k2], lc.coefs[k]);
520                    if (!ctx.F.isZero(newP)) {
521                        if (newLc.coefs[k2]) {
522                            newLc.coefs[k2] = ctx.F.add(newLc.coefs[k2], newP);
523                            if (ctx.F.isZero(newLc.coefs[k2])) delete newLc.coefs[k2];
524                        } else {
525                            newLc.coefs[k2] = newP;
526                        }
527                    }
528                    if ((typeof idxConstraint != "undefined")&&(k2!=0)) {
529                        if (!sig2constraint[k2]) sig2constraint[k2] = {};
530                        sig2constraint[k2][idxConstraint] = true;
531                    }
532                }
533            }
534        }
535        return newLc;
536    }
537
538    function substitute(lc, s, eq) {
539        if (!lc.coefs[s]) return lc;
540        const newLc = ctx.lc._clone(lc);
541        delete newLc.coefs[s];
542        for (let k2 in eq.coefs) {
543            const newP = ctx.F.mul(eq.coefs[k2], lc.coefs[s]);
544            if (!ctx.F.isZero(newP)) {
545                if (newLc.coefs[k2]) {
546                    newLc.coefs[k2] = ctx.F.add(newLc.coefs[k2], newP);
547                    if (ctx.F.isZero(newLc.coefs[k2])) delete newLc.coefs[k2];
548                } else {
549                    newLc.coefs[k2] = newP;
550                }
551            }
552        }
553        return newLc;
554    }
555
556    function isConstant(l) {
557        for (let k in l.coefs) {
558            if ((k != sONE) && (!ctx.F.isZero(l.coefs[k]))) return false;
559        }
560        if (!l.coefs[sONE] || ctx.F.isZero(l.coefs[sONE])) return false;
561        return true;
562    }
563
564    function addTolIdx(lc, newS) {
565        const sigs = Object.keys(lc.coefs);
566        for (let k=0; k<sigs.length; k++) {
567            const s = sigs[k];
568            if (s) {
569                if (!lIdx[s]) lIdx[s] = {};
570                lIdx[s][newS] = true;
571            }
572        }
573    }
574
575}
576
577