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.

89 lines
2.5 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. /*
  2. Copyright 2018 0kims association.
  3. This file is part of zksnark JavaScript library.
  4. zksnark JavaScript library is a free software: you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License as published by the
  6. Free Software Foundation, either version 3 of the License, or (at your option)
  7. 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 MERCHANTABILITY
  10. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. more details.
  12. You should have received a copy of the GNU General Public License along with
  13. 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 ZqField = require("../src/zqfield.js");
  18. const RatField = require("../src/ratfield.js");
  19. const q = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
  20. const Z = new ZqField(q);
  21. const R = new RatField(Z);
  22. const assert = chai.assert;
  23. function r(a,b) {
  24. return [bigInt(a), bigInt(b)];
  25. }
  26. describe("Rational zq Field", () => {
  27. it("Should compare correctly", () => {
  28. assert( R.equals(r(3,5), r(6,10)));
  29. assert(!R.equals(r(3,5), r(6,11)));
  30. });
  31. it("Should add correctly", () => {
  32. const a = r(7,4);
  33. const b = r(5,12);
  34. assert(R.equals( R.add(a,b), r(13, 6)));
  35. });
  36. it("Should substract", () => {
  37. const a = r(7,4);
  38. const b = r(5,12);
  39. assert(R.equals( R.sub(a,b), r(4, 3)));
  40. });
  41. it("Should multiply", () => {
  42. const a = r(7,4);
  43. const b = r(5,12);
  44. assert(R.equals( R.mul(a,b), r(35, 48)));
  45. });
  46. it("Should div", () => {
  47. const a = r(7,4);
  48. const b = r(5,12);
  49. assert(R.equals( R.div(a,b), r(7*12, 5*4)));
  50. });
  51. it("Should square", () => {
  52. const a = r(7,4);
  53. assert(R.equals( R.square(a), r(49, 16)));
  54. });
  55. it("Should affine", () => {
  56. const a = r(12,4);
  57. const aa = R.affine(a);
  58. assert(Z.equals( aa[0], bigInt(3)));
  59. assert(Z.equals( aa[1], Z.one));
  60. });
  61. it("Should convert from Z to R", () => {
  62. const vz = bigInt(34);
  63. const vr = R.fromF(vz);
  64. assert(R.equals( vr, r(34,1)));
  65. });
  66. it("Should convert from R to Z", () => {
  67. const vr = r(32, 2);
  68. const vz = R.toF(vr);
  69. assert(Z.equals( vz, bigInt(16)));
  70. });
  71. });