circom/ports/wasm/tester.js
ports/wasm/tester.jsRaw
1const chai = require("chai");
2const assert = chai.assert;
3
4const fs = require("fs");
5var tmp = require("tmp-promise");
6const path = require("path");
7const compiler = require("../../src/compiler");
8
9const bigInt = require("big-integer");
10const utils = require("../../src/utils");
11const loadR1cs = require("r1csfile").load;
12const ZqField = require("ffjavascript").ZqField;
13
14const WitnessCalculatorBuilder = require("circom_runtime").WitnessCalculatorBuilder;
15
16module.exports = wasm_tester;
17
18async function wasm_tester(circomFile, _options) {
19 tmp.setGracefulCleanup();
20
21 const dir = await tmp.dir({prefix: "circom_", unsafeCleanup: true });
22
23 // console.log(dir.path);
24
25 const baseName = path.basename(circomFile, ".circom");
26 const options = Object.assign({}, _options);
27
28 options.wasmWriteStream = fs.createWriteStream(path.join(dir.path, baseName + ".wasm"));
29 options.symWriteStream = fs.createWriteStream(path.join(dir.path, baseName + ".sym"));
30 options.r1csFileName = path.join(dir.path, baseName + ".r1cs");
31
32 const promisesArr = [];
33 promisesArr.push(new Promise(fulfill => options.wasmWriteStream.on("finish", fulfill)));
34
35 await compiler(circomFile, options);
36
37 await Promise.all(promisesArr);
38
39 const wasm = await fs.promises.readFile(path.join(dir.path, baseName + ".wasm"));
40
41 const wc = await WitnessCalculatorBuilder(wasm);
42
43 return new WasmTester(dir, baseName, wc);
44}
45
46class WasmTester {
47
48 constructor(dir, baseName, witnessCalculator) {
49 this.dir=dir;
50 this.baseName = baseName;
51 this.witnessCalculator = witnessCalculator;
52 }
53
54 async release() {
55 await this.dir.cleanup();
56 }
57
58 async calculateWitness(input) {
59
60 return await this.witnessCalculator.calculateWitness(input);
61 }
62
63 async loadSymbols() {
64 if (this.symbols) return;
65 this.symbols = {};
66 const symsStr = await fs.promises.readFile(
67 path.join(this.dir.path, this.baseName + ".sym"),
68 "utf8"
69 );
70 const lines = symsStr.split("\n");
71 for (let i=0; i<lines.length; i++) {
72 const arr = lines[i].split(",");
73 if (arr.length!=4) continue;
74 this.symbols[arr[3]] = {
75 labelIdx: Number(arr[0]),
76 varIdx: Number(arr[1]),
77 componentIdx: Number(arr[2]),
78 };
79 }
80 }
81
82 async loadConstraints() {
83 const self = this;
84 if (this.constraints) return;
85 const r1cs = await loadR1cs(path.join(this.dir.path, this.baseName + ".r1cs"),true, false);
86 self.field = new ZqField(r1cs.prime);
87 self.nVars = r1cs.nVars;
88 self.constraints = r1cs.constraints;
89 }
90
91 async assertOut(actualOut, expectedOut) {
92 const self = this;
93 if (!self.symbols) await self.loadSymbols();
94
95 checkObject("main", expectedOut);
96
97 function checkObject(prefix, eOut) {
98
99 if (Array.isArray(eOut)) {
100 for (let i=0; i<eOut.length; i++) {
101 checkObject(prefix + "["+i+"]", eOut[i]);
102 }
103 } else if ((typeof eOut == "object")&&(eOut.constructor.name == "Object")) {
104 for (let k in eOut) {
105 checkObject(prefix + "."+k, eOut[k]);
106 }
107 } else {
108 if (typeof self.symbols[prefix] == "undefined") {
109 assert(false, "Output variable not defined: "+ prefix);
110 }
111 const ba = bigInt(actualOut[self.symbols[prefix].varIdx]).toString();
112 const be = bigInt(eOut).toString();
113 assert.strictEqual(ba, be, prefix);
114 }
115 }
116 }
117
118 async getDecoratedOutput(witness) {
119 const self = this;
120 const lines = [];
121 if (!self.symbols) await self.loadSymbols();
122 for (let n in self.symbols) {
123 let v;
124 if (utils.isDefined(witness[self.symbols[n].varIdx])) {
125 v = witness[self.symbols[n].varIdx].toString();
126 } else {
127 v = "undefined";
128 }
129 lines.push(`${n} --> ${v}`);
130 }
131 return lines.join("\n");
132 }
133
134 async checkConstraints(witness) {
135 const self = this;
136 if (!self.constraints) await self.loadConstraints();
137 for (let i=0; i<self.constraints.length; i++) {
138 checkConstraint(self.constraints[i]);
139 }
140
141 function checkConstraint(constraint) {
142 const F = self.field;
143 const a = evalLC(constraint[0]);
144 const b = evalLC(constraint[1]);
145 const c = evalLC(constraint[2]);
146
147 assert (F.sub(F.mul(a,b), c).isZero(), "Constraint doesn't match");
148 }
149
150 function evalLC(lc) {
151 const F = self.field;
152 let v = F.zero;
153 for (let w in lc) {
154 v = F.add(
155 v,
156 F.mul( lc[w], witness[w] )
157 );
158 }
159 return v;
160 }
161 }
162
163}
164
165