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.

135 lines
3.8 KiB

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