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.

74 lines
1.5 KiB

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