Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the first 32 bits of the fractional part of this float?

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.

like image 702
Stephen Cagle Avatar asked Jan 12 '11 22:01

Stephen Cagle


People also ask

How do you find the fractional part of a float?

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.

What is the range of a 32 bit float?

32-bit single precision, with an approximate range of 10 -101 to 10 90 and precision of 7 decimal digits.

Is float always 32 bit?

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.


2 Answers

  1. Use your calculator on Windows to calculate sqrt(2) (1.4142135623730950488016887242097)
  2. Take the decimal part (0.4142135623730950488016887242097)
  3. Multiply by 2^32 (1779033703.9520993849027770600526)
  4. Express the whole part in hex (6A09E667)

Voila. (Apologies to OP for not doing a Python answer but I hope the method is clear.)

like image 196
John Avatar answered Oct 04 '22 16:10

John


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'
like image 33
Yann Vernier Avatar answered Oct 04 '22 15:10

Yann Vernier