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.

106 lines
4.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 in the SMTVerifier has a state.
  17. This is the state machine.
  18. The signals are
  19. levIns: 1 if we are in the level where the insertion should happen
  20. xor: 1 if the bitKey of the old and new keys are different in this level
  21. is0: Input that indicates that the oldKey is 0
  22. fnc: 0 -> VERIFY INCLUSION
  23. 1 -> VERIFY NOT INCLUSION
  24. err state is not a state itself. It's a lack of state.
  25. The end of the last level will have to be `na`
  26. levIns=0 any
  27. ┌────┐ ┌────┐
  28. │ │ │ │
  29. │ ▼ levIns=1 ▼ │
  30. │ ########### is0=1 ########### ########### │
  31. │ # # fnc=1 # # any # # │
  32. └──# top # ─────────────────────▶# i0 #───────────────▶# na #──┘
  33. ## ## ──────────┐ ## ## ┌───────▶## ##
  34. ########─────────────┐│ ######### │┌────────▶#########
  35. ││ levIns=1 ││
  36. ││ is0=0 ########### ││
  37. ││ fnc=1 # # any│
  38. │└──────────▶ # iold #────────┘│
  39. │ ## ## │
  40. │ ######### │
  41. │ │
  42. │ levIns=1 ########### │
  43. │ fnc=0 # # any
  44. └────────────▶# inew #─────────┘
  45. ## ##
  46. #########
  47. */
  48. pragma circom 2.0.0;
  49. template SMTVerifierSM() {
  50. signal input is0;
  51. signal input levIns;
  52. signal input fnc;
  53. signal input prev_top;
  54. signal input prev_i0;
  55. signal input prev_iold;
  56. signal input prev_inew;
  57. signal input prev_na;
  58. signal output st_top;
  59. signal output st_i0;
  60. signal output st_iold;
  61. signal output st_inew;
  62. signal output st_na;
  63. signal prev_top_lev_ins;
  64. signal prev_top_lev_ins_fnc;
  65. prev_top_lev_ins <== prev_top * levIns;
  66. prev_top_lev_ins_fnc <== prev_top_lev_ins*fnc; // prev_top * levIns * fnc
  67. // st_top = prev_top * (1-levIns)
  68. // = + prev_top
  69. // - prev_top * levIns
  70. st_top <== prev_top - prev_top_lev_ins;
  71. // st_inew = prev_top * levIns * (1-fnc)
  72. // = + prev_top * levIns
  73. // - prev_top * levIns * fnc
  74. st_inew <== prev_top_lev_ins - prev_top_lev_ins_fnc;
  75. // st_iold = prev_top * levIns * (1-is0)*fnc
  76. // = + prev_top * levIns * fnc
  77. // - prev_top * levIns * fnc * is0
  78. st_iold <== prev_top_lev_ins_fnc * (1 - is0);
  79. // st_i0 = prev_top * levIns * is0
  80. // = + prev_top * levIns * is0
  81. st_i0 <== prev_top_lev_ins * is0;
  82. st_na <== prev_na + prev_inew + prev_iold + prev_i0;
  83. }