Take the 2-minute tour ×
Cryptography Stack Exchange is a question and answer site for software developers, mathematicians and others interested in cryptography. It's 100% free, no registration required.

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.

share|improve this question

migrated from stackoverflow.com Nov 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
4  
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
    
Well wikipedia claims it's an example of a nothing up your sleeve number. But the fact I can't find any information on how it's derived, it does make me wonder. I think it would be dumb to assume nobody's ever looked into it though. :P –  Keith Nov 25 '13 at 20:51
5  
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
4  
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

1 Answer 1

up vote 19 down vote accepted

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.

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.