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.

165 lines
7.4 KiB

  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. Each level on a SMTProcessor has a state.
  17. The state of the level depends on the state of te botom level and on `xor` and
  18. `is0` signals.
  19. `isOldLev` 1 when is the level where oldLeaf is.
  20. `xor` signal is 0 if the index bit at the current level is the same in the old
  21. and the new index, and 1 if it is different.
  22. `is0` signal, is 1 if we are inserting/deleting in an empty leaf and 0 if we
  23. are inserting/deleting in a leaf that contains an element.
  24. The states are:
  25. top: While the index bits of the old and new insex in the top level is the same, whe are in the top state.
  26. old0: When the we reach insert level, we go to old0 state
  27. if `is0`=1.
  28. btn: Once in insert level and `is0` =0 we go to btn or new1 level if xor=1
  29. new1: This level is reached when xor=1. Here is where we insert/delete the hash of the
  30. old and the new trees with just one element.
  31. na: Not appliable. After processing it, we go to the na level.
  32. Fnction
  33. fnc[0] fnc[1]
  34. 0 0 NOP
  35. 0 1 UPDATE
  36. 1 0 INSERT
  37. 1 1 DELETE
  38. ###########
  39. # #
  40. ┌────────────────────────────▶# upd #─────────────────────┐
  41. │ ## ## │
  42. │ ######### │
  43. levIns=1 │ │
  44. fnc[0]=0 │ │ any
  45. │ │
  46. │ │
  47. │ │
  48. │ ########### │
  49. │ levIns=1 # # │
  50. levIns=0 │ is0=1 ┌────────────▶# old0 #────────┐ │ any
  51. ┌─────┐ │ fnc[0]=1│ ## ## │ │ ┌──────┐
  52. │ │ │ │ ######### │ any │ │ │
  53. │ ▼ │ │ │ ▼ ▼ │
  54. │ ########### │ │ ########### │
  55. │ # # ────────────┘ └────────▶# #│
  56. └──# top # # na #
  57. ## ## ───────────────────┐ levIns=1 ┌──▶## ##
  58. ######### │ is0=0 │ #########
  59. │ │ fnc[0]=1 │
  60. │ │ xor=1 ########### │ any
  61. │ └──────────────────▶# # │
  62. │ # new1 #──┘
  63. │ ## ##
  64. └────────────────────────────────┐ #########
  65. levIns=1 │ ▲
  66. is0=0 │ ┌─────┘
  67. fnc[0]=1 │ ###########│ xor=1
  68. xor=0 │ # #
  69. ▼# btn #
  70. ## ##
  71. #########◀───────┐
  72. │ │
  73. │ │
  74. └────────────┘
  75. xor=0
  76. ***************************************************************************************************/
  77. pragma circom 2.0.0;
  78. template SMTProcessorSM() {
  79. signal input xor;
  80. signal input is0;
  81. signal input levIns;
  82. signal input fnc[2];
  83. signal input prev_top;
  84. signal input prev_old0;
  85. signal input prev_bot;
  86. signal input prev_new1;
  87. signal input prev_na;
  88. signal input prev_upd;
  89. signal output st_top;
  90. signal output st_old0;
  91. signal output st_bot;
  92. signal output st_new1;
  93. signal output st_na;
  94. signal output st_upd;
  95. signal aux1;
  96. signal aux2;
  97. aux1 <== prev_top * levIns;
  98. aux2 <== aux1*fnc[0]; // prev_top * levIns * fnc[0]
  99. // st_top = prev_top*(1-levIns)
  100. // = + prev_top
  101. // - prev_top * levIns = aux1
  102. st_top <== prev_top - aux1;
  103. // st_old0 = prev_top * levIns * is0 * fnc[0]
  104. // = + prev_top * levIns * is0 * fnc[0] = aux2 * is0
  105. st_old0 <== aux2 * is0; // prev_top * levIns * is0 * fnc[0]
  106. // st_new1 = prev_top * levIns * (1-is0)*fnc[0] * xor + prev_bot*xor =
  107. // = + prev_top * levIns * fnc[0] * xor = aux2 * xor
  108. // - prev_top * levIns * is0 * fnc[0] * xor = st_old0 * xor
  109. // + prev_bot * xor = prev_bot * xor
  110. st_new1 <== (aux2 - st_old0 + prev_bot)*xor;
  111. // st_bot = prev_top * levIns * (1-is0)*fnc[0] * (1-xor) + prev_bot*(1-xor);
  112. // = + prev_top * levIns * fnc[0]
  113. // - prev_top * levIns * is0 * fnc[0]
  114. // - prev_top * levIns * fnc[0] * xor
  115. // + prev_top * levIns * is0 * fnc[0] * xor
  116. // + prev_bot
  117. // - prev_bot * xor
  118. st_bot <== (1-xor) * (aux2 - st_old0 + prev_bot);
  119. // st_upd = prev_top * (1-fnc[0]) *levIns;
  120. // = + prev_top * levIns
  121. // - prev_top * levIns * fnc[0]
  122. st_upd <== aux1 - aux2;
  123. // st_na = prev_new1 + prev_old0 + prev_na + prev_upd;
  124. // = + prev_new1
  125. // + prev_old0
  126. // + prev_na
  127. // + prev_upd
  128. st_na <== prev_new1 + prev_old0 + prev_na + prev_upd;
  129. }