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.

127 lines
3.3 KiB

  1. const bigInt = require("big-integer");
  2. const ZqField = require("ffjavascript").ZqField;
  3. const utils = require("./utils.js");
  4. exports.addPoint = addPoint;
  5. exports.mulPointEscalar = mulPointEscalar;
  6. exports.inCurve = inCurve;
  7. exports.inSubgroup = inSubgroup;
  8. exports.packPoint = packPoint;
  9. exports.unpackPoint = unpackPoint;
  10. exports.Generator = [
  11. bigInt("995203441582195749578291179787384436505546430278305826713579947235728471134"),
  12. bigInt("5472060717959818805561601436314318772137091100104008585924551046643952123905")
  13. ];
  14. exports.Base8 = [
  15. bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
  16. bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")
  17. ];
  18. exports.order = bigInt("21888242871839275222246405745257275088614511777268538073601725287587578984328");
  19. exports.subOrder = exports.order.shiftRight(3);
  20. exports.p = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
  21. exports.A = bigInt("168700");
  22. exports.D = bigInt("168696");
  23. function addPoint(a,b) {
  24. const F = new ZqField(exports.p);
  25. const res = [];
  26. /* does the equivalent of:
  27. 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);
  28. 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);
  29. */
  30. const beta = F.mul(a[0],b[1]);
  31. const gamma = F.mul(a[1],b[0]);
  32. const delta = F.mul(
  33. F.sub(a[1], F.mul(exports.A, a[0])),
  34. F.add(b[0], b[1])
  35. );
  36. const tau = F.mul(beta, gamma);
  37. const dtau = F.mul(exports.D, tau);
  38. res[0] = F.div(
  39. F.add(beta, gamma),
  40. F.add(bigInt.one, dtau)
  41. );
  42. res[1] = F.div(
  43. F.add(delta, F.sub(F.mul(exports.A,beta), gamma)),
  44. F.sub(bigInt.one, dtau)
  45. );
  46. return res;
  47. }
  48. function mulPointEscalar(base, e) {
  49. let res = [bigInt("0"),bigInt("1")];
  50. let rem = bigInt(e);
  51. let exp = base;
  52. while (! rem.isZero()) {
  53. if (rem.isOdd()) {
  54. res = addPoint(res, exp);
  55. }
  56. exp = addPoint(exp, exp);
  57. rem = rem.shiftRight(1);
  58. }
  59. return res;
  60. }
  61. function inSubgroup(P) {
  62. if (!inCurve(P)) return false;
  63. const res= mulPointEscalar(P, exports.subOrder);
  64. return (res[0].equals(bigInt(0))) && (res[1].equals(bigInt(1)));
  65. }
  66. function inCurve(P) {
  67. const F = new ZqField(exports.p);
  68. const x2 = F.square(P[0]);
  69. const y2 = F.square(P[1]);
  70. if (!F.eq(
  71. F.add(F.mul(exports.A, x2), y2),
  72. F.add(F.one, F.mul(F.mul(x2, y2), exports.D)))) return false;
  73. return true;
  74. }
  75. function packPoint(P) {
  76. const buff = utils.leInt2Buff(P[1], 32);
  77. if (P[0].greater(exports.p.shiftRight(1))) {
  78. buff[31] = buff[31] | 0x80;
  79. }
  80. return buff;
  81. }
  82. function unpackPoint(_buff) {
  83. const F = new ZqField(exports.p);
  84. const buff = Buffer.from(_buff);
  85. let sign = false;
  86. const P = new Array(2);
  87. if (buff[31] & 0x80) {
  88. sign = true;
  89. buff[31] = buff[31] & 0x7F;
  90. }
  91. P[1] = utils.leBuff2int(buff);
  92. if (P[1].greaterOrEquals(exports.p)) return null;
  93. const y2 = F.square(P[1]);
  94. let x = F.sqrt(F.div(
  95. F.sub(F.one, y2),
  96. F.sub(exports.A, F.mul(exports.D, y2))));
  97. if (x == null) return null;
  98. if (sign) x = F.neg(x);
  99. P[0] = x;
  100. return P;
  101. }