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.

151 lines
3.9 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. const chai = require("chai");
  2. const bigInt = require("big-integer");
  3. const BN128 = require("../src/BN128.js");
  4. const assert = chai.assert;
  5. describe("Curve G1 Test", () => {
  6. it("r*one == 0", () => {
  7. const bn128 = new BN128();
  8. const res = bn128.G1.mulEscalar(bn128.G1.g, bn128.r);
  9. assert(bn128.G1.equals(res, bn128.G1.zero), "G1 does not have range r");
  10. });
  11. it("Should add match in various in G1", () => {
  12. const bn128 = new BN128();
  13. const r1 = bigInt(33);
  14. const r2 = bigInt(44);
  15. const gr1 = bn128.G1.mulEscalar(bn128.G1.g, r1);
  16. const gr2 = bn128.G1.mulEscalar(bn128.G1.g, r2);
  17. const grsum1 = bn128.G1.add(gr1, gr2);
  18. const grsum2 = bn128.G1.mulEscalar(bn128.G1.g, r1.add(r2));
  19. assert(bn128.G1.equals(grsum1, grsum2));
  20. });
  21. });
  22. describe("Curve G2 Test", () => {
  23. it ("r*one == 0", () => {
  24. const bn128 = new BN128();
  25. const res = bn128.G2.mulEscalar(bn128.G2.g, bn128.r);
  26. assert(bn128.G2.equals(res, bn128.G2.zero), "G2 does not have range r");
  27. });
  28. it("Should add match in various in G2", () => {
  29. const bn128 = new BN128();
  30. const r1 = bigInt(33);
  31. const r2 = bigInt(44);
  32. const gr1 = bn128.G2.mulEscalar(bn128.G2.g, r1);
  33. const gr2 = bn128.G2.mulEscalar(bn128.G2.g, r2);
  34. const grsum1 = bn128.G2.add(gr1, gr2);
  35. const grsum2 = bn128.G2.mulEscalar(bn128.G2.g, r1.add(r2));
  36. /*
  37. console.log(G2.toString(grsum1));
  38. console.log(G2.toString(grsum2));
  39. */
  40. assert(bn128.G2.equals(grsum1, grsum2));
  41. });
  42. });
  43. describe("F6 testing", () => {
  44. it("Should multiply and divide in F6", () => {
  45. const bn128 = new BN128();
  46. const a =
  47. [
  48. [bigInt("1"), bigInt("2")],
  49. [bigInt("3"), bigInt("4")],
  50. [bigInt("5"), bigInt("6")]
  51. ];
  52. const b =
  53. [
  54. [bigInt("12"), bigInt("11")],
  55. [bigInt("10"), bigInt("9")],
  56. [bigInt("8"), bigInt("7")]
  57. ];
  58. const c = bn128.F6.mul(a,b);
  59. const d = bn128.F6.div(c,b);
  60. assert(bn128.F6.equals(a, d));
  61. });
  62. });
  63. describe("F12 testing", () => {
  64. it("Should multiply and divide in F12", () => {
  65. const bn128 = new BN128();
  66. const a =
  67. [
  68. [
  69. [bigInt("1"), bigInt("2")],
  70. [bigInt("3"), bigInt("4")],
  71. [bigInt("5"), bigInt("6")]
  72. ],
  73. [
  74. [bigInt("7"), bigInt("8")],
  75. [bigInt("9"), bigInt("10")],
  76. [bigInt("11"), bigInt("12")]
  77. ]
  78. ];
  79. const b =
  80. [
  81. [
  82. [bigInt("12"), bigInt("11")],
  83. [bigInt("10"), bigInt("9")],
  84. [bigInt("8"), bigInt("7")]
  85. ],
  86. [
  87. [bigInt("6"), bigInt("5")],
  88. [bigInt("4"), bigInt("3")],
  89. [bigInt("2"), bigInt("1")]
  90. ]
  91. ];
  92. const c = bn128.F12.mul(a,b);
  93. const d = bn128.F12.div(c,b);
  94. assert(bn128.F12.equals(a, d));
  95. });
  96. });
  97. describe("Pairing", () => {
  98. it("Should match pairing", () => {
  99. const bn128 = new BN128();
  100. const g1a = bn128.G1.mulEscalar(bn128.G1.g, 25);
  101. const g2a = bn128.G2.mulEscalar(bn128.G2.g, 30);
  102. const g1b = bn128.G1.mulEscalar(bn128.G1.g, 30);
  103. const g2b = bn128.G2.mulEscalar(bn128.G2.g, 25);
  104. const pre1a = bn128.precomputeG1(g1a);
  105. const pre2a = bn128.precomputeG2(g2a);
  106. const pre1b = bn128.precomputeG1(g1b);
  107. const pre2b = bn128.precomputeG2(g2b);
  108. const r1 = bn128.millerLoop(pre1a, pre2a);
  109. const r2 = bn128.millerLoop(pre1b, pre2b);
  110. const rbe = bn128.F12.mul(r1, bn128.F12.inverse(r2));
  111. const res = bn128.finalExponentiation(rbe);
  112. assert(bn128.F12.equals(res, bn128.F12.one));
  113. }).timeout(10000);
  114. });