# MD5 implementation notes

While going through RFC1321, I came across the following paragraph:

```   This step uses a 64-element table T[1 ... 64] constructed from the
sine function. Let T[i] denote the i-th element of the table, which
is equal to the integer part of 4294967296 times abs(sin(i)), where i
is in radians. The elements of the table are given in the appendix.
```

From what I understood from paragraph, it means

``````T[i] = Integer_part(4294967296 times abs(sin(i)))
``````

We know the following is true for all `x`:

``````0 <= sin(x) <= 1
``````

Since `i` is an integer, `abs(sin(i))` may very well be `0` for all values of `i`.

That means table will contain all zero values ( `4294967296` times `0` is `0`). In the implementation, this is not true. Why is this so?
Appendix contains just the raw values after calculation. It does not show how it is derived from the sine function.

-
Why do you think abs(sin(i)) would be 0? It simply takes the positive value of sine, which will still be a floating point. – edA-qa mort-ora-y Jul 5 '11 at 10:14

You seem to be assuming that since they're using an integer as the input to `sin`, that the output will be an integer as well. This also appears to be incorrect. The intent seems to be that `i` will start out as an integer. That integer will be passed to `sin`. In a language like C or C++ (for example) it will be converted to floating point in that process. The result will also be floating point. The absolute value of that floating point result will be multiplied by 232, and then the integer part of that product put into the table.

Expressed in C (for example), you'd generate the table like this:

``````#include <math.h>

int main() {
int i;
for (i=1; i<65; i++)
printf("0x%x\n", (unsigned int)(fabs(sin(i)*4294967296UL)));
return 0;
}
``````

I didn't check every value, but running this on my machine seems to produce matching values for the first few values, the last few values, and a few spot checks in between.

-

First of all, sin(x) has values from -1 to 1, not from 0 to 1.

The only positive values of x for which sin(x) has value of 0 are multiples of π. π is irrational number, no integer will ever be multiple of π

In fact for positive integer values of x, it's true that 0 < abs(sin(x)) < 1.

Integer part of 4294967296 times number between 0 and 1 exclusive, is an integer between 1 and 4294967295 (inclusive).

-

Since i is an integer, abs(sin(i)) may very well be 0 for all values of i.

Why exactly you think so?

abs(sin(1)); 0.8414709848079

abs(sin(2)); 0.90929742682568

abs(sin(3)); 0.14112000805987

abs(sin(4)); 0.75680249530793

-
oops my bad! :( – vaasu Jul 5 '11 at 10:39

You multiply all of `abs(sin(i)) * 4294967296` as floats. Only then you make the result integer. And sin(int) isn't int, it's a float between [-1,1].

-