I am looking at the following SHA256 pseudocode on wikipedia.
Specifically, I am looking at the following section.
//Initialize variables
//(first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19):
h0 := 0x6a09e667
I am trying to figure out how h0 was generated. I know from the comment that this should be the fractional part of the square root of 2. I believe I can get the fractional part of the square root of 2 by typing the following. All the following code is from the python repl.
>>> math.modf(math.sqrt(2))[0]
0.41421356237309515
At the top of the file it states that the declaration of all constants are Big Endian. I know that my environment is Small Endian because I type.
>>> import sys
>>> sys.byteorder
'little'
So, according to my manual manipulation of the hex value in h0, the Little Endian representation should be 0x67e6096a.
>>> int(0x67e6096a)
1743128938
And I am stuck. I have tried various manipulations, but non of them end up with this result. I do not know how to get the first 32 bits of the fractional part of a floating point number. I know that somehow my 0.41421356237309515 (float) result can be transformed into 1743128938 (int), but I really have no idea how. What are the steps necessary to get the first 32 bits of the fractional part of a floating point number? Python answers only please.
Thank you.
Endianness does not matter for hexadecimal constants; each digit is a nibble, with the least significant nibble last. It does matter if you deal with differing size pointers. If you do need to use byte orders, the struct module can help. Anyhow, you’ve retrieved the fractional part just fine; converting it to hex is easily done by simply multiplying and truncating, so we get an integer: