One possibility is to take the standard .NET hash code which every object has and hash *that*. That's a poor idea in general because if two objects give the same .NET hash code, then they will share hash codes for every hash function. (Of course they would give different values for different hash functions, but for a given function they would both give the same value.) The hash functions would be way too dependent on each other. However, it suffices for Bloom filters of small-to-medium size because, assuming the built-in .NET hash functions are implemented well, the chance of two distinct values returning the same hash code is very small (about one in a few billion, although it is affected by the birthday paradox).

In any case, I still had the problem of creating the hash functions. The best hash functions are injective, meaning that they never map distinct input values onto the same output value. This is impossible whenever the input domain is larger than the output domain, but in my case I would be mapping integers onto integers. Since the input and output domains are the same size, an injective function would necessarily be bijective too. It struck me that what I really needed was a permutation. If I had N random permutations of the integers, then hashing the value k with the nth hash function would simply be a matter of taking the kth element of the nth random permutation.

This left me with the problem of coming up with a way to efficiently select, say, the billionth integer from the billionth random permutation of the integers. I thought about ways of using prime and coprime numbers to generate the permutations. (Given a prime *p*, a coprime *c*, and a starting value *s*, (*s*+*c***n*) mod *p* produces the nth value in a permutation of the numbers 0 through *p*-1.) Those permutations would be rather predictable, but that might not be a problem. I'd only need one prime number, which I could find beforehand — it would the first prime greater than 2^{32} — but finding the coprimes seemed like it would be nontrivial. There's also the fact that I'd need to iterate if the output wasn't less than 2^{32}. It just seemed ugly.

Luckily, I stumbled upon the idea of using a block cipher. In fact, block ciphers meet the requirements almost perfectly. The whole purpose of a block cipher is to transform one N-bit value into another N-bit value, based on a K-bit key. It does so uniquely, with every possible N-bit input mapping to only one N-bit output, which is necessary for the cipher to be reversed. In effect, the key is used to select a particular pseudorandom permutation of the N-bit values, and the input value serves as an index into that permutation.

The only problem was that there didn't exist any block ciphers that fit my purposes. They are typically designed to be complex, cryptographically strong transformations. The output size is typically 64 or 128 bits, and the key size is typically much larger than that. (The reason should be clear: the number of potential permutations is 2^{N} factorial, while the number of keys is only 2^{K}. So the key size is much larger than the output size to allow more of the possible permutations to be accessible to the cipher.) So I set out to create my own block cipher. Or rather, I set about to strip down and optimize an existing block cipher to take an integer key and produce an integer output. I wanted a cipher that was simple to implement. The Tiny Encryption Algorithm (TEA) looked promising, but I was dissuaded by the fact that it suffers from equivalent keys. Instead, I created a stripped-down version of Skipjack, the NSA algorithm intended to be used in the Clipper chip, chosen because it was also simple, and because I assumed it would be more random. But I was enchanted with the simplicity of TEA and, bolstered by my experience converting Skipjack, I also converted XXTEA (a somewhat stronger but more complex form of TEA).

It works pretty well! I tested several versions of the ciphers by enciphering a bunch of zeros with an all-zero key in CBC mode and running the results through NIST's statistical test suite. The Skipjack-based cipher passed with consistently high scores, while the XXTEA-based cipher had high scores except for a few failures. Neither are suitable for encryption of course, but both manage to generate strongly random-looking output. More importantly, they both work well in the Bloom filter. The generic hashing approach works up to about 10 million items, when the minimum false positive rate starts to become unacceptably high (i.e. greater than 0.1%). To solve this, I implemented custom hash providers for all the common built-in types, but they use block ciphers too. So in short, if you need a way to represent permutations of a large space without having to pre-generate them, consider using block ciphers!

Block ciphers aren't the only way to do this, though. Some techniques for creating random number generators can used, especially those based on XOR and shuffling bits, as those don't lose information. You'd use them to create a random number generator with no internal state. In fact that's what a block cipher is.

**Update:** For my Bloom filter, although the block cipher approach described above worked pretty well, I eventually switched to some hashing code based on Bob Jenkins' lookup3 hash, which I optimized for 4, 8, and 16 byte values in addition to the general byte string case. It doesn't work for permuting integers or generating bijective functions — the subject of this article — because it has collisions in the output, and the output of the hash is slightly less uniform than the block cipher I was using, but it's faster and, honestly, more trustworthy. The block cipher I developed worked great for a given hash function, but there were some dependencies between hash functions sometimes. That's no slight against the idea of using block ciphers in general, though, and for the problem of generating bijective functions and permutations of integers, I still think they're a perfect fit. Even for hashing, they can work well, if you know how to develop a good one, but I'm not an expert in developing cryptographic algorithms.

Here is the (heavily inlined and nigh-unreadable) code for my own block cipher. I'm sure the round function is poorly designed, but it seems to suffice for permuting integers.

// this is a weak block cipher with a 32-bit key and a 32-bit output based on a 16:16 balanced // Feistel network. it should be clear from the small key size and poor design that it's insecure, // but it should serve its purpose of giving us a pseudorandom permutation of the output space // (i.e. of the 32-bit integers) with the key as the seed static uint syfer(uint key, uint data) { uint R = (data^key) & 0xFFFF; uint L = (data>>16) ^ (((((R>>5)^(R<<2)) + ((R>>3)^(R<<4))) ^ ((R^0x79b9) + R)) & 0xFFFF); key = (key>>3) | (key<<29); R ^= ((((L>>5)^(L<<2)) + ((L>>3)^(L<<4))) ^ ((L^0xf372) + (L^key))) & 0xFFFF; return ((L ^ ((((R>>5)^(R<<2)) + ((R>>3)^(R<<4))) ^ ((R^0x6d2b) + (R^((key>>3)|(key<<29)))))) << 16) | R; }

