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.
Using the modulo ( % ) operator The % operator is an arithmetic operator that calculates and returns the remainder after the division of two numbers. If a number is divided by 1, the remainder will be the fractional part. So, using the modulo operator will give the fractional part of a float.
32-bit single precision, with an approximate range of 10 -101 to 10 90 and precision of 7 decimal digits.
The 'int pointer' size can be changed to 64 bits on 64 bits machines, since the memory address size is 64 bits. That means your 'argument' isn't valid. A float is then still a float too: usually we say it is 32 bits, but everyone is free to deviate from it.
Voila. (Apologies to OP for not doing a Python answer but I hope the method is clear.)
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:
>>> hex(int(math.modf(math.sqrt(2))[0]*(1<<32)))
'0x6a09e667'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With