You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Deduplicator: xxh32 digest can collide on large ZIMs #426
Deduplicator keys items by an xxh32 digest only (zim/dedup.py), which is 32-bit. With n deduplicated items the expected number of collisions is about n²/2³³, so roughly 1 around 100k items and ~30 around 500k. A full iFixit ZIM has a lot of images, so two different images could end up with the same digest, and the second one would silently become an alias to the wrong image.
A few options:
also store the content size with the digest and compare both, as benoit74 suggested. Cheap, but it only lowers the odds
switch to a 128 bit hash (xxhash has xxh3_128), which is about as fast and makes collisions practically impossible
both
My suggestion would be xxh3_128 + size as the key. I can open a PR once we agree on the approach.
To make the approach concrete, this is what I have in mind for Deduplicator.add_item_for:
hash with xxhash.xxh3_128() instead of xxh32(), for both content and fpath (still reading the file in chunks)
count the size while hashing
use size.to_bytes(8, "big") + hasher.digest() as the key
The key stays bytes, so added_items keeps its dict[bytes, str] type and code reading it (like ifixit's test) doesn't need to change. Memory goes from 4 to 24 bytes per item, which seems fine next to the path we already store.
For the test, I'd force a collision by monkeypatching the hash to return a constant, and check that two items of different sizes are not aliased.
@benoit74 Does that sound good? If so I'll open a PR.
I propose that we continue using the 32 bits xxh3 and limit to 4 bytes in size.to_bytes(4, "big"); I don't expect we would hash files bigger than 4GB, and accidental collision of both size + hash looks very unlikely (this is not a security feature).
Makes sense, size + hash should be enough here. I'll keep xxh32 (I don't think xxhash has a 32-bit xxh3 variant) and use size.to_bytes(4, "big") + digest as the key. A file over 4GB would then raise an OverflowError instead of being silently mixed up, which seems fine.
I have exams coming up, so the PR will most likely be around Dec 20, or earlier if I find some time in between.
Activity
i-varunsharma commentedon Dec 4, 2026
To make the approach concrete, this is what I have in mind for
Deduplicator.add_item_for:xxhash.xxh3_128()instead ofxxh32(), for bothcontentandfpath(still reading the file in chunks)size.to_bytes(8, "big") + hasher.digest()as the keyThe key stays
bytes, soadded_itemskeeps itsdict[bytes, str]type and code reading it (like ifixit's test) doesn't need to change. Memory goes from 4 to 24 bytes per item, which seems fine next to the path we already store.For the test, I'd force a collision by monkeypatching the hash to return a constant, and check that two items of different sizes are not aliased.
@benoit74 Does that sound good? If so I'll open a PR.
benoit74 commentedon Dec 4, 2026
I propose that we continue using the 32 bits xxh3 and limit to 4 bytes in
size.to_bytes(4, "big"); I don't expect we would hash files bigger than 4GB, and accidental collision of both size + hash looks very unlikely (this is not a security feature).WDYT?
i-varunsharma commentedon Dec 4, 2026
Makes sense, size + hash should be enough here. I'll keep xxh32 (I don't think xxhash has a 32-bit xxh3 variant) and use
size.to_bytes(4, "big") + digestas the key. A file over 4GB would then raise an OverflowError instead of being silently mixed up, which seems fine.I have exams coming up, so the PR will most likely be around Dec 20, or earlier if I find some time in between.