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.

198 lines
5.3 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
  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.mulEscalar(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.mulEscalar(bn128.G1.g, r1);
  32. const gr2 = bn128.G1.mulEscalar(bn128.G1.g, r2);
  33. const grsum1 = bn128.G1.add(gr1, gr2);
  34. const grsum2 = bn128.G1.mulEscalar(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.mulEscalar(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.mulEscalar(bn128.G2.g, r1);
  49. const gr2 = bn128.G2.mulEscalar(bn128.G2.g, r2);
  50. const grsum1 = bn128.G2.add(gr1, gr2);
  51. const grsum2 = bn128.G2.mulEscalar(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. const bn128 = new BN128();
  116. const g1a = bn128.G1.mulEscalar(bn128.G1.g, 25);
  117. const g2a = bn128.G2.mulEscalar(bn128.G2.g, 30);
  118. const g1b = bn128.G1.mulEscalar(bn128.G1.g, 30);
  119. const g2b = bn128.G2.mulEscalar(bn128.G2.g, 25);
  120. const pre1a = bn128.precomputeG1(g1a);
  121. const pre2a = bn128.precomputeG2(g2a);
  122. const pre1b = bn128.precomputeG1(g1b);
  123. const pre2b = bn128.precomputeG2(g2b);
  124. const r1 = bn128.millerLoop(pre1a, pre2a);
  125. const r2 = bn128.millerLoop(pre1b, pre2b);
  126. const rbe = bn128.F12.mul(r1, bn128.F12.inverse(r2));
  127. const res = bn128.finalExponentiation(rbe);
  128. assert(bn128.F12.equals(res, bn128.F12.one));
  129. }).timeout(10000);
  130. it("Should match pairing 2", () => {
  131. const bn128 = new BN128();
  132. const g1a = bn128.G1.mulEscalar(bn128.G1.g, 25);
  133. const g2a = bn128.G2.mulEscalar(bn128.G2.g, 30);
  134. const g1b = bn128.G1.mulEscalar(bn128.G1.g, 30);
  135. const g2b = bn128.G2.mulEscalar(bn128.G2.g, 25);
  136. const pre1a = bn128.precomputeG1(g1a);
  137. const pre2a = bn128.precomputeG2(g2a);
  138. const pre1b = bn128.precomputeG1(g1b);
  139. const pre2b = bn128.precomputeG2(g2b);
  140. const r1 = bn128.millerLoop(pre1a, pre2a);
  141. const r2 = bn128.millerLoop(pre1b, pre2b);
  142. const rbe = bn128.F12.mul(r1, bn128.F12.inverse(r2));
  143. const res = bn128.finalExponentiation(rbe);
  144. assert(bn128.F12.equals(res, bn128.F12.one));
  145. }).timeout(10000);
  146. });