[New-bugs-announce] [issue34751] Hash collisions for tuples

Jeroen Demeyer report at bugs.python.org
Thu Sep 20 09:27:27 EDT 2018


New submission from Jeroen Demeyer <J.Demeyer at UGent.be>:

It's not hard to come up with a hash collision for tuples:

>>> hash( (1,0,0) )
2528505496374819208
>>> hash( (1,-2,-2) )
2528505496374819208

The underlying reason is that the hashing code mixes ^ and *. This can create collisions because, for odd x, we have x ^ (-2) == -x and this minus operation commutes with multiplication.

This can be fixed by replacing ^ with +. On top of that, the hashing code for tuples looks needlessly complicated. A simple Bernstein hash suffices.

----------
messages: 325870
nosy: jdemeyer
priority: normal
severity: normal
status: open
title: Hash collisions for tuples

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue34751>
_______________________________________


More information about the New-bugs-announce mailing list