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.

128 lines
3.5 KiB

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