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.

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