HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Choosing a non-cryptographic hash function for language with no unsigned integers

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
nonwithfunctionunsignedhashlanguageforcryptographicintegerschoosing

Problem

I'm implementing a hash table in pure UnrealScript, which only has support for signed 32-bit integers. This means no 64-bit integers and no unsigned integers. I was in the middle of implementing an FNV hash function, but ran into a potential problem.


The offset_basis for FNV-1 is dependent on n, the size of the hash:
32 bit offset_basis = 2166136261

Well, 2166136261 converts to -2128831035 when the bits are turned into an signed integer representation. My question is whether or not this will negatively affect the hash distribution and result in more collisions.

For quick reference, here is the psuedo-code for the FNV hash:

hash = offset_basis
for each octet_of_data to be hashed
    hash = hash * FNV_prime
    hash = hash xor octet_of_data
return hash


Thank you for your time.

Solution

The offset is unlikely to matter. As the FNV web page says:


almost any offset_basis will serve so long as it is non-zero

The multiplication is potentially more important. The FNV-1 hash is defined to use unsigned multiplication modulo $2^{32}$, but if UnrealScript only has signed 32-bit integers, presumably you have no way to do that kind of unsigned multiplication. At that point you're no longer using the official FNV-1 hash; you're using something different. I suspect it's still fine, though.

Context

StackExchange Computer Science Q#59381, answer score: 2

Revisions (0)

No revisions yet.