scanhash_c in
https://github.com/jgarzik/cpuminer/blob/master/sha256_generic.c shows midstate and hash1 being used.
Basically, there are 3 chunks that the sha256_transform function in that file is called for per "nonce" value. [Here is the header definition from
https://en.bitcoin.it/wiki/Block_hashing_algorithm:]
Field | Purpose | Updated when... | Size (Bytes) |
Version | Block version number | You upgrade the software and it specifies a new version | 4 |
Previous hash | Hash of the previous block | A new block comes in | 32 |
Merkle root | 256-bit hash based on all of the transactions | A transaction is accepted | 32 |
Timestamp | Current timestamp | Every few seconds | 4 |
"Bits" | Current target in compact format | The difficulty is adjusted | 4 |
Nonce | 32-bit number (starts at 0) | A hash is tried (increments) | 4 |
The first chunk is for the first 64 Bytes of the header:
Field | Size (Bytes) |
Version | 4 |
Previous hash | 32 |
Merkle root (first 28 Bytes) | 28 |
This first chunk is constant for all of the 2^32 values of the "Nonce" that are tried, since the "Nonce" bits are in the second chunk. Therefore, the hash of the first chunk is constant, as well, and doesn't need to be re-calculated for each of the 2^32 "Nonce" values that are attempted. "midstate" and "hash1" capture the relevant state after the first chunk has been hashed, so that you can start right into hashing the second chunk for each of the "Nonce" values.
The second chunk is for the second 64 Bytes of the header:
Field | Size (Bytes) |
Merkle root (last 4 Bytes) | 4 |
Timestamp | 4 |
"Bits" | 4 |
Nonce | 4 |
SHA-256 Defined bits to make the chunk 64 Bytes total | 48 |
This chunk needs to be hashed for every value of "Nonce", since it changes [in its 13th through 16th Bytes] each time.
After that is hashed, that hash is then put in another 64 Byte chunk that gets hashed, as well. That final hash is what gets compared to the target.
Field | Size (Bytes) |
Final hash of chunks 1 and 2 | 32 |
SHA-256 Defined bits to make the chunk 64 Bytes total | 32 |