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.

96 lines
2.0 KiB

  1. /*
  2. Copyright 2018 0KIMS association.
  3. This file is part of circom (Zero Knowledge Circuit Compiler).
  4. circom is a free software: you can redistribute it and/or modify it
  5. 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. circom is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  11. License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with circom. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. pragma circom 2.0.0;
  16. template XOR() {
  17. signal input a;
  18. signal input b;
  19. signal output out;
  20. out <== a + b - 2*a*b;
  21. }
  22. template AND() {
  23. signal input a;
  24. signal input b;
  25. signal output out;
  26. out <== a*b;
  27. }
  28. template OR() {
  29. signal input a;
  30. signal input b;
  31. signal output out;
  32. out <== a + b - a*b;
  33. }
  34. template NOT() {
  35. signal input in;
  36. signal output out;
  37. out <== 1 + in - 2*in;
  38. }
  39. template NAND() {
  40. signal input a;
  41. signal input b;
  42. signal output out;
  43. out <== 1 - a*b;
  44. }
  45. template NOR() {
  46. signal input a;
  47. signal input b;
  48. signal output out;
  49. out <== a*b + 1 - a - b;
  50. }
  51. template MultiAND(n) {
  52. signal input in[n];
  53. signal output out;
  54. component and1;
  55. component and2;
  56. component ands[2];
  57. if (n==1) {
  58. out <== in[0];
  59. } else if (n==2) {
  60. and1 = AND();
  61. and1.a <== in[0];
  62. and1.b <== in[1];
  63. out <== and1.out;
  64. } else {
  65. and2 = AND();
  66. var n1 = n\2;
  67. var n2 = n-n\2;
  68. ands[0] = MultiAND(n1);
  69. ands[1] = MultiAND(n2);
  70. var i;
  71. for (i=0; i<n1; i++) ands[0].in[i] <== in[i];
  72. for (i=0; i<n2; i++) ands[1].in[i] <== in[n1+i];
  73. and2.a <== ands[0].out;
  74. and2.b <== ands[1].out;
  75. out <== and2.out;
  76. }
  77. }