Skip to content

BlockSmith is a C++ Bitcoin mining simulation that implements SHA-256, double hashing, Merkle trees, block header serialization, endianness handling, and Proof-of-Work from scratch, focusing on low-level blockchain internals without external crypto libraries.

Notifications You must be signed in to change notification settings

Jaimin-Kundal/BlockSmith

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

BlockSmith

A Bitcoin Mining & Cryptography Simulator in C++


Overview

BlockSmith is a from-scratch Bitcoin cryptography and mining simulator written in C++, focused on understanding Bitcoin internals rather than relying on external libraries.

The project implements the core computational mechanisms of Bitcoin, including hashing, block construction, Merkle trees, and Proof-of-Work.

This project is purely educational and is intended to deeply explain how Bitcoin actually works under the hood.


Objective

The objective of BlockSmith is to:

  • Understand SHA-256 at the bit and byte level
  • Learn how Bitcoin uses cryptographic hashing
  • Simulate Proof-of-Work mining
  • Study Merkle tree construction
  • Explore block header serialization and endianness
  • Build protocol-level intuition for Bitcoin

SHA-256 (From Scratch)

SHA-256 is a cryptographic hash function that produces a 256-bit output from arbitrary input.

In BlockSmith, SHA-256 is implemented manually, including:

  • Message padding (as per FIPS-180-4)
  • Message schedule expansion
  • Compression function (64 rounds)
  • Bitwise operations and rotations
  • Final 32-byte digest generation

This implementation avoids all crypto libraries to ensure full understanding of the algorithm.


Double SHA-256

Bitcoin applies SHA-256 twice to critical data structures.

hash = SHA256(SHA256(data))

Double SHA-256 is used for:

  • Block headers
  • Transactions
  • Merkle tree nodes

BlockSmith implements double hashing exactly as used in Bitcoin.


Merkle Tree

A Merkle tree is a binary hash tree that secures transactions inside a block.

BlockSmith follows Bitcoin’s Merkle rules:

  • Each transaction is double SHA-256 hashed
  • Hashes are paired and concatenated
  • The concatenated result is double SHA-256 hashed
  • If the number of hashes is odd, the last hash is duplicated

The final result is the Merkle Root, which is embedded in the block header.


Block Header

Bitcoin block headers are exactly 80 bytes and contain:

  • Version
  • Previous block hash
  • Merkle root
  • Timestamp
  • Difficulty bits
  • Nonce

All integer fields are serialized using little-endian byte order, while cryptographic hashing internally operates in big-endian format.

This distinction is handled explicitly in BlockSmith.


Proof-of-Work (Mining)

Mining in Bitcoin consists of finding a nonce such that:

In BlockSmith, difficulty is simplified for clarity:

  • A block is valid if its hash starts with N leading zero bytes

This allows experimentation with different difficulty levels while preserving the core idea of Proof-of-Work.


Endianness

Endianness plays a crucial role in Bitcoin:

  • SHA-256 processes data in big-endian form
  • Bitcoin block headers are serialized in little-endian
  • Human-readable hashes are displayed in hexadecimal

BlockSmith carefully manages these conversions to remain protocol-accurate.


What This Project Teaches

By completing BlockSmith, you gain deep understanding of:

  • Cryptographic hashing internals
  • Bitcoin’s use of double SHA-256
  • Merkle tree construction
  • Block header structure
  • Proof-of-Work mechanics
  • Endianness in real-world systems

This knowledge goes far beyond surface-level Bitcoin tutorials.


Possible Future Enhancements

  • Real Bitcoin difficulty target calculation (bits → target)
  • Multi-threaded CPU mining
  • GPU-based hashing (CUDA)
  • Full block chain validation
  • Transaction scripting and UTXO model
  • Peer-to-peer networking

Final Thought

BlockSmith is not about copying Bitcoin —
it is about understanding why Bitcoin works.

If you can understand this project, you can explain Bitcoin.

Happy hacking 🚀

About

BlockSmith is a C++ Bitcoin mining simulation that implements SHA-256, double hashing, Merkle trees, block header serialization, endianness handling, and Proof-of-Work from scratch, focusing on low-level blockchain internals without external crypto libraries.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages