Trouble calculating MD5 integer part of the sines

Viewed 72

I was playing with an MD5 implementation and coded up the following for the binary part of the sines of integers, using the pseudocode found on wikipedia. I've found discrepancies between my output and the precomputed table widely available.

I'm trying to figure out if my code is incorrect or the pseudocode on Wikipedia is incorrect. Replacing math.Floor with math.Ceil in the code below fixes the discrepancies between the precomputed table and my output.

package main

import "fmt"
import "math"

func main() {
        var i float64
        for i < 64 {
                x := uint(math.Floor(float64(math.MaxUint32) * math.Abs(math.Sin(i+1))))
                fmt.Printf("%d : %x\n", uint(i+1), x)
                i = i + 1
        }
}
1 : d76aa477
2 : e8c7b755
3 : 242070db
4 : c1bdceee

Lines 1 and 2 different from the precomputed table, while line 3 is in agreement.

1 Answers

The problem is math.MaxUint32 which is off-by-one! That is, 232-1 rather than 232.

But anyway, the simplest/fastest/common/best option is to use a pre-computed table.

The definitive MD5 specification is RFC 1321:

the integer part of 4294967296 times abs(sin(i)), where i is in radians.

and in that "the integer part" is to be understood as the floor function. Definitely, with the appropriate precision of floor(abs(sin)), that gives the numbers in the RFC's table. Here it is, reformatted to 8 hex digits per entry, and cross-checked against what Wolfram's Alpha spits for BaseForm[Floor[Abs[2^32 Sin[Range[64]]]],16].

d76aa478 e8c7b756 242070db c1bdceee f57c0faf 4787c62a a8304613 fd469501 698098d8 8b44f7af ffff5bb1 895cd7be 6b901122 fd987193 a679438e 49b40821 f61e2562 c040b340 265e5a51 e9b6c7aa d62f105d 02441453 d8a1e681 e7d3fbc8 21e1cde6 c33707d6 f4d50d87 455a14ed a9e3e905 fcefa3f8 676f02d9 8d2a4c8a fffa3942 8771f681 6d9d6122 fde5380c a4beea44 4bdecfa9 f6bb4b60 bebfbc70 289b7ec6 eaa127fa d4ef3085 04881d05 d9d4d039 e6db99e5 1fa27cf8 c4ac5665 f4292244 432aff97 ab9423a7 fc93a039 655b59c3 8f0ccc92 ffeff47d 85845dd1 6fa87e4f fe2ce6e0 a3014314 4e0811a1 f7537e82 bd3af235 2ad7d2bb eb86d391 
Related