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.

172 lines
5.8 KiB

5 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 PolField = require("../src/polfield.js");
  18. const ZqField = require("../src/zqfield");
  19. const assert = chai.assert;
  20. const r = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
  21. describe("Polinomial field", () => {
  22. it("Should compute a multiplication", () => {
  23. const PF = new PolField(new ZqField(r));
  24. const a = [bigInt(1), bigInt(2), bigInt(3)];
  25. const b = [bigInt(1), bigInt(2), bigInt(3)];
  26. const res = PF.mul(a,b);
  27. assert(PF.equals(res, [bigInt(1), bigInt(4), bigInt(10), bigInt(12), bigInt(9)]));
  28. });
  29. it("Should compute a multiplication 2", () => {
  30. const PF = new PolField(new ZqField(r));
  31. const a = [bigInt(5), bigInt(1)];
  32. const b = [bigInt(-5), bigInt(1)];
  33. const res = PF.mul(a,b);
  34. assert(PF.equals(res, [bigInt(-25), bigInt(0), bigInt(1)]));
  35. });
  36. it("Should compute an addition", () => {
  37. const PF = new PolField(new ZqField(r));
  38. const a = [bigInt(5), bigInt(1)];
  39. const b = [bigInt(-5), bigInt(1)];
  40. const res = PF.add(a,b);
  41. assert(PF.equals(res, [bigInt(0), bigInt(2)]));
  42. });
  43. it("Should compute a substraction", () => {
  44. const PF = new PolField(new ZqField(r));
  45. const a = [bigInt(5), bigInt(3), bigInt(4)];
  46. const b = [bigInt(5), bigInt(1)];
  47. const res = PF.sub(a,b);
  48. assert(PF.equals(res, [bigInt(0), bigInt(2), bigInt(4)]));
  49. });
  50. it("Should compute reciprocal", () => {
  51. const PF = new PolField(new ZqField(r));
  52. const a = [bigInt(4), bigInt(1), bigInt(-3), bigInt(-1), bigInt(2),bigInt(1), bigInt(-1), bigInt(1)];
  53. const res = PF._reciprocal(a, 3, 0);
  54. assert(PF.equals(res, [bigInt(12), bigInt(15), bigInt(3), bigInt(-4), bigInt(-3), bigInt(0), bigInt(1), bigInt(1)]));
  55. });
  56. it("Should div2", () => {
  57. const PF = new PolField(new ZqField(r));
  58. // x^6
  59. const a = [bigInt(0), bigInt(0), bigInt(0), bigInt(0), bigInt(0),bigInt(0), bigInt(1)];
  60. // x^5
  61. const b = [bigInt(0), bigInt(0), bigInt(0), bigInt(0), bigInt(0), bigInt(1)];
  62. const res = PF._div2(6, b);
  63. assert(PF.equals(res, [bigInt(0), bigInt(1)]));
  64. const res2 = PF.div(a,b);
  65. assert(PF.equals(res2, [bigInt(0), bigInt(1)]));
  66. });
  67. it("Should div", () => {
  68. const PF = new PolField(new ZqField(r));
  69. const a = [bigInt(1), bigInt(2), bigInt(3), bigInt(4), bigInt(5),bigInt(6), bigInt(7)];
  70. const b = [bigInt(8), bigInt(9), bigInt(10), bigInt(11), bigInt(12), bigInt(13)];
  71. const c = PF.mul(a,b);
  72. const d = PF.div(c,b);
  73. assert(PF.equals(a, d));
  74. });
  75. it("Should div big/small", () => {
  76. const PF = new PolField(new ZqField(r));
  77. const a = [bigInt(1), bigInt(2), bigInt(3), bigInt(4), bigInt(5),bigInt(6), bigInt(7)];
  78. const b = [bigInt(8), bigInt(9)];
  79. const c = PF.mul(a,b);
  80. const d = PF.div(c,b);
  81. assert(PF.equals(a, d));
  82. });
  83. it("Should div random big", () => {
  84. const PF = new PolField(new ZqField(r));
  85. const a = [];
  86. const b = [];
  87. for (let i=0; i<1000; i++) a.push(bigInt(Math.floor(Math.random()*100000) -500000));
  88. for (let i=0; i<900; i++) b.push(bigInt(Math.floor(Math.random()*100000) -500000));
  89. const c = PF.mul(a,b);
  90. const d = PF.div(c,b);
  91. assert(PF.equals(a, d));
  92. }).timeout(10000);
  93. it("Should evaluate and zero", () => {
  94. const PF = new PolField(new ZqField(r));
  95. const p = [PF.F.neg(bigInt(2)), bigInt(1)];
  96. const v = PF.eval(p, bigInt(2));
  97. assert(PF.F.equals(v, bigInt(0)));
  98. });
  99. it("Should create lagrange polynomial minmal", () => {
  100. const PF = new PolField(new ZqField(r));
  101. const points=[];
  102. points.push([bigInt(1), bigInt(1)]);
  103. points.push([bigInt(2), bigInt(2)]);
  104. points.push([bigInt(3), bigInt(5)]);
  105. const p=PF.lagrange(points);
  106. for (let i=0; i<points.length; i++) {
  107. const v = PF.eval(p, points[i][0]);
  108. assert(PF.F.equals(v, points[i][1]));
  109. }
  110. });
  111. it("Should create lagrange polynomial", () => {
  112. const PF = new PolField(new ZqField(r));
  113. const points=[];
  114. points.push([bigInt(1), bigInt(2)]);
  115. points.push([bigInt(2), bigInt(-2)]);
  116. points.push([bigInt(3), bigInt(0)]);
  117. points.push([bigInt(4), bigInt(453345)]);
  118. const p=PF.lagrange(points);
  119. for (let i=0; i<points.length; i++) {
  120. const v = PF.eval(p, points[i][0]);
  121. assert(PF.F.equals(v, points[i][1]));
  122. }
  123. });
  124. it("Should test ruffini", () => {
  125. const PF = new PolField(new ZqField(r));
  126. const a = [bigInt(1), bigInt(2), bigInt(3), bigInt(4), bigInt(5),bigInt(6), bigInt(7)];
  127. const b = PF.mul(a, [bigInt(-7), bigInt(1)]);
  128. const c = PF.ruffini(b, bigInt(7));
  129. assert(PF.equals(a, c));
  130. });
  131. });