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.

117 lines
3.0 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 xors[nLevels];
  37. for (var i=0; i<nLevels; i++) {
  38. xors[i] = XOR();
  39. xors[i].a <== n2bOld.out[i];
  40. xors[i].b <== n2bNew.out[i];
  41. }
  42. component sm[nLevels];
  43. for (var i=0; i<nLevels; i++) {
  44. sm[i] = SMTVerifierSM();
  45. if (i==0) {
  46. sm[i].prev_top <== 1;
  47. sm[i].prev_i0 <== 0;
  48. sm[i].prev_inew <== 0;
  49. sm[i].prev_iold <== 0;
  50. sm[i].prev_na <== 0;
  51. } else {
  52. sm[i].prev_top <== sm[i-1].st_top;
  53. sm[i].prev_i0 <== sm[i-1].st_i0;
  54. sm[i].prev_inew <== sm[i-1].st_inew;
  55. sm[i].prev_iold <== sm[i-1].st_iold;
  56. sm[i].prev_na <== sm[i-1].st_na;
  57. }
  58. sm[i].is0 <== isOld0;
  59. sm[i].xor <== xors[i].out;
  60. sm[i].fnc <== fnc;
  61. sm[i].levIns <== smtLevIns.levIns[i];
  62. }
  63. // sm[nLevels-1].st_na === 1;
  64. component levels[nLevels];
  65. for (var i=nLevels-1; i != -1; i--) {
  66. levels[i] = SMTVerifierLevel();
  67. levels[i].st_top <== sm[i].st_top;
  68. levels[i].st_i0 <== sm[i].st_i0;
  69. levels[i].st_inew <== sm[i].st_inew;
  70. levels[i].st_iold <== sm[i].st_iold;
  71. levels[i].st_na <== sm[i].st_na;
  72. levels[i].sibling <== siblings[i];
  73. levels[i].old1leaf <== hash1Old.out;
  74. levels[i].new1leaf <== hash1New.out;
  75. levels[i].lrbit <== n2bNew.out[i];
  76. if (i==nLevels-1) {
  77. levels[i].child <== 0;
  78. } else {
  79. levels[i].child <== levels[i+1].root;
  80. }
  81. }
  82. // Check that if checking for non inclussuin and isOld0==0 then key!=old
  83. component areKeyEquals = IsEqual();
  84. areKeyEquals.in[0] <== oldKey;
  85. areKeyEquals.in[1] <== key;
  86. component keysOk = MultiAND(3);
  87. keysOk.in[0] <== fnc;
  88. keysOk.in[1] <== 1-isOld0;
  89. keysOk.in[2] <== areKeyEquals.out;
  90. keysOk.out === 0;
  91. // Check the roots
  92. levels[0].root === root;
  93. }