比特币区块头SHA256哈希填充规则及剩余48字节计算问询
Great question—this ties together standard SHA-256 mechanics with how Bitcoin uses them for block validation. Let’s break this down step by step:
Bitcoin Block Header SHA-256 Padding Scheme
Bitcoin doesn’t invent a custom padding method here—it strictly follows the standard SHA-256 padding rules laid out in the FIPS 180-4 specification. The process works like this for any input, including the 80-byte block header:
- First, append a single
1bit to the end of the raw block header data. - Next, add as many
0bits as needed so that the total length of the data (before adding the final length field) is exactly 448 bits modulo 512. This leaves exactly 64 bits of space to store the original input’s length. - Finally, append a 64-bit big-endian integer that represents the original input length in bits.
Where the 48 Bytes of Padding Come From
You’re correct that the block header is 80 bytes (640 bits), and SHA-256 requires inputs to be multiples of 512 bits (64 bytes) after padding. Let’s do the math to unpack the 48-byte padding:
- Start with the raw block header: 80 bytes = 640 bits.
- After adding the mandatory
1bit, we have 641 bits of data. - We need the length after adding zeros to hit 448 bits modulo 512. Let’s calculate the number of zeros needed:
- 641 divided by 512 leaves a remainder of 129 bits.
- To get to 448 bits (the target modulo 512), we need 448 - 129 = 319 additional
0bits.
- Then we add the 64-bit length field (which is 8 bytes) to record the original 640-bit length.
- Total padding bits: 1 (the initial
1bit) + 319 zeros + 64 length bits = 384 bits. - Convert that to bytes: 384 ÷ 8 = 48 bytes.
When you combine the original 80-byte header with this 48-byte padding, you get a 128-byte (1024-bit) input—exactly two 512-bit blocks, which is the required input size for SHA-256’s internal processing.
One quick extra note: Bitcoin uses double SHA-256 (SHA256(SHA256(block_header))) for block hashes. The padding happens separately for each hash pass, but the first pass uses the 80+48 byte input we just walked through.
内容的提问来源于stack exchange,提问作者user170174

