Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Associative noncommutative hash function

Tags:

hash

Is there a hash function with the following properties?

  • is associative
  • is not commutative
  • easily implementable on 32 bit integers: int32 hash(int32, int32)

If I am correct, such a function allows achieving the following goals:

  • calculate hash of concatenated string from hashes of substrings
  • calculate hash concurrently
  • calculate hash of list implemented on binary tree - including order, but excluding how tree is balanced

The best I found so far is multiplication of 4x4 matrix of bits, but that's awkward to implement and reduces space to 16 bits.

I am grateful for any help.

like image 592
Maciej Mikosik Avatar asked Dec 19 '25 08:12

Maciej Mikosik


1 Answers

Polynomial rolling hash could help:

  • H(A1,...,An) = (H(A1,...,An-1) * Base + An) Mod P

It's easy to concat two results or substract prefix/suffix from result, as long as the length is known.

like image 104
VainMan Avatar answered Dec 24 '25 12:12

VainMan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!