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.

138 lines
3.8 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. SMTVerifier is a component to verify inclusion/exclusion of an element in the tree
  17. fnc: 0 -> VERIFY INCLUSION
  18. 1 -> VERIFY NOT INCLUSION
  19. */
  20. pragma circom 2.0.0;
  21. include "../gates.circom";
  22. include "../bitify.circom";
  23. include "../comparators.circom";
  24. include "../switcher.circom";
  25. include "smtlevins.circom";
  26. include "smtverifierlevel.circom";
  27. include "smtverifiersm.circom";
  28. include "smthash_poseidon.circom";
  29. template SMTVerifier(nLevels) {
  30. signal input enabled;
  31. signal input root;
  32. signal input siblings[nLevels];
  33. signal input oldKey;
  34. signal input oldValue;
  35. signal input isOld0;
  36. signal input key;
  37. signal input value;
  38. signal input fnc;
  39. var i;
  40. component hash1Old = SMTHash1();
  41. hash1Old.key <== oldKey;
  42. hash1Old.value <== oldValue;
  43. component hash1New = SMTHash1();
  44. hash1New.key <== key;
  45. hash1New.value <== value;
  46. component n2bOld = Num2Bits_strict();
  47. component n2bNew = Num2Bits_strict();
  48. n2bOld.in <== oldKey;
  49. n2bNew.in <== key;
  50. component smtLevIns = SMTLevIns(nLevels);
  51. for (i=0; i<nLevels; i++) smtLevIns.siblings[i] <== siblings[i];
  52. smtLevIns.enabled <== enabled;
  53. component sm[nLevels];
  54. for (i=0; i<nLevels; i++) {
  55. sm[i] = SMTVerifierSM();
  56. if (i==0) {
  57. sm[i].prev_top <== enabled;
  58. sm[i].prev_i0 <== 0;
  59. sm[i].prev_inew <== 0;
  60. sm[i].prev_iold <== 0;
  61. sm[i].prev_na <== 1-enabled;
  62. } else {
  63. sm[i].prev_top <== sm[i-1].st_top;
  64. sm[i].prev_i0 <== sm[i-1].st_i0;
  65. sm[i].prev_inew <== sm[i-1].st_inew;
  66. sm[i].prev_iold <== sm[i-1].st_iold;
  67. sm[i].prev_na <== sm[i-1].st_na;
  68. }
  69. sm[i].is0 <== isOld0;
  70. sm[i].fnc <== fnc;
  71. sm[i].levIns <== smtLevIns.levIns[i];
  72. }
  73. sm[nLevels-1].st_na + sm[nLevels-1].st_iold + sm[nLevels-1].st_inew + sm[nLevels-1].st_i0 === 1;
  74. component levels[nLevels];
  75. for (i=nLevels-1; i != -1; i--) {
  76. levels[i] = SMTVerifierLevel();
  77. levels[i].st_top <== sm[i].st_top;
  78. levels[i].st_i0 <== sm[i].st_i0;
  79. levels[i].st_inew <== sm[i].st_inew;
  80. levels[i].st_iold <== sm[i].st_iold;
  81. levels[i].st_na <== sm[i].st_na;
  82. levels[i].sibling <== siblings[i];
  83. levels[i].old1leaf <== hash1Old.out;
  84. levels[i].new1leaf <== hash1New.out;
  85. levels[i].lrbit <== n2bNew.out[i];
  86. if (i==nLevels-1) {
  87. levels[i].child <== 0;
  88. } else {
  89. levels[i].child <== levels[i+1].root;
  90. }
  91. }
  92. // Check that if checking for non inclussuin and isOld0==0 then key!=old
  93. component areKeyEquals = IsEqual();
  94. areKeyEquals.in[0] <== oldKey;
  95. areKeyEquals.in[1] <== key;
  96. component keysOk = MultiAND(4);
  97. keysOk.in[0] <== fnc;
  98. keysOk.in[1] <== 1-isOld0;
  99. keysOk.in[2] <== areKeyEquals.out;
  100. keysOk.in[3] <== enabled;
  101. keysOk.out === 0;
  102. // Check the root
  103. component checkRoot = ForceEqualIfEnabled();
  104. checkRoot.enabled <== enabled;
  105. checkRoot.in[0] <== levels[0].root;
  106. checkRoot.in[1] <== root;
  107. // levels[0].root === root;
  108. }