arnaucube
arnaucube/circomPublic Self-hosted repository
circom/tobin.js
tobin.jsRaw
  1const bigInt = require("big-integer");
  2const __P__ = new bigInt("21888242871839275222246405745257275088696311157297823662689037894645226208583");
  3const __MASK__ = new bigInt(2).pow(253).minus(1);
  4const circuit = {};
  5module.exports = circuit;
  6
  7circuit.signals={
  8 "one": {
  9  "fullName": "one",
 10  "value": "1",
 11  "equivalence": "",
 12  "direction": "",
 13  "id": 0
 14 },
 15 "main.inp": {
 16  "fullName": "main.inp",
 17  "direction": "IN",
 18  "component": "main",
 19  "equivalence": "",
 20  "alias": [
 21   "main.inp"
 22  ],
 23  "id": 1
 24 },
 25 "main.out[0]": {
 26  "fullName": "main.out[0]",
 27  "direction": "OUT",
 28  "component": "main",
 29  "equivalence": "",
 30  "alias": [
 31   "main.out[0]"
 32  ],
 33  "id": 2
 34 },
 35 "main.out[1]": {
 36  "fullName": "main.out[1]",
 37  "direction": "OUT",
 38  "component": "main",
 39  "equivalence": "",
 40  "alias": [
 41   "main.out[1]"
 42  ],
 43  "id": 3
 44 },
 45 "main.out[2]": {
 46  "fullName": "main.out[2]",
 47  "direction": "OUT",
 48  "component": "main",
 49  "equivalence": "",
 50  "alias": [
 51   "main.out[2]"
 52  ],
 53  "id": 4
 54 }
 55};
 56
 57circuit.components={
 58 "main": {
 59  "signals": [
 60   "main.inp",
 61   "main.out[0]",
 62   "main.out[1]",
 63   "main.out[2]"
 64  ],
 65  "params": {
 66   "n": "3"
 67  },
 68  "template": "toBin",
 69  "inputSignals": 1
 70 }
 71};
 72
 73circuit.signalConstrains=[
 74 {
 75  "type": "QEQ",
 76  "a": {
 77   "type": "LINEARCOMBINATION",
 78   "values": {
 79    "main.out[0]": "1"
 80   }
 81  },
 82  "b": {
 83   "type": "LINEARCOMBINATION",
 84   "values": {
 85    "main.out[0]": "1",
 86    "one": "21888242871839275222246405745257275088696311157297823662689037894645226208582"
 87   }
 88  },
 89  "c": {
 90   "type": "LINEARCOMBINATION",
 91   "values": {}
 92  }
 93 },
 94 {
 95  "type": "QEQ",
 96  "a": {
 97   "type": "LINEARCOMBINATION",
 98   "values": {
 99    "main.out[1]": "1"
100   }
101  },
102  "b": {
103   "type": "LINEARCOMBINATION",
104   "values": {
105    "main.out[1]": "1",
106    "one": "21888242871839275222246405745257275088696311157297823662689037894645226208582"
107   }
108  },
109  "c": {
110   "type": "LINEARCOMBINATION",
111   "values": {}
112  }
113 },
114 {
115  "type": "QEQ",
116  "a": {
117   "type": "LINEARCOMBINATION",
118   "values": {
119    "main.out[2]": "1"
120   }
121  },
122  "b": {
123   "type": "LINEARCOMBINATION",
124   "values": {
125    "main.out[2]": "1",
126    "one": "21888242871839275222246405745257275088696311157297823662689037894645226208582"
127   }
128  },
129  "c": {
130   "type": "LINEARCOMBINATION",
131   "values": {}
132  }
133 },
134 {
135  "type": "QEQ",
136  "a": {
137   "type": "LINEARCOMBINATION",
138   "values": {}
139  },
140  "b": {
141   "type": "LINEARCOMBINATION",
142   "values": {}
143  },
144  "c": {
145   "type": "LINEARCOMBINATION",
146   "values": {
147    "main.out[0]": "1",
148    "main.out[1]": "2",
149    "main.out[2]": "4",
150    "main.inp": "21888242871839275222246405745257275088696311157297823662689037894645226208582"
151   }
152  }
153 }
154];
155
156circuit.witnessNames=[
157 [
158  "one"
159 ],
160 [
161  "main.inp"
162 ],
163 [
164  "main.out[0]"
165 ],
166 [
167  "main.out[1]"
168 ],
169 [
170  "main.out[2]"
171 ]
172];
173
174{
175}
176
177circuit.templates = [];
178
179circuit.templates["toBin"] = function(ctx) {
180    ctx.setVar("lc1", [], "0");
181    for (ctx.setVar("i", [], "0");bigInt(ctx.getVar("i",[])).lt(ctx.getVar("n",[])) ? 1 : 0;(ctx.setVar("i", [], bigInt(ctx.getVar("i",[])).add("1").mod(__P__))).add(__P__).minus(1).mod(__P__))
182    {
183        ctx.setSignal("out", [ctx.getVar("i",[])], bigInt(bigInt(ctx.getVar("i",[])).greater(256) ? 0 : bigInt(ctx.getSignal("inp", [])).shiftRight(bigInt(ctx.getVar("i",[])).value).and(__MASK__)).and("1").and(__MASK__));
184        ctx.assert(bigInt(bigInt(ctx.getSignal("out", [ctx.getVar("i",[])])).times(bigInt(ctx.getSignal("out", [ctx.getVar("i",[])])).add(__P__).minus("1").mod(__P__)).mod(__P__)).equals("0"));
185        ctx.setVar("lc1", [], bigInt(ctx.getVar("lc1",[])).add(bigInt(ctx.getSignal("out", [ctx.getVar("i",[])])).times(bigInt("2").modPow(ctx.getVar("i",[]), __P__)).mod(__P__)).mod(__P__));
186    }
187    ctx.assert(bigInt(ctx.getVar("lc1",[])).equals(ctx.getSignal("inp", [])));
188}
189;