circom/src/c_build.js
src/c_build.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 assert = require("assert");
21const bigInt = require("big-integer");
22const utils = require("./utils");
23const gen = require("./c_gen").gen;
24const newRef = require("./c_gen").newRef;
25
26module.exports = buildC;
27
28
29function buildC(ctx) {
30 ctx.definedFunctions = {};
31 ctx.functionCodes = [];
32 ctx.buildFunction = buildFunction;
33 ctx.code = "";
34 ctx.conditionalCodeHeader = "";
35 ctx.tmpNames = {};
36 ctx.getTmpName = getTmpName;
37 ctx.codes_sizes = [];
38 ctx.definedSizes = {};
39 ctx.addSizes = addSizes;
40
41 const entryTables = buildEntryTables(ctx);
42 ctx.globalNames = ctx.tmpNames;
43
44 const code = buildCode(ctx);
45 const functions = buildFuncFunctions(ctx);
46 const compnentsArray = buildComponentsArray(ctx);
47
48 const headder = buildHeader(ctx);
49 const sizes = buildSizes(ctx);
50 const mapIsInput = buildMapIsInput(ctx);
51 const wit2Sig = buildWit2Sig(ctx);
52 const circuitVar = buildCircuitVar(ctx);
53
54 return "" +
55 headder + "\n" +
56 sizes + "\n" +
57 entryTables + "\n" +
58 functions + "\n" +
59 code + "\n" +
60 compnentsArray + "\n" +
61 mapIsInput + "\n" +
62 wit2Sig +"\n" +
63 circuitVar;
64}
65
66function buildEntryTables(ctx) {
67
68 const codes_hashMaps = [];
69 const codes_componentEntries = [];
70 const definedHashTables = {};
71 for (let i=0; i<ctx.components.length; i++) {
72 const {htName, htMap} = addHashTable(i);
73
74 let code = "";
75 const componentEntriesTableName = ctx.getTmpName("entryTable_" + ctx.components[i].template);
76
77 code += `Circom_ComponentEntry ${componentEntriesTableName}[${htMap.length}] = {\n`;
78 for (let j=0; j<htMap.length; j++) {
79 const entry = ctx.components[i].names.o[htMap[j]];
80 code += j>0 ? " ," : " ";
81 const sizeName = ctx.addSizes(entry.sizes);
82
83 const ty = entry.type == "S" ? "_typeSignal" : "_typeComponent";
84 code += `{${entry.offset},${sizeName}, ${ty}}\n`;
85 }
86 code += "};\n";
87 codes_componentEntries.push(code);
88
89 ctx.components[i].htName = htName;
90 ctx.components[i].etName = componentEntriesTableName;
91 }
92
93
94 return "" +
95 "// HashMaps\n" +
96 codes_hashMaps.join("\n") + "\n" +
97 "\n" +
98 "// Component Entries\n" +
99 codes_componentEntries.join("\n") + "\n" +
100 "\n";
101
102 function addHashTable(cIdx) {
103 const keys = Object.keys(ctx.components[cIdx].names.o);
104 assert(keys.length<128);
105 keys.sort((a,b) => ((a>b) ? 1 : -1));
106 const h = utils.fnvHash(keys.join(","));
107 if (definedHashTables[h]) return definedHashTables[h];
108 definedHashTables[h] = {};
109 definedHashTables[h].htName = ctx.getTmpName("ht_"+ctx.components[cIdx].template);
110 definedHashTables[h].htMap = [];
111 const t = [];
112 for (let i=0; i<keys.length; i++) {
113 definedHashTables[h].htMap[i] = keys[i];
114
115 const h2 = utils.fnvHash(keys[i]);
116 let pos = parseInt(h2.slice(-2), 16);
117 while (t[pos]) pos = (pos + 1) % 256;
118 t[pos] = [h2, i];
119 }
120 let code = `Circom_HashEntry ${definedHashTables[h].htName}[256] = {`;
121 for (let i=0; i<256; i++) {
122 code += i>0 ? "," : "";
123 if (t[i]) {
124 code += `{0x${t[i][0]}LL, ${t[i][1]}} /* ${keys[t[i][1]]} */`;
125 } else {
126 code += "{0,0}";
127 }
128 }
129 code += "};\n";
130
131 codes_hashMaps.push(code);
132
133 return definedHashTables[h];
134 }
135}
136
137function buildCode(ctx) {
138
139 const fDefined = {};
140
141 const fnComponents = [];
142 for (let i=0; i<ctx.components.length; i++) {
143 const h = hashComponentCall(ctx, i);
144 const fName = ctx.components[i].template+"_"+h;
145 if (!fDefined[fName]) {
146
147
148 const scope = {_prefix : ""};
149 ctx.scopes = [scope];
150 ctx.conditionalCode = false;
151 ctx.code = "";
152 ctx.codeHeader = "// Header\n";
153 ctx.codeFooter = "// Footer\n";
154 ctx.tmpNames = Object.assign({},ctx.globalNames);
155
156 for (let p in ctx.components[i].params) {
157 newRef(ctx, "BIGINT", p, bigInt(ctx.components[i].params[p]));
158 }
159
160 gen(ctx, ctx.templates[ctx.components[i].template].block);
161
162 const S = `void ${fName}(Circom_CalcWit *ctx) {\n` +
163 utils.ident(
164 ctx.codeHeader + "\n" +
165 ctx.code + "\n" +
166 ctx.codeFooter
167 ) +
168 "}\n";
169
170 fnComponents.push(S);
171 }
172 ctx.components[i].fnName = fName;
173 }
174
175 return fnComponents.join("\n");
176}
177
178function buildFuncFunctions(ctx) {
179 return "// Functions\n" +
180 ctx.functionCodes.join("\n");
181}
182
183function buildComponentsArray(ctx) {
184
185 const ccodes = [];
186 ccodes.push(`Circom_Component _components[${ctx.components.length}] = {\n`);
187 for (let i=0; i< ctx.components.length; i++) {
188 ccodes.push(i>0 ? " ," : " ");
189 ccodes.push(`{${ctx.components[i].htName},${ctx.components[i].etName},${ctx.components[i].fnName}, ${ctx.components[i].nInSignals}}\n`);
190 }
191 ccodes.push("};\n");
192 const codeComponents = ccodes.join("");
193
194 return "" +
195 "// Components\n" +
196 codeComponents +
197 "\n";
198}
199
200
201function buildHeader(ctx) {
202 return "#include \"circom.h\"\n" +
203 "#include \"calcwit.h\"\n" +
204 `#define NSignals ${ctx.signals.length}\n` +
205 `#define NComponents ${ctx.components.length}\n` +
206 `#define NInputs ${ctx.components[ ctx.getComponentIdx("main") ].nInSignals}\n`+
207 `#define NOutputs ${ctx.totals[ ctx.stOUTPUT ]}\n`+
208 `#define NVars ${ctx.totals[ctx.stONE] + ctx.totals[ctx.stOUTPUT] + ctx.totals[ctx.stPUBINPUT] + ctx.totals[ctx.stPRVINPUT] + ctx.totals[ctx.stINTERNAL]}\n` +
209 `#define __P__ "${ctx.field.p.toString()}"\n` +
210 "\n";
211}
212
213function buildSizes(ctx) {
214 return "// Sizes\n" +
215 ctx.codes_sizes.join("\n");
216}
217
218
219function buildMapIsInput(ctx) {
220 const arr = [];
221 let line = "";
222 let acc = 0;
223 let i;
224 for (i=0; i<ctx.signals.length; i++) {
225 if (ctx.signals[i].o & ctx.IN) {
226 acc = acc | (1 << (i%32) );
227 }
228 if ((i+1)%32==0) {
229 line += (i>31) ? "," : " ";
230 line += toHex(acc);
231 acc = 0;
232 if ( i % (32*64) == 0) {
233 arr.push(line);
234 line = "";
235 }
236 }
237 }
238
239 if ((i%32) != 0) {
240 line += (i>31) ? "," : " ";
241 line += toHex(acc);
242 }
243 if (line != "") {
244 arr.push(line);
245 }
246
247 return "// mapIsArray\n" +
248 `u32 _mapIsInput[${Math.floor((ctx.signals.length-1) / 32)+1}] = {\n`+
249 arr.join("\n") + "\n" +
250 "};\n";
251
252 function toHex(number) {
253 if (number < 0) number = 0xFFFFFFFF + number + 1;
254 let S=number.toString(16).toUpperCase();
255 while (S.length<8) S = "0" + S;
256 return "0x"+S;
257 }
258}
259
260
261function buildWit2Sig(ctx) {
262 const codes = [];
263 const NVars =
264 ctx.totals[ctx.stONE] +
265 ctx.totals[ctx.stOUTPUT] +
266 ctx.totals[ctx.stPUBINPUT] +
267 ctx.totals[ctx.stPRVINPUT] +
268 ctx.totals[ctx.stINTERNAL];
269 const arr = Array(NVars);
270 for (let i=0; i<ctx.signals.length; i++) {
271 const outIdx = ctx.signals[i].id;
272 if (typeof outIdx == "undefined") continue;
273 if (ctx.signals[i].e>=0) continue; // If has an alias, continue..
274 assert(outIdx<NVars);
275 if (typeof arr[ctx.signals[i].id] == "undefined") {
276 arr[outIdx] = i;
277 }
278 }
279 codes.push("// Signal Table\n");
280 codes.push(`int _wit2sig[${NVars}] = {\n`);
281 let code = "";
282 for (let i=0; i<NVars; i++) {
283 code += (i>0) ? ",": " ";
284 code += arr[i];
285 if ((i>0)&&(i%64 == 0)) {
286 if (code != "") codes.push(code + "\n");
287 codes.push(code);
288 code =0;
289 }
290 }
291 if (code != "") codes.push(code + "\n");
292 codes.push("};\n");
293
294 return codes.join("");
295
296}
297
298
299function buildCircuitVar() {
300 return "Circom_Circuit _circuit = {\n" +
301 " NSignals,\n"+
302 " NComponents,\n"+
303 " NInputs,\n"+
304 " NOutputs,\n"+
305 " NVars,\n"+
306 " _wit2sig,\n"+
307 " _components,\n"+
308 " _mapIsInput,\n"+
309 " __P__\n" +
310 "};\n";
311}
312
313
314
315function hashComponentCall(ctx, cIdx) {
316 // TODO: At the moment generate a diferent function for each instance of the component
317 return cIdx;
318}
319
320
321function getTmpName(_suggestedName) {
322 let suggestedName;
323 if (_suggestedName) {
324 suggestedName = trimUnderscore(_suggestedName);
325 } else {
326 suggestedName = "tmp";
327 }
328
329 if (typeof(this.tmpNames[suggestedName]) == "undefined") {
330 this.tmpNames[suggestedName] = 1;
331 return "_"+suggestedName;
332 } else {
333 const name = "_" + suggestedName + "_" + this.tmpNames[suggestedName];
334 this.tmpNames[suggestedName]++;
335 return name;
336 }
337
338 function trimUnderscore(str) {
339 let p1=0;
340 while ((p1 < str.length)&&(str[p1] == "_")) p1++;
341 let p2=str.length;
342 while ((p2 > 0)&&(str[p2-1] == "_")) p2--;
343
344 return str.slice(p1,p2);
345 }
346
347}
348
349function addSizes(_sizes) {
350 const sizes = _sizes || [];
351 let name = "sizes";
352 for (let i=0; i<sizes.length;i++) {
353 name+="_"+sizes[i];
354 }
355 if (name=="sizes") name="sizes_0";
356
357 if (this.definedSizes[name]) return this.definedSizes[name];
358
359 const labelName = this.getTmpName(name);
360 this.definedSizes[name] = labelName;
361
362 const accSizes = utils.accSizes(sizes);
363
364 let code = `Circom_Size ${labelName}[${accSizes.length}] = {`;
365 for (let i=0; i<accSizes.length; i++) {
366 if (i>0) code += ",";
367 code += accSizes[i];
368 }
369 code += "};\n";
370 this.codes_sizes.push(code);
371
372 return labelName;
373}
374
375function buildFunction(name, paramValues) {
376 const ctx = this;
377 const h = hashFunctionCall(ctx, name, paramValues);
378
379 if (ctx.definedFunctions[h]) return ctx.definedFunctions[h];
380
381 const res = {
382 fnName: `${name}_${h}`
383 };
384
385 const oldScopes = ctx.scopes;
386 const oldConditionalCode = ctx.conditionalCode;
387 const oldCode = ctx.code;
388 const oldCodeHeader = ctx.codeHeader;
389 const oldCodeFooter = ctx.codeFooter;
390 const oldTmpNames = ctx.tmpNames;
391
392
393 const scope = {_prefix : ""};
394 ctx.scopes = [scope];
395 ctx.conditionalCode = false;
396 ctx.code = "";
397 ctx.codeHeader = "// Header\n";
398 ctx.codeFooter = "// Footer\n";
399 ctx.tmpNames = Object.assign({},ctx.globalNames);
400 ctx.returnValue = null;
401 ctx.returnSizes = null;
402
403 let paramsStr = "";
404
405 for (let i=0; i<ctx.functions[name].params.length; i++) {
406
407 if (paramValues[i].used) {
408 paramsStr += `,PBigInt ${ctx.functions[name].params[i]}`;
409 scope[ctx.functions[name].params[i]] = {
410 stack: true,
411 type: "BIGINT",
412 used: true,
413 sizes: paramValues[i].sizes,
414 label: ctx.functions[name].params[i],
415 };
416 } else {
417 scope[ctx.functions[name].params[i]] = {
418 stack: true,
419 type: "BIGINT",
420 used: false,
421 sizes: paramValues[i].sizes,
422 label: ctx.functions[name].params[i],
423 value: paramValues[i].value
424 };
425 }
426 }
427
428 gen(ctx, ctx.functions[name].block);
429
430 if (ctx.returnValue == null) {
431 if (ctx.returnSizes == null) assert(false, `Funciont ${name} does not return any value`);
432 res.type = "VARVAL_CONSTSIZE";
433 let code = `void ${name}_${h}(Circom_CalcWit *ctx, PBigInt __retValue ${paramsStr}) {`;
434 code += utils.ident(ctx.codeHeader);
435 code += utils.ident(ctx.code);
436 code += utils.ident("returnFunc:\n");
437 code += utils.ident(ctx.codeFooter);
438 code += "}\n";
439 res.returnSizes = ctx.returnSizes;
440 ctx.functionCodes.push(code);
441 } else {
442 res.type = "CONSTVAL";
443 res.returnValue = ctx.returnValue;
444 }
445
446 ctx.scopes = oldScopes;
447 ctx.conditionalCode = oldConditionalCode;
448 ctx.code = oldCode;
449 ctx.codeHeader = oldCodeHeader;
450 ctx.codeFooter = oldCodeFooter;
451 ctx.tmpNames = oldTmpNames;
452
453 ctx.definedFunctions[h] = res;
454
455 return res;
456}
457
458function hashFunctionCall(ctx, name, paramValues) {
459 // TODO
460 return "1234";
461}