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.

130 lines
3.0 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, q);
  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 q = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
  65. const r = bigInt("21888242871839275222246405745257275088614511777268538073601725287587578984328").shr(3);
  66. function isLowGrade(p, q) {
  67. const res= mulPointEscalar(p, r, q);
  68. return (res[0].equals(bigInt(0))) && (res[1].equals(bigInt(1)));
  69. }
  70. function inCurve(p) {
  71. const F = bn128.Fr;
  72. const a = bigInt("168700");
  73. const d = bigInt("168696");
  74. const x2 = F.square(p[0]);
  75. const y2 = F.square(p[1]);
  76. if (!F.equals(
  77. F.add(F.mul(a, x2), y2),
  78. F.add(F.one, F.mul(F.mul(x2, y2), d)))) return false;
  79. if (!isLowGrade(p, q)) return false;
  80. return true;
  81. }
  82. const g = [
  83. bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
  84. bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475")];
  85. if (!inCurve(g)) {
  86. throw new Error("Generator not In curve -> Some thing goes wrong...");
  87. }
  88. for (let i=0; i<25; i++) {
  89. let S = "" +i;
  90. while (S.length<16) S = "0"+S;
  91. const P = generatePoint("Iden3_PedersenGenerator_"+S);
  92. console.log(`[${P[0].toString()}, ${P[1].toString()}]`);
  93. }