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.

83 lines
1.7 KiB

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