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.

147 lines
4.5 KiB

5 years ago
  1. const assert = require("assert");
  2. const refBn128 = require("snarkjs").bn128;
  3. const refBigInt = require("snarkjs").bigInt;
  4. const buildBn128 = require("../index.js").buildBn128;
  5. describe("Basic tests for g1 in bn128", () => {
  6. it("It should do a basic point doubling G1", async () => {
  7. const bn128 = await buildBn128();
  8. const refD = refBn128.G1.double(refBn128.g1);
  9. const p1 = bn128.g1_allocPoint(refBn128.g1);
  10. bn128.g1_toMontgomery(p1, p1);
  11. bn128.g1_double(p1, p1);
  12. bn128.g1_fromMontgomery(p1, p1);
  13. const d = bn128.g1_getPoint(p1);
  14. for (let i=0; i<3; i++) {
  15. d[i] = refBigInt(d[i].toString());
  16. }
  17. assert(refBn128.G1.equals(d, refD));
  18. });
  19. it("It should add two points G1", async () => {
  20. const bn128 = await buildBn128();
  21. const refD = refBn128.G1.affine(refBn128.G1.mulScalar(refBn128.g1, 3));
  22. const p1 = bn128.g1_allocPoint(refBn128.g1);
  23. bn128.g1_toMontgomery(p1, p1);
  24. bn128.g1_double(p1, p1);
  25. const p2 = bn128.g1_allocPoint(refBn128.g1);
  26. bn128.g1_toMontgomery(p2, p2);
  27. bn128.g1_add(p1, p2, p2);
  28. bn128.g1_affine(p2, p2);
  29. bn128.g1_fromMontgomery(p2, p2);
  30. const d = bn128.g1_getPoint(p2);
  31. d[0] = refBigInt(d[0].toString());
  32. d[1] = refBigInt(d[1].toString());
  33. d[2] = refBigInt(d[2].toString());
  34. assert(d[0].equals(refD[0]));
  35. assert(d[1].equals(refD[1]));
  36. assert(d[2].equals(1));
  37. assert(refBn128.G1.equals(d, refD));
  38. });
  39. it("It should timesScalar G1", async () => {
  40. const bn128 = await buildBn128();
  41. const refD = refBn128.G1.mulScalar(refBn128.g1, 55);
  42. const p1 = bn128.g1_allocPoint(refBn128.g1);
  43. bn128.g1_toMontgomery(p1, p1);
  44. const s = bn128.allocInt(55);
  45. bn128.g1_timesScalar(p1, s, 32, p1);
  46. bn128.g1_fromMontgomery(p1, p1);
  47. const d = bn128.g1_getPoint(p1);
  48. for (let i=0; i<3; i++) {
  49. d[i] = refBigInt(d[i].toString());
  50. }
  51. assert(refBn128.G1.equals(d, refD));
  52. });
  53. it("G1n == 0", async () => {
  54. const bn128 = await buildBn128();
  55. const p1 = bn128.g1_allocPoint(refBn128.g1);
  56. bn128.g1_toMontgomery(p1, p1);
  57. const s = bn128.allocInt(bn128.r);
  58. bn128.g1_timesScalar(p1, s, 32, p1);
  59. bn128.g1_fromMontgomery(p1, p1);
  60. assert(bn128.g1_isZero(p1));
  61. });
  62. it("It should do a test", async () => {
  63. const bn128 = await buildBn128();
  64. const t = bn128.test_AddG1(100000);
  65. console.log(t);
  66. }).timeout(10000000);
  67. it("It should validate the test", async () => {
  68. const bn128 = await buildBn128();
  69. const refD = refBn128.G1.mulScalar(refBn128.g1, 100000);
  70. const p1 = bn128.g1_allocPoint(refBn128.g1);
  71. bn128.g1_toMontgomery(p1, p1);
  72. const p2 = bn128.g1_allocPoint();
  73. bn128.testAddG1(100000, p1, p2);
  74. bn128.g1_fromMontgomery(p2, p2);
  75. const d = bn128.g1_getPoint(p2);
  76. for (let i=0; i<3; i++) {
  77. d[i] = refBigInt(d[i].toString());
  78. }
  79. assert(refBn128.G1.equals(d, refD));
  80. }).timeout(10000000);
  81. it("It should do a basic point doubling in G2", async () => {
  82. const bn128 = await buildBn128();
  83. const refD = refBn128.G2.double(refBn128.g2);
  84. const p1 = bn128.g2_allocPoint(refBn128.g2);
  85. bn128.g2_toMontgomery(p1, p1);
  86. bn128.g2_double(p1, p1);
  87. bn128.g2_fromMontgomery(p1, p1);
  88. const d = bn128.g2_getPoint(p1);
  89. for (let i=0; i<3; i++) {
  90. for (let j=0; j<2; j++) {
  91. d[i][j] = refBigInt(d[i][j].toString());
  92. }
  93. }
  94. assert(refBn128.G2.equals(d, refD));
  95. });
  96. it("It should add two points in G2", async () => {
  97. const bn128 = await buildBn128();
  98. const refD = refBn128.G2.affine(refBn128.G2.mulScalar(refBn128.g2, 3));
  99. const p1 = bn128.g2_allocPoint(refBn128.g2);
  100. bn128.g2_toMontgomery(p1, p1);
  101. bn128.g2_double(p1, p1);
  102. const p2 = bn128.g2_allocPoint(refBn128.g2);
  103. bn128.g2_toMontgomery(p2, p2);
  104. bn128.g2_add(p1, p2, p2);
  105. bn128.g2_affine(p2, p2);
  106. bn128.g2_fromMontgomery(p2, p2);
  107. const d = bn128.g2_getPoint(p2);
  108. for (let i=0; i<3; i++) {
  109. for (let j=0; j<2; j++) {
  110. d[i][j] = refBigInt(d[i][j].toString());
  111. assert(d[i][j].equals(refD[i][j]));
  112. }
  113. }
  114. assert(refBn128.G2.equals(d, refD));
  115. });
  116. });