From 0b144d66149a02e8409d74fc5b3c479ba3d634fb Mon Sep 17 00:00:00 2001 From: sprtd Date: Fri, 28 Mar 2025 17:51:37 +0100 Subject: [PATCH 1/2] chore: add task-3 --- .gitignore | 1 + README.md | 32 ++++++++++++++- Scarb.lock | 20 +--------- Scarb.toml | 51 ++++-------------------- assignments/task_3.md | 34 ++++++++++++++++ src/lib.cairo | 90 +++++++++++++++++++++---------------------- 6 files changed, 118 insertions(+), 110 deletions(-) create mode 100644 assignments/task_3.md diff --git a/.gitignore b/.gitignore index 4096f8b..98cbead 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ target snfoundry_trace/ coverage/ profile/ +.DS_Store \ No newline at end of file diff --git a/README.md b/README.md index 11db7dc..ed74a51 100644 --- a/README.md +++ b/README.md @@ -1 +1,31 @@ -# Cairo Bootcamp 4.0 \ No newline at end of file +# Cairo Bootcamp 4.0 + +## Setting Up Starknet Dev Environment with Starkup +- Cairo toolchain installation - tool for installing all the Starknet essentials for development. +- Starkup supports the installation of the following tools: + - Scarb - the Cairo package manager + - Starknet Foundry - the Cairo and Starknet testing framework + - Universal Sierra Compiler - compiler for any ever-existing Sierra version + - Cairo Profiler - profiler for Cairo programming language & Starknet + - Cairo Coverage - utility for coverage reports generation for code written in Cairo programming language + - CairoLS - vscode extension + - [Starkup Github link](https://github.com/software-mansion/starkup) + +Install `starkup` using: +```sh +curl --proto '=https' --tlsv1.2 -sSf https://sh.starkup.sh | sh +``` + + + + + + + + + + + + + + diff --git a/Scarb.lock b/Scarb.lock index f174ad1..0448917 100644 --- a/Scarb.lock +++ b/Scarb.lock @@ -2,23 +2,5 @@ version = 1 [[package]] -name = "session_1" +name = "cohort_4" version = "0.1.0" -dependencies = [ - "snforge_std", -] - -[[package]] -name = "snforge_scarb_plugin" -version = "0.39.0" -source = "registry+https://scarbs.xyz/" -checksum = "sha256:61ae2333de26d9e6e4b535916f8da7916bfd95c951c765c6fbd36038289a636c" - -[[package]] -name = "snforge_std" -version = "0.39.0" -source = "registry+https://scarbs.xyz/" -checksum = "sha256:31f118f54e5d87393e50c07e8c052f03ff4af945a2b65b56e6edbd3b5b7fd127" -dependencies = [ - "snforge_scarb_plugin", -] diff --git a/Scarb.toml b/Scarb.toml index bf64623..dcbf065 100644 --- a/Scarb.toml +++ b/Scarb.toml @@ -1,52 +1,15 @@ [package] -name = "session_1" +name = "cohort_4" version = "0.1.0" edition = "2024_07" # See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html - +# [executable] [dependencies] -starknet = "2.11.2" - -[dev-dependencies] -snforge_std = "0.39.0" -assert_macros = "2.11.2" - -[[target.starknet-contract]] -sierra = true - -[scripts] -test = "snforge test" - -[tool.scarb] -allow-prebuilt-plugins = ["snforge_std"] -# Visit https://foundry-rs.github.io/starknet-foundry/appendix/scarb-toml.html for more information +[cairo] +enable-gas = false -# [tool.snforge] # Define `snforge` tool section -# exit_first = true # Stop tests execution immediately upon the first failure -# fuzzer_runs = 1234 # Number of runs of the random fuzzer -# fuzzer_seed = 1111 # Seed for the random fuzzer - -# [[tool.snforge.fork]] # Used for fork testing -# name = "SOME_NAME" # Fork name -# url = "http://your.rpc.url" # Url of the RPC provider -# block_id.tag = "latest" # Block to fork from (block tag) - -# [[tool.snforge.fork]] -# name = "SOME_SECOND_NAME" -# url = "http://your.second.rpc.url" -# block_id.number = "123" # Block to fork from (block number) - -# [[tool.snforge.fork]] -# name = "SOME_THIRD_NAME" -# url = "http://your.third.rpc.url" -# block_id.hash = "0x123" # Block to fork from (block hash) - -# [profile.dev.cairo] # Configure Cairo compiler -# unstable-add-statements-code-locations-debug-info = true # Should be used if you want to use coverage -# unstable-add-statements-functions-debug-info = true # Should be used if you want to use coverage/profiler -# inlining-strategy = "avoid" # Should be used if you want to use coverage - -# [features] # Used for conditional compilation -# enable_for_tests = [] # Feature name and list of other features that should be enabled with it +[dev-dependencies] +cairo_test = "2.11.2" +# cairo_execute = "2.11.3" diff --git a/assignments/task_3.md b/assignments/task_3.md new file mode 100644 index 0000000..df4c52d --- /dev/null +++ b/assignments/task_3.md @@ -0,0 +1,34 @@ +## Cairo Assignment 1 + +## Objective + +This assignment aims to help students understand basic arithmetic operations and logical conditions in Cairo covering the primitive data types. Each function should be modular, reusable, and well-commented. + +## Tasks +Implement the following: +### 1. Arithmetic Operations + - **Subtraction:** performs subtraction of two numbers. + - **Multiplication:** performs multiplication of two numbers. + - **Division:** performs division of two numbers (ensure handling of division by zero). + +### 2. Even or Odd Sum + - Implement a function that takes two arguments, computes their sum, and determines whether the sum is even or odd. + +### 3. Odd Number Checker + - Implement a function that checks if a given number is odd. + +### 4. Basic Logic Functions + - **Number Sign Checker:** Implement a function to check if a number is positive, negative, or zero. + - **Maximum of Two Numbers:** Implement a function to return the maximum of two numbers. + + +## Guidelines +- Each function should be modular and well-commented. +- Use meaningful variable names and snake_casing +- Ensure type safety by specifying argument and return types correctly. +- Handle edge cases such as division by zero. +- Test your functions within the `main` function before submission such that it logs the appropriate `stdout` on the terminal + +## Submission +Submit your completed Cairo code by creating a PR + diff --git a/src/lib.cairo b/src/lib.cairo index e43a81d..2861ea0 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -1,54 +1,52 @@ -/// Interface representing `HelloContract`. -/// This interface allows modification and retrieval of the contract count. -#[starknet::interface] -pub trait ICounter { - /// Increase contract count. - fn increase_count(ref self: TContractState, amount: felt252); - /// Increase contract count by one - fn increase_count_by_one(ref self: TContractState); - - /// Decrease contract count. - fn decrease_count(ref self: TContractState, amount: felt252); - - /// Decrease contract count. - fn decrease_count_by_one(ref self: TContractState); - /// Retrieve contract count. - fn get_count(self: @TContractState) -> felt252; +fn main() { + // Function calls (Uncomment to execute them) + // say_name("Sylvia Nnoruka!"); + // intro_to_felt(); + + let num_1 = 5; + let num_2 = 10; + let sum = sum_num(num_1, num_2); + println!("The sum of {} and {} is = {}", num_1, num_2, sum); + + // check_u16(6553); // Uncomment if needed + is_greater_than_50(3); } -/// Simple contract for managing count. -#[starknet::contract] -mod Counter { - use starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess}; - - #[storage] - struct Storage { - count: felt252, - } - - #[abi(embed_v0)] - impl CounterImpl of super::ICounter { - fn increase_count(ref self: ContractState, amount: felt252) { - assert(amount != 0, 'Amount cannot be 0'); - self.count.write(self.count.read() + amount); - } - - fn increase_count_by_one(ref self: ContractState) { - self.count.write(self.count.read() + 1); - } +// DATA TYPES IN CAIRO +// - felts: felt252 (Field elements) +// - ByteArray: Represents a sequence of bytes +// - Integers: +// - Signed: i8, i16, i32, i64, i128, i256 +// - Unsigned: u8, u16, u32, u64, u128, u256 +// - Boolean: bool + +// Function to demonstrate ByteArray usage +fn say_name(x: ByteArray) { + println!("{}", x); +} +// Function to demonstrate felt252 usage +fn intro_to_felt() { + let x = 40000; + println!("{}", x); +} - fn decrease_count(ref self: ContractState, amount: felt252) { - assert(amount != 0, 'Amount cannot be 0'); - self.count.write(self.count.read() - amount); - } +// Function to sum two u8 integers +fn sum_num(x: u8, y: u8) -> u8 { + return x + y; +} - fn decrease_count_by_one(ref self: ContractState) { - self.count.write(self.count.read() - 1); - } +// Function to print a u16 integer +fn check_u16(x: u16) { + println!("{x}"); +} - fn get_count(self: @ContractState) -> felt252 { - self.count.read() - } +// Function to check if a u32 integer is greater than 50 +fn is_greater_than_50(x: u32) -> bool { + if x > 50 { + println!("true"); + return true; } + println!("false"); + return false; } From ee7d92a55e76b8c012a1bed1982deb377d7c8b4c Mon Sep 17 00:00:00 2001 From: ogbuGideon Date: Wed, 2 Apr 2025 17:07:59 +0100 Subject: [PATCH 2/2] feat: simple arithmetic and logical operations --- src/lib.cairo | 110 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 72 insertions(+), 38 deletions(-) diff --git a/src/lib.cairo b/src/lib.cairo index 2861ea0..f4fd438 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -1,52 +1,86 @@ fn main() { - // Function calls (Uncomment to execute them) - // say_name("Sylvia Nnoruka!"); - // intro_to_felt(); - - let num_1 = 5; - let num_2 = 10; - let sum = sum_num(num_1, num_2); - println!("The sum of {} and {} is = {}", num_1, num_2, sum); - - // check_u16(6553); // Uncomment if needed - is_greater_than_50(3); + let multiply = multiplication(2, 10); + let div = division(0, 2); + let checker = sign_checker(6); + let odd = is_odd(3); + let max = max_num(6, 7); + let even_or_odd = even_odd_sum(3, 6); + let sub = subtraction(10, 2); + + println!("multiplication is equal to {}", multiply); + println!("checker is equal to {}", checker); + println!("odd is equal to {}", odd); + println!("max is equal to {}", max); + println!("even_or_odd is equal to {}", even_or_odd); + println!("sub is equal to {}", sub); + + match div { + Option::Some(value) => println!("answer is {}", value), + Option::None => println!("this is an error operation"), + } } -// DATA TYPES IN CAIRO -// - felts: felt252 (Field elements) -// - ByteArray: Represents a sequence of bytes -// - Integers: -// - Signed: i8, i16, i32, i64, i128, i256 -// - Unsigned: u8, u16, u32, u64, u128, u256 -// - Boolean: bool -// Function to demonstrate ByteArray usage -fn say_name(x: ByteArray) { - println!("{}", x); +// this function subtracts between two numbers +pub fn subtraction(x: u128, y: u128) -> u128 { + return x - y; } -// Function to demonstrate felt252 usage -fn intro_to_felt() { - let x = 40000; - println!("{}", x); +// this function multipli_es two numbers +pub fn multiplication(x: i16, y: i16) -> i16 { + return x * y; } -// Function to sum two u8 integers -fn sum_num(x: u8, y: u8) -> u8 { - return x + y; +// this function divides two numbers +pub fn division(x: i16, y: i16) -> Option { + if y == 0 { + return Option::None; + } else if x == 0 { + return Option::None; + } else { + return Option::Some(x / y); + } } -// Function to print a u16 integer -fn check_u16(x: u16) { - println!("{x}"); +// thi function checks if a number is positive, negative, or equal to zero +pub fn sign_checker(x: i16) -> ByteArray { + let num = x; + if num > 0 { + return "positive"; + } else if num < 0 { + return "negative"; + } else { + return "zero"; + } } -// Function to check if a u32 integer is greater than 50 -fn is_greater_than_50(x: u32) -> bool { - if x > 50 { - println!("true"); - return true; +// this function picks the maximum number between two numbers +pub fn max_num(x: i16, y: i16) -> i16 { + if x > y { + return x; + } else { + return y; } - println!("false"); - return false; } + +// pub is used to make a mod or function public andvisible to parent functions +// pub mod even_or_odd_sum; + +// this function determines whether a value is odd or not +pub fn is_odd(x: i16) -> bool { + return x % 2 != 0; +} + + +// this function checksif the result of the sum of two numbers is even or odd +pub fn even_odd_sum(x: i16, y: i16) -> ByteArray { + let add = x + y; + let odd = is_odd(add); + let res = true; + if odd == res { + return "odd"; + } else { + return "even"; + } +} +