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.

77 lines
2.1 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
  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. const crypto = require("crypto");
  16. const bigInt = require("./bigint");
  17. const fUtils = require("./futils.js");
  18. class ZqField {
  19. constructor(q) {
  20. this.q = q;
  21. this.zero = bigInt.zero;
  22. this.one = bigInt.one;
  23. this.add = bigInt.genAdd();
  24. this.double = bigInt.genDouble();
  25. this.sub = bigInt.genSub();
  26. this.neg = bigInt.genNeg();
  27. this.mul = bigInt.genMul(q);
  28. this.inverse = bigInt.genInverse(q);
  29. this.square = bigInt.genSquare(q);
  30. this.equals = bigInt.genEquals(q);
  31. this.affine = bigInt.genAffine(q);
  32. this.isZero = bigInt.genIsZero(q);
  33. this.two = this.add(this.one, this.one);
  34. this.twoinv = this.inverse(this.two);
  35. }
  36. copy(a) {
  37. return bigInt(a);
  38. }
  39. div(a, b) {
  40. return this.mul(a, this.inverse(b));
  41. }
  42. mulScalar(base, e) {
  43. return this.mul(base, bigInt(e));
  44. }
  45. exp(base, e) {
  46. return fUtils.exp(this, base, e);
  47. }
  48. toString(a) {
  49. const ca = this.affine(a);
  50. return `"0x${ca.toString(16)}"`;
  51. }
  52. random() {
  53. let res = bigInt(0);
  54. let n = bigInt(this.q);
  55. while (!n.isZero()) {
  56. res = res.shl(8).add(bigInt(crypto.randomBytes(1)[0]));
  57. n = n.shr(8);
  58. }
  59. return res;
  60. }
  61. }
  62. module.exports = ZqField;