2

Suppose you have an ordered sequence of bitsets b1, b2, b3, ..., bN.

Is there an efficient bitwise operator hash calculation which can be used to generate hashes which is also associative?

In other words, what is a recommended hashing function hash(bX, bY) such that:

hash(hash(b1, b2), b3) == hash(b1, hash(b2, b3))

Would bitwise exclusive-or XOR provide an acceptably low collision rate?

EDIT: Note that there is a related question here.

Community
  • 1
  • 1
KomodoDave
  • 7,239
  • 10
  • 60
  • 92

1 Answers1

1

XOR is associative but also commutative. It is more than you need but I fail to think of a purely associative variant that is practical. Matrix multiplication comes to mind but I'm not sure how to use that with binary hashes.

Addition is also associative so you can do a combined hash: Keep two different hashes, one combined with addition, one with XOR. A collision has to affect both to be an effective collision. Much more likely.

A disadvantage is that hash(a, b) == hash(b, a) (this is commutativity). Not sure how to remove that property.

usr
  • 168,620
  • 35
  • 240
  • 369
  • 1
    Yes good point - commutativity is certainly a downside of XOR in this case of an ordered sequence. Including addition wouldn't prevent collision with values swapped so commutativity would still be present. However subtraction would work, so your "two operations" concept is sound. Thank you for your suggestion. – KomodoDave Feb 09 '13 at 23:24