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.

193 lines
8.2 KiB

5 years ago
5 years ago
  1. const chai = require("chai");
  2. const path = require("path");
  3. const snarkjs = require("snarkjs");
  4. const crypto = require("crypto");
  5. const compiler = require("circom");
  6. const assert = chai.assert;
  7. describe("Sum test", () => {
  8. it("Should create a iszero circuit", async() => {
  9. const cirDef = await compiler(path.join(__dirname, "circuits", "iszero.circom"));
  10. const circuit = new snarkjs.Circuit(cirDef);
  11. let witness;
  12. witness = circuit.calculateWitness({ "in": 111});
  13. assert(witness[0].equals(snarkjs.bigInt(1)));
  14. assert(witness[1].equals(snarkjs.bigInt(0)));
  15. witness = circuit.calculateWitness({ "in": 0 });
  16. assert(witness[0].equals(snarkjs.bigInt(1)));
  17. assert(witness[1].equals(snarkjs.bigInt(1)));
  18. });
  19. it("Should create a isequal circuit", async() => {
  20. const cirDef = await compiler(path.join(__dirname, "circuits", "isequal.circom"));
  21. const circuit = new snarkjs.Circuit(cirDef);
  22. let witness;
  23. witness = circuit.calculateWitness({ "in[0]": "111", "in[1]": "222" });
  24. assert(witness[0].equals(snarkjs.bigInt(1)));
  25. assert(witness[1].equals(snarkjs.bigInt(0)));
  26. witness = circuit.calculateWitness({ "in[0]": "444", "in[1]": "444" });
  27. assert(witness[0].equals(snarkjs.bigInt(1)));
  28. assert(witness[1].equals(snarkjs.bigInt(1)));
  29. });
  30. it("Should create a comparison lessthan", async() => {
  31. const cirDef = await compiler(path.join(__dirname, "circuits", "lessthan.circom"));
  32. const circuit = new snarkjs.Circuit(cirDef);
  33. let witness;
  34. witness = circuit.calculateWitness({ "in[0]": "333", "in[1]": "444" });
  35. assert(witness[0].equals(snarkjs.bigInt(1)));
  36. assert(witness[1].equals(snarkjs.bigInt(1)));
  37. witness = circuit.calculateWitness({ "in[0]": "1", "in[1]": "1" });
  38. assert(witness[0].equals(snarkjs.bigInt(1)));
  39. assert(witness[1].equals(snarkjs.bigInt(0)));
  40. witness = circuit.calculateWitness({ "in[0]": "661", "in[1]": "660" });
  41. assert(witness[0].equals(snarkjs.bigInt(1)));
  42. assert(witness[1].equals(snarkjs.bigInt(0)));
  43. witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "1" });
  44. assert(witness[0].equals(snarkjs.bigInt(1)));
  45. assert(witness[1].equals(snarkjs.bigInt(1)));
  46. witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "444" });
  47. assert(witness[0].equals(snarkjs.bigInt(1)));
  48. assert(witness[1].equals(snarkjs.bigInt(1)));
  49. witness = circuit.calculateWitness({ "in[0]": "1", "in[1]": "0" });
  50. assert(witness[0].equals(snarkjs.bigInt(1)));
  51. assert(witness[1].equals(snarkjs.bigInt(0)));
  52. witness = circuit.calculateWitness({ "in[0]": "555", "in[1]": "0" });
  53. assert(witness[0].equals(snarkjs.bigInt(1)));
  54. assert(witness[1].equals(snarkjs.bigInt(0)));
  55. witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "0" });
  56. assert(witness[0].equals(snarkjs.bigInt(1)));
  57. assert(witness[1].equals(snarkjs.bigInt(0)));
  58. });
  59. it("Should create a comparison lesseqthan", async() => {
  60. const cirDef = await compiler(path.join(__dirname, "circuits", "lesseqthan.circom"));
  61. const circuit = new snarkjs.Circuit(cirDef);
  62. let witness;
  63. witness = circuit.calculateWitness({ "in[0]": "333", "in[1]": "444" });
  64. assert(witness[0].equals(snarkjs.bigInt(1)));
  65. assert(witness[1].equals(snarkjs.bigInt(1)));
  66. witness = circuit.calculateWitness({ "in[0]": "1", "in[1]": "1" });
  67. assert(witness[0].equals(snarkjs.bigInt(1)));
  68. assert(witness[1].equals(snarkjs.bigInt(1)));
  69. witness = circuit.calculateWitness({ "in[0]": "661", "in[1]": "660" });
  70. assert(witness[0].equals(snarkjs.bigInt(1)));
  71. assert(witness[1].equals(snarkjs.bigInt(0)));
  72. witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "1" });
  73. assert(witness[0].equals(snarkjs.bigInt(1)));
  74. assert(witness[1].equals(snarkjs.bigInt(1)));
  75. witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "444" });
  76. assert(witness[0].equals(snarkjs.bigInt(1)));
  77. assert(witness[1].equals(snarkjs.bigInt(1)));
  78. witness = circuit.calculateWitness({ "in[0]": "1", "in[1]": "0" });
  79. assert(witness[0].equals(snarkjs.bigInt(1)));
  80. assert(witness[1].equals(snarkjs.bigInt(0)));
  81. witness = circuit.calculateWitness({ "in[0]": "555", "in[1]": "0" });
  82. assert(witness[0].equals(snarkjs.bigInt(1)));
  83. assert(witness[1].equals(snarkjs.bigInt(0)));
  84. witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "0" });
  85. assert(witness[0].equals(snarkjs.bigInt(1)));
  86. assert(witness[1].equals(snarkjs.bigInt(1)));
  87. });
  88. it("Should create a comparison greaterthan", async() => {
  89. const cirDef = await compiler(path.join(__dirname, "circuits", "greaterthan.circom"));
  90. const circuit = new snarkjs.Circuit(cirDef);
  91. let witness;
  92. witness = circuit.calculateWitness({ "in[0]": "333", "in[1]": "444" });
  93. assert(witness[0].equals(snarkjs.bigInt(1)));
  94. assert(witness[1].equals(snarkjs.bigInt(0)));
  95. witness = circuit.calculateWitness({ "in[0]": "1", "in[1]": "1" });
  96. assert(witness[0].equals(snarkjs.bigInt(1)));
  97. assert(witness[1].equals(snarkjs.bigInt(0)));
  98. witness = circuit.calculateWitness({ "in[0]": "661", "in[1]": "660" });
  99. assert(witness[0].equals(snarkjs.bigInt(1)));
  100. assert(witness[1].equals(snarkjs.bigInt(1)));
  101. witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "1" });
  102. assert(witness[0].equals(snarkjs.bigInt(1)));
  103. assert(witness[1].equals(snarkjs.bigInt(0)));
  104. witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "444" });
  105. assert(witness[0].equals(snarkjs.bigInt(1)));
  106. assert(witness[1].equals(snarkjs.bigInt(0)));
  107. witness = circuit.calculateWitness({ "in[0]": "1", "in[1]": "0" });
  108. assert(witness[0].equals(snarkjs.bigInt(1)));
  109. assert(witness[1].equals(snarkjs.bigInt(1)));
  110. witness = circuit.calculateWitness({ "in[0]": "555", "in[1]": "0" });
  111. assert(witness[0].equals(snarkjs.bigInt(1)));
  112. assert(witness[1].equals(snarkjs.bigInt(1)));
  113. witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "0" });
  114. assert(witness[0].equals(snarkjs.bigInt(1)));
  115. assert(witness[1].equals(snarkjs.bigInt(0)));
  116. });
  117. it("Should create a comparison greatereqthan", async() => {
  118. const cirDef = await compiler(path.join(__dirname, "circuits", "greatereqthan.circom"));
  119. const circuit = new snarkjs.Circuit(cirDef);
  120. console.log("NConstraints BalancesUpdater: " + circuit.nConstraints);
  121. let witness;
  122. witness = circuit.calculateWitness({ "in[0]": "333", "in[1]": "444" });
  123. assert(witness[0].equals(snarkjs.bigInt(1)));
  124. assert(witness[1].equals(snarkjs.bigInt(0)));
  125. witness = circuit.calculateWitness({ "in[0]": "1", "in[1]": "1" });
  126. assert(witness[0].equals(snarkjs.bigInt(1)));
  127. assert(witness[1].equals(snarkjs.bigInt(1)));
  128. witness = circuit.calculateWitness({ "in[0]": "661", "in[1]": "660" });
  129. assert(witness[0].equals(snarkjs.bigInt(1)));
  130. assert(witness[1].equals(snarkjs.bigInt(1)));
  131. witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "1" });
  132. assert(witness[0].equals(snarkjs.bigInt(1)));
  133. assert(witness[1].equals(snarkjs.bigInt(0)));
  134. witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "444" });
  135. assert(witness[0].equals(snarkjs.bigInt(1)));
  136. assert(witness[1].equals(snarkjs.bigInt(0)));
  137. witness = circuit.calculateWitness({ "in[0]": "1", "in[1]": "0" });
  138. assert(witness[0].equals(snarkjs.bigInt(1)));
  139. assert(witness[1].equals(snarkjs.bigInt(1)));
  140. witness = circuit.calculateWitness({ "in[0]": "555", "in[1]": "0" });
  141. assert(witness[0].equals(snarkjs.bigInt(1)));
  142. assert(witness[1].equals(snarkjs.bigInt(1)));
  143. witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "0" });
  144. assert(witness[0].equals(snarkjs.bigInt(1)));
  145. assert(witness[1].equals(snarkjs.bigInt(1)));
  146. });
  147. });