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.

156 lines
7.2 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 add 2 same numbers", () => {
  23. const p1 = [
  24. snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
  25. snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
  26. ];
  27. const p2 = [
  28. snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
  29. snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
  30. ];
  31. const out = babyjub.addPoint(p1, p2)
  32. assert(out[0].equals(snarkjs.bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365")));
  33. assert(out[1].equals(snarkjs.bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889")));
  34. });
  35. it("Should add 2 different numbers", () => {
  36. const p1 = [
  37. snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
  38. snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
  39. ];
  40. const p2 = [
  41. snarkjs.bigInt("16540640123574156134436876038791482806971768689494387082833631921987005038935"),
  42. snarkjs.bigInt("20819045374670962167435360035096875258406992893633759881276124905556507972311"),
  43. ];
  44. const out = babyjub.addPoint(p1, p2)
  45. assert(out[0].equals(snarkjs.bigInt("7916061937171219682591368294088513039687205273691143098332585753343424131937")));
  46. assert(out[1].equals(snarkjs.bigInt("14035240266687799601661095864649209771790948434046947201833777492504781204499")));
  47. });
  48. it("should mulPointEscalar 0", () => {
  49. const p = [
  50. snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
  51. snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
  52. ];
  53. const r = babyjub.mulPointEscalar(p, snarkjs.bigInt("3"));
  54. let r2 = babyjub.addPoint(p, p);
  55. r2 = babyjub.addPoint(r2, p);
  56. assert.equal(r2[0].toString(), r[0].toString());
  57. assert.equal(r2[1].toString(), r[1].toString());
  58. assert.equal(r[0].toString(), "19372461775513343691590086534037741906533799473648040012278229434133483800898");
  59. assert.equal(r[1].toString(), "9458658722007214007257525444427903161243386465067105737478306991484593958249");
  60. });
  61. it("should mulPointEscalar 1", () => {
  62. const p = [
  63. snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
  64. snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
  65. ];
  66. const r = babyjub.mulPointEscalar(p, snarkjs.bigInt("14035240266687799601661095864649209771790948434046947201833777492504781204499"));
  67. assert.equal(r[0].toString(), "17070357974431721403481313912716834497662307308519659060910483826664480189605");
  68. assert.equal(r[1].toString(), "4014745322800118607127020275658861516666525056516280575712425373174125159339");
  69. });
  70. it("should mulPointEscalar 2", () => {
  71. const p = [
  72. snarkjs.bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
  73. snarkjs.bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889"),
  74. ];
  75. const r = babyjub.mulPointEscalar(p, snarkjs.bigInt("20819045374670962167435360035096875258406992893633759881276124905556507972311"));
  76. assert.equal(r[0].toString(), "13563888653650925984868671744672725781658357821216877865297235725727006259983");
  77. assert.equal(r[1].toString(), "8442587202676550862664528699803615547505326611544120184665036919364004251662");
  78. });
  79. it("should inCurve 1", () => {
  80. const p = [
  81. snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
  82. snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
  83. ];
  84. assert(babyjub.inCurve(p));
  85. });
  86. it("should inCurve 2", () => {
  87. const p = [
  88. snarkjs.bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
  89. snarkjs.bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889"),
  90. ];
  91. assert(babyjub.inCurve(p));
  92. });
  93. it("should inSubgroup 1", () => {
  94. const p = [
  95. snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
  96. snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
  97. ];
  98. assert(babyjub.inSubgroup(p));
  99. });
  100. it("should inSubgroup 2", () => {
  101. const p = [
  102. snarkjs.bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
  103. snarkjs.bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889"),
  104. ];
  105. assert(babyjub.inSubgroup(p));
  106. });
  107. it("should packPoint - unpackPoint 1", () => {
  108. const p = [
  109. snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
  110. snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
  111. ];
  112. const buf = babyjub.packPoint(p);
  113. assert.equal(buf.toString('hex'), '53b81ed5bffe9545b54016234682e7b2f699bd42a5e9eae27ff4051bc698ce85');
  114. const p2 = babyjub.unpackPoint(buf);
  115. assert.equal(p2[0].toString(), "17777552123799933955779906779655732241715742912184938656739573121738514868268");
  116. assert.equal(p2[1].toString(), "2626589144620713026669568689430873010625803728049924121243784502389097019475");
  117. });
  118. it("should packPoint - unpackPoint 2", () => {
  119. const p = [
  120. snarkjs.bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365"),
  121. snarkjs.bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889"),
  122. ];
  123. const buf = babyjub.packPoint(p);
  124. assert.equal(buf.toString('hex'), 'e114eb17eddf794f063a68fecac515e3620e131976108555735c8b0773929709');
  125. const p2 = babyjub.unpackPoint(buf);
  126. assert.equal(p2[0].toString(), "6890855772600357754907169075114257697580319025794532037257385534741338397365");
  127. assert.equal(p2[1].toString(), "4338620300185947561074059802482547481416142213883829469920100239455078257889");
  128. });
  129. });