diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index 8c5edc9..f1e7f73 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -1,29 +1,29 @@ -# Contributing Guidelines - -## Submitting Your Task - -1. **Fork the repository** to your GitHub account. -2. **Clone** the forked repository: - -```bash -git clone https://github.com//Web3bridge Rust Masterclass Cohort II -cd Web3bridge Rust Masterclass Cohort II -``` - -3. **Sync with upstream** regularly: - -```bash -git remote add upstream https://github.com/Bloceducare/Web3bridge Rust Masterclass Cohort II -git pull upstream master -git push origin master -``` - -4. **Navigate to the correct folder** (`week-x/day-y/your_name`). -5. **Submit a PR** following the template. -6. **Wait for mentor review** and address feedback if needed. - -## Code of Conduct - -- Follow best practices and structured folder naming. -- Be respectful in discussions and PR comments. -- Test your work before submitting. +# Contributing Guidelines + +## Submitting Your Task + +1. **Fork the repository** to your GitHub account. +2. **Clone** the forked repository: + +```bash +git clone https://github.com//Web3bridge Rust Masterclass Cohort II +cd Web3bridge Rust Masterclass Cohort II +``` + +3. **Sync with upstream** regularly: + +```bash +git remote add upstream https://github.com/Bloceducare/Web3bridge Rust Masterclass Cohort II +git pull upstream master +git push origin master +``` + +4. **Navigate to the correct folder** (`week-x/day-y/your_name`). +5. **Submit a PR** following the template. +6. **Wait for mentor review** and address feedback if needed. + +## Code of Conduct + +- Follow best practices and structured folder naming. +- Be respectful in discussions and PR comments. +- Test your work before submitting. diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md index 4af534f..4284c8a 100644 --- a/.github/ISSUE_TEMPLATE.md +++ b/.github/ISSUE_TEMPLATE.md @@ -1,29 +1,29 @@ -## Issue Report - -### Type of Issue: - -- [ ] Bug Report -- [ ] Task Clarification -- [ ] Other - -### Description: - -Describe the issue or question in detail. - -### Steps to Reproduce (if applicable): - -1. Step 1 -2. Step 2 -3. Step 3 - -### Expected Behavior: - -Explain what should have happened. - -### Actual Behavior: - -Explain what actually happened. - -### Additional Notes: - -Any screenshots or additional info. +## Issue Report + +### Type of Issue: + +- [ ] Bug Report +- [ ] Task Clarification +- [ ] Other + +### Description: + +Describe the issue or question in detail. + +### Steps to Reproduce (if applicable): + +1. Step 1 +2. Step 2 +3. Step 3 + +### Expected Behavior: + +Explain what should have happened. + +### Actual Behavior: + +Explain what actually happened. + +### Additional Notes: + +Any screenshots or additional info. diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index bfbca7c..8bcce21 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -1,23 +1,23 @@ -## Task Submission PR - -### Student Info: - -- **Registered Name:** -- **Week:** -- **Day:** - -### Task Details: - -- **Task Name:** -- **Task Description:** Briefly describe the task completed. - -### Checklist Before Submitting: - -- [ ] My code follows the repository folder structure. -- [ ] I have properly named my files and folders. -- [ ] I have documented my code where necessary. -- [ ] I have tested my solution and ensured it works as expected. - -### Additional Comments: - -If any, mention here. +## Task Submission PR + +### Student Info: + +- **Registered Name:** +- **Week:** +- **Day:** + +### Task Details: + +- **Task Name:** +- **Task Description:** Briefly describe the task completed. + +### Checklist Before Submitting: + +- [ ] My code follows the repository folder structure. +- [ ] I have properly named my files and folders. +- [ ] I have documented my code where necessary. +- [ ] I have tested my solution and ensured it works as expected. + +### Additional Comments: + +If any, mention here. diff --git a/.gitignore b/.gitignore index d920de5..5702e49 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,9 @@ -# macOS junk -.DS_Store - -# Rust build artifacts -target/ - -# Env files -.env - +# macOS junk +.DS_Store + +# Rust build artifacts +target/ + +# Env files +.env + diff --git a/README.md b/README.md index 7c0298f..47c45ef 100644 --- a/README.md +++ b/README.md @@ -1,156 +1,156 @@ -# Web3bridge Rust Masterclass Cohort II - GitHub Contribution Guide - -Welcome to the Web3bridge Rust Masterclass Cohort II training program! This repository is used for submitting tasks, tracking student progress, and grading assessments. Follow this guide to ensure a smooth contribution process. - ---- - -## For Students: Submitting Your Tasks - -### 1. Fork the Repository - -Create a personal copy of this repository on your GitHub account. - -- Visit the repository page: [Web3bridge Rust Masterclass Cohort II](https://github.com/Bloceducare/Web3bridge-Rust-Masterclass-Cohort-II) -- Click the "Fork" button at the top right of the page. - -### 2. Clone Your Forked Repository - -Download your forked repository to your local machine: - -```bash -git clone https://github.com//Web3bridge-Rust-Masterclass-Cohort-II -cd Web3bridge-Rust-Masterclass-Cohort-II -``` - -### 3. Sync Your Repository Regularly - -Keep your fork updated with the latest changes from the main repository: - -```bash -git remote add upstream https://github.com/Bloceducare/Web3bridge-Rust-Masterclass-Cohort-II -git pull upstream master -git push origin master -``` - -### 4. Navigate to the Correct Submission Folder - -Tasks are categorized by week and day. Navigate to the appropriate folder: - -```bash -cd submissions/week-/day- -``` - -Example: - -```bash -cd submissions/week-1/day-1 -``` - -### 5. Create Your Personal Folder - -Each student should create a folder using their registered name and project name: - -```bash -mkdir - -``` - -Example: - -```bash -mkdir JohnDoe-Merkle-Tree -``` - -### 6. Add Your Task Files - -Place your task files inside your folder. Ensure proper documentation and organization. - -Checkout to your branch to make life easier. Ensure it's organised properly. - -```bash -git checkout -b -``` - -### 7. Commit and Push Your Changes - -Save your changes and push them to your forked repository: - -```bash -git add . -git commit -m "Add Week Day task for " -git push origin main -``` - -Example: - -```bash -git add . -git commit -m "Add Week 1 Day 1 task for JohnDoe Merkle Tree" -git push origin main -``` - -### 8. Create a Pull Request - -- Go to the original GitHub repository in your browser: [Web3bridge-Rust-Masterclass-Cohort-II](https://github.com/Bloceducare/Web3bridge-Rust-Masterclass-Cohort-II). -- Click on the Pull Requests tab. -- Click New Pull Request and select "Compare across forks" if needed. -- Select your forked repository and branch as the source, and the master branch of the original repository as the destination. -- Provide a descriptive title and include details about your project in the description. - -Example PR Title: - -```bash -Add Week 1 Day 1 task for JohnDoe Merkle Tree -``` - -### 9. Wait for Review - -Mentors may leave comments or request changes. Make updates accordingly and push again. - ---- - -## Additional Notes - -- Ensure your project is complete, well-documented, and functional before submitting. -- Follow the repository's coding and folder structure guidelines. -- Create a new folder each week and place all files and assets related to that week's project in the folder. -- PRs should have meaningful descriptions. -- Regularly sync the repository to get the latest updates. -- If you encounter any issues, contact your training facilitator for help. - -## For Mentors: Managing Tasks - -### 1. Adding Tasks - -- Navigate to the **tasks/week-/** folder. -- Create a markdown file named `Day--task.md`. -- Provide clear instructions and expectations. -- Commit and push your changes. - -Example: - -```bash -git add tasks/week-1/Day-1-task.md -git commit -m "Add Week 1 Day 1 Merkle Tree Task" -git push origin main -``` - -### 2. Preparing the Submission Folder - -- Navigate to the **submissions/week-/** folder. -- If a task is given for a specific day, create a corresponding **day subfolder** (`day-/`). -- Example: - submissions/week-3/day-2/ - -- Students will submit inside this subfolder using their **Registered Name** and **Project Name** as their personal directory. -- Example: - submissions/week-3/day-2/JohnDoe-Merkle-Tree/ - -### 3. Reviewing Student Submissions - -- Check the Pull Requests (PRs) tab on GitHub. -- Open a PR and review the student’s work. -- Leave comments for corrections if needed. -- Approve the PR if it meets expectations. -- Merge the PR after approval. - -Thank you for contributing to Web3bridge Rust Masterclass Cohort I! Happy coding! +# Web3bridge Rust Masterclass Cohort II - GitHub Contribution Guide + +Welcome to the Web3bridge Rust Masterclass Cohort II training program! This repository is used for submitting tasks, tracking student progress, and grading assessments. Follow this guide to ensure a smooth contribution process. + +--- + +## For Students: Submitting Your Tasks + +### 1. Fork the Repository + +Create a personal copy of this repository on your GitHub account. + +- Visit the repository page: [Web3bridge Rust Masterclass Cohort II](https://github.com/Bloceducare/Web3bridge-Rust-Masterclass-Cohort-II) +- Click the "Fork" button at the top right of the page. + +### 2. Clone Your Forked Repository + +Download your forked repository to your local machine: + +```bash +git clone https://github.com//Web3bridge-Rust-Masterclass-Cohort-II +cd Web3bridge-Rust-Masterclass-Cohort-II +``` + +### 3. Sync Your Repository Regularly + +Keep your fork updated with the latest changes from the main repository: + +```bash +git remote add upstream https://github.com/Bloceducare/Web3bridge-Rust-Masterclass-Cohort-II +git pull upstream master +git push origin master +``` + +### 4. Navigate to the Correct Submission Folder + +Tasks are categorized by week and day. Navigate to the appropriate folder: + +```bash +cd submissions/week-/day- +``` + +Example: + +```bash +cd submissions/week-1/day-1 +``` + +### 5. Create Your Personal Folder + +Each student should create a folder using their registered name and project name: + +```bash +mkdir - +``` + +Example: + +```bash +mkdir JohnDoe-Merkle-Tree +``` + +### 6. Add Your Task Files + +Place your task files inside your folder. Ensure proper documentation and organization. + +Checkout to your branch to make life easier. Ensure it's organised properly. + +```bash +git checkout -b +``` + +### 7. Commit and Push Your Changes + +Save your changes and push them to your forked repository: + +```bash +git add . +git commit -m "Add Week Day task for " +git push origin main +``` + +Example: + +```bash +git add . +git commit -m "Add Week 1 Day 1 task for JohnDoe Merkle Tree" +git push origin main +``` + +### 8. Create a Pull Request + +- Go to the original GitHub repository in your browser: [Web3bridge-Rust-Masterclass-Cohort-II](https://github.com/Bloceducare/Web3bridge-Rust-Masterclass-Cohort-II). +- Click on the Pull Requests tab. +- Click New Pull Request and select "Compare across forks" if needed. +- Select your forked repository and branch as the source, and the master branch of the original repository as the destination. +- Provide a descriptive title and include details about your project in the description. + +Example PR Title: + +```bash +Add Week 1 Day 1 task for JohnDoe Merkle Tree +``` + +### 9. Wait for Review + +Mentors may leave comments or request changes. Make updates accordingly and push again. + +--- + +## Additional Notes + +- Ensure your project is complete, well-documented, and functional before submitting. +- Follow the repository's coding and folder structure guidelines. +- Create a new folder each week and place all files and assets related to that week's project in the folder. +- PRs should have meaningful descriptions. +- Regularly sync the repository to get the latest updates. +- If you encounter any issues, contact your training facilitator for help. + +## For Mentors: Managing Tasks + +### 1. Adding Tasks + +- Navigate to the **tasks/week-/** folder. +- Create a markdown file named `Day--task.md`. +- Provide clear instructions and expectations. +- Commit and push your changes. + +Example: + +```bash +git add tasks/week-1/Day-1-task.md +git commit -m "Add Week 1 Day 1 Merkle Tree Task" +git push origin main +``` + +### 2. Preparing the Submission Folder + +- Navigate to the **submissions/week-/** folder. +- If a task is given for a specific day, create a corresponding **day subfolder** (`day-/`). +- Example: + submissions/week-3/day-2/ + +- Students will submit inside this subfolder using their **Registered Name** and **Project Name** as their personal directory. +- Example: + submissions/week-3/day-2/JohnDoe-Merkle-Tree/ + +### 3. Reviewing Student Submissions + +- Check the Pull Requests (PRs) tab on GitHub. +- Open a PR and review the student’s work. +- Leave comments for corrections if needed. +- Approve the PR if it meets expectations. +- Merge the PR after approval. + +Thank you for contributing to Web3bridge Rust Masterclass Cohort I! Happy coding! diff --git a/live-session/week-1 & 2/day-10/expense-tracker/Cargo.lock b/live-session/week-1 & 2/day-10/expense-tracker/Cargo.lock index 96e0d66..4cc215b 100644 --- a/live-session/week-1 & 2/day-10/expense-tracker/Cargo.lock +++ b/live-session/week-1 & 2/day-10/expense-tracker/Cargo.lock @@ -1,7 +1,7 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 4 - -[[package]] -name = "expense-tracker" -version = "0.1.0" +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "expense-tracker" +version = "0.1.0" diff --git a/live-session/week-1 & 2/day-10/expense-tracker/Cargo.toml b/live-session/week-1 & 2/day-10/expense-tracker/Cargo.toml index 7bcfde6..746cdc8 100644 --- a/live-session/week-1 & 2/day-10/expense-tracker/Cargo.toml +++ b/live-session/week-1 & 2/day-10/expense-tracker/Cargo.toml @@ -1,6 +1,6 @@ -[package] -name = "expense-tracker" -version = "0.1.0" -edition = "2024" - -[dependencies] +[package] +name = "expense-tracker" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/live-session/week-1 & 2/day-10/expense-tracker/src/main.rs b/live-session/week-1 & 2/day-10/expense-tracker/src/main.rs index a544e80..40100f4 100644 --- a/live-session/week-1 & 2/day-10/expense-tracker/src/main.rs +++ b/live-session/week-1 & 2/day-10/expense-tracker/src/main.rs @@ -1,80 +1,80 @@ -use std::{collections::HashMap, io}; - -// Expense tracker -// Add the expenses -// Remove -// Update -// View - -// Hashmaps -// structs -// enums -// Hashmaps - -#[derive(Debug, Clone)] -enum TransactionType { - Credit, - Debit, -} -#[derive(Debug, Clone)] -struct Expense { - id: u8, - name: String, - amount: f64, - tx_type: TransactionType, -} - -struct ExpenseTracker { - values: HashMap, - next_id: u8, -} - -impl ExpenseTracker { - fn new() -> Self { - Self { - values: HashMap::new(), - next_id: 1, - } - } - - fn add(&mut self, name: String, amount: f64, tx_type: TransactionType) -> Expense { - let current_id = self.next_id; - let new_expense = Expense { - id: current_id, - name, - amount, - tx_type, - }; - self.values.insert(current_id, new_expense.clone()); - self.next_id += 1; - new_expense - } - - fn view_all(&self) -> Vec<&Expense> { - self.values.values().collect() - } - - fn update(&mut self, id: u8, amount: f64, tx_type: TransactionType) -> bool { - match self.values.get_mut(&id) { - Some(exp) => { - exp.amount = amount; - exp.tx_type = tx_type; - true - } - None => false, - } - // let updated_expense = Expense { - // id, - // amount, - // tx_type, - // }; - // self.values.put(id) - } - - fn delete(&mut self, id: u8) -> bool { - self.values.remove(&id).is_some() - } -} -fn main() { - println!("Hello, world!"); -} +use std::{collections::HashMap, io}; + +// Expense tracker +// Add the expenses +// Remove +// Update +// View + +// Hashmaps +// structs +// enums +// Hashmaps + +#[derive(Debug, Clone)] +enum TransactionType { + Credit, + Debit, +} +#[derive(Debug, Clone)] +struct Expense { + id: u8, + name: String, + amount: f64, + tx_type: TransactionType, +} + +struct ExpenseTracker { + values: HashMap, + next_id: u8, +} + +impl ExpenseTracker { + fn new() -> Self { + Self { + values: HashMap::new(), + next_id: 1, + } + } + + fn add(&mut self, name: String, amount: f64, tx_type: TransactionType) -> Expense { + let current_id = self.next_id; + let new_expense = Expense { + id: current_id, + name, + amount, + tx_type, + }; + self.values.insert(current_id, new_expense.clone()); + self.next_id += 1; + new_expense + } + + fn view_all(&self) -> Vec<&Expense> { + self.values.values().collect() + } + + fn update(&mut self, id: u8, amount: f64, tx_type: TransactionType) -> bool { + match self.values.get_mut(&id) { + Some(exp) => { + exp.amount = amount; + exp.tx_type = tx_type; + true + } + None => false, + } + // let updated_expense = Expense { + // id, + // amount, + // tx_type, + // }; + // self.values.put(id) + } + + fn delete(&mut self, id: u8) -> bool { + self.values.remove(&id).is_some() + } +} +fn main() { + println!("Hello, world!"); +} diff --git a/live-session/week-1 & 2/day1-9/.gitignore b/live-session/week-1 & 2/day1-9/.gitignore index ea8c4bf..0b42d2d 100644 --- a/live-session/week-1 & 2/day1-9/.gitignore +++ b/live-session/week-1 & 2/day1-9/.gitignore @@ -1 +1 @@ -/target +/target diff --git a/live-session/week-1 & 2/day1-9/Cargo.lock b/live-session/week-1 & 2/day1-9/Cargo.lock index 7e223d5..ae13276 100644 --- a/live-session/week-1 & 2/day1-9/Cargo.lock +++ b/live-session/week-1 & 2/day1-9/Cargo.lock @@ -1,7 +1,7 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 4 - -[[package]] -name = "todo_list" -version = "0.1.0" +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "todo_list" +version = "0.1.0" diff --git a/live-session/week-1 & 2/day1-9/Cargo.toml b/live-session/week-1 & 2/day1-9/Cargo.toml index 4691b06..8bd7c13 100644 --- a/live-session/week-1 & 2/day1-9/Cargo.toml +++ b/live-session/week-1 & 2/day1-9/Cargo.toml @@ -1,6 +1,6 @@ -[package] -name = "todo_list" -version = "0.1.0" -edition = "2024" - -[dependencies] +[package] +name = "todo_list" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/live-session/week-1 & 2/day1-9/src/bin/assignment.rs b/live-session/week-1 & 2/day1-9/src/bin/assignment.rs index b2529b3..4d98f86 100644 --- a/live-session/week-1 & 2/day1-9/src/bin/assignment.rs +++ b/live-session/week-1 & 2/day1-9/src/bin/assignment.rs @@ -1,44 +1,44 @@ -use std::io; - -enum PowerOptions { - Sleep, - Reboot, - Shutdown, - Hibernate, -} - -impl PowerOptions { - fn power_action(state: &str) -> Option { - let state = state.trim().to_lowercase(); - match state.as_str() { - "sleep" => Some(PowerOptions::Sleep), - "reboot" => Some(PowerOptions::Reboot), - "shutdown" => Some(PowerOptions::Shutdown), - "hibernate" => Some(PowerOptions::Hibernate), - _ => None, - } - } -} - -fn print_state(action: PowerOptions) { - match action { - PowerOptions::Sleep => println!("Sleeping"), - PowerOptions::Reboot => println!("Rebooting"), - PowerOptions::Shutdown => println!("Shutting Down"), - PowerOptions::Hibernate => println!("Hibernating"), - } -} - -fn main() { - let mut input = String::new(); - println!("Enter power option"); - - let user_input = io::stdin().read_line(&mut input); - - if user_input.is_ok() { - match PowerOptions::power_action(&input) { - Some(result) => print_state(result), - None => println!("Invalid power state"), - } - } -} +use std::io; + +enum PowerOptions { + Sleep, + Reboot, + Shutdown, + Hibernate, +} + +impl PowerOptions { + fn power_action(state: &str) -> Option { + let state = state.trim().to_lowercase(); + match state.as_str() { + "sleep" => Some(PowerOptions::Sleep), + "reboot" => Some(PowerOptions::Reboot), + "shutdown" => Some(PowerOptions::Shutdown), + "hibernate" => Some(PowerOptions::Hibernate), + _ => None, + } + } +} + +fn print_state(action: PowerOptions) { + match action { + PowerOptions::Sleep => println!("Sleeping"), + PowerOptions::Reboot => println!("Rebooting"), + PowerOptions::Shutdown => println!("Shutting Down"), + PowerOptions::Hibernate => println!("Hibernating"), + } +} + +fn main() { + let mut input = String::new(); + println!("Enter power option"); + + let user_input = io::stdin().read_line(&mut input); + + if user_input.is_ok() { + match PowerOptions::power_action(&input) { + Some(result) => print_state(result), + None => println!("Invalid power state"), + } + } +} diff --git a/live-session/week-1 & 2/day1-9/src/bin/common-col.rs b/live-session/week-1 & 2/day1-9/src/bin/common-col.rs index 802f136..5891f17 100644 --- a/live-session/week-1 & 2/day1-9/src/bin/common-col.rs +++ b/live-session/week-1 & 2/day1-9/src/bin/common-col.rs @@ -1,35 +1,35 @@ -use std::collections::HashMap; -// common collections -// string, vec, hashmaps - -fn main() { - let names = vec![String::from("Victory the silent speaker")]; - println!("Value of our vec is {names:?}"); - - let mut new_map: HashMap = HashMap::new(); - let abel = String::from("abel"); - new_map.insert(abel.clone(), 20); - let value = new_map.get(&abel); - - // account_number -> balance - // key value - - println!("value: {:?}", value.unwrap()); - - let new_sting = String::from("Hello"); - let o = new_sting.chars().nth(4); - - // this will fail - // let string_fail = String::from("hello"); - // string_fail[0]; - // this will fail why? - // You cannot index string like arrays so - // you basically have to use the chars().nth() - - match o { - Some(o) => println!("value of nth character: {o}"), - None => println!("value not found"), - } - - println!("value {o:?}"); -} +use std::collections::HashMap; +// common collections +// string, vec, hashmaps + +fn main() { + let names = vec![String::from("Victory the silent speaker")]; + println!("Value of our vec is {names:?}"); + + let mut new_map: HashMap = HashMap::new(); + let abel = String::from("abel"); + new_map.insert(abel.clone(), 20); + let value = new_map.get(&abel); + + // account_number -> balance + // key value + + println!("value: {:?}", value.unwrap()); + + let new_sting = String::from("Hello"); + let o = new_sting.chars().nth(4); + + // this will fail + // let string_fail = String::from("hello"); + // string_fail[0]; + // this will fail why? + // You cannot index string like arrays so + // you basically have to use the chars().nth() + + match o { + Some(o) => println!("value of nth character: {o}"), + None => println!("value not found"), + } + + println!("value {o:?}"); +} diff --git a/live-session/week-1 & 2/day1-9/src/bin/explanation.rs b/live-session/week-1 & 2/day1-9/src/bin/explanation.rs index 46d0d5b..1eca234 100644 --- a/live-session/week-1 & 2/day1-9/src/bin/explanation.rs +++ b/live-session/week-1 & 2/day1-9/src/bin/explanation.rs @@ -1,53 +1,53 @@ -fn main() { - // NOTE: - // 1. Every value has an owner. - // 2. There can only be an owner to a value at a time. - // 3. Once the owner goes out of scope the value is dropped. - - let mut greeting = String::from("Hello Abel"); - - println!("==== cap, len, ptr========="); - println!( - "Initials {} {} {:?}", - greeting.len(), - greeting.capacity(), - greeting.as_ptr() - ); - println!("Before pop print: {}", &greeting); - - greeting.push_str(", How are you?"); - - println!("==== cap, len, ptr after push========="); - println!( - "Before pop: {} {} {:?}", - greeting.len(), - greeting.capacity(), - greeting.as_ptr() - ); - - println!("Before pop: {greeting}"); - - let r = greeting.pop(); - - println!("==== cap, len, ptr after pop========="); - println!( - "Before pop: {} {} {:?}", - greeting.len(), - greeting.capacity(), - greeting.as_ptr() - ); - - if let Some(value) = r { - println!("Value: {value}") - } - - println!("{:?}", r.unwrap()); - println!("After pop:{greeting}"); - - println!("================================"); - - let hello = String::from("Hello"); - println!("{hello}"); - - // println!("{hello}"); // Diagnostics: 1. cannot find value `hello` in this scope [E0425] -} +fn main() { + // NOTE: + // 1. Every value has an owner. + // 2. There can only be an owner to a value at a time. + // 3. Once the owner goes out of scope the value is dropped. + + let mut greeting = String::from("Hello Abel"); + + println!("==== cap, len, ptr========="); + println!( + "Initials {} {} {:?}", + greeting.len(), + greeting.capacity(), + greeting.as_ptr() + ); + println!("Before pop print: {}", &greeting); + + greeting.push_str(", How are you?"); + + println!("==== cap, len, ptr after push========="); + println!( + "Before pop: {} {} {:?}", + greeting.len(), + greeting.capacity(), + greeting.as_ptr() + ); + + println!("Before pop: {greeting}"); + + let r = greeting.pop(); + + println!("==== cap, len, ptr after pop========="); + println!( + "Before pop: {} {} {:?}", + greeting.len(), + greeting.capacity(), + greeting.as_ptr() + ); + + if let Some(value) = r { + println!("Value: {value}") + } + + println!("{:?}", r.unwrap()); + println!("After pop:{greeting}"); + + println!("================================"); + + let hello = String::from("Hello"); + println!("{hello}"); + + // println!("{hello}"); // Diagnostics: 1. cannot find value `hello` in this scope [E0425] +} diff --git a/live-session/week-1 & 2/day1-9/src/bin/implement.rs b/live-session/week-1 & 2/day1-9/src/bin/implement.rs index 5d259d2..6a44a32 100644 --- a/live-session/week-1 & 2/day1-9/src/bin/implement.rs +++ b/live-session/week-1 & 2/day1-9/src/bin/implement.rs @@ -1,41 +1,41 @@ -pub struct Rectangle { - pub width: u32, - pub height: u32, -} - -pub struct RectTup(pub u32, pub u32); - -pub fn tuple_area(dimension: &RectTup) -> u32 { - dimension.0 * dimension.1 -} - -pub fn area(dimension: &Rectangle) -> u32 { - dimension.width * dimension.height -} - -impl Rectangle { - pub fn impl_area(&self) -> u32 { - self.width * self.height - } -} - -fn main() { - let rect1 = RectTup(34, 36); - - let tuple_result = tuple_area(&rect1); - - println!("Value of tuple rect is {:?}", tuple_result); - - let rect = Rectangle { - width: 20, - height: 40, - }; - - let res = rect.impl_area(); - - println!("Value of rectangle implementation is {:?}", res); - - let result = area(&rect); - - println!("Value of result is {:?}", result); -} +pub struct Rectangle { + pub width: u32, + pub height: u32, +} + +pub struct RectTup(pub u32, pub u32); + +pub fn tuple_area(dimension: &RectTup) -> u32 { + dimension.0 * dimension.1 +} + +pub fn area(dimension: &Rectangle) -> u32 { + dimension.width * dimension.height +} + +impl Rectangle { + pub fn impl_area(&self) -> u32 { + self.width * self.height + } +} + +fn main() { + let rect1 = RectTup(34, 36); + + let tuple_result = tuple_area(&rect1); + + println!("Value of tuple rect is {:?}", tuple_result); + + let rect = Rectangle { + width: 20, + height: 40, + }; + + let res = rect.impl_area(); + + println!("Value of rectangle implementation is {:?}", res); + + let result = area(&rect); + + println!("Value of result is {:?}", result); +} diff --git a/live-session/week-1 & 2/day1-9/src/bin/struct_example.rs b/live-session/week-1 & 2/day1-9/src/bin/struct_example.rs index 403212f..ea3ebe8 100644 --- a/live-session/week-1 & 2/day1-9/src/bin/struct_example.rs +++ b/live-session/week-1 & 2/day1-9/src/bin/struct_example.rs @@ -1,71 +1,71 @@ -#[derive(Debug)] -struct StudentDetails { - id: u8, - first_name: String, - last_name: String, - age: u8, - is_registered: bool, -} - -struct Color(i32, u32, f64); - -fn main() { - let mut abel = StudentDetails { - id: 1, - first_name: String::from("Abel"), - last_name: String::from("Osaretin"), - age: 2, - is_registered: true, - }; - - abel.last_name = String::from("Bimbo"); - - let mike = StudentDetails { - first_name: String::from("Mike"), - last_name: String::from("Anand"), - is_registered: false, - ..abel - }; - - println!("New student {:?}", mike); - - println!( - "Student details {:?} {:?} {:?} {:?}", - abel.first_name, abel.last_name, abel.age, abel.id - ); - - let s = dangle(); - let b = s.to_owned(); - println!("value of {b}"); - - let _black = Color(0, 0, 0.0); -} - -fn create_student(last_name: String, first_name: String, age: u8, id: u8) -> StudentDetails { - StudentDetails { - id, - first_name, - last_name, - age, - is_registered: true, - } -} - -fn create_student2(data: &StudentDetails) -> StudentDetails { - StudentDetails { - id: data.id, - first_name: data.first_name.clone(), - last_name: data.last_name.clone(), - age: data.age, - is_registered: data.is_registered, - } -} - -fn dangle() -> &'static str { - let s = "hello"; - &s -} - -fn create_student_(data: &mut StudentDetails) -> &StudentDetails { - data -} +#[derive(Debug)] +struct StudentDetails { + id: u8, + first_name: String, + last_name: String, + age: u8, + is_registered: bool, +} + +struct Color(i32, u32, f64); + +fn main() { + let mut abel = StudentDetails { + id: 1, + first_name: String::from("Abel"), + last_name: String::from("Osaretin"), + age: 2, + is_registered: true, + }; + + abel.last_name = String::from("Bimbo"); + + let mike = StudentDetails { + first_name: String::from("Mike"), + last_name: String::from("Anand"), + is_registered: false, + ..abel + }; + + println!("New student {:?}", mike); + + println!( + "Student details {:?} {:?} {:?} {:?}", + abel.first_name, abel.last_name, abel.age, abel.id + ); + + let s = dangle(); + let b = s.to_owned(); + println!("value of {b}"); + + let _black = Color(0, 0, 0.0); +} + +fn create_student(last_name: String, first_name: String, age: u8, id: u8) -> StudentDetails { + StudentDetails { + id, + first_name, + last_name, + age, + is_registered: true, + } +} + +fn create_student2(data: &StudentDetails) -> StudentDetails { + StudentDetails { + id: data.id, + first_name: data.first_name.clone(), + last_name: data.last_name.clone(), + age: data.age, + is_registered: data.is_registered, + } +} + +fn dangle() -> &'static str { + let s = "hello"; + &s +} + +fn create_student_(data: &mut StudentDetails) -> &StudentDetails { + data +} diff --git a/live-session/week-1 & 2/day1-9/src/bin/todo.rs b/live-session/week-1 & 2/day1-9/src/bin/todo.rs index 85171fc..b09b82a 100644 --- a/live-session/week-1 & 2/day1-9/src/bin/todo.rs +++ b/live-session/week-1 & 2/day1-9/src/bin/todo.rs @@ -1,138 +1,138 @@ -use std::f64; - -enum Coin { - Penny, - Nickel, - Dime, - Quarter, -} - -// penny = 1 -// Nickel = 5 -// dime = 10 -// Quarter = 25 - -fn coin_value(coin: Coin) -> u8 { - match coin { - Coin::Penny => 1, - Coin::Nickel => 5, - Coin::Dime => 10, - Coin::Quarter => 25, - } -} - -enum Menu { - Create, - Update, - MarkComplete, - Delete, - Get, - Exit, -} - -#[derive(Debug, Clone, PartialEq)] -struct Todo { - id: u8, - title: String, - is_completed: Status, -} - -#[derive(Debug, Clone, PartialEq)] -enum Status { - Incomplete, - Completed, - Pending, -} - -struct TodoList { - data: Vec, - next_id: u8, -} - -impl TodoList { - fn new() -> Self { - Self { - data: Vec::new(), - next_id: 1, - } - } - - fn create_todo(&mut self, title: String) -> u8 { - let current_id = self.next_id; - - let todo = Todo { - id: current_id, - title, - is_completed: Status::Incomplete, - }; - - self.next_id += 1; - self.data.push(todo); - current_id - } - - fn mark_todo(&mut self, id: u8) -> bool { - if let Some(todo_item) = self.data.iter_mut().find(|todo_id| todo_id.id == id) { - todo_item.is_completed = Status::Completed; - true - } else { - false - } - } - fn delete_todo2(&mut self, id: u8) -> bool { - if let Some(user_todo_index) = self.data.iter_mut().position(|todo_id| todo_id.id == id) { - self.data.remove(user_todo_index); - true - } else { - false - } - } - - fn delete_todo(&mut self, id: u8) { - self.data.retain(|todo_id| todo_id.id != id) - } - - fn update_todo(&mut self, id: u8, title: String) -> bool { - for todo in self.data.iter_mut() { - if todo.id == id { - todo.title = title; - return true; - } - } - - false - } - - fn update_todo_if_let(&mut self, id: u8, new_title: String) -> bool { - if let Some(user_todo) = self.data.iter_mut().find(|todo_id| todo_id.id == id) { - user_todo.title = new_title; - true - } else { - false - } - } - - fn update_todo_method_qubzes(&mut self, id: u8, title: String) -> String { - let data = self - .data - .iter_mut() - .find(|todo| todo.id == id) - .expect("failed"); - data.title = title.clone(); - title - } - - fn get_single_todo(&self, id: u8) -> &Todo { - self.data.iter().find(|todo| todo.id == id).expect("failed") - } - - fn get_all_todo(&self) -> &Vec { - &self.data - } -} - -fn main() { - let num = 10; - let decimal = num as f64; - println!("Value: {decimal:.2}") -} +use std::f64; + +enum Coin { + Penny, + Nickel, + Dime, + Quarter, +} + +// penny = 1 +// Nickel = 5 +// dime = 10 +// Quarter = 25 + +fn coin_value(coin: Coin) -> u8 { + match coin { + Coin::Penny => 1, + Coin::Nickel => 5, + Coin::Dime => 10, + Coin::Quarter => 25, + } +} + +enum Menu { + Create, + Update, + MarkComplete, + Delete, + Get, + Exit, +} + +#[derive(Debug, Clone, PartialEq)] +struct Todo { + id: u8, + title: String, + is_completed: Status, +} + +#[derive(Debug, Clone, PartialEq)] +enum Status { + Incomplete, + Completed, + Pending, +} + +struct TodoList { + data: Vec, + next_id: u8, +} + +impl TodoList { + fn new() -> Self { + Self { + data: Vec::new(), + next_id: 1, + } + } + + fn create_todo(&mut self, title: String) -> u8 { + let current_id = self.next_id; + + let todo = Todo { + id: current_id, + title, + is_completed: Status::Incomplete, + }; + + self.next_id += 1; + self.data.push(todo); + current_id + } + + fn mark_todo(&mut self, id: u8) -> bool { + if let Some(todo_item) = self.data.iter_mut().find(|todo_id| todo_id.id == id) { + todo_item.is_completed = Status::Completed; + true + } else { + false + } + } + fn delete_todo2(&mut self, id: u8) -> bool { + if let Some(user_todo_index) = self.data.iter_mut().position(|todo_id| todo_id.id == id) { + self.data.remove(user_todo_index); + true + } else { + false + } + } + + fn delete_todo(&mut self, id: u8) { + self.data.retain(|todo_id| todo_id.id != id) + } + + fn update_todo(&mut self, id: u8, title: String) -> bool { + for todo in self.data.iter_mut() { + if todo.id == id { + todo.title = title; + return true; + } + } + + false + } + + fn update_todo_if_let(&mut self, id: u8, new_title: String) -> bool { + if let Some(user_todo) = self.data.iter_mut().find(|todo_id| todo_id.id == id) { + user_todo.title = new_title; + true + } else { + false + } + } + + fn update_todo_method_qubzes(&mut self, id: u8, title: String) -> String { + let data = self + .data + .iter_mut() + .find(|todo| todo.id == id) + .expect("failed"); + data.title = title.clone(); + title + } + + fn get_single_todo(&self, id: u8) -> &Todo { + self.data.iter().find(|todo| todo.id == id).expect("failed") + } + + fn get_all_todo(&self) -> &Vec { + &self.data + } +} + +fn main() { + let num = 10; + let decimal = num as f64; + println!("Value: {decimal:.2}") +} diff --git a/live-session/week-1 & 2/day1-9/src/bin/week_1.rs b/live-session/week-1 & 2/day1-9/src/bin/week_1.rs index 8698191..fa60329 100644 --- a/live-session/week-1 & 2/day1-9/src/bin/week_1.rs +++ b/live-session/week-1 & 2/day1-9/src/bin/week_1.rs @@ -1,40 +1,40 @@ -fn main() { - let mut s1 = String::from("hello"); - s1 = String::from("adam"); - - // 0xf6a20020 - // 0x105779aa0 - - println!("==== cap, len, ptr S1========="); - println!("S1 {} {} {:?}", s1.len(), s1.capacity(), s1.as_ptr()); - let s2 = &s1; - - println!("{s1} {}", "world!"); - - println!("==== cap, len, ptr S2========="); - println!("S2 {} {} {:?}", s2.len(), s2.capacity(), s2.as_ptr()); - - println!("====int========="); - - let a = ["hello", "hi"]; - let b = a; - - println!("A {} {:?}", a.len(), s1.as_ptr()); - - println!("value of a is {a:?}"); - - let name = String::from("Hello"); - - take(&name); - - take_and_give_back(name); -} - -fn take(take: &String) { - println!("Rust is taken: {take}"); -} - -fn take_and_give_back(take: String) -> String { - println!("Rust is taken: {take}"); - take -} +fn main() { + let mut s1 = String::from("hello"); + s1 = String::from("adam"); + + // 0xf6a20020 + // 0x105779aa0 + + println!("==== cap, len, ptr S1========="); + println!("S1 {} {} {:?}", s1.len(), s1.capacity(), s1.as_ptr()); + let s2 = &s1; + + println!("{s1} {}", "world!"); + + println!("==== cap, len, ptr S2========="); + println!("S2 {} {} {:?}", s2.len(), s2.capacity(), s2.as_ptr()); + + println!("====int========="); + + let a = ["hello", "hi"]; + let b = a; + + println!("A {} {:?}", a.len(), s1.as_ptr()); + + println!("value of a is {a:?}"); + + let name = String::from("Hello"); + + take(&name); + + take_and_give_back(name); +} + +fn take(take: &String) { + println!("Rust is taken: {take}"); +} + +fn take_and_give_back(take: String) -> String { + println!("Rust is taken: {take}"); + take +} diff --git a/mentors/mentor-checklist.md b/mentors/mentor-checklist.md index 8eba8bb..2a98fb6 100644 --- a/mentors/mentor-checklist.md +++ b/mentors/mentor-checklist.md @@ -1,36 +1,36 @@ -# Web3bridge Rust Masterclass Cohort II - Mentor Checklist - -This checklist ensures consistency in reviewing student submissions and managing tasks effectively. - -## Task Management - -- [ ] Create a `Day--task.md` file in the appropriate `/tasks/week-x/` folder. -- [ ] Clearly define the objectives, requirements, and success criteria for the task. -- [ ] Include links to relevant resources or documentation. -- [ ] Push the task to the repository as early as possible. - -## Prepare the Submission Folder - -- [ ] Navigate to `submissions/week-/`. -- [ ] If a task is assigned for a specific day, **manually create the corresponding day subfolder** (`day-/`). -- [ ] Example: `submissions/week-3/day-2/` - -## Reviewing Submissions - -- [ ] Check the **Pull Requests (PRs)** tab for new student submissions. -- [ ] Ensure students follow the correct directory structure (`submissions/week-x/day-y//`). -- [ ] Review the code quality, completeness, and adherence to task requirements. -- [ ] Provide constructive feedback via PR comments. -- [ ] Approve and merge PRs if the submission meets expectations. -- [ ] If changes are needed, request modifications and guide the student. - -## Mentorship & Support - -- [ ] Encourage students to ask questions and clarify any doubts. -- [ ] Regularly engage with students on Discord. -- [ ] Provide additional explanations or hints where necessary. -- [ ] Share best practices and useful resources. - ---- - -Thank you for your dedication to mentoring Web3Bridge students! +# Web3bridge Rust Masterclass Cohort II - Mentor Checklist + +This checklist ensures consistency in reviewing student submissions and managing tasks effectively. + +## Task Management + +- [ ] Create a `Day--task.md` file in the appropriate `/tasks/week-x/` folder. +- [ ] Clearly define the objectives, requirements, and success criteria for the task. +- [ ] Include links to relevant resources or documentation. +- [ ] Push the task to the repository as early as possible. + +## Prepare the Submission Folder + +- [ ] Navigate to `submissions/week-/`. +- [ ] If a task is assigned for a specific day, **manually create the corresponding day subfolder** (`day-/`). +- [ ] Example: `submissions/week-3/day-2/` + +## Reviewing Submissions + +- [ ] Check the **Pull Requests (PRs)** tab for new student submissions. +- [ ] Ensure students follow the correct directory structure (`submissions/week-x/day-y//`). +- [ ] Review the code quality, completeness, and adherence to task requirements. +- [ ] Provide constructive feedback via PR comments. +- [ ] Approve and merge PRs if the submission meets expectations. +- [ ] If changes are needed, request modifications and guide the student. + +## Mentorship & Support + +- [ ] Encourage students to ask questions and clarify any doubts. +- [ ] Regularly engage with students on Discord. +- [ ] Provide additional explanations or hints where necessary. +- [ ] Share best practices and useful resources. + +--- + +Thank you for your dedication to mentoring Web3Bridge students! diff --git a/submissions/Example/day-1/Goodness-Blessing-Merkle-Tree/merkleTree.js b/submissions/Example/day-1/Goodness-Blessing-Merkle-Tree/merkleTree.js index c598426..03f3bb7 100644 --- a/submissions/Example/day-1/Goodness-Blessing-Merkle-Tree/merkleTree.js +++ b/submissions/Example/day-1/Goodness-Blessing-Merkle-Tree/merkleTree.js @@ -1,90 +1,90 @@ -// Import the crypto module for hashing -const crypto = require("crypto"); - -// Helper function to generate SHA-256 hash -function generateHash(data) { - return crypto.createHash("sha256").update(data).digest("hex"); -} - -// Function to create a Merkle Tree -function createMerkleTree(blocks) { - // Generate leaf nodes (hashes of the blocks) - let leafNodes = blocks.map((block) => generateHash(block)); - - // Build the tree - let tree = [leafNodes]; - while (tree[0].length > 1) { - let currentLevel = tree[0]; - let nextLevel = []; - for (let i = 0; i < currentLevel.length; i += 2) { - let left = currentLevel[i]; - let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; - nextLevel.push(generateHash(left + right)); - } - tree.unshift(nextLevel); - } - return tree; -} - -// Function to verify data integrity -function verifyIntegrity(originalTree, tamperedBlockIndex, tamperedBlock) { - // Tamper with the block - let tamperedLeafNodes = originalTree[originalTree.length - 1].slice(); - tamperedLeafNodes[tamperedBlockIndex] = generateHash(tamperedBlock); - - // Rebuilds the tree with the tampered block - let tamperedTree = [tamperedLeafNodes]; - while (tamperedTree[0].length > 1) { - let currentLevel = tamperedTree[0]; - let nextLevel = []; - for (let i = 0; i < currentLevel.length; i += 2) { - let left = currentLevel[i]; - let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; - nextLevel.push(generateHash(left + right)); - } - tamperedTree.unshift(nextLevel); - } - - return tamperedTree; -} - -// Main function to demonstrate the Merkle Tree -function main() { - const blocks = ["Block1", "Block2", "Block3", "Block4"]; - console.log("Original Blocks:", blocks); - - const merkleTree = createMerkleTree(blocks); - console.log("Merkle Tree:", merkleTree); - console.log("Root Hash:", merkleTree[0][0]); - - // Tamper with a block (change "Block3" to "TamperedBlock3") - const tamperedBlockIndex = 2; - const tamperedBlock = "TamperedBlock3"; - console.log( - `\nTampering with Block ${tamperedBlockIndex + 1}: "${ - blocks[tamperedBlockIndex] - }" -> "${tamperedBlock}"` - ); - - // Verify integrity - const tamperedTree = verifyIntegrity( - merkleTree, - tamperedBlockIndex, - tamperedBlock - ); - console.log("Tampered Merkle Tree:", tamperedTree); - console.log("New Root Hash:", tamperedTree[0][0]); - - // Check if the root hash changed - if (merkleTree[0][0] === tamperedTree[0][0]) { - console.log( - "Integrity Check: Root hash did NOT change. Data integrity is compromised!" - ); - } else { - console.log( - "Integrity Check: Root hash changed. Data integrity is maintained!" - ); - } -} - -main(); +// Import the crypto module for hashing +const crypto = require("crypto"); + +// Helper function to generate SHA-256 hash +function generateHash(data) { + return crypto.createHash("sha256").update(data).digest("hex"); +} + +// Function to create a Merkle Tree +function createMerkleTree(blocks) { + // Generate leaf nodes (hashes of the blocks) + let leafNodes = blocks.map((block) => generateHash(block)); + + // Build the tree + let tree = [leafNodes]; + while (tree[0].length > 1) { + let currentLevel = tree[0]; + let nextLevel = []; + for (let i = 0; i < currentLevel.length; i += 2) { + let left = currentLevel[i]; + let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; + nextLevel.push(generateHash(left + right)); + } + tree.unshift(nextLevel); + } + return tree; +} + +// Function to verify data integrity +function verifyIntegrity(originalTree, tamperedBlockIndex, tamperedBlock) { + // Tamper with the block + let tamperedLeafNodes = originalTree[originalTree.length - 1].slice(); + tamperedLeafNodes[tamperedBlockIndex] = generateHash(tamperedBlock); + + // Rebuilds the tree with the tampered block + let tamperedTree = [tamperedLeafNodes]; + while (tamperedTree[0].length > 1) { + let currentLevel = tamperedTree[0]; + let nextLevel = []; + for (let i = 0; i < currentLevel.length; i += 2) { + let left = currentLevel[i]; + let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; + nextLevel.push(generateHash(left + right)); + } + tamperedTree.unshift(nextLevel); + } + + return tamperedTree; +} + +// Main function to demonstrate the Merkle Tree +function main() { + const blocks = ["Block1", "Block2", "Block3", "Block4"]; + console.log("Original Blocks:", blocks); + + const merkleTree = createMerkleTree(blocks); + console.log("Merkle Tree:", merkleTree); + console.log("Root Hash:", merkleTree[0][0]); + + // Tamper with a block (change "Block3" to "TamperedBlock3") + const tamperedBlockIndex = 2; + const tamperedBlock = "TamperedBlock3"; + console.log( + `\nTampering with Block ${tamperedBlockIndex + 1}: "${ + blocks[tamperedBlockIndex] + }" -> "${tamperedBlock}"` + ); + + // Verify integrity + const tamperedTree = verifyIntegrity( + merkleTree, + tamperedBlockIndex, + tamperedBlock + ); + console.log("Tampered Merkle Tree:", tamperedTree); + console.log("New Root Hash:", tamperedTree[0][0]); + + // Check if the root hash changed + if (merkleTree[0][0] === tamperedTree[0][0]) { + console.log( + "Integrity Check: Root hash did NOT change. Data integrity is compromised!" + ); + } else { + console.log( + "Integrity Check: Root hash changed. Data integrity is maintained!" + ); + } +} + +main(); diff --git a/submissions/Example/day-1/Goodness-Blessing-Merkle-Tree/summary.md b/submissions/Example/day-1/Goodness-Blessing-Merkle-Tree/summary.md index 60af418..284ec0d 100644 --- a/submissions/Example/day-1/Goodness-Blessing-Merkle-Tree/summary.md +++ b/submissions/Example/day-1/Goodness-Blessing-Merkle-Tree/summary.md @@ -1,3 +1,3 @@ -# Summary - -A Merkle Tree is a data structure used in blockchain to efficiently and securely verify the contents of large datasets. It consists of leaf nodes, which are hashes of individual data blocks, and non-leaf nodes, which are hashes of their child nodes. The topmost node is called the root hash. Merkle Trees are important in blockchain because they allow for quick and secure verification of data integrity without needing to check the entire dataset. +# Summary + +A Merkle Tree is a data structure used in blockchain to efficiently and securely verify the contents of large datasets. It consists of leaf nodes, which are hashes of individual data blocks, and non-leaf nodes, which are hashes of their child nodes. The topmost node is called the root hash. Merkle Trees are important in blockchain because they allow for quick and secure verification of data integrity without needing to check the entire dataset. diff --git a/submissions/Example/day-1/Goodness-Blessing-Merkle-Tree/verification.md b/submissions/Example/day-1/Goodness-Blessing-Merkle-Tree/verification.md index 2687736..6dbbbdd 100644 --- a/submissions/Example/day-1/Goodness-Blessing-Merkle-Tree/verification.md +++ b/submissions/Example/day-1/Goodness-Blessing-Merkle-Tree/verification.md @@ -1,3 +1,3 @@ -# Verification - -If one of the data blocks, such as Block3, is tampered with, the hash of Block3 will change. This change will propagate up the Merkle Tree, altering the non-leaf node hashes and ultimately changing the root hash. This is important for data integrity in blockchain because any change in the data will be immediately detectable by comparing the root hash, ensuring the data remains secure and unaltered. +# Verification + +If one of the data blocks, such as Block3, is tampered with, the hash of Block3 will change. This change will propagate up the Merkle Tree, altering the non-leaf node hashes and ultimately changing the root hash. This is important for data integrity in blockchain because any change in the data will be immediately detectable by comparing the root hash, ensuring the data remains secure and unaltered. diff --git a/submissions/Example/day-1/Semilore-White-Merkle-Tree/merkleTree.js b/submissions/Example/day-1/Semilore-White-Merkle-Tree/merkleTree.js index c598426..03f3bb7 100644 --- a/submissions/Example/day-1/Semilore-White-Merkle-Tree/merkleTree.js +++ b/submissions/Example/day-1/Semilore-White-Merkle-Tree/merkleTree.js @@ -1,90 +1,90 @@ -// Import the crypto module for hashing -const crypto = require("crypto"); - -// Helper function to generate SHA-256 hash -function generateHash(data) { - return crypto.createHash("sha256").update(data).digest("hex"); -} - -// Function to create a Merkle Tree -function createMerkleTree(blocks) { - // Generate leaf nodes (hashes of the blocks) - let leafNodes = blocks.map((block) => generateHash(block)); - - // Build the tree - let tree = [leafNodes]; - while (tree[0].length > 1) { - let currentLevel = tree[0]; - let nextLevel = []; - for (let i = 0; i < currentLevel.length; i += 2) { - let left = currentLevel[i]; - let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; - nextLevel.push(generateHash(left + right)); - } - tree.unshift(nextLevel); - } - return tree; -} - -// Function to verify data integrity -function verifyIntegrity(originalTree, tamperedBlockIndex, tamperedBlock) { - // Tamper with the block - let tamperedLeafNodes = originalTree[originalTree.length - 1].slice(); - tamperedLeafNodes[tamperedBlockIndex] = generateHash(tamperedBlock); - - // Rebuilds the tree with the tampered block - let tamperedTree = [tamperedLeafNodes]; - while (tamperedTree[0].length > 1) { - let currentLevel = tamperedTree[0]; - let nextLevel = []; - for (let i = 0; i < currentLevel.length; i += 2) { - let left = currentLevel[i]; - let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; - nextLevel.push(generateHash(left + right)); - } - tamperedTree.unshift(nextLevel); - } - - return tamperedTree; -} - -// Main function to demonstrate the Merkle Tree -function main() { - const blocks = ["Block1", "Block2", "Block3", "Block4"]; - console.log("Original Blocks:", blocks); - - const merkleTree = createMerkleTree(blocks); - console.log("Merkle Tree:", merkleTree); - console.log("Root Hash:", merkleTree[0][0]); - - // Tamper with a block (change "Block3" to "TamperedBlock3") - const tamperedBlockIndex = 2; - const tamperedBlock = "TamperedBlock3"; - console.log( - `\nTampering with Block ${tamperedBlockIndex + 1}: "${ - blocks[tamperedBlockIndex] - }" -> "${tamperedBlock}"` - ); - - // Verify integrity - const tamperedTree = verifyIntegrity( - merkleTree, - tamperedBlockIndex, - tamperedBlock - ); - console.log("Tampered Merkle Tree:", tamperedTree); - console.log("New Root Hash:", tamperedTree[0][0]); - - // Check if the root hash changed - if (merkleTree[0][0] === tamperedTree[0][0]) { - console.log( - "Integrity Check: Root hash did NOT change. Data integrity is compromised!" - ); - } else { - console.log( - "Integrity Check: Root hash changed. Data integrity is maintained!" - ); - } -} - -main(); +// Import the crypto module for hashing +const crypto = require("crypto"); + +// Helper function to generate SHA-256 hash +function generateHash(data) { + return crypto.createHash("sha256").update(data).digest("hex"); +} + +// Function to create a Merkle Tree +function createMerkleTree(blocks) { + // Generate leaf nodes (hashes of the blocks) + let leafNodes = blocks.map((block) => generateHash(block)); + + // Build the tree + let tree = [leafNodes]; + while (tree[0].length > 1) { + let currentLevel = tree[0]; + let nextLevel = []; + for (let i = 0; i < currentLevel.length; i += 2) { + let left = currentLevel[i]; + let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; + nextLevel.push(generateHash(left + right)); + } + tree.unshift(nextLevel); + } + return tree; +} + +// Function to verify data integrity +function verifyIntegrity(originalTree, tamperedBlockIndex, tamperedBlock) { + // Tamper with the block + let tamperedLeafNodes = originalTree[originalTree.length - 1].slice(); + tamperedLeafNodes[tamperedBlockIndex] = generateHash(tamperedBlock); + + // Rebuilds the tree with the tampered block + let tamperedTree = [tamperedLeafNodes]; + while (tamperedTree[0].length > 1) { + let currentLevel = tamperedTree[0]; + let nextLevel = []; + for (let i = 0; i < currentLevel.length; i += 2) { + let left = currentLevel[i]; + let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; + nextLevel.push(generateHash(left + right)); + } + tamperedTree.unshift(nextLevel); + } + + return tamperedTree; +} + +// Main function to demonstrate the Merkle Tree +function main() { + const blocks = ["Block1", "Block2", "Block3", "Block4"]; + console.log("Original Blocks:", blocks); + + const merkleTree = createMerkleTree(blocks); + console.log("Merkle Tree:", merkleTree); + console.log("Root Hash:", merkleTree[0][0]); + + // Tamper with a block (change "Block3" to "TamperedBlock3") + const tamperedBlockIndex = 2; + const tamperedBlock = "TamperedBlock3"; + console.log( + `\nTampering with Block ${tamperedBlockIndex + 1}: "${ + blocks[tamperedBlockIndex] + }" -> "${tamperedBlock}"` + ); + + // Verify integrity + const tamperedTree = verifyIntegrity( + merkleTree, + tamperedBlockIndex, + tamperedBlock + ); + console.log("Tampered Merkle Tree:", tamperedTree); + console.log("New Root Hash:", tamperedTree[0][0]); + + // Check if the root hash changed + if (merkleTree[0][0] === tamperedTree[0][0]) { + console.log( + "Integrity Check: Root hash did NOT change. Data integrity is compromised!" + ); + } else { + console.log( + "Integrity Check: Root hash changed. Data integrity is maintained!" + ); + } +} + +main(); diff --git a/submissions/Example/day-1/Semilore-White-Merkle-Tree/summary.md b/submissions/Example/day-1/Semilore-White-Merkle-Tree/summary.md index 60af418..284ec0d 100644 --- a/submissions/Example/day-1/Semilore-White-Merkle-Tree/summary.md +++ b/submissions/Example/day-1/Semilore-White-Merkle-Tree/summary.md @@ -1,3 +1,3 @@ -# Summary - -A Merkle Tree is a data structure used in blockchain to efficiently and securely verify the contents of large datasets. It consists of leaf nodes, which are hashes of individual data blocks, and non-leaf nodes, which are hashes of their child nodes. The topmost node is called the root hash. Merkle Trees are important in blockchain because they allow for quick and secure verification of data integrity without needing to check the entire dataset. +# Summary + +A Merkle Tree is a data structure used in blockchain to efficiently and securely verify the contents of large datasets. It consists of leaf nodes, which are hashes of individual data blocks, and non-leaf nodes, which are hashes of their child nodes. The topmost node is called the root hash. Merkle Trees are important in blockchain because they allow for quick and secure verification of data integrity without needing to check the entire dataset. diff --git a/submissions/Example/day-1/Semilore-White-Merkle-Tree/verification.md b/submissions/Example/day-1/Semilore-White-Merkle-Tree/verification.md index 2687736..6dbbbdd 100644 --- a/submissions/Example/day-1/Semilore-White-Merkle-Tree/verification.md +++ b/submissions/Example/day-1/Semilore-White-Merkle-Tree/verification.md @@ -1,3 +1,3 @@ -# Verification - -If one of the data blocks, such as Block3, is tampered with, the hash of Block3 will change. This change will propagate up the Merkle Tree, altering the non-leaf node hashes and ultimately changing the root hash. This is important for data integrity in blockchain because any change in the data will be immediately detectable by comparing the root hash, ensuring the data remains secure and unaltered. +# Verification + +If one of the data blocks, such as Block3, is tampered with, the hash of Block3 will change. This change will propagate up the Merkle Tree, altering the non-leaf node hashes and ultimately changing the root hash. This is important for data integrity in blockchain because any change in the data will be immediately detectable by comparing the root hash, ensuring the data remains secure and unaltered. diff --git a/submissions/Example/day-2/Goodness-Blessing-Merkle-Tree/merkleTree.js b/submissions/Example/day-2/Goodness-Blessing-Merkle-Tree/merkleTree.js index c598426..03f3bb7 100644 --- a/submissions/Example/day-2/Goodness-Blessing-Merkle-Tree/merkleTree.js +++ b/submissions/Example/day-2/Goodness-Blessing-Merkle-Tree/merkleTree.js @@ -1,90 +1,90 @@ -// Import the crypto module for hashing -const crypto = require("crypto"); - -// Helper function to generate SHA-256 hash -function generateHash(data) { - return crypto.createHash("sha256").update(data).digest("hex"); -} - -// Function to create a Merkle Tree -function createMerkleTree(blocks) { - // Generate leaf nodes (hashes of the blocks) - let leafNodes = blocks.map((block) => generateHash(block)); - - // Build the tree - let tree = [leafNodes]; - while (tree[0].length > 1) { - let currentLevel = tree[0]; - let nextLevel = []; - for (let i = 0; i < currentLevel.length; i += 2) { - let left = currentLevel[i]; - let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; - nextLevel.push(generateHash(left + right)); - } - tree.unshift(nextLevel); - } - return tree; -} - -// Function to verify data integrity -function verifyIntegrity(originalTree, tamperedBlockIndex, tamperedBlock) { - // Tamper with the block - let tamperedLeafNodes = originalTree[originalTree.length - 1].slice(); - tamperedLeafNodes[tamperedBlockIndex] = generateHash(tamperedBlock); - - // Rebuilds the tree with the tampered block - let tamperedTree = [tamperedLeafNodes]; - while (tamperedTree[0].length > 1) { - let currentLevel = tamperedTree[0]; - let nextLevel = []; - for (let i = 0; i < currentLevel.length; i += 2) { - let left = currentLevel[i]; - let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; - nextLevel.push(generateHash(left + right)); - } - tamperedTree.unshift(nextLevel); - } - - return tamperedTree; -} - -// Main function to demonstrate the Merkle Tree -function main() { - const blocks = ["Block1", "Block2", "Block3", "Block4"]; - console.log("Original Blocks:", blocks); - - const merkleTree = createMerkleTree(blocks); - console.log("Merkle Tree:", merkleTree); - console.log("Root Hash:", merkleTree[0][0]); - - // Tamper with a block (change "Block3" to "TamperedBlock3") - const tamperedBlockIndex = 2; - const tamperedBlock = "TamperedBlock3"; - console.log( - `\nTampering with Block ${tamperedBlockIndex + 1}: "${ - blocks[tamperedBlockIndex] - }" -> "${tamperedBlock}"` - ); - - // Verify integrity - const tamperedTree = verifyIntegrity( - merkleTree, - tamperedBlockIndex, - tamperedBlock - ); - console.log("Tampered Merkle Tree:", tamperedTree); - console.log("New Root Hash:", tamperedTree[0][0]); - - // Check if the root hash changed - if (merkleTree[0][0] === tamperedTree[0][0]) { - console.log( - "Integrity Check: Root hash did NOT change. Data integrity is compromised!" - ); - } else { - console.log( - "Integrity Check: Root hash changed. Data integrity is maintained!" - ); - } -} - -main(); +// Import the crypto module for hashing +const crypto = require("crypto"); + +// Helper function to generate SHA-256 hash +function generateHash(data) { + return crypto.createHash("sha256").update(data).digest("hex"); +} + +// Function to create a Merkle Tree +function createMerkleTree(blocks) { + // Generate leaf nodes (hashes of the blocks) + let leafNodes = blocks.map((block) => generateHash(block)); + + // Build the tree + let tree = [leafNodes]; + while (tree[0].length > 1) { + let currentLevel = tree[0]; + let nextLevel = []; + for (let i = 0; i < currentLevel.length; i += 2) { + let left = currentLevel[i]; + let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; + nextLevel.push(generateHash(left + right)); + } + tree.unshift(nextLevel); + } + return tree; +} + +// Function to verify data integrity +function verifyIntegrity(originalTree, tamperedBlockIndex, tamperedBlock) { + // Tamper with the block + let tamperedLeafNodes = originalTree[originalTree.length - 1].slice(); + tamperedLeafNodes[tamperedBlockIndex] = generateHash(tamperedBlock); + + // Rebuilds the tree with the tampered block + let tamperedTree = [tamperedLeafNodes]; + while (tamperedTree[0].length > 1) { + let currentLevel = tamperedTree[0]; + let nextLevel = []; + for (let i = 0; i < currentLevel.length; i += 2) { + let left = currentLevel[i]; + let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; + nextLevel.push(generateHash(left + right)); + } + tamperedTree.unshift(nextLevel); + } + + return tamperedTree; +} + +// Main function to demonstrate the Merkle Tree +function main() { + const blocks = ["Block1", "Block2", "Block3", "Block4"]; + console.log("Original Blocks:", blocks); + + const merkleTree = createMerkleTree(blocks); + console.log("Merkle Tree:", merkleTree); + console.log("Root Hash:", merkleTree[0][0]); + + // Tamper with a block (change "Block3" to "TamperedBlock3") + const tamperedBlockIndex = 2; + const tamperedBlock = "TamperedBlock3"; + console.log( + `\nTampering with Block ${tamperedBlockIndex + 1}: "${ + blocks[tamperedBlockIndex] + }" -> "${tamperedBlock}"` + ); + + // Verify integrity + const tamperedTree = verifyIntegrity( + merkleTree, + tamperedBlockIndex, + tamperedBlock + ); + console.log("Tampered Merkle Tree:", tamperedTree); + console.log("New Root Hash:", tamperedTree[0][0]); + + // Check if the root hash changed + if (merkleTree[0][0] === tamperedTree[0][0]) { + console.log( + "Integrity Check: Root hash did NOT change. Data integrity is compromised!" + ); + } else { + console.log( + "Integrity Check: Root hash changed. Data integrity is maintained!" + ); + } +} + +main(); diff --git a/submissions/Example/day-2/Goodness-Blessing-Merkle-Tree/summary.md b/submissions/Example/day-2/Goodness-Blessing-Merkle-Tree/summary.md index 60af418..284ec0d 100644 --- a/submissions/Example/day-2/Goodness-Blessing-Merkle-Tree/summary.md +++ b/submissions/Example/day-2/Goodness-Blessing-Merkle-Tree/summary.md @@ -1,3 +1,3 @@ -# Summary - -A Merkle Tree is a data structure used in blockchain to efficiently and securely verify the contents of large datasets. It consists of leaf nodes, which are hashes of individual data blocks, and non-leaf nodes, which are hashes of their child nodes. The topmost node is called the root hash. Merkle Trees are important in blockchain because they allow for quick and secure verification of data integrity without needing to check the entire dataset. +# Summary + +A Merkle Tree is a data structure used in blockchain to efficiently and securely verify the contents of large datasets. It consists of leaf nodes, which are hashes of individual data blocks, and non-leaf nodes, which are hashes of their child nodes. The topmost node is called the root hash. Merkle Trees are important in blockchain because they allow for quick and secure verification of data integrity without needing to check the entire dataset. diff --git a/submissions/Example/day-2/Goodness-Blessing-Merkle-Tree/verification.md b/submissions/Example/day-2/Goodness-Blessing-Merkle-Tree/verification.md index 2687736..6dbbbdd 100644 --- a/submissions/Example/day-2/Goodness-Blessing-Merkle-Tree/verification.md +++ b/submissions/Example/day-2/Goodness-Blessing-Merkle-Tree/verification.md @@ -1,3 +1,3 @@ -# Verification - -If one of the data blocks, such as Block3, is tampered with, the hash of Block3 will change. This change will propagate up the Merkle Tree, altering the non-leaf node hashes and ultimately changing the root hash. This is important for data integrity in blockchain because any change in the data will be immediately detectable by comparing the root hash, ensuring the data remains secure and unaltered. +# Verification + +If one of the data blocks, such as Block3, is tampered with, the hash of Block3 will change. This change will propagate up the Merkle Tree, altering the non-leaf node hashes and ultimately changing the root hash. This is important for data integrity in blockchain because any change in the data will be immediately detectable by comparing the root hash, ensuring the data remains secure and unaltered. diff --git a/submissions/Example/day-2/Semilore-White-Merkle-Tree/merkleTree.js b/submissions/Example/day-2/Semilore-White-Merkle-Tree/merkleTree.js index c598426..03f3bb7 100644 --- a/submissions/Example/day-2/Semilore-White-Merkle-Tree/merkleTree.js +++ b/submissions/Example/day-2/Semilore-White-Merkle-Tree/merkleTree.js @@ -1,90 +1,90 @@ -// Import the crypto module for hashing -const crypto = require("crypto"); - -// Helper function to generate SHA-256 hash -function generateHash(data) { - return crypto.createHash("sha256").update(data).digest("hex"); -} - -// Function to create a Merkle Tree -function createMerkleTree(blocks) { - // Generate leaf nodes (hashes of the blocks) - let leafNodes = blocks.map((block) => generateHash(block)); - - // Build the tree - let tree = [leafNodes]; - while (tree[0].length > 1) { - let currentLevel = tree[0]; - let nextLevel = []; - for (let i = 0; i < currentLevel.length; i += 2) { - let left = currentLevel[i]; - let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; - nextLevel.push(generateHash(left + right)); - } - tree.unshift(nextLevel); - } - return tree; -} - -// Function to verify data integrity -function verifyIntegrity(originalTree, tamperedBlockIndex, tamperedBlock) { - // Tamper with the block - let tamperedLeafNodes = originalTree[originalTree.length - 1].slice(); - tamperedLeafNodes[tamperedBlockIndex] = generateHash(tamperedBlock); - - // Rebuilds the tree with the tampered block - let tamperedTree = [tamperedLeafNodes]; - while (tamperedTree[0].length > 1) { - let currentLevel = tamperedTree[0]; - let nextLevel = []; - for (let i = 0; i < currentLevel.length; i += 2) { - let left = currentLevel[i]; - let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; - nextLevel.push(generateHash(left + right)); - } - tamperedTree.unshift(nextLevel); - } - - return tamperedTree; -} - -// Main function to demonstrate the Merkle Tree -function main() { - const blocks = ["Block1", "Block2", "Block3", "Block4"]; - console.log("Original Blocks:", blocks); - - const merkleTree = createMerkleTree(blocks); - console.log("Merkle Tree:", merkleTree); - console.log("Root Hash:", merkleTree[0][0]); - - // Tamper with a block (change "Block3" to "TamperedBlock3") - const tamperedBlockIndex = 2; - const tamperedBlock = "TamperedBlock3"; - console.log( - `\nTampering with Block ${tamperedBlockIndex + 1}: "${ - blocks[tamperedBlockIndex] - }" -> "${tamperedBlock}"` - ); - - // Verify integrity - const tamperedTree = verifyIntegrity( - merkleTree, - tamperedBlockIndex, - tamperedBlock - ); - console.log("Tampered Merkle Tree:", tamperedTree); - console.log("New Root Hash:", tamperedTree[0][0]); - - // Check if the root hash changed - if (merkleTree[0][0] === tamperedTree[0][0]) { - console.log( - "Integrity Check: Root hash did NOT change. Data integrity is compromised!" - ); - } else { - console.log( - "Integrity Check: Root hash changed. Data integrity is maintained!" - ); - } -} - -main(); +// Import the crypto module for hashing +const crypto = require("crypto"); + +// Helper function to generate SHA-256 hash +function generateHash(data) { + return crypto.createHash("sha256").update(data).digest("hex"); +} + +// Function to create a Merkle Tree +function createMerkleTree(blocks) { + // Generate leaf nodes (hashes of the blocks) + let leafNodes = blocks.map((block) => generateHash(block)); + + // Build the tree + let tree = [leafNodes]; + while (tree[0].length > 1) { + let currentLevel = tree[0]; + let nextLevel = []; + for (let i = 0; i < currentLevel.length; i += 2) { + let left = currentLevel[i]; + let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; + nextLevel.push(generateHash(left + right)); + } + tree.unshift(nextLevel); + } + return tree; +} + +// Function to verify data integrity +function verifyIntegrity(originalTree, tamperedBlockIndex, tamperedBlock) { + // Tamper with the block + let tamperedLeafNodes = originalTree[originalTree.length - 1].slice(); + tamperedLeafNodes[tamperedBlockIndex] = generateHash(tamperedBlock); + + // Rebuilds the tree with the tampered block + let tamperedTree = [tamperedLeafNodes]; + while (tamperedTree[0].length > 1) { + let currentLevel = tamperedTree[0]; + let nextLevel = []; + for (let i = 0; i < currentLevel.length; i += 2) { + let left = currentLevel[i]; + let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; + nextLevel.push(generateHash(left + right)); + } + tamperedTree.unshift(nextLevel); + } + + return tamperedTree; +} + +// Main function to demonstrate the Merkle Tree +function main() { + const blocks = ["Block1", "Block2", "Block3", "Block4"]; + console.log("Original Blocks:", blocks); + + const merkleTree = createMerkleTree(blocks); + console.log("Merkle Tree:", merkleTree); + console.log("Root Hash:", merkleTree[0][0]); + + // Tamper with a block (change "Block3" to "TamperedBlock3") + const tamperedBlockIndex = 2; + const tamperedBlock = "TamperedBlock3"; + console.log( + `\nTampering with Block ${tamperedBlockIndex + 1}: "${ + blocks[tamperedBlockIndex] + }" -> "${tamperedBlock}"` + ); + + // Verify integrity + const tamperedTree = verifyIntegrity( + merkleTree, + tamperedBlockIndex, + tamperedBlock + ); + console.log("Tampered Merkle Tree:", tamperedTree); + console.log("New Root Hash:", tamperedTree[0][0]); + + // Check if the root hash changed + if (merkleTree[0][0] === tamperedTree[0][0]) { + console.log( + "Integrity Check: Root hash did NOT change. Data integrity is compromised!" + ); + } else { + console.log( + "Integrity Check: Root hash changed. Data integrity is maintained!" + ); + } +} + +main(); diff --git a/submissions/Example/day-2/Semilore-White-Merkle-Tree/summary.md b/submissions/Example/day-2/Semilore-White-Merkle-Tree/summary.md index 60af418..284ec0d 100644 --- a/submissions/Example/day-2/Semilore-White-Merkle-Tree/summary.md +++ b/submissions/Example/day-2/Semilore-White-Merkle-Tree/summary.md @@ -1,3 +1,3 @@ -# Summary - -A Merkle Tree is a data structure used in blockchain to efficiently and securely verify the contents of large datasets. It consists of leaf nodes, which are hashes of individual data blocks, and non-leaf nodes, which are hashes of their child nodes. The topmost node is called the root hash. Merkle Trees are important in blockchain because they allow for quick and secure verification of data integrity without needing to check the entire dataset. +# Summary + +A Merkle Tree is a data structure used in blockchain to efficiently and securely verify the contents of large datasets. It consists of leaf nodes, which are hashes of individual data blocks, and non-leaf nodes, which are hashes of their child nodes. The topmost node is called the root hash. Merkle Trees are important in blockchain because they allow for quick and secure verification of data integrity without needing to check the entire dataset. diff --git a/submissions/Example/day-2/Semilore-White-Merkle-Tree/verification.md b/submissions/Example/day-2/Semilore-White-Merkle-Tree/verification.md index 2687736..6dbbbdd 100644 --- a/submissions/Example/day-2/Semilore-White-Merkle-Tree/verification.md +++ b/submissions/Example/day-2/Semilore-White-Merkle-Tree/verification.md @@ -1,3 +1,3 @@ -# Verification - -If one of the data blocks, such as Block3, is tampered with, the hash of Block3 will change. This change will propagate up the Merkle Tree, altering the non-leaf node hashes and ultimately changing the root hash. This is important for data integrity in blockchain because any change in the data will be immediately detectable by comparing the root hash, ensuring the data remains secure and unaltered. +# Verification + +If one of the data blocks, such as Block3, is tampered with, the hash of Block3 will change. This change will propagate up the Merkle Tree, altering the non-leaf node hashes and ultimately changing the root hash. This is important for data integrity in blockchain because any change in the data will be immediately detectable by comparing the root hash, ensuring the data remains secure and unaltered. diff --git a/submissions/week-1/day-1/Goodness-Blessing-Merkle-Tree/merkleTree.js b/submissions/week-1/day-1/Goodness-Blessing-Merkle-Tree/merkleTree.js index c598426..03f3bb7 100644 --- a/submissions/week-1/day-1/Goodness-Blessing-Merkle-Tree/merkleTree.js +++ b/submissions/week-1/day-1/Goodness-Blessing-Merkle-Tree/merkleTree.js @@ -1,90 +1,90 @@ -// Import the crypto module for hashing -const crypto = require("crypto"); - -// Helper function to generate SHA-256 hash -function generateHash(data) { - return crypto.createHash("sha256").update(data).digest("hex"); -} - -// Function to create a Merkle Tree -function createMerkleTree(blocks) { - // Generate leaf nodes (hashes of the blocks) - let leafNodes = blocks.map((block) => generateHash(block)); - - // Build the tree - let tree = [leafNodes]; - while (tree[0].length > 1) { - let currentLevel = tree[0]; - let nextLevel = []; - for (let i = 0; i < currentLevel.length; i += 2) { - let left = currentLevel[i]; - let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; - nextLevel.push(generateHash(left + right)); - } - tree.unshift(nextLevel); - } - return tree; -} - -// Function to verify data integrity -function verifyIntegrity(originalTree, tamperedBlockIndex, tamperedBlock) { - // Tamper with the block - let tamperedLeafNodes = originalTree[originalTree.length - 1].slice(); - tamperedLeafNodes[tamperedBlockIndex] = generateHash(tamperedBlock); - - // Rebuilds the tree with the tampered block - let tamperedTree = [tamperedLeafNodes]; - while (tamperedTree[0].length > 1) { - let currentLevel = tamperedTree[0]; - let nextLevel = []; - for (let i = 0; i < currentLevel.length; i += 2) { - let left = currentLevel[i]; - let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; - nextLevel.push(generateHash(left + right)); - } - tamperedTree.unshift(nextLevel); - } - - return tamperedTree; -} - -// Main function to demonstrate the Merkle Tree -function main() { - const blocks = ["Block1", "Block2", "Block3", "Block4"]; - console.log("Original Blocks:", blocks); - - const merkleTree = createMerkleTree(blocks); - console.log("Merkle Tree:", merkleTree); - console.log("Root Hash:", merkleTree[0][0]); - - // Tamper with a block (change "Block3" to "TamperedBlock3") - const tamperedBlockIndex = 2; - const tamperedBlock = "TamperedBlock3"; - console.log( - `\nTampering with Block ${tamperedBlockIndex + 1}: "${ - blocks[tamperedBlockIndex] - }" -> "${tamperedBlock}"` - ); - - // Verify integrity - const tamperedTree = verifyIntegrity( - merkleTree, - tamperedBlockIndex, - tamperedBlock - ); - console.log("Tampered Merkle Tree:", tamperedTree); - console.log("New Root Hash:", tamperedTree[0][0]); - - // Check if the root hash changed - if (merkleTree[0][0] === tamperedTree[0][0]) { - console.log( - "Integrity Check: Root hash did NOT change. Data integrity is compromised!" - ); - } else { - console.log( - "Integrity Check: Root hash changed. Data integrity is maintained!" - ); - } -} - -main(); +// Import the crypto module for hashing +const crypto = require("crypto"); + +// Helper function to generate SHA-256 hash +function generateHash(data) { + return crypto.createHash("sha256").update(data).digest("hex"); +} + +// Function to create a Merkle Tree +function createMerkleTree(blocks) { + // Generate leaf nodes (hashes of the blocks) + let leafNodes = blocks.map((block) => generateHash(block)); + + // Build the tree + let tree = [leafNodes]; + while (tree[0].length > 1) { + let currentLevel = tree[0]; + let nextLevel = []; + for (let i = 0; i < currentLevel.length; i += 2) { + let left = currentLevel[i]; + let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; + nextLevel.push(generateHash(left + right)); + } + tree.unshift(nextLevel); + } + return tree; +} + +// Function to verify data integrity +function verifyIntegrity(originalTree, tamperedBlockIndex, tamperedBlock) { + // Tamper with the block + let tamperedLeafNodes = originalTree[originalTree.length - 1].slice(); + tamperedLeafNodes[tamperedBlockIndex] = generateHash(tamperedBlock); + + // Rebuilds the tree with the tampered block + let tamperedTree = [tamperedLeafNodes]; + while (tamperedTree[0].length > 1) { + let currentLevel = tamperedTree[0]; + let nextLevel = []; + for (let i = 0; i < currentLevel.length; i += 2) { + let left = currentLevel[i]; + let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; + nextLevel.push(generateHash(left + right)); + } + tamperedTree.unshift(nextLevel); + } + + return tamperedTree; +} + +// Main function to demonstrate the Merkle Tree +function main() { + const blocks = ["Block1", "Block2", "Block3", "Block4"]; + console.log("Original Blocks:", blocks); + + const merkleTree = createMerkleTree(blocks); + console.log("Merkle Tree:", merkleTree); + console.log("Root Hash:", merkleTree[0][0]); + + // Tamper with a block (change "Block3" to "TamperedBlock3") + const tamperedBlockIndex = 2; + const tamperedBlock = "TamperedBlock3"; + console.log( + `\nTampering with Block ${tamperedBlockIndex + 1}: "${ + blocks[tamperedBlockIndex] + }" -> "${tamperedBlock}"` + ); + + // Verify integrity + const tamperedTree = verifyIntegrity( + merkleTree, + tamperedBlockIndex, + tamperedBlock + ); + console.log("Tampered Merkle Tree:", tamperedTree); + console.log("New Root Hash:", tamperedTree[0][0]); + + // Check if the root hash changed + if (merkleTree[0][0] === tamperedTree[0][0]) { + console.log( + "Integrity Check: Root hash did NOT change. Data integrity is compromised!" + ); + } else { + console.log( + "Integrity Check: Root hash changed. Data integrity is maintained!" + ); + } +} + +main(); diff --git a/submissions/week-1/day-1/Goodness-Blessing-Merkle-Tree/summary.md b/submissions/week-1/day-1/Goodness-Blessing-Merkle-Tree/summary.md index 60af418..284ec0d 100644 --- a/submissions/week-1/day-1/Goodness-Blessing-Merkle-Tree/summary.md +++ b/submissions/week-1/day-1/Goodness-Blessing-Merkle-Tree/summary.md @@ -1,3 +1,3 @@ -# Summary - -A Merkle Tree is a data structure used in blockchain to efficiently and securely verify the contents of large datasets. It consists of leaf nodes, which are hashes of individual data blocks, and non-leaf nodes, which are hashes of their child nodes. The topmost node is called the root hash. Merkle Trees are important in blockchain because they allow for quick and secure verification of data integrity without needing to check the entire dataset. +# Summary + +A Merkle Tree is a data structure used in blockchain to efficiently and securely verify the contents of large datasets. It consists of leaf nodes, which are hashes of individual data blocks, and non-leaf nodes, which are hashes of their child nodes. The topmost node is called the root hash. Merkle Trees are important in blockchain because they allow for quick and secure verification of data integrity without needing to check the entire dataset. diff --git a/submissions/week-1/day-1/Goodness-Blessing-Merkle-Tree/verification.md b/submissions/week-1/day-1/Goodness-Blessing-Merkle-Tree/verification.md index 2687736..6dbbbdd 100644 --- a/submissions/week-1/day-1/Goodness-Blessing-Merkle-Tree/verification.md +++ b/submissions/week-1/day-1/Goodness-Blessing-Merkle-Tree/verification.md @@ -1,3 +1,3 @@ -# Verification - -If one of the data blocks, such as Block3, is tampered with, the hash of Block3 will change. This change will propagate up the Merkle Tree, altering the non-leaf node hashes and ultimately changing the root hash. This is important for data integrity in blockchain because any change in the data will be immediately detectable by comparing the root hash, ensuring the data remains secure and unaltered. +# Verification + +If one of the data blocks, such as Block3, is tampered with, the hash of Block3 will change. This change will propagate up the Merkle Tree, altering the non-leaf node hashes and ultimately changing the root hash. This is important for data integrity in blockchain because any change in the data will be immediately detectable by comparing the root hash, ensuring the data remains secure and unaltered. diff --git a/submissions/week-1/day-2/Goodness-Blessing-Merkle-Tree/merkleTree.js b/submissions/week-1/day-2/Goodness-Blessing-Merkle-Tree/merkleTree.js index c598426..03f3bb7 100644 --- a/submissions/week-1/day-2/Goodness-Blessing-Merkle-Tree/merkleTree.js +++ b/submissions/week-1/day-2/Goodness-Blessing-Merkle-Tree/merkleTree.js @@ -1,90 +1,90 @@ -// Import the crypto module for hashing -const crypto = require("crypto"); - -// Helper function to generate SHA-256 hash -function generateHash(data) { - return crypto.createHash("sha256").update(data).digest("hex"); -} - -// Function to create a Merkle Tree -function createMerkleTree(blocks) { - // Generate leaf nodes (hashes of the blocks) - let leafNodes = blocks.map((block) => generateHash(block)); - - // Build the tree - let tree = [leafNodes]; - while (tree[0].length > 1) { - let currentLevel = tree[0]; - let nextLevel = []; - for (let i = 0; i < currentLevel.length; i += 2) { - let left = currentLevel[i]; - let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; - nextLevel.push(generateHash(left + right)); - } - tree.unshift(nextLevel); - } - return tree; -} - -// Function to verify data integrity -function verifyIntegrity(originalTree, tamperedBlockIndex, tamperedBlock) { - // Tamper with the block - let tamperedLeafNodes = originalTree[originalTree.length - 1].slice(); - tamperedLeafNodes[tamperedBlockIndex] = generateHash(tamperedBlock); - - // Rebuilds the tree with the tampered block - let tamperedTree = [tamperedLeafNodes]; - while (tamperedTree[0].length > 1) { - let currentLevel = tamperedTree[0]; - let nextLevel = []; - for (let i = 0; i < currentLevel.length; i += 2) { - let left = currentLevel[i]; - let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; - nextLevel.push(generateHash(left + right)); - } - tamperedTree.unshift(nextLevel); - } - - return tamperedTree; -} - -// Main function to demonstrate the Merkle Tree -function main() { - const blocks = ["Block1", "Block2", "Block3", "Block4"]; - console.log("Original Blocks:", blocks); - - const merkleTree = createMerkleTree(blocks); - console.log("Merkle Tree:", merkleTree); - console.log("Root Hash:", merkleTree[0][0]); - - // Tamper with a block (change "Block3" to "TamperedBlock3") - const tamperedBlockIndex = 2; - const tamperedBlock = "TamperedBlock3"; - console.log( - `\nTampering with Block ${tamperedBlockIndex + 1}: "${ - blocks[tamperedBlockIndex] - }" -> "${tamperedBlock}"` - ); - - // Verify integrity - const tamperedTree = verifyIntegrity( - merkleTree, - tamperedBlockIndex, - tamperedBlock - ); - console.log("Tampered Merkle Tree:", tamperedTree); - console.log("New Root Hash:", tamperedTree[0][0]); - - // Check if the root hash changed - if (merkleTree[0][0] === tamperedTree[0][0]) { - console.log( - "Integrity Check: Root hash did NOT change. Data integrity is compromised!" - ); - } else { - console.log( - "Integrity Check: Root hash changed. Data integrity is maintained!" - ); - } -} - -main(); +// Import the crypto module for hashing +const crypto = require("crypto"); + +// Helper function to generate SHA-256 hash +function generateHash(data) { + return crypto.createHash("sha256").update(data).digest("hex"); +} + +// Function to create a Merkle Tree +function createMerkleTree(blocks) { + // Generate leaf nodes (hashes of the blocks) + let leafNodes = blocks.map((block) => generateHash(block)); + + // Build the tree + let tree = [leafNodes]; + while (tree[0].length > 1) { + let currentLevel = tree[0]; + let nextLevel = []; + for (let i = 0; i < currentLevel.length; i += 2) { + let left = currentLevel[i]; + let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; + nextLevel.push(generateHash(left + right)); + } + tree.unshift(nextLevel); + } + return tree; +} + +// Function to verify data integrity +function verifyIntegrity(originalTree, tamperedBlockIndex, tamperedBlock) { + // Tamper with the block + let tamperedLeafNodes = originalTree[originalTree.length - 1].slice(); + tamperedLeafNodes[tamperedBlockIndex] = generateHash(tamperedBlock); + + // Rebuilds the tree with the tampered block + let tamperedTree = [tamperedLeafNodes]; + while (tamperedTree[0].length > 1) { + let currentLevel = tamperedTree[0]; + let nextLevel = []; + for (let i = 0; i < currentLevel.length; i += 2) { + let left = currentLevel[i]; + let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; + nextLevel.push(generateHash(left + right)); + } + tamperedTree.unshift(nextLevel); + } + + return tamperedTree; +} + +// Main function to demonstrate the Merkle Tree +function main() { + const blocks = ["Block1", "Block2", "Block3", "Block4"]; + console.log("Original Blocks:", blocks); + + const merkleTree = createMerkleTree(blocks); + console.log("Merkle Tree:", merkleTree); + console.log("Root Hash:", merkleTree[0][0]); + + // Tamper with a block (change "Block3" to "TamperedBlock3") + const tamperedBlockIndex = 2; + const tamperedBlock = "TamperedBlock3"; + console.log( + `\nTampering with Block ${tamperedBlockIndex + 1}: "${ + blocks[tamperedBlockIndex] + }" -> "${tamperedBlock}"` + ); + + // Verify integrity + const tamperedTree = verifyIntegrity( + merkleTree, + tamperedBlockIndex, + tamperedBlock + ); + console.log("Tampered Merkle Tree:", tamperedTree); + console.log("New Root Hash:", tamperedTree[0][0]); + + // Check if the root hash changed + if (merkleTree[0][0] === tamperedTree[0][0]) { + console.log( + "Integrity Check: Root hash did NOT change. Data integrity is compromised!" + ); + } else { + console.log( + "Integrity Check: Root hash changed. Data integrity is maintained!" + ); + } +} + +main(); diff --git a/submissions/week-1/day-2/Goodness-Blessing-Merkle-Tree/summary.md b/submissions/week-1/day-2/Goodness-Blessing-Merkle-Tree/summary.md index 60af418..284ec0d 100644 --- a/submissions/week-1/day-2/Goodness-Blessing-Merkle-Tree/summary.md +++ b/submissions/week-1/day-2/Goodness-Blessing-Merkle-Tree/summary.md @@ -1,3 +1,3 @@ -# Summary - -A Merkle Tree is a data structure used in blockchain to efficiently and securely verify the contents of large datasets. It consists of leaf nodes, which are hashes of individual data blocks, and non-leaf nodes, which are hashes of their child nodes. The topmost node is called the root hash. Merkle Trees are important in blockchain because they allow for quick and secure verification of data integrity without needing to check the entire dataset. +# Summary + +A Merkle Tree is a data structure used in blockchain to efficiently and securely verify the contents of large datasets. It consists of leaf nodes, which are hashes of individual data blocks, and non-leaf nodes, which are hashes of their child nodes. The topmost node is called the root hash. Merkle Trees are important in blockchain because they allow for quick and secure verification of data integrity without needing to check the entire dataset. diff --git a/submissions/week-1/day-2/Goodness-Blessing-Merkle-Tree/verification.md b/submissions/week-1/day-2/Goodness-Blessing-Merkle-Tree/verification.md index 2687736..6dbbbdd 100644 --- a/submissions/week-1/day-2/Goodness-Blessing-Merkle-Tree/verification.md +++ b/submissions/week-1/day-2/Goodness-Blessing-Merkle-Tree/verification.md @@ -1,3 +1,3 @@ -# Verification - -If one of the data blocks, such as Block3, is tampered with, the hash of Block3 will change. This change will propagate up the Merkle Tree, altering the non-leaf node hashes and ultimately changing the root hash. This is important for data integrity in blockchain because any change in the data will be immediately detectable by comparing the root hash, ensuring the data remains secure and unaltered. +# Verification + +If one of the data blocks, such as Block3, is tampered with, the hash of Block3 will change. This change will propagate up the Merkle Tree, altering the non-leaf node hashes and ultimately changing the root hash. This is important for data integrity in blockchain because any change in the data will be immediately detectable by comparing the root hash, ensuring the data remains secure and unaltered. diff --git a/submissions/week-2/day-1/Goodness-Blessing-Merkle-Tree/merkleTree.js b/submissions/week-2/day-1/Goodness-Blessing-Merkle-Tree/merkleTree.js index c598426..03f3bb7 100644 --- a/submissions/week-2/day-1/Goodness-Blessing-Merkle-Tree/merkleTree.js +++ b/submissions/week-2/day-1/Goodness-Blessing-Merkle-Tree/merkleTree.js @@ -1,90 +1,90 @@ -// Import the crypto module for hashing -const crypto = require("crypto"); - -// Helper function to generate SHA-256 hash -function generateHash(data) { - return crypto.createHash("sha256").update(data).digest("hex"); -} - -// Function to create a Merkle Tree -function createMerkleTree(blocks) { - // Generate leaf nodes (hashes of the blocks) - let leafNodes = blocks.map((block) => generateHash(block)); - - // Build the tree - let tree = [leafNodes]; - while (tree[0].length > 1) { - let currentLevel = tree[0]; - let nextLevel = []; - for (let i = 0; i < currentLevel.length; i += 2) { - let left = currentLevel[i]; - let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; - nextLevel.push(generateHash(left + right)); - } - tree.unshift(nextLevel); - } - return tree; -} - -// Function to verify data integrity -function verifyIntegrity(originalTree, tamperedBlockIndex, tamperedBlock) { - // Tamper with the block - let tamperedLeafNodes = originalTree[originalTree.length - 1].slice(); - tamperedLeafNodes[tamperedBlockIndex] = generateHash(tamperedBlock); - - // Rebuilds the tree with the tampered block - let tamperedTree = [tamperedLeafNodes]; - while (tamperedTree[0].length > 1) { - let currentLevel = tamperedTree[0]; - let nextLevel = []; - for (let i = 0; i < currentLevel.length; i += 2) { - let left = currentLevel[i]; - let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; - nextLevel.push(generateHash(left + right)); - } - tamperedTree.unshift(nextLevel); - } - - return tamperedTree; -} - -// Main function to demonstrate the Merkle Tree -function main() { - const blocks = ["Block1", "Block2", "Block3", "Block4"]; - console.log("Original Blocks:", blocks); - - const merkleTree = createMerkleTree(blocks); - console.log("Merkle Tree:", merkleTree); - console.log("Root Hash:", merkleTree[0][0]); - - // Tamper with a block (change "Block3" to "TamperedBlock3") - const tamperedBlockIndex = 2; - const tamperedBlock = "TamperedBlock3"; - console.log( - `\nTampering with Block ${tamperedBlockIndex + 1}: "${ - blocks[tamperedBlockIndex] - }" -> "${tamperedBlock}"` - ); - - // Verify integrity - const tamperedTree = verifyIntegrity( - merkleTree, - tamperedBlockIndex, - tamperedBlock - ); - console.log("Tampered Merkle Tree:", tamperedTree); - console.log("New Root Hash:", tamperedTree[0][0]); - - // Check if the root hash changed - if (merkleTree[0][0] === tamperedTree[0][0]) { - console.log( - "Integrity Check: Root hash did NOT change. Data integrity is compromised!" - ); - } else { - console.log( - "Integrity Check: Root hash changed. Data integrity is maintained!" - ); - } -} - -main(); +// Import the crypto module for hashing +const crypto = require("crypto"); + +// Helper function to generate SHA-256 hash +function generateHash(data) { + return crypto.createHash("sha256").update(data).digest("hex"); +} + +// Function to create a Merkle Tree +function createMerkleTree(blocks) { + // Generate leaf nodes (hashes of the blocks) + let leafNodes = blocks.map((block) => generateHash(block)); + + // Build the tree + let tree = [leafNodes]; + while (tree[0].length > 1) { + let currentLevel = tree[0]; + let nextLevel = []; + for (let i = 0; i < currentLevel.length; i += 2) { + let left = currentLevel[i]; + let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; + nextLevel.push(generateHash(left + right)); + } + tree.unshift(nextLevel); + } + return tree; +} + +// Function to verify data integrity +function verifyIntegrity(originalTree, tamperedBlockIndex, tamperedBlock) { + // Tamper with the block + let tamperedLeafNodes = originalTree[originalTree.length - 1].slice(); + tamperedLeafNodes[tamperedBlockIndex] = generateHash(tamperedBlock); + + // Rebuilds the tree with the tampered block + let tamperedTree = [tamperedLeafNodes]; + while (tamperedTree[0].length > 1) { + let currentLevel = tamperedTree[0]; + let nextLevel = []; + for (let i = 0; i < currentLevel.length; i += 2) { + let left = currentLevel[i]; + let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; + nextLevel.push(generateHash(left + right)); + } + tamperedTree.unshift(nextLevel); + } + + return tamperedTree; +} + +// Main function to demonstrate the Merkle Tree +function main() { + const blocks = ["Block1", "Block2", "Block3", "Block4"]; + console.log("Original Blocks:", blocks); + + const merkleTree = createMerkleTree(blocks); + console.log("Merkle Tree:", merkleTree); + console.log("Root Hash:", merkleTree[0][0]); + + // Tamper with a block (change "Block3" to "TamperedBlock3") + const tamperedBlockIndex = 2; + const tamperedBlock = "TamperedBlock3"; + console.log( + `\nTampering with Block ${tamperedBlockIndex + 1}: "${ + blocks[tamperedBlockIndex] + }" -> "${tamperedBlock}"` + ); + + // Verify integrity + const tamperedTree = verifyIntegrity( + merkleTree, + tamperedBlockIndex, + tamperedBlock + ); + console.log("Tampered Merkle Tree:", tamperedTree); + console.log("New Root Hash:", tamperedTree[0][0]); + + // Check if the root hash changed + if (merkleTree[0][0] === tamperedTree[0][0]) { + console.log( + "Integrity Check: Root hash did NOT change. Data integrity is compromised!" + ); + } else { + console.log( + "Integrity Check: Root hash changed. Data integrity is maintained!" + ); + } +} + +main(); diff --git a/submissions/week-2/day-1/Goodness-Blessing-Merkle-Tree/summary.md b/submissions/week-2/day-1/Goodness-Blessing-Merkle-Tree/summary.md index 60af418..284ec0d 100644 --- a/submissions/week-2/day-1/Goodness-Blessing-Merkle-Tree/summary.md +++ b/submissions/week-2/day-1/Goodness-Blessing-Merkle-Tree/summary.md @@ -1,3 +1,3 @@ -# Summary - -A Merkle Tree is a data structure used in blockchain to efficiently and securely verify the contents of large datasets. It consists of leaf nodes, which are hashes of individual data blocks, and non-leaf nodes, which are hashes of their child nodes. The topmost node is called the root hash. Merkle Trees are important in blockchain because they allow for quick and secure verification of data integrity without needing to check the entire dataset. +# Summary + +A Merkle Tree is a data structure used in blockchain to efficiently and securely verify the contents of large datasets. It consists of leaf nodes, which are hashes of individual data blocks, and non-leaf nodes, which are hashes of their child nodes. The topmost node is called the root hash. Merkle Trees are important in blockchain because they allow for quick and secure verification of data integrity without needing to check the entire dataset. diff --git a/submissions/week-2/day-1/Goodness-Blessing-Merkle-Tree/verification.md b/submissions/week-2/day-1/Goodness-Blessing-Merkle-Tree/verification.md index 2687736..6dbbbdd 100644 --- a/submissions/week-2/day-1/Goodness-Blessing-Merkle-Tree/verification.md +++ b/submissions/week-2/day-1/Goodness-Blessing-Merkle-Tree/verification.md @@ -1,3 +1,3 @@ -# Verification - -If one of the data blocks, such as Block3, is tampered with, the hash of Block3 will change. This change will propagate up the Merkle Tree, altering the non-leaf node hashes and ultimately changing the root hash. This is important for data integrity in blockchain because any change in the data will be immediately detectable by comparing the root hash, ensuring the data remains secure and unaltered. +# Verification + +If one of the data blocks, such as Block3, is tampered with, the hash of Block3 will change. This change will propagate up the Merkle Tree, altering the non-leaf node hashes and ultimately changing the root hash. This is important for data integrity in blockchain because any change in the data will be immediately detectable by comparing the root hash, ensuring the data remains secure and unaltered. diff --git a/submissions/week-2/day-1/Semilore-White-Merkle-Tree/merkleTree.js b/submissions/week-2/day-1/Semilore-White-Merkle-Tree/merkleTree.js index c598426..03f3bb7 100644 --- a/submissions/week-2/day-1/Semilore-White-Merkle-Tree/merkleTree.js +++ b/submissions/week-2/day-1/Semilore-White-Merkle-Tree/merkleTree.js @@ -1,90 +1,90 @@ -// Import the crypto module for hashing -const crypto = require("crypto"); - -// Helper function to generate SHA-256 hash -function generateHash(data) { - return crypto.createHash("sha256").update(data).digest("hex"); -} - -// Function to create a Merkle Tree -function createMerkleTree(blocks) { - // Generate leaf nodes (hashes of the blocks) - let leafNodes = blocks.map((block) => generateHash(block)); - - // Build the tree - let tree = [leafNodes]; - while (tree[0].length > 1) { - let currentLevel = tree[0]; - let nextLevel = []; - for (let i = 0; i < currentLevel.length; i += 2) { - let left = currentLevel[i]; - let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; - nextLevel.push(generateHash(left + right)); - } - tree.unshift(nextLevel); - } - return tree; -} - -// Function to verify data integrity -function verifyIntegrity(originalTree, tamperedBlockIndex, tamperedBlock) { - // Tamper with the block - let tamperedLeafNodes = originalTree[originalTree.length - 1].slice(); - tamperedLeafNodes[tamperedBlockIndex] = generateHash(tamperedBlock); - - // Rebuilds the tree with the tampered block - let tamperedTree = [tamperedLeafNodes]; - while (tamperedTree[0].length > 1) { - let currentLevel = tamperedTree[0]; - let nextLevel = []; - for (let i = 0; i < currentLevel.length; i += 2) { - let left = currentLevel[i]; - let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; - nextLevel.push(generateHash(left + right)); - } - tamperedTree.unshift(nextLevel); - } - - return tamperedTree; -} - -// Main function to demonstrate the Merkle Tree -function main() { - const blocks = ["Block1", "Block2", "Block3", "Block4"]; - console.log("Original Blocks:", blocks); - - const merkleTree = createMerkleTree(blocks); - console.log("Merkle Tree:", merkleTree); - console.log("Root Hash:", merkleTree[0][0]); - - // Tamper with a block (change "Block3" to "TamperedBlock3") - const tamperedBlockIndex = 2; - const tamperedBlock = "TamperedBlock3"; - console.log( - `\nTampering with Block ${tamperedBlockIndex + 1}: "${ - blocks[tamperedBlockIndex] - }" -> "${tamperedBlock}"` - ); - - // Verify integrity - const tamperedTree = verifyIntegrity( - merkleTree, - tamperedBlockIndex, - tamperedBlock - ); - console.log("Tampered Merkle Tree:", tamperedTree); - console.log("New Root Hash:", tamperedTree[0][0]); - - // Check if the root hash changed - if (merkleTree[0][0] === tamperedTree[0][0]) { - console.log( - "Integrity Check: Root hash did NOT change. Data integrity is compromised!" - ); - } else { - console.log( - "Integrity Check: Root hash changed. Data integrity is maintained!" - ); - } -} - -main(); +// Import the crypto module for hashing +const crypto = require("crypto"); + +// Helper function to generate SHA-256 hash +function generateHash(data) { + return crypto.createHash("sha256").update(data).digest("hex"); +} + +// Function to create a Merkle Tree +function createMerkleTree(blocks) { + // Generate leaf nodes (hashes of the blocks) + let leafNodes = blocks.map((block) => generateHash(block)); + + // Build the tree + let tree = [leafNodes]; + while (tree[0].length > 1) { + let currentLevel = tree[0]; + let nextLevel = []; + for (let i = 0; i < currentLevel.length; i += 2) { + let left = currentLevel[i]; + let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; + nextLevel.push(generateHash(left + right)); + } + tree.unshift(nextLevel); + } + return tree; +} + +// Function to verify data integrity +function verifyIntegrity(originalTree, tamperedBlockIndex, tamperedBlock) { + // Tamper with the block + let tamperedLeafNodes = originalTree[originalTree.length - 1].slice(); + tamperedLeafNodes[tamperedBlockIndex] = generateHash(tamperedBlock); + + // Rebuilds the tree with the tampered block + let tamperedTree = [tamperedLeafNodes]; + while (tamperedTree[0].length > 1) { + let currentLevel = tamperedTree[0]; + let nextLevel = []; + for (let i = 0; i < currentLevel.length; i += 2) { + let left = currentLevel[i]; + let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; + nextLevel.push(generateHash(left + right)); + } + tamperedTree.unshift(nextLevel); + } + + return tamperedTree; +} + +// Main function to demonstrate the Merkle Tree +function main() { + const blocks = ["Block1", "Block2", "Block3", "Block4"]; + console.log("Original Blocks:", blocks); + + const merkleTree = createMerkleTree(blocks); + console.log("Merkle Tree:", merkleTree); + console.log("Root Hash:", merkleTree[0][0]); + + // Tamper with a block (change "Block3" to "TamperedBlock3") + const tamperedBlockIndex = 2; + const tamperedBlock = "TamperedBlock3"; + console.log( + `\nTampering with Block ${tamperedBlockIndex + 1}: "${ + blocks[tamperedBlockIndex] + }" -> "${tamperedBlock}"` + ); + + // Verify integrity + const tamperedTree = verifyIntegrity( + merkleTree, + tamperedBlockIndex, + tamperedBlock + ); + console.log("Tampered Merkle Tree:", tamperedTree); + console.log("New Root Hash:", tamperedTree[0][0]); + + // Check if the root hash changed + if (merkleTree[0][0] === tamperedTree[0][0]) { + console.log( + "Integrity Check: Root hash did NOT change. Data integrity is compromised!" + ); + } else { + console.log( + "Integrity Check: Root hash changed. Data integrity is maintained!" + ); + } +} + +main(); diff --git a/submissions/week-2/day-1/Semilore-White-Merkle-Tree/summary.md b/submissions/week-2/day-1/Semilore-White-Merkle-Tree/summary.md index 60af418..284ec0d 100644 --- a/submissions/week-2/day-1/Semilore-White-Merkle-Tree/summary.md +++ b/submissions/week-2/day-1/Semilore-White-Merkle-Tree/summary.md @@ -1,3 +1,3 @@ -# Summary - -A Merkle Tree is a data structure used in blockchain to efficiently and securely verify the contents of large datasets. It consists of leaf nodes, which are hashes of individual data blocks, and non-leaf nodes, which are hashes of their child nodes. The topmost node is called the root hash. Merkle Trees are important in blockchain because they allow for quick and secure verification of data integrity without needing to check the entire dataset. +# Summary + +A Merkle Tree is a data structure used in blockchain to efficiently and securely verify the contents of large datasets. It consists of leaf nodes, which are hashes of individual data blocks, and non-leaf nodes, which are hashes of their child nodes. The topmost node is called the root hash. Merkle Trees are important in blockchain because they allow for quick and secure verification of data integrity without needing to check the entire dataset. diff --git a/submissions/week-2/day-1/Semilore-White-Merkle-Tree/verification.md b/submissions/week-2/day-1/Semilore-White-Merkle-Tree/verification.md index 2687736..6dbbbdd 100644 --- a/submissions/week-2/day-1/Semilore-White-Merkle-Tree/verification.md +++ b/submissions/week-2/day-1/Semilore-White-Merkle-Tree/verification.md @@ -1,3 +1,3 @@ -# Verification - -If one of the data blocks, such as Block3, is tampered with, the hash of Block3 will change. This change will propagate up the Merkle Tree, altering the non-leaf node hashes and ultimately changing the root hash. This is important for data integrity in blockchain because any change in the data will be immediately detectable by comparing the root hash, ensuring the data remains secure and unaltered. +# Verification + +If one of the data blocks, such as Block3, is tampered with, the hash of Block3 will change. This change will propagate up the Merkle Tree, altering the non-leaf node hashes and ultimately changing the root hash. This is important for data integrity in blockchain because any change in the data will be immediately detectable by comparing the root hash, ensuring the data remains secure and unaltered. diff --git a/submissions/week-2/day-2/Goodness-Blessing-Merkle-Tree/merkleTree.js b/submissions/week-2/day-2/Goodness-Blessing-Merkle-Tree/merkleTree.js index c598426..03f3bb7 100644 --- a/submissions/week-2/day-2/Goodness-Blessing-Merkle-Tree/merkleTree.js +++ b/submissions/week-2/day-2/Goodness-Blessing-Merkle-Tree/merkleTree.js @@ -1,90 +1,90 @@ -// Import the crypto module for hashing -const crypto = require("crypto"); - -// Helper function to generate SHA-256 hash -function generateHash(data) { - return crypto.createHash("sha256").update(data).digest("hex"); -} - -// Function to create a Merkle Tree -function createMerkleTree(blocks) { - // Generate leaf nodes (hashes of the blocks) - let leafNodes = blocks.map((block) => generateHash(block)); - - // Build the tree - let tree = [leafNodes]; - while (tree[0].length > 1) { - let currentLevel = tree[0]; - let nextLevel = []; - for (let i = 0; i < currentLevel.length; i += 2) { - let left = currentLevel[i]; - let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; - nextLevel.push(generateHash(left + right)); - } - tree.unshift(nextLevel); - } - return tree; -} - -// Function to verify data integrity -function verifyIntegrity(originalTree, tamperedBlockIndex, tamperedBlock) { - // Tamper with the block - let tamperedLeafNodes = originalTree[originalTree.length - 1].slice(); - tamperedLeafNodes[tamperedBlockIndex] = generateHash(tamperedBlock); - - // Rebuilds the tree with the tampered block - let tamperedTree = [tamperedLeafNodes]; - while (tamperedTree[0].length > 1) { - let currentLevel = tamperedTree[0]; - let nextLevel = []; - for (let i = 0; i < currentLevel.length; i += 2) { - let left = currentLevel[i]; - let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; - nextLevel.push(generateHash(left + right)); - } - tamperedTree.unshift(nextLevel); - } - - return tamperedTree; -} - -// Main function to demonstrate the Merkle Tree -function main() { - const blocks = ["Block1", "Block2", "Block3", "Block4"]; - console.log("Original Blocks:", blocks); - - const merkleTree = createMerkleTree(blocks); - console.log("Merkle Tree:", merkleTree); - console.log("Root Hash:", merkleTree[0][0]); - - // Tamper with a block (change "Block3" to "TamperedBlock3") - const tamperedBlockIndex = 2; - const tamperedBlock = "TamperedBlock3"; - console.log( - `\nTampering with Block ${tamperedBlockIndex + 1}: "${ - blocks[tamperedBlockIndex] - }" -> "${tamperedBlock}"` - ); - - // Verify integrity - const tamperedTree = verifyIntegrity( - merkleTree, - tamperedBlockIndex, - tamperedBlock - ); - console.log("Tampered Merkle Tree:", tamperedTree); - console.log("New Root Hash:", tamperedTree[0][0]); - - // Check if the root hash changed - if (merkleTree[0][0] === tamperedTree[0][0]) { - console.log( - "Integrity Check: Root hash did NOT change. Data integrity is compromised!" - ); - } else { - console.log( - "Integrity Check: Root hash changed. Data integrity is maintained!" - ); - } -} - -main(); +// Import the crypto module for hashing +const crypto = require("crypto"); + +// Helper function to generate SHA-256 hash +function generateHash(data) { + return crypto.createHash("sha256").update(data).digest("hex"); +} + +// Function to create a Merkle Tree +function createMerkleTree(blocks) { + // Generate leaf nodes (hashes of the blocks) + let leafNodes = blocks.map((block) => generateHash(block)); + + // Build the tree + let tree = [leafNodes]; + while (tree[0].length > 1) { + let currentLevel = tree[0]; + let nextLevel = []; + for (let i = 0; i < currentLevel.length; i += 2) { + let left = currentLevel[i]; + let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; + nextLevel.push(generateHash(left + right)); + } + tree.unshift(nextLevel); + } + return tree; +} + +// Function to verify data integrity +function verifyIntegrity(originalTree, tamperedBlockIndex, tamperedBlock) { + // Tamper with the block + let tamperedLeafNodes = originalTree[originalTree.length - 1].slice(); + tamperedLeafNodes[tamperedBlockIndex] = generateHash(tamperedBlock); + + // Rebuilds the tree with the tampered block + let tamperedTree = [tamperedLeafNodes]; + while (tamperedTree[0].length > 1) { + let currentLevel = tamperedTree[0]; + let nextLevel = []; + for (let i = 0; i < currentLevel.length; i += 2) { + let left = currentLevel[i]; + let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; + nextLevel.push(generateHash(left + right)); + } + tamperedTree.unshift(nextLevel); + } + + return tamperedTree; +} + +// Main function to demonstrate the Merkle Tree +function main() { + const blocks = ["Block1", "Block2", "Block3", "Block4"]; + console.log("Original Blocks:", blocks); + + const merkleTree = createMerkleTree(blocks); + console.log("Merkle Tree:", merkleTree); + console.log("Root Hash:", merkleTree[0][0]); + + // Tamper with a block (change "Block3" to "TamperedBlock3") + const tamperedBlockIndex = 2; + const tamperedBlock = "TamperedBlock3"; + console.log( + `\nTampering with Block ${tamperedBlockIndex + 1}: "${ + blocks[tamperedBlockIndex] + }" -> "${tamperedBlock}"` + ); + + // Verify integrity + const tamperedTree = verifyIntegrity( + merkleTree, + tamperedBlockIndex, + tamperedBlock + ); + console.log("Tampered Merkle Tree:", tamperedTree); + console.log("New Root Hash:", tamperedTree[0][0]); + + // Check if the root hash changed + if (merkleTree[0][0] === tamperedTree[0][0]) { + console.log( + "Integrity Check: Root hash did NOT change. Data integrity is compromised!" + ); + } else { + console.log( + "Integrity Check: Root hash changed. Data integrity is maintained!" + ); + } +} + +main(); diff --git a/submissions/week-2/day-2/Goodness-Blessing-Merkle-Tree/summary.md b/submissions/week-2/day-2/Goodness-Blessing-Merkle-Tree/summary.md index 60af418..284ec0d 100644 --- a/submissions/week-2/day-2/Goodness-Blessing-Merkle-Tree/summary.md +++ b/submissions/week-2/day-2/Goodness-Blessing-Merkle-Tree/summary.md @@ -1,3 +1,3 @@ -# Summary - -A Merkle Tree is a data structure used in blockchain to efficiently and securely verify the contents of large datasets. It consists of leaf nodes, which are hashes of individual data blocks, and non-leaf nodes, which are hashes of their child nodes. The topmost node is called the root hash. Merkle Trees are important in blockchain because they allow for quick and secure verification of data integrity without needing to check the entire dataset. +# Summary + +A Merkle Tree is a data structure used in blockchain to efficiently and securely verify the contents of large datasets. It consists of leaf nodes, which are hashes of individual data blocks, and non-leaf nodes, which are hashes of their child nodes. The topmost node is called the root hash. Merkle Trees are important in blockchain because they allow for quick and secure verification of data integrity without needing to check the entire dataset. diff --git a/submissions/week-2/day-2/Goodness-Blessing-Merkle-Tree/verification.md b/submissions/week-2/day-2/Goodness-Blessing-Merkle-Tree/verification.md index 2687736..6dbbbdd 100644 --- a/submissions/week-2/day-2/Goodness-Blessing-Merkle-Tree/verification.md +++ b/submissions/week-2/day-2/Goodness-Blessing-Merkle-Tree/verification.md @@ -1,3 +1,3 @@ -# Verification - -If one of the data blocks, such as Block3, is tampered with, the hash of Block3 will change. This change will propagate up the Merkle Tree, altering the non-leaf node hashes and ultimately changing the root hash. This is important for data integrity in blockchain because any change in the data will be immediately detectable by comparing the root hash, ensuring the data remains secure and unaltered. +# Verification + +If one of the data blocks, such as Block3, is tampered with, the hash of Block3 will change. This change will propagate up the Merkle Tree, altering the non-leaf node hashes and ultimately changing the root hash. This is important for data integrity in blockchain because any change in the data will be immediately detectable by comparing the root hash, ensuring the data remains secure and unaltered. diff --git a/submissions/week-2/day-2/Semilore-White-Merkle-Tree/merkleTree.js b/submissions/week-2/day-2/Semilore-White-Merkle-Tree/merkleTree.js index c598426..03f3bb7 100644 --- a/submissions/week-2/day-2/Semilore-White-Merkle-Tree/merkleTree.js +++ b/submissions/week-2/day-2/Semilore-White-Merkle-Tree/merkleTree.js @@ -1,90 +1,90 @@ -// Import the crypto module for hashing -const crypto = require("crypto"); - -// Helper function to generate SHA-256 hash -function generateHash(data) { - return crypto.createHash("sha256").update(data).digest("hex"); -} - -// Function to create a Merkle Tree -function createMerkleTree(blocks) { - // Generate leaf nodes (hashes of the blocks) - let leafNodes = blocks.map((block) => generateHash(block)); - - // Build the tree - let tree = [leafNodes]; - while (tree[0].length > 1) { - let currentLevel = tree[0]; - let nextLevel = []; - for (let i = 0; i < currentLevel.length; i += 2) { - let left = currentLevel[i]; - let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; - nextLevel.push(generateHash(left + right)); - } - tree.unshift(nextLevel); - } - return tree; -} - -// Function to verify data integrity -function verifyIntegrity(originalTree, tamperedBlockIndex, tamperedBlock) { - // Tamper with the block - let tamperedLeafNodes = originalTree[originalTree.length - 1].slice(); - tamperedLeafNodes[tamperedBlockIndex] = generateHash(tamperedBlock); - - // Rebuilds the tree with the tampered block - let tamperedTree = [tamperedLeafNodes]; - while (tamperedTree[0].length > 1) { - let currentLevel = tamperedTree[0]; - let nextLevel = []; - for (let i = 0; i < currentLevel.length; i += 2) { - let left = currentLevel[i]; - let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; - nextLevel.push(generateHash(left + right)); - } - tamperedTree.unshift(nextLevel); - } - - return tamperedTree; -} - -// Main function to demonstrate the Merkle Tree -function main() { - const blocks = ["Block1", "Block2", "Block3", "Block4"]; - console.log("Original Blocks:", blocks); - - const merkleTree = createMerkleTree(blocks); - console.log("Merkle Tree:", merkleTree); - console.log("Root Hash:", merkleTree[0][0]); - - // Tamper with a block (change "Block3" to "TamperedBlock3") - const tamperedBlockIndex = 2; - const tamperedBlock = "TamperedBlock3"; - console.log( - `\nTampering with Block ${tamperedBlockIndex + 1}: "${ - blocks[tamperedBlockIndex] - }" -> "${tamperedBlock}"` - ); - - // Verify integrity - const tamperedTree = verifyIntegrity( - merkleTree, - tamperedBlockIndex, - tamperedBlock - ); - console.log("Tampered Merkle Tree:", tamperedTree); - console.log("New Root Hash:", tamperedTree[0][0]); - - // Check if the root hash changed - if (merkleTree[0][0] === tamperedTree[0][0]) { - console.log( - "Integrity Check: Root hash did NOT change. Data integrity is compromised!" - ); - } else { - console.log( - "Integrity Check: Root hash changed. Data integrity is maintained!" - ); - } -} - -main(); +// Import the crypto module for hashing +const crypto = require("crypto"); + +// Helper function to generate SHA-256 hash +function generateHash(data) { + return crypto.createHash("sha256").update(data).digest("hex"); +} + +// Function to create a Merkle Tree +function createMerkleTree(blocks) { + // Generate leaf nodes (hashes of the blocks) + let leafNodes = blocks.map((block) => generateHash(block)); + + // Build the tree + let tree = [leafNodes]; + while (tree[0].length > 1) { + let currentLevel = tree[0]; + let nextLevel = []; + for (let i = 0; i < currentLevel.length; i += 2) { + let left = currentLevel[i]; + let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; + nextLevel.push(generateHash(left + right)); + } + tree.unshift(nextLevel); + } + return tree; +} + +// Function to verify data integrity +function verifyIntegrity(originalTree, tamperedBlockIndex, tamperedBlock) { + // Tamper with the block + let tamperedLeafNodes = originalTree[originalTree.length - 1].slice(); + tamperedLeafNodes[tamperedBlockIndex] = generateHash(tamperedBlock); + + // Rebuilds the tree with the tampered block + let tamperedTree = [tamperedLeafNodes]; + while (tamperedTree[0].length > 1) { + let currentLevel = tamperedTree[0]; + let nextLevel = []; + for (let i = 0; i < currentLevel.length; i += 2) { + let left = currentLevel[i]; + let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; + nextLevel.push(generateHash(left + right)); + } + tamperedTree.unshift(nextLevel); + } + + return tamperedTree; +} + +// Main function to demonstrate the Merkle Tree +function main() { + const blocks = ["Block1", "Block2", "Block3", "Block4"]; + console.log("Original Blocks:", blocks); + + const merkleTree = createMerkleTree(blocks); + console.log("Merkle Tree:", merkleTree); + console.log("Root Hash:", merkleTree[0][0]); + + // Tamper with a block (change "Block3" to "TamperedBlock3") + const tamperedBlockIndex = 2; + const tamperedBlock = "TamperedBlock3"; + console.log( + `\nTampering with Block ${tamperedBlockIndex + 1}: "${ + blocks[tamperedBlockIndex] + }" -> "${tamperedBlock}"` + ); + + // Verify integrity + const tamperedTree = verifyIntegrity( + merkleTree, + tamperedBlockIndex, + tamperedBlock + ); + console.log("Tampered Merkle Tree:", tamperedTree); + console.log("New Root Hash:", tamperedTree[0][0]); + + // Check if the root hash changed + if (merkleTree[0][0] === tamperedTree[0][0]) { + console.log( + "Integrity Check: Root hash did NOT change. Data integrity is compromised!" + ); + } else { + console.log( + "Integrity Check: Root hash changed. Data integrity is maintained!" + ); + } +} + +main(); diff --git a/submissions/week-2/day-2/Semilore-White-Merkle-Tree/summary.md b/submissions/week-2/day-2/Semilore-White-Merkle-Tree/summary.md index 60af418..284ec0d 100644 --- a/submissions/week-2/day-2/Semilore-White-Merkle-Tree/summary.md +++ b/submissions/week-2/day-2/Semilore-White-Merkle-Tree/summary.md @@ -1,3 +1,3 @@ -# Summary - -A Merkle Tree is a data structure used in blockchain to efficiently and securely verify the contents of large datasets. It consists of leaf nodes, which are hashes of individual data blocks, and non-leaf nodes, which are hashes of their child nodes. The topmost node is called the root hash. Merkle Trees are important in blockchain because they allow for quick and secure verification of data integrity without needing to check the entire dataset. +# Summary + +A Merkle Tree is a data structure used in blockchain to efficiently and securely verify the contents of large datasets. It consists of leaf nodes, which are hashes of individual data blocks, and non-leaf nodes, which are hashes of their child nodes. The topmost node is called the root hash. Merkle Trees are important in blockchain because they allow for quick and secure verification of data integrity without needing to check the entire dataset. diff --git a/submissions/week-2/day-2/Semilore-White-Merkle-Tree/verification.md b/submissions/week-2/day-2/Semilore-White-Merkle-Tree/verification.md index 2687736..6dbbbdd 100644 --- a/submissions/week-2/day-2/Semilore-White-Merkle-Tree/verification.md +++ b/submissions/week-2/day-2/Semilore-White-Merkle-Tree/verification.md @@ -1,3 +1,3 @@ -# Verification - -If one of the data blocks, such as Block3, is tampered with, the hash of Block3 will change. This change will propagate up the Merkle Tree, altering the non-leaf node hashes and ultimately changing the root hash. This is important for data integrity in blockchain because any change in the data will be immediately detectable by comparing the root hash, ensuring the data remains secure and unaltered. +# Verification + +If one of the data blocks, such as Block3, is tampered with, the hash of Block3 will change. This change will propagate up the Merkle Tree, altering the non-leaf node hashes and ultimately changing the root hash. This is important for data integrity in blockchain because any change in the data will be immediately detectable by comparing the root hash, ensuring the data remains secure and unaltered. diff --git a/submissions/week-2/day-3/SamuelAchilike-Enums-Assignment/assignment_1_enums b/submissions/week-2/day-3/SamuelAchilike-Enums-Assignment/assignment_1_enums new file mode 160000 index 0000000..4a9910a --- /dev/null +++ b/submissions/week-2/day-3/SamuelAchilike-Enums-Assignment/assignment_1_enums @@ -0,0 +1 @@ +Subproject commit 4a9910a94bb9f4ba1ef759bbefda8c07ae192736 diff --git a/submissions/week-3/day-1/Goodness-Blessing-Merkle-Tree/merkleTree.js b/submissions/week-3/day-1/Goodness-Blessing-Merkle-Tree/merkleTree.js index c598426..03f3bb7 100644 --- a/submissions/week-3/day-1/Goodness-Blessing-Merkle-Tree/merkleTree.js +++ b/submissions/week-3/day-1/Goodness-Blessing-Merkle-Tree/merkleTree.js @@ -1,90 +1,90 @@ -// Import the crypto module for hashing -const crypto = require("crypto"); - -// Helper function to generate SHA-256 hash -function generateHash(data) { - return crypto.createHash("sha256").update(data).digest("hex"); -} - -// Function to create a Merkle Tree -function createMerkleTree(blocks) { - // Generate leaf nodes (hashes of the blocks) - let leafNodes = blocks.map((block) => generateHash(block)); - - // Build the tree - let tree = [leafNodes]; - while (tree[0].length > 1) { - let currentLevel = tree[0]; - let nextLevel = []; - for (let i = 0; i < currentLevel.length; i += 2) { - let left = currentLevel[i]; - let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; - nextLevel.push(generateHash(left + right)); - } - tree.unshift(nextLevel); - } - return tree; -} - -// Function to verify data integrity -function verifyIntegrity(originalTree, tamperedBlockIndex, tamperedBlock) { - // Tamper with the block - let tamperedLeafNodes = originalTree[originalTree.length - 1].slice(); - tamperedLeafNodes[tamperedBlockIndex] = generateHash(tamperedBlock); - - // Rebuilds the tree with the tampered block - let tamperedTree = [tamperedLeafNodes]; - while (tamperedTree[0].length > 1) { - let currentLevel = tamperedTree[0]; - let nextLevel = []; - for (let i = 0; i < currentLevel.length; i += 2) { - let left = currentLevel[i]; - let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; - nextLevel.push(generateHash(left + right)); - } - tamperedTree.unshift(nextLevel); - } - - return tamperedTree; -} - -// Main function to demonstrate the Merkle Tree -function main() { - const blocks = ["Block1", "Block2", "Block3", "Block4"]; - console.log("Original Blocks:", blocks); - - const merkleTree = createMerkleTree(blocks); - console.log("Merkle Tree:", merkleTree); - console.log("Root Hash:", merkleTree[0][0]); - - // Tamper with a block (change "Block3" to "TamperedBlock3") - const tamperedBlockIndex = 2; - const tamperedBlock = "TamperedBlock3"; - console.log( - `\nTampering with Block ${tamperedBlockIndex + 1}: "${ - blocks[tamperedBlockIndex] - }" -> "${tamperedBlock}"` - ); - - // Verify integrity - const tamperedTree = verifyIntegrity( - merkleTree, - tamperedBlockIndex, - tamperedBlock - ); - console.log("Tampered Merkle Tree:", tamperedTree); - console.log("New Root Hash:", tamperedTree[0][0]); - - // Check if the root hash changed - if (merkleTree[0][0] === tamperedTree[0][0]) { - console.log( - "Integrity Check: Root hash did NOT change. Data integrity is compromised!" - ); - } else { - console.log( - "Integrity Check: Root hash changed. Data integrity is maintained!" - ); - } -} - -main(); +// Import the crypto module for hashing +const crypto = require("crypto"); + +// Helper function to generate SHA-256 hash +function generateHash(data) { + return crypto.createHash("sha256").update(data).digest("hex"); +} + +// Function to create a Merkle Tree +function createMerkleTree(blocks) { + // Generate leaf nodes (hashes of the blocks) + let leafNodes = blocks.map((block) => generateHash(block)); + + // Build the tree + let tree = [leafNodes]; + while (tree[0].length > 1) { + let currentLevel = tree[0]; + let nextLevel = []; + for (let i = 0; i < currentLevel.length; i += 2) { + let left = currentLevel[i]; + let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; + nextLevel.push(generateHash(left + right)); + } + tree.unshift(nextLevel); + } + return tree; +} + +// Function to verify data integrity +function verifyIntegrity(originalTree, tamperedBlockIndex, tamperedBlock) { + // Tamper with the block + let tamperedLeafNodes = originalTree[originalTree.length - 1].slice(); + tamperedLeafNodes[tamperedBlockIndex] = generateHash(tamperedBlock); + + // Rebuilds the tree with the tampered block + let tamperedTree = [tamperedLeafNodes]; + while (tamperedTree[0].length > 1) { + let currentLevel = tamperedTree[0]; + let nextLevel = []; + for (let i = 0; i < currentLevel.length; i += 2) { + let left = currentLevel[i]; + let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; + nextLevel.push(generateHash(left + right)); + } + tamperedTree.unshift(nextLevel); + } + + return tamperedTree; +} + +// Main function to demonstrate the Merkle Tree +function main() { + const blocks = ["Block1", "Block2", "Block3", "Block4"]; + console.log("Original Blocks:", blocks); + + const merkleTree = createMerkleTree(blocks); + console.log("Merkle Tree:", merkleTree); + console.log("Root Hash:", merkleTree[0][0]); + + // Tamper with a block (change "Block3" to "TamperedBlock3") + const tamperedBlockIndex = 2; + const tamperedBlock = "TamperedBlock3"; + console.log( + `\nTampering with Block ${tamperedBlockIndex + 1}: "${ + blocks[tamperedBlockIndex] + }" -> "${tamperedBlock}"` + ); + + // Verify integrity + const tamperedTree = verifyIntegrity( + merkleTree, + tamperedBlockIndex, + tamperedBlock + ); + console.log("Tampered Merkle Tree:", tamperedTree); + console.log("New Root Hash:", tamperedTree[0][0]); + + // Check if the root hash changed + if (merkleTree[0][0] === tamperedTree[0][0]) { + console.log( + "Integrity Check: Root hash did NOT change. Data integrity is compromised!" + ); + } else { + console.log( + "Integrity Check: Root hash changed. Data integrity is maintained!" + ); + } +} + +main(); diff --git a/submissions/week-3/day-1/Goodness-Blessing-Merkle-Tree/summary.md b/submissions/week-3/day-1/Goodness-Blessing-Merkle-Tree/summary.md index 60af418..284ec0d 100644 --- a/submissions/week-3/day-1/Goodness-Blessing-Merkle-Tree/summary.md +++ b/submissions/week-3/day-1/Goodness-Blessing-Merkle-Tree/summary.md @@ -1,3 +1,3 @@ -# Summary - -A Merkle Tree is a data structure used in blockchain to efficiently and securely verify the contents of large datasets. It consists of leaf nodes, which are hashes of individual data blocks, and non-leaf nodes, which are hashes of their child nodes. The topmost node is called the root hash. Merkle Trees are important in blockchain because they allow for quick and secure verification of data integrity without needing to check the entire dataset. +# Summary + +A Merkle Tree is a data structure used in blockchain to efficiently and securely verify the contents of large datasets. It consists of leaf nodes, which are hashes of individual data blocks, and non-leaf nodes, which are hashes of their child nodes. The topmost node is called the root hash. Merkle Trees are important in blockchain because they allow for quick and secure verification of data integrity without needing to check the entire dataset. diff --git a/submissions/week-3/day-1/Goodness-Blessing-Merkle-Tree/verification.md b/submissions/week-3/day-1/Goodness-Blessing-Merkle-Tree/verification.md index 2687736..6dbbbdd 100644 --- a/submissions/week-3/day-1/Goodness-Blessing-Merkle-Tree/verification.md +++ b/submissions/week-3/day-1/Goodness-Blessing-Merkle-Tree/verification.md @@ -1,3 +1,3 @@ -# Verification - -If one of the data blocks, such as Block3, is tampered with, the hash of Block3 will change. This change will propagate up the Merkle Tree, altering the non-leaf node hashes and ultimately changing the root hash. This is important for data integrity in blockchain because any change in the data will be immediately detectable by comparing the root hash, ensuring the data remains secure and unaltered. +# Verification + +If one of the data blocks, such as Block3, is tampered with, the hash of Block3 will change. This change will propagate up the Merkle Tree, altering the non-leaf node hashes and ultimately changing the root hash. This is important for data integrity in blockchain because any change in the data will be immediately detectable by comparing the root hash, ensuring the data remains secure and unaltered. diff --git a/submissions/week-3/day-1/Semilore-White-Merkle-Tree/merkleTree.js b/submissions/week-3/day-1/Semilore-White-Merkle-Tree/merkleTree.js index c598426..03f3bb7 100644 --- a/submissions/week-3/day-1/Semilore-White-Merkle-Tree/merkleTree.js +++ b/submissions/week-3/day-1/Semilore-White-Merkle-Tree/merkleTree.js @@ -1,90 +1,90 @@ -// Import the crypto module for hashing -const crypto = require("crypto"); - -// Helper function to generate SHA-256 hash -function generateHash(data) { - return crypto.createHash("sha256").update(data).digest("hex"); -} - -// Function to create a Merkle Tree -function createMerkleTree(blocks) { - // Generate leaf nodes (hashes of the blocks) - let leafNodes = blocks.map((block) => generateHash(block)); - - // Build the tree - let tree = [leafNodes]; - while (tree[0].length > 1) { - let currentLevel = tree[0]; - let nextLevel = []; - for (let i = 0; i < currentLevel.length; i += 2) { - let left = currentLevel[i]; - let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; - nextLevel.push(generateHash(left + right)); - } - tree.unshift(nextLevel); - } - return tree; -} - -// Function to verify data integrity -function verifyIntegrity(originalTree, tamperedBlockIndex, tamperedBlock) { - // Tamper with the block - let tamperedLeafNodes = originalTree[originalTree.length - 1].slice(); - tamperedLeafNodes[tamperedBlockIndex] = generateHash(tamperedBlock); - - // Rebuilds the tree with the tampered block - let tamperedTree = [tamperedLeafNodes]; - while (tamperedTree[0].length > 1) { - let currentLevel = tamperedTree[0]; - let nextLevel = []; - for (let i = 0; i < currentLevel.length; i += 2) { - let left = currentLevel[i]; - let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; - nextLevel.push(generateHash(left + right)); - } - tamperedTree.unshift(nextLevel); - } - - return tamperedTree; -} - -// Main function to demonstrate the Merkle Tree -function main() { - const blocks = ["Block1", "Block2", "Block3", "Block4"]; - console.log("Original Blocks:", blocks); - - const merkleTree = createMerkleTree(blocks); - console.log("Merkle Tree:", merkleTree); - console.log("Root Hash:", merkleTree[0][0]); - - // Tamper with a block (change "Block3" to "TamperedBlock3") - const tamperedBlockIndex = 2; - const tamperedBlock = "TamperedBlock3"; - console.log( - `\nTampering with Block ${tamperedBlockIndex + 1}: "${ - blocks[tamperedBlockIndex] - }" -> "${tamperedBlock}"` - ); - - // Verify integrity - const tamperedTree = verifyIntegrity( - merkleTree, - tamperedBlockIndex, - tamperedBlock - ); - console.log("Tampered Merkle Tree:", tamperedTree); - console.log("New Root Hash:", tamperedTree[0][0]); - - // Check if the root hash changed - if (merkleTree[0][0] === tamperedTree[0][0]) { - console.log( - "Integrity Check: Root hash did NOT change. Data integrity is compromised!" - ); - } else { - console.log( - "Integrity Check: Root hash changed. Data integrity is maintained!" - ); - } -} - -main(); +// Import the crypto module for hashing +const crypto = require("crypto"); + +// Helper function to generate SHA-256 hash +function generateHash(data) { + return crypto.createHash("sha256").update(data).digest("hex"); +} + +// Function to create a Merkle Tree +function createMerkleTree(blocks) { + // Generate leaf nodes (hashes of the blocks) + let leafNodes = blocks.map((block) => generateHash(block)); + + // Build the tree + let tree = [leafNodes]; + while (tree[0].length > 1) { + let currentLevel = tree[0]; + let nextLevel = []; + for (let i = 0; i < currentLevel.length; i += 2) { + let left = currentLevel[i]; + let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; + nextLevel.push(generateHash(left + right)); + } + tree.unshift(nextLevel); + } + return tree; +} + +// Function to verify data integrity +function verifyIntegrity(originalTree, tamperedBlockIndex, tamperedBlock) { + // Tamper with the block + let tamperedLeafNodes = originalTree[originalTree.length - 1].slice(); + tamperedLeafNodes[tamperedBlockIndex] = generateHash(tamperedBlock); + + // Rebuilds the tree with the tampered block + let tamperedTree = [tamperedLeafNodes]; + while (tamperedTree[0].length > 1) { + let currentLevel = tamperedTree[0]; + let nextLevel = []; + for (let i = 0; i < currentLevel.length; i += 2) { + let left = currentLevel[i]; + let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; + nextLevel.push(generateHash(left + right)); + } + tamperedTree.unshift(nextLevel); + } + + return tamperedTree; +} + +// Main function to demonstrate the Merkle Tree +function main() { + const blocks = ["Block1", "Block2", "Block3", "Block4"]; + console.log("Original Blocks:", blocks); + + const merkleTree = createMerkleTree(blocks); + console.log("Merkle Tree:", merkleTree); + console.log("Root Hash:", merkleTree[0][0]); + + // Tamper with a block (change "Block3" to "TamperedBlock3") + const tamperedBlockIndex = 2; + const tamperedBlock = "TamperedBlock3"; + console.log( + `\nTampering with Block ${tamperedBlockIndex + 1}: "${ + blocks[tamperedBlockIndex] + }" -> "${tamperedBlock}"` + ); + + // Verify integrity + const tamperedTree = verifyIntegrity( + merkleTree, + tamperedBlockIndex, + tamperedBlock + ); + console.log("Tampered Merkle Tree:", tamperedTree); + console.log("New Root Hash:", tamperedTree[0][0]); + + // Check if the root hash changed + if (merkleTree[0][0] === tamperedTree[0][0]) { + console.log( + "Integrity Check: Root hash did NOT change. Data integrity is compromised!" + ); + } else { + console.log( + "Integrity Check: Root hash changed. Data integrity is maintained!" + ); + } +} + +main(); diff --git a/submissions/week-3/day-1/Semilore-White-Merkle-Tree/summary.md b/submissions/week-3/day-1/Semilore-White-Merkle-Tree/summary.md index 60af418..284ec0d 100644 --- a/submissions/week-3/day-1/Semilore-White-Merkle-Tree/summary.md +++ b/submissions/week-3/day-1/Semilore-White-Merkle-Tree/summary.md @@ -1,3 +1,3 @@ -# Summary - -A Merkle Tree is a data structure used in blockchain to efficiently and securely verify the contents of large datasets. It consists of leaf nodes, which are hashes of individual data blocks, and non-leaf nodes, which are hashes of their child nodes. The topmost node is called the root hash. Merkle Trees are important in blockchain because they allow for quick and secure verification of data integrity without needing to check the entire dataset. +# Summary + +A Merkle Tree is a data structure used in blockchain to efficiently and securely verify the contents of large datasets. It consists of leaf nodes, which are hashes of individual data blocks, and non-leaf nodes, which are hashes of their child nodes. The topmost node is called the root hash. Merkle Trees are important in blockchain because they allow for quick and secure verification of data integrity without needing to check the entire dataset. diff --git a/submissions/week-3/day-1/Semilore-White-Merkle-Tree/verification.md b/submissions/week-3/day-1/Semilore-White-Merkle-Tree/verification.md index 2687736..6dbbbdd 100644 --- a/submissions/week-3/day-1/Semilore-White-Merkle-Tree/verification.md +++ b/submissions/week-3/day-1/Semilore-White-Merkle-Tree/verification.md @@ -1,3 +1,3 @@ -# Verification - -If one of the data blocks, such as Block3, is tampered with, the hash of Block3 will change. This change will propagate up the Merkle Tree, altering the non-leaf node hashes and ultimately changing the root hash. This is important for data integrity in blockchain because any change in the data will be immediately detectable by comparing the root hash, ensuring the data remains secure and unaltered. +# Verification + +If one of the data blocks, such as Block3, is tampered with, the hash of Block3 will change. This change will propagate up the Merkle Tree, altering the non-leaf node hashes and ultimately changing the root hash. This is important for data integrity in blockchain because any change in the data will be immediately detectable by comparing the root hash, ensuring the data remains secure and unaltered. diff --git a/submissions/week-3/day-2/Goodness-Blessing-Merkle-Tree/merkleTree.js b/submissions/week-3/day-2/Goodness-Blessing-Merkle-Tree/merkleTree.js index c598426..03f3bb7 100644 --- a/submissions/week-3/day-2/Goodness-Blessing-Merkle-Tree/merkleTree.js +++ b/submissions/week-3/day-2/Goodness-Blessing-Merkle-Tree/merkleTree.js @@ -1,90 +1,90 @@ -// Import the crypto module for hashing -const crypto = require("crypto"); - -// Helper function to generate SHA-256 hash -function generateHash(data) { - return crypto.createHash("sha256").update(data).digest("hex"); -} - -// Function to create a Merkle Tree -function createMerkleTree(blocks) { - // Generate leaf nodes (hashes of the blocks) - let leafNodes = blocks.map((block) => generateHash(block)); - - // Build the tree - let tree = [leafNodes]; - while (tree[0].length > 1) { - let currentLevel = tree[0]; - let nextLevel = []; - for (let i = 0; i < currentLevel.length; i += 2) { - let left = currentLevel[i]; - let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; - nextLevel.push(generateHash(left + right)); - } - tree.unshift(nextLevel); - } - return tree; -} - -// Function to verify data integrity -function verifyIntegrity(originalTree, tamperedBlockIndex, tamperedBlock) { - // Tamper with the block - let tamperedLeafNodes = originalTree[originalTree.length - 1].slice(); - tamperedLeafNodes[tamperedBlockIndex] = generateHash(tamperedBlock); - - // Rebuilds the tree with the tampered block - let tamperedTree = [tamperedLeafNodes]; - while (tamperedTree[0].length > 1) { - let currentLevel = tamperedTree[0]; - let nextLevel = []; - for (let i = 0; i < currentLevel.length; i += 2) { - let left = currentLevel[i]; - let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; - nextLevel.push(generateHash(left + right)); - } - tamperedTree.unshift(nextLevel); - } - - return tamperedTree; -} - -// Main function to demonstrate the Merkle Tree -function main() { - const blocks = ["Block1", "Block2", "Block3", "Block4"]; - console.log("Original Blocks:", blocks); - - const merkleTree = createMerkleTree(blocks); - console.log("Merkle Tree:", merkleTree); - console.log("Root Hash:", merkleTree[0][0]); - - // Tamper with a block (change "Block3" to "TamperedBlock3") - const tamperedBlockIndex = 2; - const tamperedBlock = "TamperedBlock3"; - console.log( - `\nTampering with Block ${tamperedBlockIndex + 1}: "${ - blocks[tamperedBlockIndex] - }" -> "${tamperedBlock}"` - ); - - // Verify integrity - const tamperedTree = verifyIntegrity( - merkleTree, - tamperedBlockIndex, - tamperedBlock - ); - console.log("Tampered Merkle Tree:", tamperedTree); - console.log("New Root Hash:", tamperedTree[0][0]); - - // Check if the root hash changed - if (merkleTree[0][0] === tamperedTree[0][0]) { - console.log( - "Integrity Check: Root hash did NOT change. Data integrity is compromised!" - ); - } else { - console.log( - "Integrity Check: Root hash changed. Data integrity is maintained!" - ); - } -} - -main(); +// Import the crypto module for hashing +const crypto = require("crypto"); + +// Helper function to generate SHA-256 hash +function generateHash(data) { + return crypto.createHash("sha256").update(data).digest("hex"); +} + +// Function to create a Merkle Tree +function createMerkleTree(blocks) { + // Generate leaf nodes (hashes of the blocks) + let leafNodes = blocks.map((block) => generateHash(block)); + + // Build the tree + let tree = [leafNodes]; + while (tree[0].length > 1) { + let currentLevel = tree[0]; + let nextLevel = []; + for (let i = 0; i < currentLevel.length; i += 2) { + let left = currentLevel[i]; + let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; + nextLevel.push(generateHash(left + right)); + } + tree.unshift(nextLevel); + } + return tree; +} + +// Function to verify data integrity +function verifyIntegrity(originalTree, tamperedBlockIndex, tamperedBlock) { + // Tamper with the block + let tamperedLeafNodes = originalTree[originalTree.length - 1].slice(); + tamperedLeafNodes[tamperedBlockIndex] = generateHash(tamperedBlock); + + // Rebuilds the tree with the tampered block + let tamperedTree = [tamperedLeafNodes]; + while (tamperedTree[0].length > 1) { + let currentLevel = tamperedTree[0]; + let nextLevel = []; + for (let i = 0; i < currentLevel.length; i += 2) { + let left = currentLevel[i]; + let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; + nextLevel.push(generateHash(left + right)); + } + tamperedTree.unshift(nextLevel); + } + + return tamperedTree; +} + +// Main function to demonstrate the Merkle Tree +function main() { + const blocks = ["Block1", "Block2", "Block3", "Block4"]; + console.log("Original Blocks:", blocks); + + const merkleTree = createMerkleTree(blocks); + console.log("Merkle Tree:", merkleTree); + console.log("Root Hash:", merkleTree[0][0]); + + // Tamper with a block (change "Block3" to "TamperedBlock3") + const tamperedBlockIndex = 2; + const tamperedBlock = "TamperedBlock3"; + console.log( + `\nTampering with Block ${tamperedBlockIndex + 1}: "${ + blocks[tamperedBlockIndex] + }" -> "${tamperedBlock}"` + ); + + // Verify integrity + const tamperedTree = verifyIntegrity( + merkleTree, + tamperedBlockIndex, + tamperedBlock + ); + console.log("Tampered Merkle Tree:", tamperedTree); + console.log("New Root Hash:", tamperedTree[0][0]); + + // Check if the root hash changed + if (merkleTree[0][0] === tamperedTree[0][0]) { + console.log( + "Integrity Check: Root hash did NOT change. Data integrity is compromised!" + ); + } else { + console.log( + "Integrity Check: Root hash changed. Data integrity is maintained!" + ); + } +} + +main(); diff --git a/submissions/week-3/day-2/Goodness-Blessing-Merkle-Tree/summary.md b/submissions/week-3/day-2/Goodness-Blessing-Merkle-Tree/summary.md index 60af418..284ec0d 100644 --- a/submissions/week-3/day-2/Goodness-Blessing-Merkle-Tree/summary.md +++ b/submissions/week-3/day-2/Goodness-Blessing-Merkle-Tree/summary.md @@ -1,3 +1,3 @@ -# Summary - -A Merkle Tree is a data structure used in blockchain to efficiently and securely verify the contents of large datasets. It consists of leaf nodes, which are hashes of individual data blocks, and non-leaf nodes, which are hashes of their child nodes. The topmost node is called the root hash. Merkle Trees are important in blockchain because they allow for quick and secure verification of data integrity without needing to check the entire dataset. +# Summary + +A Merkle Tree is a data structure used in blockchain to efficiently and securely verify the contents of large datasets. It consists of leaf nodes, which are hashes of individual data blocks, and non-leaf nodes, which are hashes of their child nodes. The topmost node is called the root hash. Merkle Trees are important in blockchain because they allow for quick and secure verification of data integrity without needing to check the entire dataset. diff --git a/submissions/week-3/day-2/Goodness-Blessing-Merkle-Tree/verification.md b/submissions/week-3/day-2/Goodness-Blessing-Merkle-Tree/verification.md index 2687736..6dbbbdd 100644 --- a/submissions/week-3/day-2/Goodness-Blessing-Merkle-Tree/verification.md +++ b/submissions/week-3/day-2/Goodness-Blessing-Merkle-Tree/verification.md @@ -1,3 +1,3 @@ -# Verification - -If one of the data blocks, such as Block3, is tampered with, the hash of Block3 will change. This change will propagate up the Merkle Tree, altering the non-leaf node hashes and ultimately changing the root hash. This is important for data integrity in blockchain because any change in the data will be immediately detectable by comparing the root hash, ensuring the data remains secure and unaltered. +# Verification + +If one of the data blocks, such as Block3, is tampered with, the hash of Block3 will change. This change will propagate up the Merkle Tree, altering the non-leaf node hashes and ultimately changing the root hash. This is important for data integrity in blockchain because any change in the data will be immediately detectable by comparing the root hash, ensuring the data remains secure and unaltered. diff --git a/submissions/week-3/day-2/Semilore-White-Merkle-Tree/merkleTree.js b/submissions/week-3/day-2/Semilore-White-Merkle-Tree/merkleTree.js index c598426..03f3bb7 100644 --- a/submissions/week-3/day-2/Semilore-White-Merkle-Tree/merkleTree.js +++ b/submissions/week-3/day-2/Semilore-White-Merkle-Tree/merkleTree.js @@ -1,90 +1,90 @@ -// Import the crypto module for hashing -const crypto = require("crypto"); - -// Helper function to generate SHA-256 hash -function generateHash(data) { - return crypto.createHash("sha256").update(data).digest("hex"); -} - -// Function to create a Merkle Tree -function createMerkleTree(blocks) { - // Generate leaf nodes (hashes of the blocks) - let leafNodes = blocks.map((block) => generateHash(block)); - - // Build the tree - let tree = [leafNodes]; - while (tree[0].length > 1) { - let currentLevel = tree[0]; - let nextLevel = []; - for (let i = 0; i < currentLevel.length; i += 2) { - let left = currentLevel[i]; - let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; - nextLevel.push(generateHash(left + right)); - } - tree.unshift(nextLevel); - } - return tree; -} - -// Function to verify data integrity -function verifyIntegrity(originalTree, tamperedBlockIndex, tamperedBlock) { - // Tamper with the block - let tamperedLeafNodes = originalTree[originalTree.length - 1].slice(); - tamperedLeafNodes[tamperedBlockIndex] = generateHash(tamperedBlock); - - // Rebuilds the tree with the tampered block - let tamperedTree = [tamperedLeafNodes]; - while (tamperedTree[0].length > 1) { - let currentLevel = tamperedTree[0]; - let nextLevel = []; - for (let i = 0; i < currentLevel.length; i += 2) { - let left = currentLevel[i]; - let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; - nextLevel.push(generateHash(left + right)); - } - tamperedTree.unshift(nextLevel); - } - - return tamperedTree; -} - -// Main function to demonstrate the Merkle Tree -function main() { - const blocks = ["Block1", "Block2", "Block3", "Block4"]; - console.log("Original Blocks:", blocks); - - const merkleTree = createMerkleTree(blocks); - console.log("Merkle Tree:", merkleTree); - console.log("Root Hash:", merkleTree[0][0]); - - // Tamper with a block (change "Block3" to "TamperedBlock3") - const tamperedBlockIndex = 2; - const tamperedBlock = "TamperedBlock3"; - console.log( - `\nTampering with Block ${tamperedBlockIndex + 1}: "${ - blocks[tamperedBlockIndex] - }" -> "${tamperedBlock}"` - ); - - // Verify integrity - const tamperedTree = verifyIntegrity( - merkleTree, - tamperedBlockIndex, - tamperedBlock - ); - console.log("Tampered Merkle Tree:", tamperedTree); - console.log("New Root Hash:", tamperedTree[0][0]); - - // Check if the root hash changed - if (merkleTree[0][0] === tamperedTree[0][0]) { - console.log( - "Integrity Check: Root hash did NOT change. Data integrity is compromised!" - ); - } else { - console.log( - "Integrity Check: Root hash changed. Data integrity is maintained!" - ); - } -} - -main(); +// Import the crypto module for hashing +const crypto = require("crypto"); + +// Helper function to generate SHA-256 hash +function generateHash(data) { + return crypto.createHash("sha256").update(data).digest("hex"); +} + +// Function to create a Merkle Tree +function createMerkleTree(blocks) { + // Generate leaf nodes (hashes of the blocks) + let leafNodes = blocks.map((block) => generateHash(block)); + + // Build the tree + let tree = [leafNodes]; + while (tree[0].length > 1) { + let currentLevel = tree[0]; + let nextLevel = []; + for (let i = 0; i < currentLevel.length; i += 2) { + let left = currentLevel[i]; + let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; + nextLevel.push(generateHash(left + right)); + } + tree.unshift(nextLevel); + } + return tree; +} + +// Function to verify data integrity +function verifyIntegrity(originalTree, tamperedBlockIndex, tamperedBlock) { + // Tamper with the block + let tamperedLeafNodes = originalTree[originalTree.length - 1].slice(); + tamperedLeafNodes[tamperedBlockIndex] = generateHash(tamperedBlock); + + // Rebuilds the tree with the tampered block + let tamperedTree = [tamperedLeafNodes]; + while (tamperedTree[0].length > 1) { + let currentLevel = tamperedTree[0]; + let nextLevel = []; + for (let i = 0; i < currentLevel.length; i += 2) { + let left = currentLevel[i]; + let right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left; + nextLevel.push(generateHash(left + right)); + } + tamperedTree.unshift(nextLevel); + } + + return tamperedTree; +} + +// Main function to demonstrate the Merkle Tree +function main() { + const blocks = ["Block1", "Block2", "Block3", "Block4"]; + console.log("Original Blocks:", blocks); + + const merkleTree = createMerkleTree(blocks); + console.log("Merkle Tree:", merkleTree); + console.log("Root Hash:", merkleTree[0][0]); + + // Tamper with a block (change "Block3" to "TamperedBlock3") + const tamperedBlockIndex = 2; + const tamperedBlock = "TamperedBlock3"; + console.log( + `\nTampering with Block ${tamperedBlockIndex + 1}: "${ + blocks[tamperedBlockIndex] + }" -> "${tamperedBlock}"` + ); + + // Verify integrity + const tamperedTree = verifyIntegrity( + merkleTree, + tamperedBlockIndex, + tamperedBlock + ); + console.log("Tampered Merkle Tree:", tamperedTree); + console.log("New Root Hash:", tamperedTree[0][0]); + + // Check if the root hash changed + if (merkleTree[0][0] === tamperedTree[0][0]) { + console.log( + "Integrity Check: Root hash did NOT change. Data integrity is compromised!" + ); + } else { + console.log( + "Integrity Check: Root hash changed. Data integrity is maintained!" + ); + } +} + +main(); diff --git a/submissions/week-3/day-2/Semilore-White-Merkle-Tree/summary.md b/submissions/week-3/day-2/Semilore-White-Merkle-Tree/summary.md index 60af418..284ec0d 100644 --- a/submissions/week-3/day-2/Semilore-White-Merkle-Tree/summary.md +++ b/submissions/week-3/day-2/Semilore-White-Merkle-Tree/summary.md @@ -1,3 +1,3 @@ -# Summary - -A Merkle Tree is a data structure used in blockchain to efficiently and securely verify the contents of large datasets. It consists of leaf nodes, which are hashes of individual data blocks, and non-leaf nodes, which are hashes of their child nodes. The topmost node is called the root hash. Merkle Trees are important in blockchain because they allow for quick and secure verification of data integrity without needing to check the entire dataset. +# Summary + +A Merkle Tree is a data structure used in blockchain to efficiently and securely verify the contents of large datasets. It consists of leaf nodes, which are hashes of individual data blocks, and non-leaf nodes, which are hashes of their child nodes. The topmost node is called the root hash. Merkle Trees are important in blockchain because they allow for quick and secure verification of data integrity without needing to check the entire dataset. diff --git a/submissions/week-3/day-2/Semilore-White-Merkle-Tree/verification.md b/submissions/week-3/day-2/Semilore-White-Merkle-Tree/verification.md index 2687736..6dbbbdd 100644 --- a/submissions/week-3/day-2/Semilore-White-Merkle-Tree/verification.md +++ b/submissions/week-3/day-2/Semilore-White-Merkle-Tree/verification.md @@ -1,3 +1,3 @@ -# Verification - -If one of the data blocks, such as Block3, is tampered with, the hash of Block3 will change. This change will propagate up the Merkle Tree, altering the non-leaf node hashes and ultimately changing the root hash. This is important for data integrity in blockchain because any change in the data will be immediately detectable by comparing the root hash, ensuring the data remains secure and unaltered. +# Verification + +If one of the data blocks, such as Block3, is tampered with, the hash of Block3 will change. This change will propagate up the Merkle Tree, altering the non-leaf node hashes and ultimately changing the root hash. This is important for data integrity in blockchain because any change in the data will be immediately detectable by comparing the root hash, ensuring the data remains secure and unaltered. diff --git a/tasks/Example/Day-5-task.md b/tasks/Example/Day-5-task.md index 89a0aae..4074c86 100644 --- a/tasks/Example/Day-5-task.md +++ b/tasks/Example/Day-5-task.md @@ -1,63 +1,63 @@ -# Task: Understanding Merkle Trees - -## Objective - -The goal of this task is to help you understand the basic concept of a Merkle Tree, its structure, and its importance in blockchain. - -## Background - -A Merkle Tree is a fundamental data structure used in blockchain systems to efficiently and securely verify the contents of large datasets. In a Merkle Tree, each leaf node represents a hash of a data block, and each non-leaf node represents a hash of its child nodes. - -## Task Instructions - -### Part 1: Understanding the Basics - -1. **Research**: - - - Look up the definition of a Merkle Tree. - - Understand the terms: leaf nodes, non-leaf nodes, root hash, and hash function. - - Find out why Merkle Trees are important in blockchain. - -2. **Summarize**: - - Write a short paragraph (100-150 words) explaining what a Merkle Tree is and why it is used in blockchain. - -### Part 2: Building a Simple Merkle Tree - -1. **Data Preparation**: - - - Create a list of 4 data blocks (e.g., strings like "Block1", "Block2", "Block3", "Block4"). - -2. **Hashing**: - - - Use a simple hash function (e.g., SHA-256) to hash each data block. These hashes will be your leaf nodes. - -3. **Construct the Tree**: - - - Combine the hashes of the first two blocks and hash the result to create the first non-leaf node. - - Repeat the process for the next two blocks. - - Finally, combine the two non-leaf node hashes and hash the result to create the root hash. - -4. **Visual Representation**: - - Draw a simple diagram of your Merkle Tree, labeling all nodes (leaf and non-leaf) with their corresponding hashes. - -### Part 3: Verification - -1. **Verify Integrity**: - - - Assume one of the data blocks (e.g., "Block3") has been tampered with. Show how the root hash changes and explain why this is important for data integrity in blockchain. - -2. **Write-Up**: - - Write a short explanation (100-150 words) on how Merkle Trees help in verifying data integrity in blockchain systems. - -## Deadline - -- Submit your completed task by 12PM Tomorrow. - -## Resources - -- [Merkle Tree Wikipedia](https://en.wikipedia.org/wiki/Merkle_tree) -- [Blockchain Basics: Merkle Trees](https://www.investopedia.com/terms/m/merkle-tree.asp) - ---- - -Good luck! +# Task: Understanding Merkle Trees + +## Objective + +The goal of this task is to help you understand the basic concept of a Merkle Tree, its structure, and its importance in blockchain. + +## Background + +A Merkle Tree is a fundamental data structure used in blockchain systems to efficiently and securely verify the contents of large datasets. In a Merkle Tree, each leaf node represents a hash of a data block, and each non-leaf node represents a hash of its child nodes. + +## Task Instructions + +### Part 1: Understanding the Basics + +1. **Research**: + + - Look up the definition of a Merkle Tree. + - Understand the terms: leaf nodes, non-leaf nodes, root hash, and hash function. + - Find out why Merkle Trees are important in blockchain. + +2. **Summarize**: + - Write a short paragraph (100-150 words) explaining what a Merkle Tree is and why it is used in blockchain. + +### Part 2: Building a Simple Merkle Tree + +1. **Data Preparation**: + + - Create a list of 4 data blocks (e.g., strings like "Block1", "Block2", "Block3", "Block4"). + +2. **Hashing**: + + - Use a simple hash function (e.g., SHA-256) to hash each data block. These hashes will be your leaf nodes. + +3. **Construct the Tree**: + + - Combine the hashes of the first two blocks and hash the result to create the first non-leaf node. + - Repeat the process for the next two blocks. + - Finally, combine the two non-leaf node hashes and hash the result to create the root hash. + +4. **Visual Representation**: + - Draw a simple diagram of your Merkle Tree, labeling all nodes (leaf and non-leaf) with their corresponding hashes. + +### Part 3: Verification + +1. **Verify Integrity**: + + - Assume one of the data blocks (e.g., "Block3") has been tampered with. Show how the root hash changes and explain why this is important for data integrity in blockchain. + +2. **Write-Up**: + - Write a short explanation (100-150 words) on how Merkle Trees help in verifying data integrity in blockchain systems. + +## Deadline + +- Submit your completed task by 12PM Tomorrow. + +## Resources + +- [Merkle Tree Wikipedia](https://en.wikipedia.org/wiki/Merkle_tree) +- [Blockchain Basics: Merkle Trees](https://www.investopedia.com/terms/m/merkle-tree.asp) + +--- + +Good luck! diff --git a/tasks/week-1/Day-2-task.md b/tasks/week-1/Day-2-task.md index 88ff0d6..22ad1cd 100644 --- a/tasks/week-1/Day-2-task.md +++ b/tasks/week-1/Day-2-task.md @@ -1,30 +1,30 @@ -# Task 1: Study Chapters of The Rust Book - -## Objective - -Study the **remaining of Chapter 3** and **all of Chapter 4** of [The Rust Programming Language](https://doc.rust-lang.org/book/) and: - -- Summarize what you understand. -- List what you do **not** understand. - -This way, we can all **collaboratively share knowledge** and **identify our gaps**. - ---- - -# Task 2: Basic Arithmetic in Rust - -## Objective - -Write a Rust program with separate functions to: - -- Add two numbers -- Subtract two numbers -- Multiply two numbers -- Divide two numbers -- Run the program at once so you can get all the results and find a good way to distinguish the 4 function call outputs. -- Then you should attach a screenshot of the printed version, just one image containing the result of the logs of that function. -- Attach the screenshot to the code and Push your code to github also, don't submit screenshot alone. - -Then call each of them in `main()` with values and run the program using `cargo run`. - ---- +# Task 1: Study Chapters of The Rust Book + +## Objective + +Study the **remaining of Chapter 3** and **all of Chapter 4** of [The Rust Programming Language](https://doc.rust-lang.org/book/) and: + +- Summarize what you understand. +- List what you do **not** understand. + +This way, we can all **collaboratively share knowledge** and **identify our gaps**. + +--- + +# Task 2: Basic Arithmetic in Rust + +## Objective + +Write a Rust program with separate functions to: + +- Add two numbers +- Subtract two numbers +- Multiply two numbers +- Divide two numbers +- Run the program at once so you can get all the results and find a good way to distinguish the 4 function call outputs. +- Then you should attach a screenshot of the printed version, just one image containing the result of the logs of that function. +- Attach the screenshot to the code and Push your code to github also, don't submit screenshot alone. + +Then call each of them in `main()` with values and run the program using `cargo run`. + +--- diff --git a/tasks/week-2/day-3/1.md b/tasks/week-2/day-3/1.md index 2345edc..4795a95 100644 --- a/tasks/week-2/day-3/1.md +++ b/tasks/week-2/day-3/1.md @@ -1,22 +1,22 @@ -# Topic: User Input - -## Requirements - -- Verify user input against pre-defined keywords. -- The keywords represent possible power options for a computer: - - Off - - Sleep - - Reboot - - Shutdown - - Hibernate -- If the user enters one of the keywords, print a message to the console indicating which action will be taken. - - **Example:** If the user types `"shutdown"`, display a message such as **"shutting down"**. -- If the entered keyword does not exist, display an appropriate error message. - -## Notes - -- Use an `enum` to store the possible power states. -- Use a function with a `match` expression to print out the power messages. - - The function should accept the enum as an input. -- Use a `match` expression to convert the user input into the power state enum. -- The program should be **case-insensitive**. +# Topic: User Input + +## Requirements + +- Verify user input against pre-defined keywords. +- The keywords represent possible power options for a computer: + - Off + - Sleep + - Reboot + - Shutdown + - Hibernate +- If the user enters one of the keywords, print a message to the console indicating which action will be taken. + - **Example:** If the user types `"shutdown"`, display a message such as **"shutting down"**. +- If the entered keyword does not exist, display an appropriate error message. + +## Notes + +- Use an `enum` to store the possible power states. +- Use a function with a `match` expression to print out the power messages. + - The function should accept the enum as an input. +- Use a `match` expression to convert the user input into the power state enum. +- The program should be **case-insensitive**. diff --git a/tasks/week-2/day-5/assignment.md b/tasks/week-2/day-5/assignment.md index 3e2fe0b..58f7e21 100644 --- a/tasks/week-2/day-5/assignment.md +++ b/tasks/week-2/day-5/assignment.md @@ -1,14 +1,14 @@ -# Assignment - -### Complete the expense tracker project - -## Requirements - -- Print out operations a user can carry out -- Ensure that the program keeps running even after reading, adding, updating or deleting. -- User can quit the program with 'q' and should be prompted y/n -- After updating or adding the user should be able to see the values they just added. -- At the end of the operation all interactions should be written to a file. -- Abstract into modules - ---- +# Assignment + +### Complete the expense tracker project + +## Requirements + +- Print out operations a user can carry out +- Ensure that the program keeps running even after reading, adding, updating or deleting. +- User can quit the program with 'q' and should be prompted y/n +- After updating or adding the user should be able to see the values they just added. +- At the end of the operation all interactions should be written to a file. +- Abstract into modules + +---