

You would like to "restart" each computation from that value $X_1$ directly, to avoid recomputing it again and again.ĭoing that over an existing library implementing SHA-256 may or may not be easy, depending on what facilities that library offers. Since all your 128-byte messages begin with the exact same 64-byte header, all the hash computations formally begin by processing the same block $M_1$, with the same $X_0$ (the conventional initial value), resulting in the same $X_1 = f(M_1, X_0)$. Your "midstate" is $X_1$: that's the contents of the "state" after having processed the first block. Process each block in due sequence, by computing $X_i = f(M_i, X_$ since that value will not be used thereafter). Set $X_0$ (a 256-bit value) to a conventional initial value (which is specified in the SHA-256 standard, section 5.3.3). Input message $M$ is first padded by appending between 129 and 640 bits (inclusive), resulting into a padded message $M'$ whose length (in bits) is a multiple of 512. Hashing or encrypting twice to increase security? on crypto.SHA-256 uses an internal compression function $f$ which takes two inputs, of size 512 and 256 bits respectively, and outputs 256 bits.This construction has some minor weaknesses (not relevant to bitcoin), so I wouldn't recommend it for new protocols, and would use HMAC with constant key, or truncated SHA512 instead. To avoid this property, Ferguson and Schneier suggested using SHA256d = SHA256(SHA256(x)) which avoids length-extension attacks. I don't think Bitcoin ever uses hashes in a way that would suffer from length extensions, but I guess Satoshi went with the safe choice of preventing it everywhere.

DOUBLE SHA256 HASH CALCULATOR MAC
The most relevant example is using H(k||m) as MAC, where an attacker can easily calculate a MAC for m||m'. This is usually not a problem, but there are some uses where it totally breaks the security. This allows an attacker who knows H(x) to calculate H(x||y) without knowing x. SHA-2, like all Merkle-Damgard hashes suffers from a property called "length-extension". So why does he hash twice? I suspect it's in order to prevent length-extension attacks. If we assume a conservative 4 billion targets, and a 128 bit security level, this leads to 256 bit hashes for collision resistance and 160 bit hashes for multi-target second-preimages. You need a 2*n bit hash to achieve n bit collision resistance, and you need a t*n bit hash to achieve n bit second pre-image resistance.

This is consistent with a goal of achieving 128 bits of security. It seems like Satoshi chose Hash256 whenever collisions are a problem, and Hash160 when only (multi target) second pre-images matter. hash of the block header (and thus the proof of work and the link to the previous block).generating the checksum in a Bitcoin address.SHA256(SHA256(x)) called Hash256 which produces a 256 bit output hashing the public key to generate part of a Bitcoin addresses.RIPEMD160(SHA256(x)) called Hash160 which produces a 160 bit output Bitcoin uses double hashing almost everywhere it hashes in one of two variants:
