Giter Club home page Giter Club logo

crypto-101-merkle-tree's Introduction

Merkle Trees and Merkle Proofs in Crypto: Code by example

Table of Contents

Getting Started
What are Merkle Trees?
Exploring Merkle Proofs
Documentation Improvements

Getting Started:

  1. Clone the Repository: Use git clone https://github.com/Citrullin/crypto-101-merkle-tree.git to clone the repository.
  2. Install npm Dependencies: Navigate to the cloned directory (cd crypto-101-merkle-tree) and install dependencies using npm install.

What are Merkle Trees?

The MerkleProof example contains important security pracises

Merkle trees, also known as hash trees, are a fundamental concept in cryptography. At their core, they are a collection of hashed data organized in a binary tree structure. However, instead of going into complex technicalities, let's explore them through a simple JavaScript example.

Imagine a scenario where we need to verify a user's car insurance information. Traditionally, this involves complex and expensive processes like visual document verification and identity checks. To simplify and streamline this process, we can leverage Verifiable Credentials and Decentralized Identifiers (DID), especially in conjunction with Distributed Ledger Technology (DLT) like blockchain.

In our example, let's consider a government-issued ID, such as a driver's license. Each piece of information associated with the ID, like the driver's name or insurance details, can be represented as a hashed block. The top hash in our Merkle tree represents the entire claim, signed by the government, ensuring its validity.

Now, let's dive into constructing the Merkle tree. The top hash serves as the root, and each leaf node represents a hashed piece of information, such as the driver's first name or insurance ID. As we move up the tree, we combine pairs of hashes at each level until we reach the root hash. This root hash acts as the verification point for the entire claim.

Step 1. Hash every leaf

(Insert Visualization of hashing every leaf)

Step 2. Take a pair of the hashed leafs. Combine them and hash them as well.

(Insert Visualization of taking a pair of two leafs)

Step 3. As long as you have more than one hash left, you keep taking two pairs and hash them.

(Insert Visualization of moving one level up and hash)

Step 4. Done. At some point you have only one hash left. This is your root hash.

(Insert Visualization of root hash left)

But enough theory; let's explore a practical JavaScript implementation. You can find the code here.

Exploring Merkle Proofs:

Merkle proofs are essential for verifying data integrity in a decentralized environment. Let's delve deeper into this concept with an example of DAO authentication.

In this scenario, we utilize a blockchain for user authentication and management. Each user's Merkle tree contains hashed data such as salts, usernames, emails, and public keys. This information forms the basis of authentication.

Authentication occurs entirely on the DLT. Anyone with access to the blockchain can process user login requests. However, security is important. In our example, hashed leaf nodes are included in Merkle proofs. This is not the optimal solution and be improved. A more efficient approach involves sending only the diff subtree as proof.

To enhance security, passwords should always be hased together with a salt. This makes rainbow table attacks hard to execude. Additionally, the use of "pepper" can further strengthen defenses. User-specific salts add an extra layer of security, mitigating risks associated with rainbow table attacks or user information exposure.

For a practical demonstration, refer to the code available in auth.js or execute npm run auth. The code is documented with comments and more advanced logging can be enabled. If you struggle to follow the code in auth.js or MerkleTree.js, you should check out the unit tests in Merkletree.test.js.

Documentation Improvements

  • Add Illustrations
  • Replace git clone with actual url
  • Clean code up, naming convention etc.
  • Add Links to keywords, such as DLT, DID, DAO
  • Unit tests
  • More Logging capabilities
  • Extend with actual DLT backend
  • Use actual AES encryption (Browser implementation?)

crypto-101-merkle-tree's People

Contributors

citrullin avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.