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.

118 lines
6.3 KiB

  1. /***************************************************************************************************
  2. Each level on a SMTInsert has a state.
  3. The state of the level depends on the state of te botom level and on `xor` and
  4. `is0` signals.
  5. `isOldLev` 1 when is the level where oldLeaf is.
  6. `xor` signal is 0 if the index bit at the current level is the same in the old
  7. and the new index, and 1 if it is different.
  8. `is0` signal, is 1 if we are inserting in an empty leaf and 0 if we are inserting
  9. in a leaf that contains an element.
  10. The states are:
  11. top: While the index bits of the old and new insex in the top level is the same, whe are in the top state.
  12. old0 and old1: When the we reach insert level, we go to old0 and old1 states
  13. according to `is0` signal.
  14. btn: Once in old1 we go to btn until xor=1
  15. new1: This level is reached when xor=1. Here is where we insert the hash of the
  16. old and the new trees with just one element.
  17. na: Not appliable. After inserting it, we go to the na level.
  18. Fnction
  19. fnc[0] fnc[1]
  20. 0 0 NOP
  21. 0 1 UPDATE
  22. 1 0 INSERT
  23. 1 1 DELETE
  24. ###########
  25. # #
  26. ┌────────────────────────────▶# upd #─────────────────────┐
  27. │ ## ## │
  28. │ ######### │
  29. levIns=1 │ │
  30. fnc[0]=0 │ │ any
  31. │ │
  32. │ │
  33. │ │
  34. │ ########### │
  35. │ levIns=1 # # │
  36. levIns=0 │ is0=1 ┌────────────▶# old0 #────────┐ │ any
  37. ┌─────┐ │ fnc[0]=1│ ## ## │ │ ┌──────┐
  38. │ │ │ │ ######### │ any │ │ │
  39. │ ▼ │ │ │ ▼ ▼ │
  40. │ ########### │ │ ########### │
  41. │ # # ────────────┘ └────────▶# #│
  42. └──# top # # na #
  43. ## ## ────┐ ┌──▶## ##
  44. ######### │ │ #########
  45. │ │
  46. │ ########### ########### │ any
  47. levIns=1 │ # # xor=1 # # │
  48. is0=0 └───▶# old1 #─────────────▶# new1 #──┘
  49. fnc[0]=1 ## ## ## ##
  50. #########│ #########
  51. │ ▲
  52. └───┐ ┌─────┘
  53. xor=0 │ ###########│ xor=1
  54. │ # #
  55. ▼# btn #
  56. ## ##
  57. #########◀───────┐
  58. │ │
  59. │ │
  60. └────────────┘
  61. xor=0
  62. ***************************************************************************************************/
  63. template SMTInsertSM() {
  64. signal input xor;
  65. signal input is0;
  66. signal input levIns;
  67. signal input fnc[2];
  68. signal input prev_top;
  69. signal input prev_old0;
  70. signal input prev_old1;
  71. signal input prev_bot;
  72. signal input prev_new1;
  73. signal input prev_na;
  74. signal input prev_upd;
  75. signal output st_top;
  76. signal output st_old0;
  77. signal output st_old1;
  78. signal output st_bot;
  79. signal output st_new1;
  80. signal output st_na;
  81. signal output st_upd;
  82. signal aux1;
  83. signal aux2;
  84. aux1 <== prev_top * levIns;
  85. aux2 <== aux1*is0;
  86. st_top <== prev_top - aux1 // prev_top * (1-levIns) =
  87. // = prev_top - aux1;
  88. st_old0 <== aux2 * fnc[0]; // (prev_top * levIns * is0)*fnc[0] = aux2
  89. st_old1 <== (aux1 - aux2)*fnc[0]; // (prev_top * levIns * (1-is0))*fnc[0] =
  90. // = (aux1 * (1-is0))*fnc[0] =
  91. // = (aux1 - aux2) * fnc[0]
  92. st_new1 <== (prev_old1 + prev_bot)*xor // prev_old1*xor + prev_bot*xor =
  93. // = (prev_old1 + prev_bot)*xor;
  94. st_bot <== -st_new1 + prev_old1 + prev_bot // prev_old1*(1-xor) + prev_bot*(1-xor) =
  95. // = - prev_old1*xor -prev_bot*xor + prev_old1 + prev_bot =
  96. // = -st_new1 + prev_old1 + prev_bot
  97. st_na <== prev_new1 + prev_old0 + prev_na + prev_upd;
  98. st_upd <== aux1*(1-fnc[0]); // prev_top*levIns*(1-fnc[0]) =
  99. // = aux1 * (1-fnc[0])
  100. }