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.

423 lines
9.0 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
  1. /*
  2. Copyright 2018 0kims association.
  3. This file is part of zksnark JavaScript library.
  4. zksnark JavaScript library 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. zksnark JavaScript library 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. zksnark JavaScript library. 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.valueOf == b.valueOf;
  166. };
  167. wBigInt.prototype.eq = wBigInt.prototype.equals;
  168. } else {
  169. wBigInt = bigInt;
  170. // Affine
  171. wBigInt.genAffine = (q) => {
  172. const nq = wBigInt.zero.minus(q);
  173. return (a) => {
  174. let aux = a;
  175. if (aux.isNegative()) {
  176. if (aux.lesserOrEquals(nq)) {
  177. aux = aux.mod(q);
  178. }
  179. if (aux.isNegative()) {
  180. aux = aux.add(q);
  181. }
  182. } else {
  183. if (aux.greaterOrEquals(q)) {
  184. aux = aux.mod(q);
  185. }
  186. }
  187. return aux;
  188. };
  189. };
  190. // Inverse
  191. wBigInt.genInverse = (q) => {
  192. return (a) => a.affine(q).modInv(q);
  193. };
  194. // Add
  195. wBigInt.genAdd = (q) => {
  196. if (q) {
  197. return (a,b) => {
  198. const r = a.add(b);
  199. return r.greaterOrEquals(q) ? r.minus(q) : r;
  200. };
  201. } else {
  202. return (a,b) => a.add(b);
  203. }
  204. };
  205. // Sub
  206. wBigInt.genSub = (q) => {
  207. if (q) {
  208. return (a,b) => a.greaterOrEquals(b) ? a.minus(b) : a.minus(b).add(q);
  209. } else {
  210. return (a,b) => a.minus(b);
  211. }
  212. };
  213. wBigInt.genNeg = (q) => {
  214. if (q) {
  215. return (a) => a.isZero() ? a : q.minus(a);
  216. } else {
  217. return (a) => wBigInt.zero.minus(a);
  218. }
  219. };
  220. // Mul
  221. wBigInt.genMul = (q) => {
  222. if (q) {
  223. return (a,b) => a.times(b).mod(q);
  224. } else {
  225. return (a,b) => a.times(b);
  226. }
  227. };
  228. // Shr
  229. wBigInt.genShr = () => {
  230. return (a,b) => a.shiftRight(wBigInt(b).value);
  231. };
  232. // Shr
  233. wBigInt.genShl = (q) => {
  234. if (q) {
  235. return (a,b) => a.shiftLeft(wBigInt(b).value).mod(q);
  236. } else {
  237. return (a,b) => a.shiftLeft(wBigInt(b).value);
  238. }
  239. };
  240. // Square
  241. wBigInt.genSquare = (q) => {
  242. if (q) {
  243. return (a) => a.square().mod(q);
  244. } else {
  245. return (a) => a.square();
  246. }
  247. };
  248. // Double
  249. wBigInt.genDouble = (q) => {
  250. if (q) {
  251. return (a) => a.add(a).mod(q);
  252. } else {
  253. return (a) => a.add(a);
  254. }
  255. };
  256. // Equals
  257. wBigInt.genEquals = (q) => {
  258. if (q) {
  259. return (a,b) => a.affine(q).equals(b.affine(q));
  260. } else {
  261. return (a,b) => a.equals(b);
  262. }
  263. };
  264. // IsZero
  265. wBigInt.genIsZero = (q) => {
  266. if (q) {
  267. return (a) => (a.affine(q).isZero());
  268. } else {
  269. return (a) => a.isZero();
  270. }
  271. };
  272. }
  273. wBigInt.affine = function(a, q) {
  274. return wBigInt.genAffine(q)(a);
  275. };
  276. wBigInt.prototype.affine = function (q) {
  277. return wBigInt.affine(this, q);
  278. };
  279. wBigInt.inverse = function(a, q) {
  280. return wBigInt.genInverse(q)(a);
  281. };
  282. wBigInt.prototype.inverse = function (q) {
  283. return wBigInt.genInverse(this, q);
  284. };
  285. wBigInt.add = function(a, b, q) {
  286. return wBigInt.genAdd(q)(a,b);
  287. };
  288. wBigInt.prototype.add = function (a, q) {
  289. return wBigInt.genAdd(q)(this, a);
  290. };
  291. wBigInt.sub = function(a, b, q) {
  292. return wBigInt.genSub(q)(a,b);
  293. };
  294. wBigInt.prototype.sub = function (a, q) {
  295. return wBigInt.genSub(q)(this, a);
  296. };
  297. wBigInt.neg = function(a, q) {
  298. return wBigInt.genNeg(q)(a);
  299. };
  300. wBigInt.prototype.neg = function (q) {
  301. return wBigInt.genNeg(q)(this);
  302. };
  303. wBigInt.mul = function(a, b, q) {
  304. return wBigInt.genMul(q)(a,b);
  305. };
  306. wBigInt.prototype.mul = function (a, q) {
  307. return wBigInt.genMul(q)(this, a);
  308. };
  309. wBigInt.shr = function(a, b, q) {
  310. return wBigInt.genShr(q)(a,b);
  311. };
  312. wBigInt.prototype.shr = function (a, q) {
  313. return wBigInt.genShr(q)(this, a);
  314. };
  315. wBigInt.shl = function(a, b, q) {
  316. return wBigInt.genShl(q)(a,b);
  317. };
  318. wBigInt.prototype.shl = function (a, q) {
  319. return wBigInt.genShl(q)(this, a);
  320. };
  321. wBigInt.equals = function(a, b, q) {
  322. return wBigInt.genEquals(q)(a,b);
  323. };
  324. wBigInt.prototype.equals = function (a, q) {
  325. return wBigInt.genEquals(q)(this, a);
  326. };
  327. wBigInt.square = function(a, q) {
  328. return wBigInt.genSquare(q)(a);
  329. };
  330. wBigInt.prototype.square = function (q) {
  331. return wBigInt.genSquare(q)(this);
  332. };
  333. wBigInt.double = function(a, q) {
  334. return wBigInt.genDouble(q)(a);
  335. };
  336. wBigInt.prototype.double = function (q) {
  337. return wBigInt.genDouble(q)(this);
  338. };
  339. wBigInt.isZero = function(a, q) {
  340. return wBigInt.genIsZero(q)(a);
  341. };
  342. wBigInt.prototype.isZero = function (q) {
  343. return wBigInt.genIsZero(q)(this);
  344. };
  345. module.exports = wBigInt;