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.

127 lines
3.6 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
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(w[0].equals(bigInt(1)));
  35. // console.log(i + " -> " + w[circuit.getSignalIdx("main.out")].toString());
  36. assert(w[circuit.getSignalIdx("main.out")].equals(ct16[i]));
  37. }
  38. });
  39. it("Should create a constant multiplexer 3", async () => {
  40. const cirDef = await compiler(path.join(__dirname, "circuits", "mux3_1.circom"));
  41. const circuit = new snarkjs.Circuit(cirDef);
  42. console.log("NConstrains Mux3: " + circuit.nConstraints);
  43. const ct8 = [
  44. bigInt("37"),
  45. bigInt("47"),
  46. bigInt("53"),
  47. bigInt("71"),
  48. bigInt("89"),
  49. bigInt("107"),
  50. bigInt("163"),
  51. bigInt("191")
  52. ];
  53. for (let i=0; i<8; i++) {
  54. const w = circuit.calculateWitness({ "selector": i });
  55. assert(w[0].equals(bigInt(1)));
  56. // console.log(i + " -> " + w[circuit.getSignalIdx("main.out")].toString());
  57. assert(w[circuit.getSignalIdx("main.out")].equals(ct8[i]));
  58. }
  59. });
  60. it("Should create a constant multiplexer 2", async () => {
  61. const cirDef = await compiler(path.join(__dirname, "circuits", "mux2_1.circom"));
  62. const circuit = new snarkjs.Circuit(cirDef);
  63. console.log("NConstrains Mux2: " + circuit.nConstraints);
  64. const ct8 = [
  65. bigInt("37"),
  66. bigInt("47"),
  67. bigInt("53"),
  68. bigInt("71"),
  69. ];
  70. for (let i=0; i<4; i++) {
  71. const w = circuit.calculateWitness({ "selector": i });
  72. assert(w[0].equals(bigInt(1)));
  73. // console.log(i + " -> " + w[circuit.getSignalIdx("main.out")].toString());
  74. assert(w[circuit.getSignalIdx("main.out")].equals(ct8[i]));
  75. }
  76. });
  77. it("Should create a constant multiplexer 1", async () => {
  78. const cirDef = await compiler(path.join(__dirname, "circuits", "mux1_1.circom"));
  79. const circuit = new snarkjs.Circuit(cirDef);
  80. console.log("NConstrains Mux1: " + circuit.nConstraints);
  81. const ct8 = [
  82. bigInt("37"),
  83. bigInt("47"),
  84. ];
  85. for (let i=0; i<2; i++) {
  86. const w = circuit.calculateWitness({ "selector": i });
  87. assert(w[0].equals(bigInt(1)));
  88. // console.log(i + " -> " + w[circuit.getSignalIdx("main.out")].toString());
  89. assert(w[circuit.getSignalIdx("main.out")].equals(ct8[i]));
  90. }
  91. });
  92. });