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.

133 lines
3.7 KiB

6 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
  1. const chai = require("chai");
  2. const path = require("path");
  3. const snarkjs = require("snarkjs");
  4. const compiler = require("circom");
  5. const assert = chai.assert;
  6. const bigInt = snarkjs.bigInt;
  7. describe("Mux4 test", () => {
  8. it("Should create a constant multiplexer 4", async () => {
  9. const cirDef = await compiler(path.join(__dirname, "circuits", "mux4_1.circom"));
  10. // console.log(JSON.stringify(cirDef, null, 1));
  11. // assert.equal(cirDef.nVars, 2);
  12. const circuit = new snarkjs.Circuit(cirDef);
  13. console.log("NConstrains Mux4: " + circuit.nConstraints);
  14. const ct16 = [
  15. bigInt("123"),
  16. bigInt("456"),
  17. bigInt("789"),
  18. bigInt("012"),
  19. bigInt("111"),
  20. bigInt("222"),
  21. bigInt("333"),
  22. bigInt("4546"),
  23. bigInt("134523"),
  24. bigInt("44356"),
  25. bigInt("15623"),
  26. bigInt("4566"),
  27. bigInt("1223"),
  28. bigInt("4546"),
  29. bigInt("4256"),
  30. bigInt("4456")
  31. ];
  32. for (let i=0; i<16; i++) {
  33. const w = circuit.calculateWitness({ "selector": i });
  34. assert(circuit.checkWitness(w));
  35. assert(w[0].equals(bigInt(1)));
  36. // console.log(i + " -> " + w[circuit.getSignalIdx("main.out")].toString());
  37. assert(w[circuit.getSignalIdx("main.out")].equals(ct16[i]));
  38. }
  39. });
  40. it("Should create a constant multiplexer 3", async () => {
  41. const cirDef = await compiler(path.join(__dirname, "circuits", "mux3_1.circom"));
  42. const circuit = new snarkjs.Circuit(cirDef);
  43. console.log("NConstrains Mux3: " + circuit.nConstraints);
  44. const ct8 = [
  45. bigInt("37"),
  46. bigInt("47"),
  47. bigInt("53"),
  48. bigInt("71"),
  49. bigInt("89"),
  50. bigInt("107"),
  51. bigInt("163"),
  52. bigInt("191")
  53. ];
  54. for (let i=0; i<8; i++) {
  55. const w = circuit.calculateWitness({ "selector": i });
  56. assert(w[0].equals(bigInt(1)));
  57. // console.log(i + " -> " + w[circuit.getSignalIdx("main.out")].toString());
  58. assert(w[circuit.getSignalIdx("main.out")].equals(ct8[i]));
  59. }
  60. });
  61. it("Should create a constant multiplexer 2", async () => {
  62. const cirDef = await compiler(path.join(__dirname, "circuits", "mux2_1.circom"));
  63. const circuit = new snarkjs.Circuit(cirDef);
  64. console.log("NConstrains Mux2: " + circuit.nConstraints);
  65. const ct8 = [
  66. bigInt("37"),
  67. bigInt("47"),
  68. bigInt("53"),
  69. bigInt("71"),
  70. ];
  71. for (let i=0; i<4; i++) {
  72. const w = circuit.calculateWitness({ "selector": i });
  73. assert(circuit.checkWitness(w));
  74. assert(w[0].equals(bigInt(1)));
  75. // console.log(i + " -> " + w[circuit.getSignalIdx("main.out")].toString());
  76. assert(w[circuit.getSignalIdx("main.out")].equals(ct8[i]));
  77. }
  78. });
  79. it("Should create a constant multiplexer 1", async () => {
  80. const cirDef = await compiler(path.join(__dirname, "circuits", "mux1_1.circom"));
  81. const circuit = new snarkjs.Circuit(cirDef);
  82. console.log("NConstrains Mux1: " + circuit.nConstraints);
  83. const ct8 = [
  84. bigInt("37"),
  85. bigInt("47"),
  86. ];
  87. for (let i=0; i<2; i++) {
  88. const w = circuit.calculateWitness({ "selector": i });
  89. assert(circuit.checkWitness(w));
  90. assert(w[0].equals(bigInt(1)));
  91. // console.log(i + " -> " + w[circuit.getSignalIdx("main.out")].toString());
  92. assert(w[circuit.getSignalIdx("main.out")].equals(ct8[i]));
  93. }
  94. });
  95. });