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.

437 lines
9.3 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. /*
  2. Copyright 2018 0kims association.
  3. This file is part of snarkjs.
  4. snarkjs is a free software: you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License as published by the
  6. Free Software Foundation, either version 3 of the License, or (at your option)
  7. any later version.
  8. snarkjs is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. more details.
  12. You should have received a copy of the GNU General Public License along with
  13. snarkjs. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. /* global BigInt */
  16. const bigInt = require("big-integer");
  17. let wBigInt;
  18. if (typeof(BigInt) != "undefined") {
  19. wBigInt = BigInt;
  20. wBigInt.one = wBigInt(1);
  21. wBigInt.zero = wBigInt(0);
  22. // Affine
  23. wBigInt.genAffine = (q) => {
  24. const nq = -q;
  25. return (a) => {
  26. let aux = a;
  27. if (aux < 0) {
  28. if (aux <= nq) {
  29. aux = aux % q;
  30. }
  31. if (aux < wBigInt.zero) {
  32. aux = aux + q;
  33. }
  34. } else {
  35. if (aux >= q) {
  36. aux = aux % q;
  37. }
  38. }
  39. return aux.valueOf();
  40. };
  41. };
  42. // Inverse
  43. wBigInt.genInverse = (q) => {
  44. return (a) => {
  45. let t = wBigInt.zero;
  46. let r = q;
  47. let newt = wBigInt.one;
  48. let newr = wBigInt.affine(a, q);
  49. while (newr!=wBigInt.zero) {
  50. let q = r/newr;
  51. [t, newt] = [newt, t-q*newt];
  52. [r, newr] = [newr, r-q*newr];
  53. }
  54. if (t<wBigInt.zero) t += q;
  55. return t;
  56. };
  57. };
  58. // Add
  59. wBigInt.genAdd = (q) => {
  60. if (q) {
  61. return (a,b) => (a+b) % q;
  62. } else {
  63. return (a,b) => a+b;
  64. }
  65. };
  66. // Sub
  67. wBigInt.genSub = (q) => {
  68. if (q) {
  69. return (a,b) => (a-b) % q;
  70. } else {
  71. return (a,b) => a-b;
  72. }
  73. };
  74. // Neg
  75. wBigInt.genNeg = (q) => {
  76. if (q) {
  77. return (a) => (-a) % q;
  78. } else {
  79. return (a) => -a;
  80. }
  81. };
  82. // Mul
  83. wBigInt.genMul = (q) => {
  84. if (q) {
  85. return (a,b) => (a*b) % q;
  86. } else {
  87. return (a,b) => a*b;
  88. }
  89. };
  90. // Shr
  91. wBigInt.genShr = () => {
  92. return (a,b) => a >> wBigInt(b);
  93. };
  94. // Shl
  95. wBigInt.genShl = (q) => {
  96. if (q) {
  97. return (a,b) => (a << wBigInt(b)) % q;
  98. } else {
  99. return (a,b) => a << wBigInt(b);
  100. }
  101. };
  102. // Equals
  103. wBigInt.genEquals = (q) => {
  104. if (q) {
  105. return (a,b) => (a.affine(q) == b.affine(q));
  106. } else {
  107. return (a,b) => a == b;
  108. }
  109. };
  110. // Square
  111. wBigInt.genSquare = (q) => {
  112. if (q) {
  113. return (a) => (a*a) %q;
  114. } else {
  115. return (a) => a*a;
  116. }
  117. };
  118. // Double
  119. wBigInt.genDouble = (q) => {
  120. if (q) {
  121. return (a) => (a+a) %q;
  122. } else {
  123. return (a) => a+a;
  124. }
  125. };
  126. // IsZero
  127. wBigInt.genIsZero = (q) => {
  128. if (q) {
  129. return (a) => (a.affine(q) == wBigInt.zero);
  130. } else {
  131. return (a) => a == wBigInt.zero;
  132. }
  133. };
  134. // Other minor functions
  135. wBigInt.prototype.isOdd = function() {
  136. return (this & wBigInt.one) == wBigInt(1);
  137. };
  138. wBigInt.prototype.isNegative = function() {
  139. return this < wBigInt.zero;
  140. };
  141. wBigInt.prototype.and = function(m) {
  142. return this & m;
  143. };
  144. wBigInt.prototype.mod = function(c) {
  145. return this % c;
  146. };
  147. wBigInt.prototype.modPow = function(e, m) {
  148. return this ** e % m;
  149. };
  150. wBigInt.prototype.greaterOrEquals = function(b) {
  151. return this >= b;
  152. };
  153. wBigInt.prototype.greater = function(b) {
  154. return this > b;
  155. };
  156. wBigInt.prototype.gt = wBigInt.prototype.greater;
  157. wBigInt.prototype.lesserOrEquals = function(b) {
  158. return this <= b;
  159. };
  160. wBigInt.prototype.lesser = function(b) {
  161. return this < b;
  162. };
  163. wBigInt.prototype.lt = wBigInt.prototype.lesser;
  164. wBigInt.prototype.equals = function(b) {
  165. return this == b;
  166. };
  167. wBigInt.prototype.eq = wBigInt.prototype.equals;
  168. wBigInt.prototype.neq = function(b) {
  169. return this != b;
  170. };
  171. } else {
  172. var oldProto = bigInt.prototype;
  173. wBigInt = function(a) {
  174. if ((typeof a == "string") && (a.slice(0,2) == "0x")) {
  175. return bigInt(a.slice(2), 16);
  176. } else {
  177. return bigInt(a);
  178. }
  179. };
  180. wBigInt.one = bigInt.one;
  181. wBigInt.zero = bigInt.zero;
  182. wBigInt.prototype = oldProto;
  183. // Affine
  184. wBigInt.genAffine = (q) => {
  185. const nq = wBigInt.zero.minus(q);
  186. return (a) => {
  187. let aux = a;
  188. if (aux.isNegative()) {
  189. if (aux.lesserOrEquals(nq)) {
  190. aux = aux.mod(q);
  191. }
  192. if (aux.isNegative()) {
  193. aux = aux.add(q);
  194. }
  195. } else {
  196. if (aux.greaterOrEquals(q)) {
  197. aux = aux.mod(q);
  198. }
  199. }
  200. return aux;
  201. };
  202. };
  203. // Inverse
  204. wBigInt.genInverse = (q) => {
  205. return (a) => a.affine(q).modInv(q);
  206. };
  207. // Add
  208. wBigInt.genAdd = (q) => {
  209. if (q) {
  210. return (a,b) => {
  211. const r = a.add(b);
  212. return r.greaterOrEquals(q) ? r.minus(q) : r;
  213. };
  214. } else {
  215. return (a,b) => a.add(b);
  216. }
  217. };
  218. // Sub
  219. wBigInt.genSub = (q) => {
  220. if (q) {
  221. return (a,b) => a.greaterOrEquals(b) ? a.minus(b) : a.minus(b).add(q);
  222. } else {
  223. return (a,b) => a.minus(b);
  224. }
  225. };
  226. wBigInt.genNeg = (q) => {
  227. if (q) {
  228. return (a) => a.isZero() ? a : q.minus(a);
  229. } else {
  230. return (a) => wBigInt.zero.minus(a);
  231. }
  232. };
  233. // Mul
  234. wBigInt.genMul = (q) => {
  235. if (q) {
  236. return (a,b) => a.times(b).mod(q);
  237. } else {
  238. return (a,b) => a.times(b);
  239. }
  240. };
  241. // Shr
  242. wBigInt.genShr = () => {
  243. return (a,b) => a.shiftRight(wBigInt(b).value);
  244. };
  245. // Shr
  246. wBigInt.genShl = (q) => {
  247. if (q) {
  248. return (a,b) => a.shiftLeft(wBigInt(b).value).mod(q);
  249. } else {
  250. return (a,b) => a.shiftLeft(wBigInt(b).value);
  251. }
  252. };
  253. // Square
  254. wBigInt.genSquare = (q) => {
  255. if (q) {
  256. return (a) => a.square().mod(q);
  257. } else {
  258. return (a) => a.square();
  259. }
  260. };
  261. // Double
  262. wBigInt.genDouble = (q) => {
  263. if (q) {
  264. return (a) => a.add(a).mod(q);
  265. } else {
  266. return (a) => a.add(a);
  267. }
  268. };
  269. // Equals
  270. wBigInt.genEquals = (q) => {
  271. if (q) {
  272. return (a,b) => a.affine(q).equals(b.affine(q));
  273. } else {
  274. return (a,b) => a.equals(b);
  275. }
  276. };
  277. // IsZero
  278. wBigInt.genIsZero = (q) => {
  279. if (q) {
  280. return (a) => (a.affine(q).isZero());
  281. } else {
  282. return (a) => a.isZero();
  283. }
  284. };
  285. }
  286. wBigInt.affine = function(a, q) {
  287. return wBigInt.genAffine(q)(a);
  288. };
  289. wBigInt.prototype.affine = function (q) {
  290. return wBigInt.affine(this, q);
  291. };
  292. wBigInt.inverse = function(a, q) {
  293. return wBigInt.genInverse(q)(a);
  294. };
  295. wBigInt.prototype.inverse = function (q) {
  296. return wBigInt.genInverse(q)(this);
  297. };
  298. wBigInt.add = function(a, b, q) {
  299. return wBigInt.genAdd(q)(a,b);
  300. };
  301. wBigInt.prototype.add = function (a, q) {
  302. return wBigInt.genAdd(q)(this, a);
  303. };
  304. wBigInt.sub = function(a, b, q) {
  305. return wBigInt.genSub(q)(a,b);
  306. };
  307. wBigInt.prototype.sub = function (a, q) {
  308. return wBigInt.genSub(q)(this, a);
  309. };
  310. wBigInt.neg = function(a, q) {
  311. return wBigInt.genNeg(q)(a);
  312. };
  313. wBigInt.prototype.neg = function (q) {
  314. return wBigInt.genNeg(q)(this);
  315. };
  316. wBigInt.mul = function(a, b, q) {
  317. return wBigInt.genMul(q)(a,b);
  318. };
  319. wBigInt.prototype.mul = function (a, q) {
  320. return wBigInt.genMul(q)(this, a);
  321. };
  322. wBigInt.shr = function(a, b, q) {
  323. return wBigInt.genShr(q)(a,b);
  324. };
  325. wBigInt.prototype.shr = function (a, q) {
  326. return wBigInt.genShr(q)(this, a);
  327. };
  328. wBigInt.shl = function(a, b, q) {
  329. return wBigInt.genShl(q)(a,b);
  330. };
  331. wBigInt.prototype.shl = function (a, q) {
  332. return wBigInt.genShl(q)(this, a);
  333. };
  334. wBigInt.equals = function(a, b, q) {
  335. return wBigInt.genEquals(q)(a,b);
  336. };
  337. wBigInt.prototype.equals = function (a, q) {
  338. return wBigInt.genEquals(q)(this, a);
  339. };
  340. wBigInt.square = function(a, q) {
  341. return wBigInt.genSquare(q)(a);
  342. };
  343. wBigInt.prototype.square = function (q) {
  344. return wBigInt.genSquare(q)(this);
  345. };
  346. wBigInt.double = function(a, q) {
  347. return wBigInt.genDouble(q)(a);
  348. };
  349. wBigInt.prototype.double = function (q) {
  350. return wBigInt.genDouble(q)(this);
  351. };
  352. wBigInt.isZero = function(a, q) {
  353. return wBigInt.genIsZero(q)(a);
  354. };
  355. wBigInt.prototype.isZero = function (q) {
  356. return wBigInt.genIsZero(q)(this);
  357. };
  358. module.exports = wBigInt;