> For the complete documentation index, see [llms.txt](https://polyhedra-network.gitbook.io/zkbridge-btc-oracle/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://polyhedra-network.gitbook.io/zkbridge-btc-oracle/bitcoin-data-formats.md).

# Bitcoin Data Formats

## Bitcoin block headers

The Bitcoin block header is a compact 80-byte structure that provides proof of work and links to the blockchain's history, ensuring integrity and continuity. The format of a Bitcoin block header consists of the following fields, laid out in a sequential manner:

1. **Version (4 bytes)**: A version number indicating which set of block validation rules to follow. The version field is used to facilitate protocol upgrades.
2. **Previous Block Hash (32 bytes)**: A reference to the hash of the previous (parent) block in the chain. This creates the blockchain's linkage and ensures the immutability of the preceding blocks.
3. **Merkle Root (32 bytes)**: The root hash of the Merkle tree constructed from all transactions included in the block. This serves as an integrity check for all transactions.
4. **Timestamp (4 bytes)**: A Unix epoch time when the miner started hashing the header (according to the miner).
5. **Bits (4 bytes)**: A compact representation (using a form of floating-point notation) of the target threshold this block’s hash must be less than or equal to for the block to be added to the block chain (i.e., the difficulty at the time the block was mined).
6. **Nonce (4 bytes)**: A 32-bit (4-byte) field, which the miner increments during the mining process until the hash of the block header is less than or equal to the target (i.e., a successful proof of work).

In Solidity, the parsing of a Bitcoin block header, as seen in the `parseHeader` function, is a process of extracting these values from a sequence of bytes. This typically involves manipulating and reversing endianness where necessary — Bitcoin uses little-endian format for its block headers, and Ethereum uses big-endian for its number representations. Thus, reversal functions like `Endian.reverse256` are applied to ensure the bytes are correctly interpreted on the Ethereum platform.

The hash of a Bitcoin block header is computed using a double SHA256 algorithm, which provides a strong cryptographic checksum that miners change by iterating the nonce until the hash matches the network's target difficulty.

## Merkle Root Calculation

The Merkle Root is a single hash that represents all transactions in a Bitcoin block, ensuring data integrity by summarizing the transaction list in a space-efficient manner using a structure called a Merkle Tree.

### Step-by-Step Calculation:

1. **Hash All Transactions**: Each transaction in the block is hashed using the SHA256 algorithm twice (double-SHA256). These hashes are the leaves of the Merkle Tree.
2. **Construct the Merkle Tree**: The Merkle Tree is built by consecutively hashing pairs of transaction hashes (concatenated together), creating parent hashes for the next tree level.
3. **Handling an Odd Number of Hashes**:
   * **Pairing**: Hashes are paired sequentially and hashed together to form the next level of the tree.
   * **Odd Hash Duplication**: If there is an odd number of hashes at any level, the last hash is duplicated and concatenated with itself, forming the final pair for hashing.
   * **Continue Pairing**: These newly formed hashes at each level are considered for the next pairing cycle, and the process repeats.
   * **Repeat Duplication**: If any subsequent level also has an odd number of hashes, the last hash is again duplicated for pairing.
4. **Completing the Tree**: This process of pairing, hashing, and duplicating (as needed) continues upwards until there is only one hash left.
5. **Finalize the Merkle Root**: The last remaining hash is the Merkle Root, encompassing all transactions' information. This hash is stored within the block header and is a fundamental part of block verification. If there is only one transaction, the double-SHA256 hash of that transaction is the Merkle Root.

See <https://en.bitcoin.it/wiki/Protocol_documentation> for more details.
