snarkjs/src/circuit.js
src/circuit.jsRaw
1const bigInt = require("./bigint.js");
2
3const __P__ = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
4const __MASK__ = bigInt("28948022309329048855892746252171976963317496166410141009864396001978282409983"); // 0x3FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
5const calculateWitness = require("./calculateWitness.js");
6
7module.exports = class Circuit {
8 constructor(circuitDef) {
9 this.nPubInputs = circuitDef.nPubInputs;
10 this.nPrvInputs = circuitDef.nPrvInputs;
11 this.nInputs = circuitDef.nInputs;
12 this.nOutputs = circuitDef.nOutputs;
13 this.nVars = circuitDef.nVars;
14 this.nSignals = circuitDef.nSignals;
15 this.nConstants = circuitDef.nConstants;
16
17 this.nConstrains = circuitDef.constrains.length;
18
19 this.signalName2Idx = circuitDef.signalName2Idx;
20 this.components = circuitDef.components;
21 this.componentName2Idx = circuitDef.componentName2Idx;
22 this.signals = circuitDef.signals;
23 this.constrains = circuitDef.constrains;
24
25 this.templates = {};
26 for (let t in circuitDef.templates) {
27 this.templates[t] = eval(" const __f= " +circuitDef.templates[t] + "\n__f");
28 }
29
30 this.functions = {};
31 for (let f in circuitDef.functions) {
32 this.functions[f] = {
33 params: circuitDef.functions[f].params,
34 func: eval(" const __f= " +circuitDef.functions[f].func + "\n__f;")
35 };
36 }
37 }
38
39 calculateWitness(input) {
40 return calculateWitness(this, input);
41 }
42
43 getSignalIdx(name) {
44 if (typeof(this.signalName2Idx[name]) != "undefined") return this.signalName2Idx[name];
45 if (!isNaN(name)) return Number(name);
46 throw new Error("Invalid signal identifier: ", name);
47 }
48
49 // returns the index of the i'th output
50 outputIdx(i) {
51 if (i>=this.nOutputs) throw new Error("Accessing an invalid output: "+i);
52 return i+1;
53 }
54
55 // returns the index of the i'th input
56 inputIdx(i) {
57 if (i>=this.nInputs) throw new Error("Accessing an invalid input: "+i);
58 return this.nOutputs + 1 + i;
59 }
60
61 // returns the index of the i'th public input
62 pubInputIdx(i) {
63 if (i>=this.nPubInputs) throw new Error("Accessing an invalid pubInput: "+i);
64 return this.inputIdx(i);
65 }
66
67 // returns the index of the i'th private input
68 prvInputIdx(i) {
69 if (i>=this.nPrvInputs) throw new Error("Accessing an invalid prvInput: "+i);
70 return this.inputIdx(this.nPubInputs + i);
71 }
72
73 // returns the index of the i'th variable
74 varIdx(i) {
75 if (i>=this.nVars) throw new Error("Accessing an invalid variable: "+i);
76 return i;
77 }
78
79 // returns the index of the i'th constant
80 constantIdx(i) {
81 if (i>=this.nConstants) throw new Error("Accessing an invalid constant: "+i);
82 return this.nVars + i;
83 }
84
85 // returns the index of the i'th signal
86 signalIdx(i) {
87 if (i>=this.nSignls) throw new Error("Accessing an invalid signal: "+i);
88 return i;
89 }
90
91 signalNames(i) {
92 return this.signals[ this.getSignalIdx(i) ].names.join(", ");
93 }
94
95 a(constrain, signalIdx) {
96 return bigInt(this.constrains[constrain][0][signalIdx] || 0 );
97 }
98
99 b(constrain, signalIdx) {
100 return bigInt(this.constrains[constrain][1][signalIdx] || 0);
101 }
102
103 c(constrain, signalIdx) {
104 return bigInt(this.constrains[constrain][2][signalIdx] || 0);
105 }
106};