Shuffle All (with cryptography)

Just a quick note on a cryptography trick I learned many years ago, with thanks to #crypto on IRC.

If you wish to iterate a sequence of length n in a random order you can use an ordinary index in the range [0,n), but use format-preserving encryption to transform it to another index. Since encryption is reversible with the key, and the output is the same size as the input, each index will appear exactly once in the output. Mathematicians and I would say format-preserving encryption is a pseudorandom permutation: it appears random, and it reorders a sequence without losing or duplicating any entries. So the output is a reordered sequence that we don’t need to store:

0⟶3 1⟶4 2⟶1 3⟶0 4⟶2

It gets neater. Note that we don’t even need to keep the original, in-order index anywhere; we can just decrypt the index of the current element, add or subtract something to move in either direction, and encrypt again.1 This also means we could jump to any point in the shuffled sequence and still easily know what's next. For the permutation above, the “next” function is:

0⟶2 1⟶0 2⟶3 3⟶4 4⟶1

You could therefore use a format-preserving encryption primitive to implement a perfect playlist shuffle that is entirely stateless.2 Given the complexity of some media players I’ve seen, I wonder if this might be a simplification.


  1. More formally: The successor function S is a cyclic permutation in (ℤ∕nℤ). We are taking the conjugate permutation of S by E k, E kSE k -1. Conjugation preserves the cycle type of S.↩

  2. Or nearly stateless if you have a way to reshuffle. Still a lot easier to manage than temporary lists that take O(n) space.↩

 ↑ back to top