11

Is there a hash function with following properties?

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

If I am correct, such function allows achieving 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 thats awkward to implement and reduces space to 16bits.

I am grateful for any help.

Maciej Mikosik
  • 355
  • 1
  • 5
  • 14

2 Answers2

0

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.

VainMan
  • 2,025
  • 8
  • 23
0

Matrix multiplication is associative and non-commutative.

You could try representing your hashes as matrices but this will result in a loss of information if they have 0 determinant (which is likely!).

So instead you should generate a triangle matrix with a diagonal of 1's to ensure that you have a determinant of 1 (this guarantees that composition does not loose information).

Furthermore the composition of triangle matrices produces a new triangle matrix, making reading the composition the same as generation.

Note: to use this method the length of your hash must be a triangle number!

  • What do you mean by composition of matrices? Matrix multiplication (if so, wouldn't you run out or precision really fast?)? – yosmo78 Feb 07 '23 at 05:30