snarkjs/src/circuit.js
src/circuit.jsRaw
1/*
2 Copyright 2018 0kims association.
3
4 This file is part of zksnark JavaScript library.
5
6 zksnark JavaScript library is a free software: you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as published by the
8 Free Software Foundation, either version 3 of the License, or (at your option)
9 any later version.
10
11 zksnark JavaScript library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 more details.
15
16 You should have received a copy of the GNU General Public License along with
17 zksnark JavaScript library. If not, see <https://www.gnu.org/licenses/>.
18*/
19
20const bigInt = require("./bigint.js");
21
22const __P__ = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
23const __MASK__ = bigInt("28948022309329048855892746252171976963317496166410141009864396001978282409983"); // 0x3FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
24const calculateWitness = require("./calculateWitness.js");
25
26module.exports = class Circuit {
27 constructor(circuitDef) {
28 this.nPubInputs = circuitDef.nPubInputs;
29 this.nPrvInputs = circuitDef.nPrvInputs;
30 this.nInputs = circuitDef.nInputs;
31 this.nOutputs = circuitDef.nOutputs;
32 this.nVars = circuitDef.nVars;
33 this.nSignals = circuitDef.nSignals;
34 this.nConstants = circuitDef.nConstants;
35
36 this.nConstraints = circuitDef.constraints.length;
37
38 this.signalName2Idx = circuitDef.signalName2Idx;
39 this.components = circuitDef.components;
40 this.componentName2Idx = circuitDef.componentName2Idx;
41 this.signals = circuitDef.signals;
42 this.constraints = circuitDef.constraints;
43
44 this.templates = {};
45 for (let t in circuitDef.templates) {
46 this.templates[t] = eval(" const __f= " +circuitDef.templates[t] + "\n__f");
47 }
48
49 this.functions = {};
50 for (let f in circuitDef.functions) {
51 this.functions[f] = {
52 params: circuitDef.functions[f].params,
53 func: eval(" const __f= " +circuitDef.functions[f].func + "\n__f;")
54 };
55 }
56 }
57
58 calculateWitness(input) {
59 return calculateWitness(this, input);
60 }
61
62 getSignalIdx(name) {
63 if (typeof(this.signalName2Idx[name]) != "undefined") return this.signalName2Idx[name];
64 if (!isNaN(name)) return Number(name);
65 throw new Error("Invalid signal identifier: ", name);
66 }
67
68 // returns the index of the i'th output
69 outputIdx(i) {
70 if (i>=this.nOutputs) throw new Error("Accessing an invalid output: "+i);
71 return i+1;
72 }
73
74 // returns the index of the i'th input
75 inputIdx(i) {
76 if (i>=this.nInputs) throw new Error("Accessing an invalid input: "+i);
77 return this.nOutputs + 1 + i;
78 }
79
80 // returns the index of the i'th public input
81 pubInputIdx(i) {
82 if (i>=this.nPubInputs) throw new Error("Accessing an invalid pubInput: "+i);
83 return this.inputIdx(i);
84 }
85
86 // returns the index of the i'th private input
87 prvInputIdx(i) {
88 if (i>=this.nPrvInputs) throw new Error("Accessing an invalid prvInput: "+i);
89 return this.inputIdx(this.nPubInputs + i);
90 }
91
92 // returns the index of the i'th variable
93 varIdx(i) {
94 if (i>=this.nVars) throw new Error("Accessing an invalid variable: "+i);
95 return i;
96 }
97
98 // returns the index of the i'th constant
99 constantIdx(i) {
100 if (i>=this.nConstants) throw new Error("Accessing an invalid constant: "+i);
101 return this.nVars + i;
102 }
103
104 // returns the index of the i'th signal
105 signalIdx(i) {
106 if (i>=this.nSignls) throw new Error("Accessing an invalid signal: "+i);
107 return i;
108 }
109
110 signalNames(i) {
111 return this.signals[ this.getSignalIdx(i) ].names.join(", ");
112 }
113
114 a(constraint, signalIdx) {
115 return bigInt(this.constraints[constraint][0][signalIdx] || 0 );
116 }
117
118 b(constraint, signalIdx) {
119 return bigInt(this.constraints[constraint][1][signalIdx] || 0);
120 }
121
122 c(constraint, signalIdx) {
123 return bigInt(this.constraints[constraint][2][signalIdx] || 0);
124 }
125};