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.

164 lines
7.2 KiB

  1. const chai = require("chai");
  2. const bigInt = require("big-integer");
  3. const babyjub = require("../src/babyjub.js");
  4. const assert = chai.assert;
  5. // const bigInt = require("big-integer");
  6. describe("Baby Jub js test", function () {
  7. this.timeout(100000);
  8. it("Should add point (0,1) and (0,1)", () => {
  9. const p1 = [
  10. bigInt(0),
  11. bigInt(1)];
  12. const p2 = [
  13. bigInt(0),
  14. bigInt(1)
  15. ];
  16. const out = babyjub.addPoint(p1, p2);
  17. assert(out[0].equals(0));
  18. assert(out[1].equals(1));
  19. });
  20. it("Should base be 8*generator", () => {
  21. let res;
  22. res = babyjub.addPoint(babyjub.Generator, babyjub.Generator);
  23. res = babyjub.addPoint(res, res);
  24. res = babyjub.addPoint(res, res);
  25. assert(res[0].equals(babyjub.Base8[0]));
  26. assert(res[1].equals(babyjub.Base8[1]));
  27. });
  28. it("Should add 2 same numbers", () => {
  29. const p1 = [
  30. bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
  31. bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
  32. ];
  33. const p2 = [
  34. bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
  35. bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
  36. ];
  37. const out = babyjub.addPoint(p1, p2);
  38. assert(out[0].equals(bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365")));
  39. assert(out[1].equals(bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889")));
  40. });
  41. it("Should add 2 different numbers", () => {
  42. const p1 = [
  43. bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
  44. bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
  45. ];
  46. const p2 = [
  47. bigInt("16540640123574156134436876038791482806971768689494387082833631921987005038935"),
  48. bigInt("20819045374670962167435360035096875258406992893633759881276124905556507972311"),
  49. ];
  50. const out = babyjub.addPoint(p1, p2);
  51. assert(out[0].equals(bigInt("7916061937171219682591368294088513039687205273691143098332585753343424131937")));
  52. assert(out[1].equals(bigInt("14035240266687799601661095864649209771790948434046947201833777492504781204499")));
  53. });
  54. it("should mulPointEscalar 0", () => {
  55. const p = [
  56. bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
  57. bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
  58. ];
  59. const r = babyjub.mulPointEscalar(p, bigInt("3"));
  60. let r2 = babyjub.addPoint(p, p);
  61. r2 = babyjub.addPoint(r2, p);
  62. assert.equal(r2[0].toString(), r[0].toString());
  63. assert.equal(r2[1].toString(), r[1].toString());
  64. assert.equal(r[0].toString(), "19372461775513343691590086534037741906533799473648040012278229434133483800898");
  65. assert.equal(r[1].toString(), "9458658722007214007257525444427903161243386465067105737478306991484593958249");
  66. });
  67. it("should mulPointEscalar 1", () => {
  68. const p = [
  69. bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
  70. bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
  71. ];
  72. const r = babyjub.mulPointEscalar(p, bigInt("14035240266687799601661095864649209771790948434046947201833777492504781204499"));
  73. assert.equal(r[0].toString(), "17070357974431721403481313912716834497662307308519659060910483826664480189605");
  74. assert.equal(r[1].toString(), "4014745322800118607127020275658861516666525056516280575712425373174125159339");
  75. });
  76. it("should mulPointEscalar 2", () => {
  77. const p = [
  78. bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
  79. bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889"),
  80. ];
  81. const r = babyjub.mulPointEscalar(p, bigInt("20819045374670962167435360035096875258406992893633759881276124905556507972311"));
  82. assert.equal(r[0].toString(), "13563888653650925984868671744672725781658357821216877865297235725727006259983");
  83. assert.equal(r[1].toString(), "8442587202676550862664528699803615547505326611544120184665036919364004251662");
  84. });
  85. it("should inCurve 1", () => {
  86. const p = [
  87. bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
  88. bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
  89. ];
  90. assert(babyjub.inCurve(p));
  91. });
  92. it("should inCurve 2", () => {
  93. const p = [
  94. bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
  95. bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889"),
  96. ];
  97. assert(babyjub.inCurve(p));
  98. });
  99. it("should inSubgroup 1", () => {
  100. const p = [
  101. bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
  102. bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
  103. ];
  104. assert(babyjub.inSubgroup(p));
  105. });
  106. it("should inSubgroup 2", () => {
  107. const p = [
  108. bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
  109. bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889"),
  110. ];
  111. assert(babyjub.inSubgroup(p));
  112. });
  113. it("should packPoint - unpackPoint 1", () => {
  114. const p = [
  115. bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
  116. bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
  117. ];
  118. const buf = babyjub.packPoint(p);
  119. assert.equal(buf.toString("hex"), "53b81ed5bffe9545b54016234682e7b2f699bd42a5e9eae27ff4051bc698ce85");
  120. const p2 = babyjub.unpackPoint(buf);
  121. assert.equal(p2[0].toString(), "17777552123799933955779906779655732241715742912184938656739573121738514868268");
  122. assert.equal(p2[1].toString(), "2626589144620713026669568689430873010625803728049924121243784502389097019475");
  123. });
  124. it("should packPoint - unpackPoint 2", () => {
  125. const p = [
  126. bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
  127. bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889"),
  128. ];
  129. const buf = babyjub.packPoint(p);
  130. assert.equal(buf.toString("hex"), "e114eb17eddf794f063a68fecac515e3620e131976108555735c8b0773929709");
  131. const p2 = babyjub.unpackPoint(buf);
  132. assert.equal(p2[0].toString(), "6890855772600357754907169075114257697580319025794532037257385534741338397365");
  133. assert.equal(p2[1].toString(), "4338620300185947561074059802482547481416142213883829469920100239455078257889");
  134. });
  135. });