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.

81 lines
3.3 KiB

  1. /*
  2. This component finds the level where the oldInsert is done.
  3. The rules are:
  4. levIns[i] == 1 if its level and all the child levels have a sibling of 0 and
  5. the parent level has a sibling != 0. Considere that the root level always has
  6. a parent with a sibling != 0.
  7. ┌──────────────┐
  8. │ │
  9. │ │───▶ levIns[0] <== (1-done[i])
  10. │ │
  11. └──────────────┘
  12. done[0]
  13. done[i-1] <== levIns[i] + done[i]
  14. ┌───────────┐ ┌──────────────┐
  15. │ │ │ │
  16. sibling[i-1]───▶│IsZero[i-1]│─▶│ │───▶ levIns[i] <== (1-done[i])*(1-isZero[i-1].out)
  17. │ │ │ │
  18. └───────────┘ └──────────────┘
  19. done[i]
  20. done[n-2] <== levIns[n-1]
  21. ┌───────────┐ ┌──────────────┐
  22. │ │ │ │
  23. sibling[n-2]───▶│IsZero[n-2]│─▶│ │────▶ levIns[n-1] <== (1-isZero[n-2].out)
  24. │ │ │ │
  25. └───────────┘ └──────────────┘
  26. ┌───────────┐
  27. │ │
  28. sibling[n-1]───▶│IsZero[n-1]│────▶ === 0
  29. │ │
  30. └───────────┘
  31. */
  32. template SMTLevIns(nLevels) {
  33. signal input enabled;
  34. signal input siblings[nLevels];
  35. signal output levIns[nLevels];
  36. signal done[nLevels-1]; // Indicates if the insLevel has aready been detecetd.
  37. component isZero[nLevels];
  38. for (var i=0; i<nLevels; i++) {
  39. isZero[i] = IsZero();
  40. isZero[i].in <== siblings[i];
  41. }
  42. // The last level must always have a sibling of 0. If not, then it cannot be inserted.
  43. (isZero[nLevels-2].out - 1) * enabled === 0;
  44. levIns[nLevels-1] <== (1-isZero[nLevels-2].out);
  45. done[nLevels-2] <== levIns[nLevels-1];
  46. for (var i=nLevels-2; i>0; i--) {
  47. levIns[i] <== (1-done[i])*(1-isZero[i-1].out)
  48. done[i-1] <== levIns[i] + done[i];
  49. }
  50. levIns[0] <== (1-done[0]);
  51. }