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