1

For one of my pet projects I would like to create an arbitrary number of different integer hashes. After a bit of research I figured that universal hashing is the way to go. However, I struggle with the (numpy) implementation. Say I'm trying to use the hash family h(a,b)(x) = ((a * x + b) mod p) mod m and my x can be anywhere in the range of uint32. Then choosing p >= max(x) and a, b < p means that in the worst case a * x + b overflows not only uint32 but also uint64. I tried to find an implementation which solves this problem but only found clever ways to speed up the modulo operation and nothing about overflow.

Any suggestions for how to implement this are greatly appreciated. Thanks :-)

1 Answer 1

1

(x + y) % z == ((x % z) + (y % z)) % z. So you could take the modulus before doing the sum:

  1. Cast a and x to uint64. (Multiply two uint32 will never overflow uint64).
  2. Compute h = (a * x) % p + b
  3. Return (h - p) if h > p else h. (Alternatively: return h % p)
Sign up to request clarification or add additional context in comments.

1 Comment

well, that was easy. Thanks! :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.