|
| 1 | +include "merkle_check.pil"; |
| 2 | + |
| 3 | +include "../ff_gt.pil"; |
| 4 | +include "../poseidon2_hash.pil"; |
| 5 | +include "../constants_gen.pil"; |
| 6 | +include "../precomputed.pil"; |
| 7 | + |
| 8 | +// This gadget is used to track the unique class ids that have been retrieved in the TX. |
| 9 | +// It's a transient indexed tree that starts empty (with a prefill) on every transaction, |
| 10 | +// and it's discarded at the end of the transaction execution. |
| 11 | +// The leaves only contain the class id and the indexed tree pointers. |
| 12 | +// |
| 13 | +// Read usage: |
| 14 | +// sel { |
| 15 | +// class_id, |
| 16 | +// leaf_not_exists, |
| 17 | +// retrieved_bytecodes_tree_root |
| 18 | +// } in retrieved_bytecodes_tree_check.sel { |
| 19 | +// retrieved_bytecodes_tree_check.class_id, |
| 20 | +// retrieved_bytecodes_tree_check.leaf_not_exists, |
| 21 | +// retrieved_bytecodes_tree_check.root |
| 22 | +// }; |
| 23 | +// |
| 24 | +// Write usage: |
| 25 | +// sel { |
| 26 | +// class_id, |
| 27 | +// sel, // 1 |
| 28 | +// prev_retrieved_bytecodes_tree_root, |
| 29 | +// prev_retrieved_bytecodes_tree_size, |
| 30 | +// next_retrieved_bytecodes_tree_root, |
| 31 | +// next_retrieved_bytecodes_tree_size |
| 32 | +// } in retrieved_bytecodes_tree_check.sel { |
| 33 | +// retrieved_bytecodes_tree_check.class_id, |
| 34 | +// retrieved_bytecodes_tree_check.write, |
| 35 | +// retrieved_bytecodes_tree_check.root, |
| 36 | +// retrieved_bytecodes_tree_check.tree_size_before_write, |
| 37 | +// retrieved_bytecodes_tree_check.write_root, |
| 38 | +// retrieved_bytecodes_tree_check.tree_size_after_write |
| 39 | +// }; |
| 40 | + |
| 41 | +namespace retrieved_bytecodes_tree_check; |
| 42 | + pol commit sel; |
| 43 | + sel * (1 - sel) = 0; |
| 44 | + |
| 45 | + #[skippable_if] |
| 46 | + sel = 0; |
| 47 | + |
| 48 | + // Inputs to the gadget |
| 49 | + pol commit write; |
| 50 | + write * (1 - write) = 0; |
| 51 | + pol READ = 1 - write; |
| 52 | + |
| 53 | + pol commit class_id; |
| 54 | + pol commit root; |
| 55 | + pol commit leaf_not_exists; |
| 56 | + leaf_not_exists * (1 - leaf_not_exists) = 0; |
| 57 | + pol EXISTS = 1 - leaf_not_exists; |
| 58 | + |
| 59 | + // Write specific inputs |
| 60 | + pol commit write_root; |
| 61 | + pol commit tree_size_before_write; |
| 62 | + pol commit tree_size_after_write; |
| 63 | + |
| 64 | + // Hints |
| 65 | + pol commit low_leaf_class_id; |
| 66 | + pol commit low_leaf_next_index; |
| 67 | + pol commit low_leaf_next_class_id; |
| 68 | + |
| 69 | + pol commit updated_low_leaf_next_index; |
| 70 | + pol commit updated_low_leaf_next_class_id; |
| 71 | + |
| 72 | + pol commit low_leaf_index; |
| 73 | + |
| 74 | + // ========= HANDLE REDUNDANT WRITES ========= |
| 75 | + pol commit should_insert; |
| 76 | + should_insert = write * leaf_not_exists; |
| 77 | + // On a failing write, the root must not change |
| 78 | + write * EXISTS * (root - write_root) = 0; |
| 79 | + |
| 80 | + tree_size_after_write = tree_size_before_write + should_insert; |
| 81 | + |
| 82 | + // ========= COMPUTE LOW LEAF UPDATE ========= |
| 83 | + should_insert * (tree_size_before_write - updated_low_leaf_next_index) = 0; |
| 84 | + should_insert * (class_id - updated_low_leaf_next_class_id) = 0; |
| 85 | + |
| 86 | + // ========= LOW LEAF MERKLE CHECK ========= |
| 87 | + pol commit low_leaf_hash; |
| 88 | + // The intermediate root is the root of the tree after the low leaf update but before the new leaf is inserted. |
| 89 | + pol commit intermediate_root; |
| 90 | + // TODO: We need this temporarily while we do not allow for aliases in the lookup tuple |
| 91 | + pol commit tree_height; |
| 92 | + sel * (constants.AVM_RETRIEVED_BYTECODES_TREE_HEIGHT - tree_height) = 0; |
| 93 | + |
| 94 | + #[LOW_LEAF_POSEIDON2] |
| 95 | + sel { sel, low_leaf_class_id, low_leaf_next_class_id, low_leaf_next_index, low_leaf_hash } |
| 96 | + in poseidon2_hash.end { poseidon2_hash.start, poseidon2_hash.input_0, poseidon2_hash.input_1, poseidon2_hash.input_2, poseidon2_hash.output }; |
| 97 | + |
| 98 | + pol commit updated_low_leaf_hash; |
| 99 | + |
| 100 | + #[UPDATED_LOW_LEAF_POSEIDON2] |
| 101 | + should_insert { sel, low_leaf_class_id, updated_low_leaf_next_class_id, updated_low_leaf_next_index, updated_low_leaf_hash } |
| 102 | + in poseidon2_hash.end { poseidon2_hash.start, poseidon2_hash.input_0, poseidon2_hash.input_1, poseidon2_hash.input_2, poseidon2_hash.output }; |
| 103 | + |
| 104 | + #[LOW_LEAF_MERKLE_CHECK] |
| 105 | + sel { should_insert, low_leaf_hash, updated_low_leaf_hash, |
| 106 | + low_leaf_index, tree_height, root, intermediate_root } |
| 107 | + in merkle_check.start { merkle_check.write, merkle_check.read_node, merkle_check.write_node, |
| 108 | + merkle_check.index, merkle_check.path_len, merkle_check.read_root, merkle_check.write_root }; |
| 109 | + |
| 110 | + // ========= LOW LEAF VALIDATION ========= |
| 111 | + pol commit class_id_low_leaf_class_id_diff_inv; |
| 112 | + pol CLASS_ID_LOW_LEAF_CLASS_ID_DIFF = class_id - low_leaf_class_id; |
| 113 | + |
| 114 | + // CLASS_ID_LOW_LEAF_CLASS_ID_DIFF == 0 <==> EXISTS == 1 |
| 115 | + #[EXISTS_CHECK] |
| 116 | + sel * (CLASS_ID_LOW_LEAF_CLASS_ID_DIFF * (EXISTS * (1 - class_id_low_leaf_class_id_diff_inv) + class_id_low_leaf_class_id_diff_inv) - 1 + EXISTS) = 0; |
| 117 | + |
| 118 | + // If the leaf doesn't exist, we need to validate that the class id is greater than the low leaf class id |
| 119 | + |
| 120 | + #[LOW_LEAF_CLASS_ID_VALIDATION] |
| 121 | + leaf_not_exists { class_id, low_leaf_class_id, sel } |
| 122 | + in ff_gt.sel_gt { ff_gt.a, ff_gt.b, ff_gt.result }; |
| 123 | + |
| 124 | + // If next class id is not zero (which would be infinity), it has to be greater than the class id. |
| 125 | + // We commit next_class_id_is_nonzero instead of next_class_id_is_zero since it'll be used as a selector for a lookup |
| 126 | + pol commit next_class_id_is_nonzero; |
| 127 | + next_class_id_is_nonzero * (1 - next_class_id_is_nonzero) = 0; |
| 128 | + pol NEXT_CLASS_ID_IS_ZERO = 1 - next_class_id_is_nonzero; |
| 129 | + |
| 130 | + pol commit next_class_id_inv; |
| 131 | + #[NEXT_CLASS_ID_IS_ZERO_CHECK] |
| 132 | + leaf_not_exists * (low_leaf_next_class_id * (NEXT_CLASS_ID_IS_ZERO * (1 - next_class_id_inv) + next_class_id_inv) - 1 + NEXT_CLASS_ID_IS_ZERO) = 0; |
| 133 | + |
| 134 | + #[LOW_LEAF_NEXT_CLASS_ID_VALIDATION] |
| 135 | + next_class_id_is_nonzero { low_leaf_next_class_id, class_id, sel } |
| 136 | + in ff_gt.sel_gt { ff_gt.a, ff_gt.b, ff_gt.result }; |
| 137 | + |
| 138 | + // ========= NEW LEAF INSERTION ========= |
| 139 | + pol commit new_leaf_hash; |
| 140 | + |
| 141 | + #[NEW_LEAF_POSEIDON2] |
| 142 | + should_insert { sel, class_id, low_leaf_next_class_id, low_leaf_next_index, new_leaf_hash } |
| 143 | + in poseidon2_hash.end { poseidon2_hash.start, poseidon2_hash.input_0, poseidon2_hash.input_1, poseidon2_hash.input_2, poseidon2_hash.output }; |
| 144 | + |
| 145 | + #[NEW_LEAF_MERKLE_CHECK] |
| 146 | + should_insert { sel, precomputed.zero, new_leaf_hash, |
| 147 | + tree_size_before_write, tree_height, intermediate_root, write_root } |
| 148 | + in merkle_check.start { merkle_check.write, merkle_check.read_node, merkle_check.write_node, |
| 149 | + merkle_check.index, merkle_check.path_len, merkle_check.read_root, merkle_check.write_root }; |
| 150 | + |
0 commit comments