Starknet and Solidity: a comparison on safety

Check out Nethermind’s article titled: “Cairo: the Starknet way to writing safe code”

TLDR

  • Language Design & Syntax:

    • Solidity: Influenced by C++ and JavaScript, Solidity is object-oriented and designed for creating Ethereum smart-contracts. It’s the language you find on the L1 and in L2 rollups such as Arbitrum, Scroll or Linea.
    • Cairo: Influenced by Rust, Cairo uses a functional programming style that emphasizes immutability and statelessness. It is the programming language for smart-contracts on Starknet. It is also meant for writing provable programs and goes beyond blockchain applications.
  • Storage Handling:

    • Solidity: Uses linear, incrementally assigned storage slots. Devs must manage storage carefully, especially in upgradable contracts, to avoid overwriting existing data.
    • Cairo: Uses a hashing mechanism to determine storage slots, which reduces the risks of collision and makes the management of storage straightforward and safer in upgradable contracts.
  • Native Currency Handling:

    • Solidity: Supports payable functions that directly handle Ethereum’s native currency, ETH.
    • Cairo: Does not have native currency support. All transactions (including network fees) are handled via tokens, simplifying contract logic and potentially reducing security risks related to currency transactions.
  • Upgradeability:

    • Solidity: Commonly uses proxy patterns for contract upgrades, which can be complex.
    • Cairo: Uses a “class hash” system where contracts point to shared logic via hashes, simplifying upgrades and reducing the likelihood of errors during upgrade transactions.
  • Type Safety:

    • Solidity: Allows explicit type conversions which can lead to data loss if not handled carefully.
    • Cairo: Uses safe type conversions, using constructs like try_into to prevent unintended data truncation.
  • Default Variable Mutability:

    • Solidity: Variables are mutable by default unless explicitly declared as constant or immutable.
    • Cairo: Adopts Rust’s approach where variables are immutable by default, promoting safer and clearer code, especially in a multi-contract environment.
1 Like