# How is the MD2 hash function S-table constructed from Pi?

For fun, I'm learning more about cryptography and hashing. I'm implementing the MD2 hash function following RFC 1319 (http://tools.ietf.org/html/rfc1319). I'll preface by saying I know there are libraries, I know this is an old hash, and I do not intend to use this in anything real-world, so don't anyone freak out.

In learning about S-tables I notice MD2 uses an S-table based on Pi. What I don't understand, is how these numbers relate to Pi. And oddly, I can't find anywhere that describes it. The RFC just says:

This step uses a 256-byte "random" permutation constructed from the digits of pi.

In the reference code, there is the following comment:

Permutation of 0..255 constructed from the digits of pi. It gives a "random" nonlinear byte substitution operation.

Looking at the first few bytes:

41, 46, 67

I cannot see how you get these numbers from Pi. I've tried looking at the binary representation of Pi, and these numbers do not seem to match up with the first bytes of pi.

My usual Googling just leads me back to the RFC or other implementations which seem to copy the above comment exactly. No where can I find where the construction of this table from Pi is explained.

-

## migrated from stackoverflow.comNov 25 '13 at 12:19

This question came from our site for professional and enthusiast programmers.

Are you asking how are these numbers $\pi$ or why are these numbers taken from $\pi$? – figlesquidge Nov 25 '13 at 12:29
Interesting question, I don't know the answer yet (and couldn't find it with some googling). Might be actually not a nothing-up-my-sleeve-number, but only pretending to be one ;-) – Paŭlo Ebermann Nov 25 '13 at 20:10
The authors of Network Security: Private Communications in a Public World cast some doubt on whether this table is actually based on Pi. – archie Nov 26 '13 at 0:30
Also note that RC2 (in RFC2268) also has a permutation supposedly based on the digits of $\pi$ in its key schedule. The final table in that RFC is also unexplained (and used in other algorithms from RSA as well). These were also never explained, AFAIK. – Henno Brandsma Dec 10 '13 at 16:06
I would say the proper answer to this question would be a concise enough explanation that I could implement an algorithm that took an input of pi, and output a result that was this S-table. – Keith Aug 1 '14 at 5:17

I sent an email to Ron Rivest and got an answer back.

The digits of $\pi$ are used as a sort of random number generator that is used in the Durstenfeld shuffle (see also Knuth vol 3, sec 3.4.2).

Below is some pseudocode adapted from the description and code he sent me.


S = [0, 1, ..., 255]
digits_Pi = [3, 1, 4, 1, 5, 9, ...] # the digits of pi

def rand(n):
x = next(digits_Pi)
y = 10

if n > 10:
x = x*10 + next(digits_Pi)
y = 100
if n > 100:
x  = x*10 + next(digits_Pi)
y = 1000

if x < (n*(y/n)): # division here is integer division
return x % n
else:
# x value is too large, don't use it
return rand(n)

for i in 2...256: #inclusive
j = rand(i)
tmp = S[j]
S[j] = S[i-1]
S[i-1] = tmp


The next function just steps through the digits of pi. You'll notice that rand does not use some of it's possible outputs. Dr. Rivest says that using these values would not give a uniform distribution as they are too large.

-
I'm working on verifying, writing a test case from your psuedocode. – Keith Aug 2 '14 at 5:38
And it verifies.... awesome! Thanks for the answer. Glad Dr. Rivest answered one of us who asked. :) – Keith Aug 2 '14 at 6:04
For what it’s worth: C proggy that creates the MD2 S-Box – e-sushi Aug 15 '14 at 4:47