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.

127 lines
2.9 KiB

  1. const bn128 = require("snarkjs").bn128;
  2. const bigInt = require("snarkjs").bigInt;
  3. const createBlakeHash = require("blake-hash");
  4. const assert = require("assert");
  5. function addPoint(a,b, q) {
  6. const cta = bigInt("168700");
  7. const d = bigInt("168696");
  8. const res = [];
  9. res[0] = bigInt((a[0]*b[1] + b[0]*a[1]) * bigInt(bigInt("1") + d*a[0]*b[0]*a[1]*b[1]).inverse(q)).affine(q);
  10. res[1] = bigInt((a[1]*b[1] - cta*a[0]*b[0]) * bigInt(bigInt("1") - d*a[0]*b[0]*a[1]*b[1]).inverse(q)).affine(q);
  11. return res;
  12. }
  13. function mulPointEscalar(base, e, q) {
  14. let res = [bigInt("0"),bigInt("1")];
  15. let rem = bigInt(e);
  16. let exp = base;
  17. while (! rem.isZero()) {
  18. if (rem.isOdd()) {
  19. res = addPoint(res, exp, q);
  20. }
  21. exp = addPoint(exp, exp, q);
  22. rem = rem.shr(1);
  23. }
  24. return res;
  25. }
  26. function getPoint(S) {
  27. const F = bn128.Fr;
  28. const h = createBlakeHash("blake256").update(S).digest();
  29. assert(h.length == 32);
  30. let sign = false;
  31. if (h[31] & 0x80) {
  32. h[31] = h[31] & 0x7F;
  33. sign = true;
  34. }
  35. let y = bigInt(0);
  36. for (let i=0; i<32; i++) {
  37. y = y.shl(8);
  38. y = y.add(bigInt(h[i]));
  39. }
  40. const a = bigInt("168700");
  41. const d = bigInt("168696");
  42. const y2 = F.square(y);
  43. let x = F.sqrt(F.div(
  44. F.sub(F.one, y2),
  45. F.sub(a, F.mul(d, y2))));
  46. if (x == null) return null;
  47. if (sign) x = F.neg(x);
  48. const p = [bn128.Fr.affine(x), bn128.Fr.affine(y)];
  49. const p8 =mulPointEscalar(p, 8, bn128.r);
  50. return p8;
  51. }
  52. function generatePoint(S) {
  53. let p= null;
  54. let idx = 0;
  55. while (p==null) {
  56. let sidx = "" + idx;
  57. while (sidx.length<16) sidx = "0"+sidx;
  58. p = getPoint(S+"_"+sidx);
  59. idx++;
  60. }
  61. assert(inCurve(p), "Point not in curve");
  62. return p;
  63. }
  64. const r = bigInt("21888242871839275222246405745257275088614511777268538073601725287587578984328").shr(3);
  65. function isLowGrade(p) {
  66. const res= mulPointEscalar(p, r, bn128.r);
  67. return (res[0].equals(bigInt(0))) && (res[1].equals(bigInt(1)));
  68. }
  69. function inCurve(p) {
  70. const F = bn128.Fr;
  71. const a = bigInt("168700");
  72. const d = bigInt("168696");
  73. const x2 = F.square(p[0]);
  74. const y2 = F.square(p[1]);
  75. if (!F.equals(
  76. F.add(F.mul(a, x2), y2),
  77. F.add(F.one, F.mul(F.mul(x2, y2), d)))) return false;
  78. if (!isLowGrade(p)) return false;
  79. return true;
  80. }
  81. const g = [
  82. bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
  83. bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475")];
  84. if (!inCurve(g)) {
  85. throw new Error("Generator not In curve -> Some thing goes wrong...");
  86. }
  87. for (let i=0; i<25; i++) {
  88. let S = "" +i;
  89. while (S.length<16) S = "0"+S;
  90. const P = generatePoint("Iden3_PedersenGenerator_"+S);
  91. console.log(`[${P[0].toString()}, ${P[1].toString()}]`);
  92. }