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.

137 lines
3.8 KiB

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