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.

55 lines
1.6 KiB

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. const bigInt = require("big-integer");
  16. module.exports.stringifyBigInts = stringifyBigInts;
  17. module.exports.unstringifyBigInts = unstringifyBigInts;
  18. function stringifyBigInts(o) {
  19. if ((typeof(o) == "bigint") || (o instanceof bigInt)) {
  20. return o.toString(10);
  21. } else if (Array.isArray(o)) {
  22. return o.map(stringifyBigInts);
  23. } else if (typeof o == "object") {
  24. const res = {};
  25. for (let k in o) {
  26. res[k] = stringifyBigInts(o[k]);
  27. }
  28. return res;
  29. } else {
  30. return o;
  31. }
  32. }
  33. function unstringifyBigInts(o) {
  34. if ((typeof(o) == "string") && (/^[0-9]+$/.test(o) )) {
  35. return bigInt(o);
  36. } else if (Array.isArray(o)) {
  37. return o.map(unstringifyBigInts);
  38. } else if (typeof o == "object") {
  39. const res = {};
  40. for (let k in o) {
  41. res[k] = unstringifyBigInts(o[k]);
  42. }
  43. return res;
  44. } else {
  45. return o;
  46. }
  47. }