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.

113 lines
2.9 KiB

5 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. /*
  16. Copyright 2018 0KIMS association.
  17. This file is part of circom (Zero Knowledge Circuit Compiler).
  18. circom is a free software: you can redistribute it and/or modify it
  19. under the terms of the GNU General Public License as published by
  20. the Free Software Foundation, either version 3 of the License, or
  21. (at your option) any later version.
  22. circom is distributed in the hope that it will be useful, but WITHOUT
  23. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  24. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  25. License for more details.
  26. You should have received a copy of the GNU General Public License
  27. along with circom. If not, see <https://www.gnu.org/licenses/>.
  28. */
  29. // --> Assignation without constraint
  30. // <-- Assignation without constraint
  31. // === Constraint
  32. // <== Assignation with constraint
  33. // ==> Assignation with constraint
  34. // All variables are members of the field F[p]
  35. // https://github.com/zcash-hackworks/sapling-crypto
  36. // https://github.com/ebfull/bellman
  37. /*
  38. function log2(a) {
  39. if (a==0) {
  40. return 0;
  41. }
  42. let n = 1;
  43. let r = 1;
  44. while (n<a) {
  45. r++;
  46. n *= 2;
  47. }
  48. return r;
  49. }
  50. */
  51. template EscalarProduct(w) {
  52. signal input in1[w];
  53. signal input in2[w];
  54. signal output out;
  55. signal aux[w];
  56. var lc = 0;
  57. for (var i=0; i<w; i++) {
  58. aux[i] <== in1[i]*in2[i];
  59. lc = lc + aux[i];
  60. }
  61. out <== lc;
  62. }
  63. template Decoder(w) {
  64. signal input inp;
  65. signal output out[w];
  66. signal output success;
  67. var lc=0;
  68. for (var i=0; i<w; i++) {
  69. out[i] <-- (inp == i) ? 1 : 0;
  70. out[i] * (inp-i) === 0;
  71. lc = lc + out[i];
  72. }
  73. lc ==> success;
  74. success * (success -1) === 0;
  75. }
  76. template Multiplexer(wIn, nIn) {
  77. signal input inp[nIn][wIn];
  78. signal input sel;
  79. signal output out[wIn];
  80. component dec = Decoder(nIn);
  81. component ep[wIn];
  82. for (var k=0; k<wIn; k++) {
  83. ep[k] = EscalarProduct(nIn);
  84. }
  85. sel ==> dec.inp;
  86. for (var j=0; j<wIn; j++) {
  87. for (var k=0; k<nIn; k++) {
  88. inp[k][j] ==> ep[j].in1[k];
  89. dec.out[k] ==> ep[j].in2[k];
  90. }
  91. ep[j].out ==> out[j];
  92. }
  93. dec.success === 1;
  94. }