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.

103 lines
3.8 KiB

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