(page: bytes)
| 170 | |
| 171 | |
| 172 | def page_commit(page: bytes) -> bytes: |
| 173 | assert len(page) == PAGE_SIZE |
| 174 | |
| 175 | slot_bitmap = compute_slot_bitmap(page) |
| 176 | if slot_bitmap == 0: |
| 177 | return hash_seal(0, None) |
| 178 | |
| 179 | pair_bitmap = derive_pair_bitmap(slot_bitmap) |
| 180 | |
| 181 | # Phase 1: hash each active pair-leaf. |
| 182 | scratch: dict[int, bytes] = {} |
| 183 | bits = pair_bitmap |
| 184 | while bits: |
| 185 | idx = (bits & -bits).bit_length() - 1 |
| 186 | scratch[idx] = hash_leaf(page[idx * 64:(idx + 1) * 64]) |
| 187 | bits &= bits - 1 |
| 188 | |
| 189 | # Phase 2: bitmap-driven bottom-up merge. |
| 190 | bm = pair_bitmap |
| 191 | for bit in range(6): |
| 192 | if bin(bm).count("1") <= 1: |
| 193 | break |
| 194 | merges: list[tuple[int, int]] = [] |
| 195 | bits = bm |
| 196 | prev = -1 |
| 197 | while bits: |
| 198 | pos = (bits & -bits).bit_length() - 1 |
| 199 | bits &= bits - 1 |
| 200 | sibling = ( |
| 201 | prev != -1 |
| 202 | and (prev >> (bit + 1)) == (pos >> (bit + 1)) |
| 203 | and ((prev >> bit) & 1) == 0 |
| 204 | ) |
| 205 | if sibling: |
| 206 | merges.append((prev, pos)) |
| 207 | prev = -1 |
| 208 | else: |
| 209 | prev = pos |
| 210 | for left, right in merges: |
| 211 | scratch[left] = hash_parent(scratch[left], scratch[right]) |
| 212 | del scratch[right] |
| 213 | bm &= ~(1 << right) |
| 214 | |
| 215 | # Phase 3: seal. |
| 216 | root_idx = (bm & -bm).bit_length() - 1 |
| 217 | return hash_seal(slot_bitmap, scratch[root_idx]) |
| 218 | |
| 219 | |
| 220 | # ── Reference vectors (must match the C++ implementation) ─────────────────── |
no test coverage detected