From 627552b611764feadfefe22be68bea0c996d2bc8 Mon Sep 17 00:00:00 2001 From: savagechucks Date: Wed, 29 Apr 2026 23:14:20 +0100 Subject: [PATCH 1/2] feat: Submission of assignment --- cairo_program/src/integer.cairo | 14 ++++++++++---- cairo_program/src/lib.cairo | 4 ++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/cairo_program/src/integer.cairo b/cairo_program/src/integer.cairo index 9501eca..3fa56a4 100644 --- a/cairo_program/src/integer.cairo +++ b/cairo_program/src/integer.cairo @@ -1,8 +1,8 @@ #[executable] fn main() { - let result: u8 = add_num(5, 6); - println!("the sum of x & y is: {}", result); - assert(result == 11, 'invalid sum logic'); + let add_result: u8 = add_num(5, 6); + println!("the sum of x & y is: {}", add_result); + assert(add_result == 11, 'invalid sum logic'); let sub_result: u8 = sub_num(10, 5); println!("sub result is: {}", sub_result); @@ -11,10 +11,16 @@ fn main() { // addition logic fn add_num(x: u8, y: u8) -> u8 { - x + y + return x + y; } // subtraction logic fn sub_num(x: u8, y: u8) -> u8 { + // logic for returning negatgive value + if x > y{ + return + } return x - y; } + + diff --git a/cairo_program/src/lib.cairo b/cairo_program/src/lib.cairo index acc7644..b1c3620 100644 --- a/cairo_program/src/lib.cairo +++ b/cairo_program/src/lib.cairo @@ -1,5 +1,5 @@ // mod hello_world; // mod short_string; -// mod integer; +mod integer; // mod bool; -mod bytearray; \ No newline at end of file +// mod bytearray; \ No newline at end of file From bdbe090db87b0242cdbaa5ab1df6f0bda799dfc6 Mon Sep 17 00:00:00 2001 From: savagechucks Date: Mon, 18 May 2026 10:52:29 +0100 Subject: [PATCH 2/2] feat: Isaac Submission of Assignment --- cairo_program/src/bool.cairo | 1 - cairo_program/src/integer.cairo | 37 +++++++++++++++++++++---- starknet_contracts/Scarb.toml | 5 ++++ starknet_contracts/src/arithmetic.cairo | 28 +++++++++++++++++++ starknet_contracts/src/lib.cairo | 33 ++++++++++++++++++++-- 5 files changed, 96 insertions(+), 8 deletions(-) create mode 100644 starknet_contracts/src/arithmetic.cairo diff --git a/cairo_program/src/bool.cairo b/cairo_program/src/bool.cairo index cafad1e..e8d4f47 100644 --- a/cairo_program/src/bool.cairo +++ b/cairo_program/src/bool.cairo @@ -18,7 +18,6 @@ fn is_adult(x: u8) -> bool { return true; } - // determine even numbers fn is_even(x: u8) -> bool { if x % 2 == 0 { diff --git a/cairo_program/src/integer.cairo b/cairo_program/src/integer.cairo index 3fa56a4..36555f2 100644 --- a/cairo_program/src/integer.cairo +++ b/cairo_program/src/integer.cairo @@ -1,3 +1,5 @@ +use core::num::traits::OverflowingMul; + #[executable] fn main() { let add_result: u8 = add_num(5, 6); @@ -7,6 +9,20 @@ fn main() { let sub_result: u8 = sub_num(10, 5); println!("sub result is: {}", sub_result); assert(sub_result == 5, 'invalid sub logic'); + + // Test subtraction that should panic (10 - 15 = negative) + // sub_num(10, 15); // This would panic + + let mul_result: u8 = mul_num(255, 1); + println!("mul result is: {}", mul_result); + assert(mul_result == 255, 'invalid mul logic'); + + let div_result: u8 = div_num(20, 0); + println!("div result is: {}", div_result); + assert(div_result == 5, 'invalid div logic'); + + // Test division by zero - should panic + // div_num(10, 0); // This would panic } // addition logic @@ -16,11 +32,22 @@ fn add_num(x: u8, y: u8) -> u8 { // subtraction logic fn sub_num(x: u8, y: u8) -> u8 { - // logic for returning negatgive value - if x > y{ - return - } - return x - y; + assert!(!(y > x), "negative result not allowed"); + x - y } +// multiplication logic +fn mul_num(x: u8, y: u8) -> u8 { + let (result_, overflowed_) = x.overflowing_mul(y); + + assert!(!(overflowed_), "multiplication overflowed"); + + result_ +} + +// division logic with division by zero check +fn div_num(x: u8, y: u8) -> u8 { + assert!(!(y == 0), "Cannot be Zero"); + x / y +} diff --git a/starknet_contracts/Scarb.toml b/starknet_contracts/Scarb.toml index 29cf20f..b24c100 100644 --- a/starknet_contracts/Scarb.toml +++ b/starknet_contracts/Scarb.toml @@ -5,8 +5,13 @@ edition = "2024_07" # See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html + [dependencies] starknet = "2.18.0" +cairo_execute = "2.18.0" + +[cairo] +enable-gas = true [dev-dependencies] snforge_std = "0.56.0" diff --git a/starknet_contracts/src/arithmetic.cairo b/starknet_contracts/src/arithmetic.cairo new file mode 100644 index 0000000..939782b --- /dev/null +++ b/starknet_contracts/src/arithmetic.cairo @@ -0,0 +1,28 @@ +use core::num::traits::OverflowingMul; + +// addition logic +pub fn add_num(x: u32, y: u32) -> u32 { + return x + y; +} + +// subtraction logic +fn sub_num(x: u8, y: u8) -> u8 { + assert!(!(y > x), "negative result not allowed"); + x - y +} + +// multiplication logic +fn mul_num(x: u8, y: u8) -> u8 { + let (result_, overflowed_) = x.overflowing_mul(y); + + assert!(!(overflowed_), "multiplication overflowed"); + + result_ +} + +// division logic with division by zero check +fn div_num(x: u8, y: u8) -> u8 { + assert!(!(y == 0), "Cannot be Zero"); + x / y +} + diff --git a/starknet_contracts/src/lib.cairo b/starknet_contracts/src/lib.cairo index f1b82bb..02257ad 100644 --- a/starknet_contracts/src/lib.cairo +++ b/starknet_contracts/src/lib.cairo @@ -1,5 +1,7 @@ /// Interface representing `HelloContract`. /// This interface allows modification and retrieval of the contract's storage count. + +mod arithmetic; #[starknet::interface] pub trait ICounter { /// Increase count. @@ -10,21 +12,48 @@ pub trait ICounter { /// Simple contract for managing count. #[starknet::contract] -mod Counter { +pub mod Counter { use starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess}; + use starknet::{ContractAddress, get_caller_address}; + use super::arithmetic::{add_num}; #[storage] struct Storage { count: u32, + owner: ContractAddress, + } + + #[constructor] + fn constructor(ref self: ContractState, owner: ContractAddress) { + self.owner.write(owner); // no .into() needed } + } + + #[generate_trait] + impl PrivateImpl of PrivateTrait { + fn assert_only_owner(self: @ContractState ) { + assert(get_caller_address() == self.owner.read(), 'Caller not the Owner'); + } } + + #[abi(embed_v0)] impl CounterImpl of super::ICounter { fn increase_count(ref self: ContractState, amount: u32) { + + // Only owner + self.assert_only_owner(); assert(amount != 0, 'Amount cannot be 0'); - self.count.write(self.count.read() + amount); + + // Read current count and add the amount + let current_count = self.count.read(); + let new_count = add_num(current_count, amount); + self.count.write(new_count); + + assert(new_count == current_count + amount, 'Count increase failed'); } + fn get_count(self: @ContractState) -> u32 { self.count.read() }