0% found this document useful (0 votes)
68 views3 pages

BCLab - Experiment No1

blockchain

Uploaded by

cleverchatelet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views3 pages

BCLab - Experiment No1

blockchain

Uploaded by

cleverchatelet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Don Bosco Institute of Technology, Kulra(W)

Department of Computer Engineering


CSDL7022: Blockchain Lab2024-25

Experiment No. 01

Experiment Title Cryptography in Blockchain, Merkle root tree hash

For a given string create Merkel Tree and display it’s contents.

Student Name Ananya Solanki

Roll No. 66

Objectives: 1) Students will be able to learn cryptography in blockchain

2) Students will be able to apply hash function on given text

3) Students will be able to implement the Merkel Tree

Course Outcome:
CSDL7022.1 Explain and create cryptographic hash using Merkel Tree.
Theory /Algorithm: Explain -1) Blockchain
2) Hash tree
3) Merkel Tree with example

1. Blockchain: Blockchain is a decentralized system for securely storing


and transferring digital information. It uses a chain of blocks distributed
across a network of computers to create a transparent and tamper-
resistant record of data and transactions. It is widely used beyond
cryptocurrencies for various applications in different industries.
Blockchain technology provides transparency, security, and
decentralization in data management and transactions across various
industries.

2. Hash tree: A hash tree, or Merkle tree, is a data structure that organizes
data into a hierarchical structure of hashed values for efficient
verification of data integrity and authenticity. It is commonly used in
blockchain technology to ensure the security and validity of transactions
and data.
Hash trees, or Merkle trees, enable quick and efficient verification of data
integrity by comparing the root hash with a trusted hash value, ensuring
security in blockchain transactions and data storage.

3. Merkel Tree with example: A merkle tree is used in cryptography and


computer science to verify the authenticity of a data efficiently. Merkle
trees are extremely crucial in the fields of cryptography and blockchain
technology for ensuring data integrity and security. In this article, we will
learn the significance of Merkle trees and to implement a merkle tree in
go. Here we are going to use two different examples to perform this
implementation in the first example we will build the Merkle tree
recursively through the `MerkleNode` struct with hash, left, and right
pointer, and in the second example we will employ an iterative method
for building the tree.
Ex: Consider a scenario where four data blocks, A, B, C, and D, are
hashed individually. These hashed values are then paired and hashed
together, creating two intermediate hashes: H(AB) and H(CD). Finally,
the Merkle root is computed by hashing the concatenation of these
intermediate hashes: H(H(AB) + H(CD)).

Program Code: package main


import (
"crypto/sha256"
"fmt"
)
type MerkleNode struct {
Hash string
Left *MerkleNode
Right *MerkleNode
}
func calculateHash(data string) string {
hash := sha256.Sum256([]byte(data))
return fmt.Sprintf("%x", hash)
}
func buildMerkleTree(data []string) *MerkleNode {
var nodes []*MerkleNode
for _, d := range data {
node := &MerkleNode{Hash: calculateHash(d)}
nodes = append(nodes, node)
}
for len(nodes) > 1 {
var newLevel []*MerkleNode
for i := 0; i < len(nodes); i += 2 {
left := nodes[i]
right := left
if i+1 < len(nodes) {
right = nodes[i+1]
}
parent := &MerkleNode{Hash: calculateHash(left.Hash + right.Hash),
Left: left, Right: right}
newLevel = append(newLevel, parent)
}
nodes = newLevel
}
return nodes[0]
}
func printTree(node *MerkleNode, indent string) {
if node != nil {
fmt.Println(indent + "Hash:", node.Hash)
if node.Left != nil {
printTree(node.Left, indent+" ")
}
if node.Right != nil {
printTree(node.Right, indent+" ")
}
}
}
func main() {
data := []string{"A", "B", "C", "D"}
root := buildMerkleTree(data)
printTree(root, "")
fmt.Println("Root Hash:", root.Hash)
}
Output of the
program:

Outcome of the A Merkle tree is defined as a cryptographic structure that ensures data integrity
Experiment: by hashing smaller portions of data and then hierarchically combining them to
form a single root hash. In this article, we tried to implement a Merkle tree in
Go lang using 2 methods – namely the recursive and iterative approaches. The
former highlights the self-similar nature of Merkle trees which makes it
conceptually clear and intuitive. However, a loop-based mechanism that
consumes fewer system resources is more appropriate for handling large
datasets and avoiding stack-overflow issues.

References: Google, Elearn, tutorialspoint

Course in-charge-Mayura Gavhane

You might also like