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.

111 lines
3.0 KiB

  1. const bn128 = require("snarkjs").bn128;
  2. const bigInt = require("snarkjs").bigInt;
  3. const babyJub = require("./babyjub");
  4. const createBlakeHash = require("blake-hash");
  5. const GENPOINT_PREFIX = "PedersenGenerator";
  6. const windowSize = 4;
  7. const nWindowsPerSegment = 50;
  8. exports.hash = pedersenHash;
  9. exports.getBasePoint = getBasePoint;
  10. function pedersenHash(msg) {
  11. const bitsPerSegment = windowSize*nWindowsPerSegment;
  12. const bits = buffer2bits(msg);
  13. const nSegments = Math.floor((bits.length - 1)/(windowSize*nWindowsPerSegment)) +1;
  14. let accP = [bigInt.zero,bigInt.one];
  15. for (let s=0; s<nSegments; s++) {
  16. let nWindows;
  17. if (s == nSegments-1) {
  18. nWindows = Math.floor(((bits.length - (nSegments - 1)*bitsPerSegment) - 1) / windowSize) +1;
  19. } else {
  20. nWindows = nWindowsPerSegment;
  21. }
  22. let escalar = bigInt.zero;
  23. let exp = bigInt.one;
  24. for (let w=0; w<nWindows; w++) {
  25. let o = s*bitsPerSegment + w*windowSize;
  26. let acc = bigInt.one;
  27. for (let b=0; ((b<windowSize-1)&&(o<bits.length)) ; b++) {
  28. if (bits[o]) {
  29. acc = acc.add( bigInt.one.shl(b) );
  30. }
  31. o++;
  32. }
  33. if (o<bits.length) {
  34. if (bits[o]) {
  35. acc = acc.neg();
  36. }
  37. o++;
  38. }
  39. escalar = escalar.add(acc.mul(exp));
  40. exp = exp.shl(windowSize+1);
  41. }
  42. if (escalar.lesser(bigInt.zero)) {
  43. escalar = babyJub.subOrder.add(escalar);
  44. }
  45. accP = babyJub.addPoint(accP, babyJub.mulPointEscalar(getBasePoint(s), escalar));
  46. }
  47. return babyJub.packPoint(accP);
  48. }
  49. let bases = [];
  50. function getBasePoint(pointIdx) {
  51. if (pointIdx<bases.length) return bases[pointIdx];
  52. let p= null;
  53. let tryIdx = 0;
  54. while (p==null) {
  55. const S = GENPOINT_PREFIX + "_" + padLeftZeros(pointIdx, 32) + "_" + padLeftZeros(tryIdx, 32);
  56. const h = createBlakeHash("blake256").update(S).digest();
  57. h[31] = h[31] & 0xBF; // Set 255th bit to 0 (256th is the signal and 254th is the last possible bit to 1)
  58. p = babyJub.unpackPoint(h);
  59. tryIdx++;
  60. }
  61. const p8 = babyJub.mulPointEscalar(p, 8);
  62. if (!babyJub.inSubgroup(p8)) {
  63. throw new Error("Point not in curve");
  64. }
  65. bases[pointIdx] = p8;
  66. return p8;
  67. }
  68. function padLeftZeros(idx, n) {
  69. let sidx = "" + idx;
  70. while (sidx.length<n) sidx = "0"+sidx;
  71. return sidx;
  72. }
  73. /*
  74. Input a buffer
  75. Returns an array of booleans. 0 is LSB of first byte and so on.
  76. */
  77. function buffer2bits(buff) {
  78. const res = new Array(buff.length*8);
  79. for (let i=0; i<buff.length; i++) {
  80. const b = buff[i];
  81. res[i*8] = b & 0x01;
  82. res[i*8+1] = b & 0x02;
  83. res[i*8+2] = b & 0x04;
  84. res[i*8+3] = b & 0x08;
  85. res[i*8+4] = b & 0x10;
  86. res[i*8+5] = b & 0x20;
  87. res[i*8+6] = b & 0x40;
  88. res[i*8+7] = b & 0x80;
  89. }
  90. return res;
  91. }