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.

80 lines
2.8 KiB

5 years ago
  1. /*
  2. Copyright 2019 0KIMS association.
  3. This file is part of websnark (Web Assembly zkSnark Prover).
  4. websnark is a free software: you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. websnark is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  11. License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with websnark. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. const bigInt = require("big-integer");
  16. const buildF1m =require("./build_f1m.js");
  17. module.exports = function buildF1(module, _q, _prefix, _f1mPrefix, _intPrefix) {
  18. const q = bigInt(_q);
  19. const n64 = Math.floor((q.minus(1).bitLength() - 1)/64) +1;
  20. const n8 = n64*8;
  21. const prefix = _prefix || "f1";
  22. if (module.modules[prefix]) return prefix; // already builded
  23. module.modules[prefix] = {
  24. n64: n64
  25. };
  26. const intPrefix = _intPrefix || "int";
  27. const f1mPrefix = buildF1m(module, q, _f1mPrefix, intPrefix);
  28. const pR2 = module.modules[f1mPrefix].pR2;
  29. const pq = module.modules[f1mPrefix].pq;
  30. function buildMul() {
  31. const pAux1 = module.alloc(n8);
  32. const f = module.addFunction(prefix+ "_mul");
  33. f.addParam("x", "i32");
  34. f.addParam("y", "i32");
  35. f.addParam("r", "i32");
  36. const c = f.getCodeBuilder();
  37. f.addCode(c.call(f1mPrefix + "_mul", c.getLocal("x"), c.getLocal("y"), c.i32_const(pAux1)));
  38. f.addCode(c.call(f1mPrefix + "_mul", c.i32_const(pAux1), c.i32_const(pR2), c.getLocal("r")));
  39. }
  40. function buildInverse() {
  41. const f = module.addFunction(prefix+ "_inverse");
  42. f.addParam("x", "i32");
  43. f.addParam("r", "i32");
  44. const c = f.getCodeBuilder();
  45. f.addCode(c.call(intPrefix + "_inverseMod", c.getLocal("x"), c.i32_const(pq), c.getLocal("r")));
  46. }
  47. buildMul();
  48. buildInverse();
  49. module.exportFunction(f1mPrefix + "_add", prefix + "_add");
  50. module.exportFunction(f1mPrefix + "_sub", prefix + "_sub");
  51. module.exportFunction(f1mPrefix + "_neg", prefix + "_neg");
  52. module.exportFunction(prefix + "_mul");
  53. module.exportFunction(prefix + "_inverse");
  54. module.exportFunction(f1mPrefix + "_copy", prefix+"_copy");
  55. module.exportFunction(f1mPrefix + "_zero", prefix+"_zero");
  56. module.exportFunction(f1mPrefix + "_one", prefix+"_one");
  57. module.exportFunction(f1mPrefix + "_isZero", prefix+"_isZero");
  58. module.exportFunction(f1mPrefix + "_eq", prefix+"_eq");
  59. return prefix;
  60. };