You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
3.7 KiB

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