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.

84 lines
1.8 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 getPoint(S) {
  6. const F = bn128.Fr;
  7. const h = createBlakeHash("blake256").update(S).digest();
  8. assert(h.length == 32);
  9. let sign = false;
  10. if (h[31] & 0x80) {
  11. h[31] = h[31] & 0x7F;
  12. sign = true;
  13. }
  14. let x = bigInt(0);
  15. for (let i=0; i<32; i++) {
  16. x = x.shl(8);
  17. x = x.add(bigInt(h[i]));
  18. }
  19. const a = bigInt("168700");
  20. const d = bigInt("168696");
  21. const x2 = F.square(x);
  22. let y = F.sqrt(F.div(
  23. F.sub(F.one, F.mul(a, x2)),
  24. F.sub(F.one, F.mul(d, x2))));
  25. if (y == null) return null;
  26. if (sign) y = F.neg(y);
  27. return [bn128.Fr.affine(x), bn128.Fr.affine(y)];
  28. }
  29. function generatePoint(S) {
  30. let p= null;
  31. let idx = 0;
  32. while (p==null) {
  33. let sidx = "" + idx;
  34. while (sidx.length<16) sidx = "0"+sidx;
  35. p = getPoint(S+"_"+sidx);
  36. idx++;
  37. }
  38. assert(inCurve(p));
  39. return p;
  40. }
  41. function inCurve(p) {
  42. const F = bn128.Fr;
  43. const a = bigInt("168700");
  44. const d = bigInt("168696");
  45. const x2 = F.square(p[0]);
  46. const y2 = F.square(p[1]);
  47. return F.equals(
  48. F.add(F.mul(a, x2), y2),
  49. F.add(F.one, F.mul(F.mul(x2, y2), d)));
  50. }
  51. const g = [
  52. bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
  53. bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475")];
  54. if (!inCurve(g)) {
  55. throw new Error("Generator not In curve -> Some thing goes wrong...");
  56. }
  57. for (let i=0; i<25; i++) {
  58. let S = "" +i;
  59. while (S.length<16) S = "0"+S;
  60. const P = generatePoint("Iden3_PedersenGenerator_"+S);
  61. console.log(`[${P[0].toString()}, ${P[1].toString()}]`);
  62. }