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.

109 lines
2.8 KiB

  1. /*
  2. SMTVerifier is a component to verify inclusion/exclusion of an element in the tree
  3. fnc: 0 -> VERIFY INCLUSION
  4. 1 -> VERIFY NOT INCLUSION
  5. */
  6. include "../gates.circom";
  7. include "../bitify.circom";
  8. include "../comparators.circom";
  9. include "../switcher.circom";
  10. include "smtlevins.circom";
  11. include "smtverifierlevel.circom";
  12. include "smtverifiersm.circom";
  13. include "smthash.circom";
  14. template SMTVerifier(nLevels) {
  15. signal input root;
  16. signal input siblings[nLevels];
  17. signal input oldKey;
  18. signal input oldValue;
  19. signal input isOld0;
  20. signal input key;
  21. signal input value;
  22. signal input fnc;
  23. component hash1Old = SMTHash1();
  24. hash1Old.key <== oldKey;
  25. hash1Old.value <== oldValue;
  26. component hash1New = SMTHash1();
  27. hash1New.key <== key;
  28. hash1New.value <== value;
  29. component n2bOld = Num2Bits_strict();
  30. component n2bNew = Num2Bits_strict();
  31. n2bOld.in <== oldKey;
  32. n2bNew.in <== key;
  33. component smtLevIns = SMTLevIns(nLevels);
  34. for (var i=0; i<nLevels; i++) smtLevIns.siblings[i] <== siblings[i];
  35. smtLevIns.enabled <== 1;
  36. component sm[nLevels];
  37. for (var i=0; i<nLevels; i++) {
  38. sm[i] = SMTVerifierSM();
  39. if (i==0) {
  40. sm[i].prev_top <== 1;
  41. sm[i].prev_i0 <== 0;
  42. sm[i].prev_inew <== 0;
  43. sm[i].prev_iold <== 0;
  44. sm[i].prev_na <== 0;
  45. } else {
  46. sm[i].prev_top <== sm[i-1].st_top;
  47. sm[i].prev_i0 <== sm[i-1].st_i0;
  48. sm[i].prev_inew <== sm[i-1].st_inew;
  49. sm[i].prev_iold <== sm[i-1].st_iold;
  50. sm[i].prev_na <== sm[i-1].st_na;
  51. }
  52. sm[i].is0 <== isOld0;
  53. sm[i].fnc <== fnc;
  54. sm[i].levIns <== smtLevIns.levIns[i];
  55. }
  56. sm[nLevels-1].st_na === 1;
  57. component levels[nLevels];
  58. for (var i=nLevels-1; i != -1; i--) {
  59. levels[i] = SMTVerifierLevel();
  60. levels[i].st_top <== sm[i].st_top;
  61. levels[i].st_i0 <== sm[i].st_i0;
  62. levels[i].st_inew <== sm[i].st_inew;
  63. levels[i].st_iold <== sm[i].st_iold;
  64. levels[i].st_na <== sm[i].st_na;
  65. levels[i].sibling <== siblings[i];
  66. levels[i].old1leaf <== hash1Old.out;
  67. levels[i].new1leaf <== hash1New.out;
  68. levels[i].lrbit <== n2bNew.out[i];
  69. if (i==nLevels-1) {
  70. levels[i].child <== 0;
  71. } else {
  72. levels[i].child <== levels[i+1].root;
  73. }
  74. }
  75. // Check that if checking for non inclussuin and isOld0==0 then key!=old
  76. component areKeyEquals = IsEqual();
  77. areKeyEquals.in[0] <== oldKey;
  78. areKeyEquals.in[1] <== key;
  79. component keysOk = MultiAND(3);
  80. keysOk.in[0] <== fnc;
  81. keysOk.in[1] <== 1-isOld0;
  82. keysOk.in[2] <== areKeyEquals.out;
  83. keysOk.out === 0;
  84. // Check the roots
  85. levels[0].root === root;
  86. }