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.

164 lines
7.4 KiB

6 years ago
6 years ago
6 years ago
6 years ago
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. /***************************************************************************************************
  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. template SMTProcessorSM() {
  78. signal input xor;
  79. signal input is0;
  80. signal input levIns;
  81. signal input fnc[2];
  82. signal input prev_top;
  83. signal input prev_old0;
  84. signal input prev_bot;
  85. signal input prev_new1;
  86. signal input prev_na;
  87. signal input prev_upd;
  88. signal output st_top;
  89. signal output st_old0;
  90. signal output st_bot;
  91. signal output st_new1;
  92. signal output st_na;
  93. signal output st_upd;
  94. signal aux1;
  95. signal aux2;
  96. aux1 <== prev_top * levIns;
  97. aux2 <== aux1*fnc[0]; // prev_top * levIns * fnc[0]
  98. // st_top = prev_top*(1-levIns)
  99. // = + prev_top
  100. // - prev_top * levIns = aux1
  101. st_top <== prev_top - aux1;
  102. // st_old0 = prev_top * levIns * is0 * fnc[0]
  103. // = + prev_top * levIns * is0 * fnc[0] = aux2 * is0
  104. st_old0 <== aux2 * is0; // prev_top * levIns * is0 * fnc[0]
  105. // st_new1 = prev_top * levIns * (1-is0)*fnc[0] * xor + prev_bot*xor =
  106. // = + prev_top * levIns * fnc[0] * xor = aux2 * xor
  107. // - prev_top * levIns * is0 * fnc[0] * xor = st_old0 * xor
  108. // + prev_bot * xor = prev_bot * xor
  109. st_new1 <== (aux2 - st_old0 + prev_bot)*xor;
  110. // st_bot = prev_top * levIns * (1-is0)*fnc[0] * (1-xor) + prev_bot*(1-xor);
  111. // = + prev_top * levIns * fnc[0]
  112. // - prev_top * levIns * is0 * fnc[0]
  113. // - prev_top * levIns * fnc[0] * xor
  114. // + prev_top * levIns * is0 * fnc[0] * xor
  115. // + prev_bot
  116. // - prev_bot * xor
  117. st_bot <== (1-xor) * (aux2 - st_old0 + prev_bot)
  118. // st_upd = prev_top * (1-fnc[0]) *levIns;
  119. // = + prev_top * levIns
  120. // - prev_top * levIns * fnc[0]
  121. st_upd <== aux1 - aux2
  122. // st_na = prev_new1 + prev_old0 + prev_na + prev_upd;
  123. // = + prev_new1
  124. // + prev_old0
  125. // + prev_na
  126. // + prev_upd
  127. st_na <== prev_new1 + prev_old0 + prev_na + prev_upd;
  128. }