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.

93 lines
2.2 KiB

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. // --> Assignation without constraint
  16. // <-- Assignation without constraint
  17. // === Constraint
  18. // <== Assignation with constraint
  19. // ==> Assignation with constraint
  20. // All variables are members of the field F[p]
  21. // https://github.com/zcash-hackworks/sapling-crypto
  22. // https://github.com/ebfull/bellman
  23. /*
  24. function log2(a) {
  25. if (a==0) {
  26. return 0;
  27. }
  28. let n = 1;
  29. let r = 1;
  30. while (n<a) {
  31. r++;
  32. n *= 2;
  33. }
  34. return r;
  35. }
  36. */
  37. template EscalarProduct(w) {
  38. signal input in1[w];
  39. signal input in2[w];
  40. signal output out;
  41. signal aux[w];
  42. var lc = 0;
  43. for (var i=0; i<w; i++) {
  44. aux[i] <== in1[i]*in2[i];
  45. lc = lc + aux[i];
  46. }
  47. out <== lc;
  48. }
  49. template Decoder(w) {
  50. signal input inp;
  51. signal output out[w];
  52. signal output success;
  53. var lc=0;
  54. for (var i=0; i<w; i++) {
  55. out[i] <-- (inp == i) ? 1 : 0;
  56. out[i] * (inp-i) === 0;
  57. lc = lc + out[i];
  58. }
  59. lc ==> success;
  60. success * (success -1) === 0;
  61. }
  62. template Multiplexor(wIn, nIn) {
  63. signal input inp[nIn][wIn];
  64. signal input sel;
  65. signal output out[wIn];
  66. component Decoder(nIn) dec;
  67. component EscalarProduct(nIn) ep[wIn];
  68. sel ==> dec.inp;
  69. for (var j=0; j<wIn; j++) {
  70. for (var k=0; k<nIn; k++) {
  71. inp[k][j] ==> ep[j].in1[k];
  72. dec.out[k] ==> ep[j].in2[k];
  73. }
  74. ep[j].out ==> out[j];
  75. }
  76. dec.success === 1;
  77. }
  78. component Multiplexor(8,3) main;