Skip to content

ismael6499/evm-arithmetic-safety

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🛡️ EVM Arithmetic Safety & Logic Encapsulation

Solidity License

A reference implementation exploring arithmetic safety within the EVM, focusing on mixed-type operations (Signed vs. Unsigned), gas-efficient validation patterns, and strict logic encapsulation.

Unlike high-level languages where arithmetic is abstracted, Solidity requires rigorous handling of storage slots and overflow protection. This project serves as a pattern for handling int256 state changes and enforcing "Fail Early" mechanisms.

🏗 Architecture & Patterns

1. Type Safety (Signed vs. Unsigned)

  • Mixed State Strategies: The contract explicitly handles int256 for operations that may result in negative balances or values (Subtraction/Division), demonstrating awareness of EVM storage behavior for signed integers vs. standard uint256 usage.
  • Underflow/Overflow Protection: Leverages Solidity 0.8.x built-in overflow checks while maintaining explicit logic for business-rule constraints.

2. Gas Optimization

  • Custom Errors vs Strings: Replaced standard require(condition, "Message") with custom error DivisionByZero() and error MaxPowerExceeded().
    • Impact: Reduces bytecode size and runtime gas costs during revert operations.
  • Fail-Early Modifiers: Implementation of declarative validation (checkMaxPowerNumber) to decouple security checks from core business logic, ensuring transactions revert before executing heavy arithmetic opcodes.

3. Encapsulation & Observability

  • Internal Logic Separation: Core logic (e.g., _subtractionLogic) is isolated in internal pure functions, reducing the attack surface of the public API.
  • Event Logging: Emission of indexed events (Addition, Subtraction) to allow off-chain indexers (The Graph) to track state changes without querying contract storage.

🛠 Tech Stack

  • Language: Solidity ^0.8.24
  • Features: Custom Errors, Modifiers, Event Logging
  • License: LGPL-3.0-only

📝 Contract Interface

The system exposes a secured API for arithmetic operations:

// Example of secured entry point
function power(uint256 _num1) external checkMaxPowerNumber(_num1) {
    result = result ** _num1;
}

Reference implementation for secure arithmetic handling on Ethereum.

About

EVM arithmetic safety patterns reference implementation. Demonstrates signed integer handling, gas-optimized validation using custom errors, and logic encapsulation. Built with Solidity 0.8.24.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors