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.

118 lines
4.5 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  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. template MultiMux4(n) {
  16. signal input c[n][16]; // Constants
  17. signal input s[4]; // Selector
  18. signal output out[n];
  19. signal a3210[n];
  20. signal a321[n];
  21. signal a320[n];
  22. signal a310[n];
  23. signal a32[n];
  24. signal a31[n];
  25. signal a30[n];
  26. signal a3[n];
  27. signal a210[n];
  28. signal a21[n];
  29. signal a20[n];
  30. signal a10[n];
  31. signal a2[n];
  32. signal a1[n];
  33. signal a0[n];
  34. signal a[n];
  35. // 4 constrains for the intermediary variables
  36. signal s10;
  37. s10 <== s[1] * s[0];
  38. signal s20;
  39. s20 <== s[2] * s[0];
  40. signal s21;
  41. s21 <== s[2] * s[1];
  42. signal s210;
  43. s210 <== s21 * s[0];
  44. for (var i=0; i<n; i++) {
  45. a3210[i] <== ( c[i][15]-c[i][14]-c[i][13]+c[i][12] - c[i][11]+c[i][10]+c[i][ 9]-c[i][ 8]
  46. -c[i][ 7]+c[i][ 6]+c[i][ 5]-c[i][ 4] + c[i][ 3]-c[i][ 2]-c[i][ 1]+c[i][ 0] ) * s210;
  47. a321[i] <== ( c[i][14]-c[i][12]-c[i][10]+c[i][ 8] - c[i][ 6]+c[i][ 4]+c[i][ 2]-c[i][ 0] ) * s21;
  48. a320[i] <== ( c[i][13]-c[i][12]-c[i][ 9]+c[i][ 8] - c[i][ 5]+c[i][ 4]+c[i][ 1]-c[i][ 0] ) * s20;
  49. a310[i] <== ( c[i][11]-c[i][10]-c[i][ 9]+c[i][ 8] - c[i][ 3]+c[i][ 2]+c[i][ 1]-c[i][ 0] ) * s10;
  50. a32[i] <== ( c[i][12]-c[i][ 8]-c[i][ 4]+c[i][ 0] ) * s[2];
  51. a31[i] <== ( c[i][10]-c[i][ 8]-c[i][ 2]+c[i][ 0] ) * s[1];
  52. a30[i] <== ( c[i][ 9]-c[i][ 8]-c[i][ 1]+c[i][ 0] ) * s[0];
  53. a3[i] <== ( c[i][ 8]-c[i][ 0] );
  54. a210[i] <== ( c[i][ 7]-c[i][ 6]-c[i][ 5]+c[i][ 4] - c[i][ 3]+c[i][ 2]+c[i][ 1]-c[i][ 0] ) * s210;
  55. a21[i] <== ( c[i][ 6]-c[i][ 4]-c[i][ 2]+c[i][ 0] ) * s21;
  56. a20[i] <== ( c[i][ 5]-c[i][ 4]-c[i][ 1]+c[i][ 0] ) * s20;
  57. a10[i] <== ( c[i][ 3]-c[i][ 2]-c[i][ 1]+c[i][ 0] ) * s10;
  58. a2[i] <== ( c[i][ 4]-c[i][ 0] ) * s[2];
  59. a1[i] <== ( c[i][ 2]-c[i][ 0] ) * s[1];
  60. a0[i] <== ( c[i][ 1]-c[i][ 0] ) * s[0];
  61. a[i] <== ( c[i][ 0] )
  62. out[i] <== ( a3210[i] + a321[i] + a320[i] + a310[i] + a32[i] + a31[i] + a30[i] + a3[i] ) * s[3] +
  63. ( a210[i] + a21[i] + a20[i] + a10[i] + a2[i] + a1[i] + a0[i] + a[i] );
  64. /*
  65. out[i] <== ( s210 * ( c[i][15]-c[i][14]-c[i][13]+c[i][12] - c[i][11]+c[i][10]+c[i][ 9]-c[i][ 8]
  66. -c[i][ 7]+c[i][ 6]+c[i][ 5]-c[i][ 4] + c[i][ 3]-c[i][ 2]-c[i][ 1]+c[i][ 0] ) +
  67. s21 * ( c[i][14]-c[i][12]-c[i][10]+c[i][ 8] - c[i][ 6]+c[i][ 4]+c[i][ 2]-c[i][ 0] ) +
  68. s20 * ( c[i][13]-c[i][12]-c[i][ 9]+c[i][ 8] - c[i][ 5]+c[i][ 4]+c[i][ 1]-c[i][ 0] ) +
  69. s10 * ( c[i][11]-c[i][10]-c[i][ 9]+c[i][ 8] - c[i][ 3]+c[i][ 2]+c[i][ 1]-c[i][ 0] ) +
  70. s[2] * ( c[i][12]-c[i][ 8]-c[i][ 4]+c[i][ 0] ) +
  71. s[1] * ( c[i][10]-c[i][ 8]-c[i][ 2]+c[i][ 0] ) +
  72. s[0] * ( c[i][ 9]-c[i][ 8]-c[i][ 1]+c[i][ 0] ) +
  73. ( c[i][ 8]-c[i][ 0] ) ) * s[3] +
  74. ( s210 * ( c[i][ 7]-c[i][ 6]-c[i][ 5]+c[i][ 4] - c[i][ 3]+c[i][ 2]+c[i][ 1]-c[i][ 0] ) +
  75. s21 * ( c[i][ 6]-c[i][ 4]-c[i][ 2]+c[i][ 0] ) +
  76. s20 * ( c[i][ 5]-c[i][ 4]-c[i][ 1]+c[i][ 0] ) +
  77. s10 * ( c[i][ 3]-c[i][ 2]-c[i][ 1]+c[i][ 0] ) +
  78. s[2] * ( c[i][ 4]-c[i][ 0] ) +
  79. s[1] * ( c[i][ 2]-c[i][ 0] ) +
  80. s[0] * ( c[i][ 1]-c[i][ 0] ) +
  81. ( c[i][ 0] ));
  82. */
  83. }
  84. }
  85. template Mux4() {
  86. var i;
  87. signal input c[16]; // Constants
  88. signal input s[4]; // Selector
  89. signal output out;
  90. component mux = MultiMux4(1);
  91. for (i=0; i<16; i++) {
  92. mux.c[0][i] <== c[i];
  93. }
  94. for (i=0; i<4; i++) {
  95. s[i] ==> mux.s[i];
  96. }
  97. mux.out[0] ==> out;
  98. }