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.

191 lines
5.4 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
6 years ago
  1. /*
  2. Copyright 2018 0kims association
  3. This file is part of zksnark javascript library.
  4. zksnark javascript library is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. zksnark javascript library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with zksnark javascript library. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. const chai = require("chai");
  16. const bigInt = require("../src/bigint.js");
  17. const BN128 = require("../src/BN128.js");
  18. const F1Field = require("../src/zqfield.js");
  19. const assert = chai.assert;
  20. describe("F1 testing", () => {
  21. it("Should compute euclidean", () => {
  22. const F = new F1Field(bigInt(7));
  23. const res = F.inverse(bigInt(4));
  24. assert(F.equals(res, bigInt(2)));
  25. });
  26. it("Should multiply and divide in F1", () => {
  27. const bn128 = new BN128();
  28. const a = bigInt("1");
  29. const b = bn128.F1.affine(bigInt("-3"));
  30. const c = bn128.F1.mul(a,b);
  31. const d = bn128.F1.div(c,b);
  32. assert(bn128.F1.equals(a, d));
  33. });
  34. });
  35. describe("Curve G1 Test", () => {
  36. it("r*one == 0", () => {
  37. const bn128 = new BN128();
  38. const res = bn128.G1.mulScalar(bn128.G1.g, bn128.r);
  39. assert(bn128.G1.equals(res, bn128.G1.zero), "G1 does not have range r");
  40. });
  41. it("Should add match in various in G1", () => {
  42. const bn128 = new BN128();
  43. const r1 = bigInt(33);
  44. const r2 = bigInt(44);
  45. const gr1 = bn128.G1.mulScalar(bn128.G1.g, r1);
  46. const gr2 = bn128.G1.mulScalar(bn128.G1.g, r2);
  47. const grsum1 = bn128.G1.add(gr1, gr2);
  48. const grsum2 = bn128.G1.mulScalar(bn128.G1.g, r1.add(r2));
  49. assert(bn128.G1.equals(grsum1, grsum2));
  50. });
  51. });
  52. describe("Curve G2 Test", () => {
  53. it ("r*one == 0", () => {
  54. const bn128 = new BN128();
  55. const res = bn128.G2.mulScalar(bn128.G2.g, bn128.r);
  56. assert(bn128.G2.equals(res, bn128.G2.zero), "G2 does not have range r");
  57. });
  58. it("Should add match in various in G2", () => {
  59. const bn128 = new BN128();
  60. const r1 = bigInt(33);
  61. const r2 = bigInt(44);
  62. const gr1 = bn128.G2.mulScalar(bn128.G2.g, r1);
  63. const gr2 = bn128.G2.mulScalar(bn128.G2.g, r2);
  64. const grsum1 = bn128.G2.add(gr1, gr2);
  65. const grsum2 = bn128.G2.mulScalar(bn128.G2.g, r1.add(r2));
  66. /*
  67. console.log(G2.toString(grsum1));
  68. console.log(G2.toString(grsum2));
  69. */
  70. assert(bn128.G2.equals(grsum1, grsum2));
  71. });
  72. });
  73. describe("F6 testing", () => {
  74. it("Should multiply and divide in F6", () => {
  75. const bn128 = new BN128();
  76. const a =
  77. [
  78. [bigInt("1"), bigInt("2")],
  79. [bigInt("3"), bigInt("4")],
  80. [bigInt("5"), bigInt("6")]
  81. ];
  82. const b =
  83. [
  84. [bigInt("12"), bigInt("11")],
  85. [bigInt("10"), bigInt("9")],
  86. [bigInt("8"), bigInt("7")]
  87. ];
  88. const c = bn128.F6.mul(a,b);
  89. const d = bn128.F6.div(c,b);
  90. assert(bn128.F6.equals(a, d));
  91. });
  92. });
  93. describe("F12 testing", () => {
  94. it("Should multiply and divide in F12", () => {
  95. const bn128 = new BN128();
  96. const a =
  97. [
  98. [
  99. [bigInt("1"), bigInt("2")],
  100. [bigInt("3"), bigInt("4")],
  101. [bigInt("5"), bigInt("6")]
  102. ],
  103. [
  104. [bigInt("7"), bigInt("8")],
  105. [bigInt("9"), bigInt("10")],
  106. [bigInt("11"), bigInt("12")]
  107. ]
  108. ];
  109. const b =
  110. [
  111. [
  112. [bigInt("12"), bigInt("11")],
  113. [bigInt("10"), bigInt("9")],
  114. [bigInt("8"), bigInt("7")]
  115. ],
  116. [
  117. [bigInt("6"), bigInt("5")],
  118. [bigInt("4"), bigInt("3")],
  119. [bigInt("2"), bigInt("1")]
  120. ]
  121. ];
  122. const c = bn128.F12.mul(a,b);
  123. const d = bn128.F12.div(c,b);
  124. assert(bn128.F12.equals(a, d));
  125. });
  126. });
  127. describe("Pairing", () => {
  128. it("Should match pairing", () => {
  129. for (let i=0; i<1; i++) {
  130. const bn128 = new BN128();
  131. const g1a = bn128.G1.mulScalar(bn128.G1.g, 25);
  132. const g2a = bn128.G2.mulScalar(bn128.G2.g, 30);
  133. const g1b = bn128.G1.mulScalar(bn128.G1.g, 30);
  134. const g2b = bn128.G2.mulScalar(bn128.G2.g, 25);
  135. const pre1a = bn128.precomputeG1(g1a);
  136. const pre2a = bn128.precomputeG2(g2a);
  137. const pre1b = bn128.precomputeG1(g1b);
  138. const pre2b = bn128.precomputeG2(g2b);
  139. const r1 = bn128.millerLoop(pre1a, pre2a);
  140. const r2 = bn128.millerLoop(pre1b, pre2b);
  141. const rbe = bn128.F12.mul(r1, bn128.F12.inverse(r2));
  142. const res = bn128.finalExponentiation(rbe);
  143. assert(bn128.F12.equals(res, bn128.F12.one));
  144. }
  145. }).timeout(10000);
  146. });