Skip to content

feat: add arithmetic operations #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: task-3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ target
snfoundry_trace/
coverage/
profile/
.DS_Store
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,31 @@
# Cairo Bootcamp 4.0
# 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
```














20 changes: 1 addition & 19 deletions Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
51 changes: 7 additions & 44 deletions Scarb.toml
Original file line number Diff line number Diff line change
@@ -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"
34 changes: 34 additions & 0 deletions assignments/task_3.md
Original file line number Diff line number Diff line change
@@ -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

79 changes: 25 additions & 54 deletions src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,54 +1,25 @@
/// Interface representing `HelloContract`.
/// This interface allows modification and retrieval of the contract count.
#[starknet::interface]
pub trait ICounter<TContractState> {
/// 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;
}

/// 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<ContractState> {
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);
}


fn decrease_count(ref self: ContractState, amount: felt252) {
assert(amount != 0, 'Amount cannot be 0');
self.count.write(self.count.read() - amount);
}

fn decrease_count_by_one(ref self: ContractState) {
self.count.write(self.count.read() - 1);
}

fn get_count(self: @ContractState) -> felt252 {
self.count.read()
}
}
}
mod mod_math;
use mod_math::ModMath;

fn main() {
let a = 21;
let b = 4;
let sub = ModMath::subtract_numbers(a, b);
let mul = ModMath::multiply_numbers(a, b);
let div = ModMath::divide_numbers(a, b);
let even_or_odd = ModMath::even_or_odd_sum(a, b);
let first_odd_num = ModMath::first_num_is_odd(a);
let second_odd_num = ModMath::second_num_is_odd(b);
let positive_num = ModMath::positive_number(a);
let max_num = ModMath::num_is_max(a,b);

//Output
println!("Subtraction: {}", sub);
println!("Multiplication: {}", mul);
println!("Division: {}", div);
println!("Sum_is_Even:{}", even_or_odd);
println!("First_Number_is_Odd:{}", first_odd_num);
println!("Second_Number_is_Odd:{}", second_odd_num);
println!("Positive_Number:{}", positive_num);
println!("Maximum_Number:{}", max_num);
}
61 changes: 61 additions & 0 deletions src/mod_math.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
pub mod ModMath {
//subtraction function
pub fn subtract_numbers(a: u8, b: u8) -> u8 {
return a - b;
}

//multiplication function
pub fn multiply_numbers(a: u8, b: u8) -> u8 {
return a * b;
}
//division function
pub fn divide_numbers(a: u8, b: u8) -> u8 {
if a / b == 0 {
return 0;
} else {
return a / b;
}
}
//function to check if sum is even or odd
pub fn even_or_odd_sum(a: u8, b: u8) -> bool {

let sum = a + b;
if (sum % 2 == 0)
{return true;
}else{
return false;
}

}

//function to check if first number is odd
pub fn first_num_is_odd(a: u8) -> bool {
if a % 2 == 0 {
return false;
} else {
return true;
}
}

pub fn second_num_is_odd(b: u8) -> bool {
if b % 2 == 0 {
return false;
} else {
return true;
}
}

//function to check for positive sign
pub fn positive_number(a: u8) -> bool {
if a > 0 {return true;}
else {return false;}
}

//function to check for maximum number
pub fn num_is_max(a: u8, b: u8) -> u8 {
if a > b {return a;} else
{return b;}

}
}