[Math] hash function for matrices over finite field (Matlab)

finite-fieldshash functionmatrices

What is a good hash function for small nonsingular matrices over a field $\mathbb{F}_p$ for $p$ prime? I'm looking for an integer function which is close to being injective (but not necessarily perfectly injective) and doesn't take too long to compute in Matlab.

Things I've tried include the trace, the 3 diagonal elements (this can be turned into an integer function $A \mapsto A_{11}+pA_{22}+p^2A_{33}+…$), 4 randomly chosen elements, and the 3 column sums. But all of these make my program take an unreasonably long time for groups of order more than a few thousand.

Many thanks for any help with this!

Best Answer

There is a very simple algorithm which gives you a hash function, assuming you store your matrix entries with integers in $\{0,1,...,p-1\}$:

hash = 0
for i=1..n
  for j=1..n
    hash=p*hash+A[i][j]

Also please note that the examples you provide are not really good hash functions. There are way too many collisions, which would give serious performance hits if used in e.g. a hash table. The example I provided above is a perfect hash function (i.e. an injective map from the $\mathbb{F}_p$ matrices to $\mathbb{Z}$), so no collisions can exist here (unless $n$ and $p$ are large enough to cause overflow, but even then the probability is very small).

I don't think one can do much better than this, since any good hash would take all matrix entries into account, and this only does one addition and one multiplication per entry. If this is still not working for you, chances are that you don't just need a hash.

Related Question