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.

105 lines
2.6 KiB

  1. const bn128 = require("snarkjs").bn128;
  2. const bigInt = require("snarkjs").bigInt;
  3. exports.addPoint = addPoint;
  4. exports.mulPointEscalar = mulPointEscalar;
  5. exports.inCurve = inCurve;
  6. exports.inSubgroup = inSubgroup;
  7. exports.packPoint = packPoint;
  8. exports.unpackPoint = unpackPoint;
  9. exports.Base8 = [
  10. bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
  11. bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475")
  12. ];
  13. exports.order = bigInt("21888242871839275222246405745257275088614511777268538073601725287587578984328");
  14. exports.subOrder = exports.order.shr(3);
  15. exports.p = bn128.r;
  16. function addPoint(a,b) {
  17. const q = bn128.r;
  18. const cta = bigInt("168700");
  19. const d = bigInt("168696");
  20. const res = [];
  21. 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);
  22. 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);
  23. return res;
  24. }
  25. function mulPointEscalar(base, e) {
  26. let res = [bigInt("0"),bigInt("1")];
  27. let rem = bigInt(e);
  28. let exp = base;
  29. while (! rem.isZero()) {
  30. if (rem.isOdd()) {
  31. res = addPoint(res, exp);
  32. }
  33. exp = addPoint(exp, exp);
  34. rem = rem.shr(1);
  35. }
  36. return res;
  37. }
  38. function inSubgroup(P) {
  39. if (!inCurve(P)) return false;
  40. const res= mulPointEscalar(P, exports.subOrder);
  41. return (res[0].equals(bigInt(0))) && (res[1].equals(bigInt(1)));
  42. }
  43. function inCurve(P) {
  44. const F = bn128.Fr;
  45. const a = bigInt("168700");
  46. const d = bigInt("168696");
  47. const x2 = F.square(P[0]);
  48. const y2 = F.square(P[1]);
  49. if (!F.equals(
  50. F.add(F.mul(a, x2), y2),
  51. F.add(F.one, F.mul(F.mul(x2, y2), d)))) return false;
  52. return true;
  53. }
  54. function packPoint(P) {
  55. const buff = bigInt.leInt2Buff(P[1], 32);
  56. if (P[0].greater(exports.p.shr(1))) {
  57. buff[31] = buff[31] | 0x80;
  58. }
  59. return buff;
  60. }
  61. function unpackPoint(_buff) {
  62. const F = bn128.Fr;
  63. const buff = Buffer.from(_buff);
  64. let sign = false;
  65. const P = new Array(2);
  66. if (buff[31] & 0x80) {
  67. sign = true;
  68. buff[31] = buff[31] & 0x7F;
  69. }
  70. P[1] = bigInt.leBuff2int(buff);
  71. if (P[1].greaterOrEquals(exports.p)) return null;
  72. const a = bigInt("168700");
  73. const d = bigInt("168696");
  74. const y2 = F.square(P[1]);
  75. let x = F.sqrt(F.div(
  76. F.sub(F.one, y2),
  77. F.sub(a, F.mul(d, y2))));
  78. if (x == null) return null;
  79. if (sign) x = F.neg(x);
  80. P[0] = F.affine(x);
  81. return P;
  82. }