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.

166 lines
7.5 KiB

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