And here is the code for the stripped-down version of Skipjack:

// this is a weak block cipher with a 32-bit key and a 32-bit output. it should be clear // from the small key size that it's insecure, but if that's not enough, it's a greatly // weakened form of skipjack, which has already been broken. but it should serve its // purpose of giving us a pseudorandom permutation of the output space (i.e. of the // 32-bit integers) with the key as the seed static uint slip32(uint key, uint data) { uint low = data & 0xFFFF, high = data >> 16; // split the data into two parts // perform four Feistel rounds. the Luby-Rackoff analysis proves that if the round // function is a cryptographically secure pseudorandom function, then 3 rounds // suffice to create a pseudorandom permutation and 4 rounds suffice to make a "strong" // pseudorandom permutation. i have no idea if the round function is cryptographically // secure, but the result will nonetheless be some kind of permutation, and it looks // pretty random in statistical tests, which is good enough for my purposes high ^= round(key, low); // rotate the key bytes here rather than using it as a circular array inside round() key = (key>>8) | (key<<24); low ^= round(key, high) ^ 1; key = (key>>8) | (key<<24); high ^= round(key, low) ^ 2; key = (key>>8) | (key<<24); low ^= round(key, high) ^ 3; return (low<<16) | high; // swap the halves and recombine them } static uint round(uint key, uint word) { uint g0, g1, g2, g3; g0 = permutation[(byte)(word ^ key)] ^ (word>>8); g1 = permutation[(byte)(g0 ^ (key>>8))] ^ word; g2 = permutation[(byte)(g1 ^ (key>>16))] ^ g0; g3 = permutation[(byte)(g2 ^ (key>>24))] ^ g1; return (uint)((byte)g2<<8) | (byte)g3; } // this is a permutation of all possible bytes (the same used by skipjack) static readonly byte[] permutation = new byte[256] { 0xa3,0xd7,0x09,0x83,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0x11,0xbe,0x92,0x4f,0x24,0xc5,0x32,0x36,0x9d,0xcf,0xf3,0xa6,0xbb,0xac, 0x5e,0x6c,0xa9,0x13,0x57,0x25,0xb5,0xe3,0xbd,0xa8,0x3a,0x01,0x05,0x59,0x2a,0x46 };

Finally, here is an example of how the block ciphers can be used, first in a generic hash and then in a type-specific hash. Obviously the generic approach suffers from the problem, described above, that the hash functions are not independent, but whatcha gonna do?

static int GetHashCode(int hashFunction, T item) { int hash = EqualityComparer<T>.Default.GetHashCode(item); // hash function 0 will be the built-in .NET hash function. other hash functions will // be constructed using a weak 32-bit block cipher with the hash function as the key // and the .NET hash as the data to be encrypted. this effectively uses the .NET hash // value as an index into a random permutation of the 32-bit integers if(hashFunction != 0) hash = (int)syfer((uint)hashFunction, (uint)hash); return hash; } static int GetHashCode(int hashFunction, ulong item) { // you could also special case hash function 0 here. use two different hash functions // to avoid the result always being zero when 'item' is zero. it would be more efficient // to develop fast block ciphers for 64-bit values rather than combining two 32-bit // values, but that's an exercise for the reader ;-) return (int)(syfer((uint)hashFunction, (uint)item) ^ syfer((uint)hashFunction+1, (uint)(item>>32))); }

## Comments

Great idea, it just saved me!

I too, needed a simple pseudo random permutation without any security requirements.

Ok, when pasted into a c# solution your functions compile right away and produce output.

Still, just to be sure, maybe you yould post a handful of random test vectors?

Lots of Greetings!

Volker

Sure, I don't mind posting some test vectors. I think if you copy and paste the code you'll get the same output that I do. In fact, I'm going to copy and paste the code in order to create the test vectors. Nonetheless, here they are. Each line gives the name of the algorithm, the key, and the first ten values from the permutation selected by that key.

syfer 00000000: 25CE7D54 041A7FD3 1E3A7F84 9F49789F 05AB7FDA 37687EC4 35447EAA 16878124 486185C1 7EB2845A

slip32 00000000: 78CE18C0 5AEFA907 0607E508 43102198 628506BA 1E4AB673 3DCE2A1A 6FB97AA8 D39E0070 85271B0E

syfer 000003E8: 464526D7 AF9025E4 D56A38E3 B83A265C 9B6A3649 CAD93955 FDD33795 65F53155 993B3562 F299370E

slip32 000003E8: A0A880BF 2F18BF44 E71FA259 38384D89 2AA1B40D A5796515 EA6D19C2 351BCEB5 7437E9F1 3B1CE19E

syfer C4653600: 5FFBFAF7 CF09F219 0CAFF18F 2758F029 0345F7E7 614AF650 EC6DFC33 FC04FD28 B2CECD8A 4EFBCCEE

slip32 C4653600: 28C8EE0F 8CDA07E7 E6FA3392 B41E533D 003F2C52 DD865E6B 7D5C7D57 67BA8617 14BAE312 5BC8C2C3

They were generated with this code: