Bitcoin Forum
June 13, 2021, 06:47:21 PM *
News: Latest Bitcoin Core release: 0.21.1 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Understanding SHA256 (for dummies)  (Read 96 times)
irukandji
Sr. Member
****
Offline Offline

Activity: 620
Merit: 253



View Profile
October 24, 2020, 06:00:40 AM
 #1

So, with the SHA256 process (?) we can take any string of words and numbers and *do something* to it and we get a 256 string of ones and zeroes .Small changes to the input give an unrelated new string/key.

This can also be converted to other forms/keys (?) 

Hopefully I got that right.
My questions is....What do we do to the original set of words and numbers?    What is the process?

If t is difficult to explain, then, is there a good analogy or way to explain it?

Thanks
1623610041
Hero Member
*
Offline Offline

Posts: 1623610041

View Profile Personal Message (Offline)

Ignore
1623610041
Reply with quote  #2

1623610041
Report to moderator
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
1623610041
Hero Member
*
Offline Offline

Posts: 1623610041

View Profile Personal Message (Offline)

Ignore
1623610041
Reply with quote  #2

1623610041
Report to moderator
1623610041
Hero Member
*
Offline Offline

Posts: 1623610041

View Profile Personal Message (Offline)

Ignore
1623610041
Reply with quote  #2

1623610041
Report to moderator
1623610041
Hero Member
*
Offline Offline

Posts: 1623610041

View Profile Personal Message (Offline)

Ignore
1623610041
Reply with quote  #2

1623610041
Report to moderator
NotATether
Hero Member
*****
Offline Offline

Activity: 546
Merit: 1569


Cryptographic Crawler


View Profile WWW
October 24, 2020, 07:59:57 AM
 #2

There is some pseudocode of SHA256 on Wikipedia at https://en.wikipedia.org/wiki/SHA-2#Pseudocode, I will try to explain what that does.

First of all the digest size of SHA256 is obviously 256 bytes; this is the length of the output of the SHA256 function.

Second, the string, which can be an arbitrary byte array like an executable program, gets a single 1 bit appended at the end, the enough zeros to make the total length a multiple of 512, and is then split into 512-bit chunks.

The digest function of SHA256 is applied 64 times (rounds) and transforms 256 bits (split into 8 32-bit ints) into 256 bits, also split into 8 32-bit ints.

With each 512-bit chunk, you take a large 2048-bit buffer array to use as temporary space, put the first 512-bit chunk at the beginning of the array and apply a transformation on each group of 512 bits, moving further along the buffer after each iteration. Eventually all 2048 bits of the buffer "w" are transformed.

I paste the relevant code snippet from the pseudocode below:

Code:
(message is the input with a 1 and enough 0s appended)
Process the message in successive 512-bit chunks:
break message into 512-bit chunks
for each chunk
    create a 64-entry message schedule array w[0..63] of 32-bit words
    (The initial values in w[0..63] don't matter, so many implementations zero them here)
    copy chunk into first 16 words w[0..15] of the message schedule array

    Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array:
    for i from 16 to 63
        (You don't have to understand what this does except that it overwrites the 32-bit integers between w[i-16] to w[i])
        (w is array of 32-bit ints)
        s0 := (w[i-15] rightrotate  7) xor (w[i-15] rightrotate 18) xor (w[i-15] rightshift  3)
        s1 := (w[i- 2] rightrotate 17) xor (w[i- 2] rightrotate 19) xor (w[i- 2] rightshift 10)
        w[i] := w[i-16] + s0 + w[i-7] + s1

The 8 32-bit variables that are used in the hashing are the hashes, these are appended together to make the 256-bit digest for each round.

At first they are initialized with "first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19" (they are written in binary, and not decimal format. This is the math behind how to derive sqrt(2) in binary for example.):

Code:
h0 := 0x6a09e667
h1 := 0xbb67ae85
h2 := 0x3c6ef372
h3 := 0xa54ff53a
h4 := 0x510e527f
h5 := 0x9b05688c
h6 := 0x1f83d9ab
h7 := 0x5be0cd19

Then the  digest algorithm takes these initial hash values, "w" and another set of constants "k" to make new "h" values which are then added to h0,h1,...h7. So after one round of SHA256 completes, you have 8 different "h" values. All 64 rounds are applied for each chunk in succession.

There is also "k", which is just an array of 64 32-bit round constants, they are first initialized as the (according to wikipedia) "first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311". They are not changed after each round, they remain constant and the same. It's too big to paste here but it's in the article.

Graphical explanation of the digest process from bitcoinwiki.org, which shows where the chunks are inserted and transformed:


(Source: https://en.bitcoinwiki.org/wiki/SHA-256)



Again, none of this is my own work, I'm not the source for it, this explanation was derived from the Wikipedia article I linked above.

odolvlobo
Legendary
*
Offline Offline

Activity: 3248
Merit: 1912



View Profile
October 24, 2020, 03:07:39 PM
 #3

This video shows the SHA-256 process very clearly: Mining Bitcoin with pencil and paper

Buy stuff on Amazon with BTC or convert Amazon points to BTC here: Purse.io
Join an anti-signature campaign: Click ignore on the members of signature campaigns.
PGP Fingerprint: 6B6BC26599EC24EF7E29A405EAF050539D0B2925 Signing address: 13GAVJo8YaAuenj6keiEykwxWUZ7jMoSLt
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!