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.

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