> For the complete documentation index, see [llms.txt](https://pumpkindotfun.gitbook.io/pumpkin-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://pumpkindotfun.gitbook.io/pumpkin-docs/resources/lottery-verification-guide/generate-merkle-root.md).

# Generate Merkle Root

### Generating Merkle Root for verification <a href="#generating-merkle-root-for-verification" id="generating-merkle-root-for-verification"></a>

#### **Step 1: Construct Leaf Bytes**

For each holder:

1. Encode the public key as 32 bytes
2. Convert the `start`, `end`, and `leaf index` values into 8-byte little-endian integers.
3. Concatenate the values in this order:

​leaf\_bytes = pubkey\_bytes || start\_bytes || end\_bytes || leaf\_index\_bytes

> This ensures the leaf structure matches the on-chain verification logic.

#### **Step 2: Hash Leaves (Double Hashing)**

* Apply Keccak256 to the leaf bytes.
* Then, hash the result again (double hashing):

​leaf\_hash = Keccak256(Keccak256(leaf\_bytes))

#### **Step 3: Build the Merkle Tree**

1. Start with all the double-hashed leaves.
2. Iteratively build parent nodes:
3. Pair adjacent hashes (`left`, `right`) and compute the parent hash:

parent\_hash = Keccak256(left || right)

* If there is an odd number of nodes at the current level, duplicate the last node:

parent\_hash = Keccak256(last\_node || last\_node)3. Replace the current level with the newly computed parent hashes. 4. Repeat until only one hash remains.

#### **Step 4: Merkle Root**

* The final single hash at the top of the tree is the Merkle root.
* This root is used in the smart contract to verify individual proofs against the tree.

#### **Important Notes:**

* Leaf ordering: The order of leaves determines the root; it must be consistent with proof generation.
* Intermediate nodes: Only single Keccak256 hash is applied per parent node.
* Odd leaves: The last node is duplicated if the number of nodes at a level is odd.
* Double hash: Only applies to leaves, not to parent nodes.
* Hashing algorithm: Use Keccak256 consistently across leaves and nodes.
