Summary of "Re{solve} Hackathon Workshop: Cairo Programming Language"
Summary of "Re{solve} Hackathon Workshop: Cairo Programming Language"
Overview: This workshop, led by David Barto (Developer Advocate at StarkWare) and Suton Chen, introduces the Cairo Programming Language, focusing on its general-purpose capabilities beyond just smart contracts on StarkNet. The session includes a detailed tutorial on setting up the development environment, basic programming concepts in Cairo, and testing methodologies.
Key Technological Concepts and Product Features:
- Cairo Programming Language:
- Used primarily for writing smart contracts on StarkNet but also supports general-purpose programming.
- Syntax bears resemblance to Rust but differs fundamentally in compilation and proof generation.
- Default numeric type is
felt252(field element with 252 bits), which is unique to Cairo and StarkNet's cryptographic foundations.
- Development Environment Setup:
- StarkUp: The main tool to install Cairo dependencies, including:
- ASDF: A version manager installed alongside to handle multiple Cairo versions per project without Docker.
- Installation instructions provided for Mac OS, Linux, and Windows (via WSL).
- Project Initialization and Management:
- Basic Cairo Programming Concepts:
- Functions: Defined with
fn, requiring explicit return types. - Modules: Files are modules; linked via
moddeclarations inlib.cairo. - Unit Testing: Use
#[cfg(test)]and#[test]attributes withscarb testto run tests. - Shadowing and Mutability:
- Variables immutable by default.
- Shadowing allowed by redefining variables with the same name.
- Explicit
mutkeyword enables mutability. - Constants declared with
constkeyword and uppercase naming convention.
- Functions: Defined with
- Data Types and Arithmetic:
- Unsigned integers:
u8,u16,u32,u64,u128,usize. - Signed integers:
i8,i32,i64,i128(noi256orisize). - Arithmetic operations supported with overflow and underflow protection that cause panics (tests use
#[should_panic]). - Felt252 (Field Elements):
- Default numeric type if no explicit type is given.
- Supports addition, subtraction, multiplication.
- No support for division or modulo (causes compilation errors).
- No overflow or underflow protection due to modular arithmetic nature.
- Used internally for cryptographic proofs.
- Can encode "short strings" (up to 31 ASCII characters) by packing characters into a single felt.
- Byte Arrays: For strings longer than 31 characters, supports unlimited length but only ASCII characters (no UTF-8 or accented characters).
- Unsigned integers:
- Advanced Math with Dependencies:
- Cairo supports importing external libraries via
scarbpackage manager. - Example:
alexandria_mathpackage for advanced math functions like fast exponentiation and square root. - Demonstrated adding dependencies in
scarb.tomland usingusestatements to bring functions into scope.
- Cairo supports importing external libraries via
- Gas Usage:
- Learning Resources:
- Official Cairo Book: https://www.cairo-lang.org/docs/
- Recorded 3-hour deep-dive workshop on YouTube (link shared in chat).
- Starklings: Interactive browser-based exercises for learning Cairo.
- StarkNet Base Camp: A course that uses Starklings as graduation criteria.
- Upcoming Workshop:
- Workshop #2 will focus on building smart contracts and testing with StarkNet Foundry.
Summary of the Tutorial/Guide Elements:
- Step-by-step installation of Cairo environment using StarkUp.
- Creating and configuring a new Cairo project with
scarb. - Writing simple functions, defining modules, and running unit tests.
- Explaining mutability, shadowing, and constants.
- Handling integers and felts, including
Category
Technology