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.

86 lines
2.3 KiB

  1. const Scalar = require("ffjavascript").Scalar
  2. const Web3Utils = require("web3-utils");
  3. const ZqField = require("ffjavascript").ZqField;
  4. const F = new ZqField(Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617"));
  5. const SEED = "mimcsponge";
  6. const NROUNDS = 220;
  7. exports.getIV = (seed) => {
  8. if (typeof seed === "undefined") seed = SEED;
  9. const c = Web3Utils.keccak256(seed+"_iv");
  10. const cn = Scalar.fromString(Web3Utils.toBN(c).toString());
  11. const iv = cn.mod(F.p);
  12. return iv;
  13. };
  14. exports.getConstants = (seed, nRounds) => {
  15. if (typeof seed === "undefined") seed = SEED;
  16. if (typeof nRounds === "undefined") nRounds = NROUNDS;
  17. const cts = new Array(nRounds);
  18. let c = Web3Utils.keccak256(SEED);
  19. for (let i=1; i<nRounds; i++) {
  20. c = Web3Utils.keccak256(c);
  21. const n1 = Web3Utils.toBN(c).mod(Web3Utils.toBN(F.p.toString()));
  22. const c2 = Web3Utils.padLeft(Web3Utils.toHex(n1), 64);
  23. cts[i] = F.e(Web3Utils.toBN(c2).toString());
  24. }
  25. cts[0] = F.e(0);
  26. cts[cts.length - 1] = F.e(0);
  27. return cts;
  28. };
  29. const cts = exports.getConstants(SEED, NROUNDS);
  30. exports.hash = (_xL_in, _xR_in, _k) =>{
  31. let xL = F.e(_xL_in);
  32. let xR = F.e(_xR_in);
  33. const k = F.e(_k);
  34. for (let i=0; i<NROUNDS; i++) {
  35. const c = cts[i];
  36. const t = (i==0) ? F.add(xL, k) : F.add(F.add(xL, k), c);
  37. const xR_tmp = F.e(xR);
  38. if (i < (NROUNDS - 1)) {
  39. xR = xL;
  40. xL = F.add(xR_tmp, F.pow(t, 5));
  41. } else {
  42. xR = F.add(xR_tmp, F.pow(t, 5));
  43. }
  44. }
  45. return {
  46. xL: F.normalize(xL),
  47. xR: F.normalize(xR),
  48. };
  49. };
  50. exports.multiHash = (arr, key, numOutputs) => {
  51. if (typeof(numOutputs) === "undefined") {
  52. numOutputs = 1;
  53. }
  54. if (typeof(key) === "undefined") {
  55. key = F.zero;
  56. }
  57. let R = F.zero;
  58. let C = F.zero;
  59. for (let i=0; i<arr.length; i++) {
  60. R = F.add(R, F.e(arr[i]));
  61. const S = exports.hash(R, C, key);
  62. R = S.xL;
  63. C = S.xR;
  64. }
  65. let outputs = [R];
  66. for (let i=1; i < numOutputs; i++) {
  67. const S = exports.hash(R, C, key);
  68. R = S.xL;
  69. C = S.xR;
  70. outputs.push(R);
  71. }
  72. if (numOutputs == 1) {
  73. return F.normalize(outputs[0]);
  74. } else {
  75. return outputs.map(x => F.normalize(x));
  76. }
  77. };