MCPcopy Create free account
hub / github.com/category-labs/monad / page_commit

Function page_commit

scripts/page_commit_reference.py:172–217  ·  view source on GitHub ↗
(page: bytes)

Source from the content-addressed store, hash-verified

170
171
172def 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) ───────────────────

Callers 2

selftestFunction · 0.70
mainFunction · 0.70

Calls 7

compute_slot_bitmapFunction · 0.85
hash_sealFunction · 0.85
derive_pair_bitmapFunction · 0.85
hash_leafFunction · 0.85
hash_parentFunction · 0.85
countMethod · 0.80
appendMethod · 0.45

Tested by

no test coverage detected