smithery.ai

cairo-numeric-types

Guide implementing custom numeric types in Cairo with invariant enforcement, overflow safety, and standard trait implementations; use when implementing fixed-point, signed integers, or other numeric primitives.

First seen Apr 5, 2026

Installation

$ npx skills add https://smithery.ai

Similar popular skills

Related neighbors and high-traction skills in the same topics — useful to compare before installing.

Also in this package

Other skills from smithery.ai · top by installs.

npx skills add https://smithery.ai

Browse all from smithery.ai

More details

Agent compatibility

Declared targets from SKILL.md / docs. Unmarked agents are not listed — the skill may still install via the CLI.

Claude Code Not declared
Cursor Not declared
Codex Not declared
GitHub Copilot Not declared
Windsurf Not declared
Gemini CLI Not declared
Cline Not declared
OpenCode Not declared

Package contents

Files included with this skill beyond the listing page.

  • skill md SKILL.md 3,821 B
  • docs SUMMARY.md 237 B

History

  1. First seen on skills.sh
  2. First recorded snapshot · 1 installs

SKILL.md

Cairo Numeric Types

Overview

Guide implementing production-quality custom numeric types (fixed-point, big integers, signed integers) in Cairo following Ekubo-style patterns.

Quick Use

  • Read references/numeric-types.md before answering.
  • Study Ekubo's i129 - the gold standard for signed integers in Cairo.
  • Use constructor functions to enforce invariants (e.g., no negative zero).
  • Provide both checked (returns Option) and unchecked (panics) API variants.
  • Implement standard traits: PartialEq, PartialOrd, Add, Sub, Mul, Neg, Zero, One.

Response Checklist

Invariant Enforcement

  • Use a private constructor function that enforces invariants (Ekubo i129 pattern).
  • Example: fn i256new(mag: U256, neg: bool) -> I256 { I256 { mag, neg: neg && !u256is_zero(mag) } }
  • Never allow invalid states like negative zero.

Defensive Normalization

  • In eq, cmp, and Hash, normalize inputs defensively to handle potentially invalid states.
  • Example: let aneg = a.neg && !u256is_zero(a.mag); before comparing.

API Safety

  • Name unsafe functions explicitly: fromrawunchecked, not just from_raw.
  • Provide fromrawchecked returning Option<T> for untrusted input.
  • Document safety requirements with /// # Safety sections.

Checked vs Panicking Arithmetic

  • Implement checked versions first (return Option<T>).
  • Panicking versions should be thin wrappers: checked_op(...).expect('error msg').
  • Handle edge cases: Neg on MIN value overflows (returns None in checked).

Standard Trait Implementations

  • PartialEq: Takes @Self (snapshot) parameters, dereference with *.
  • PartialOrd: Implement lt, le, gt, ge for full ordering.
  • Add, Sub, Mul: Return owned values, panic on overflow.
  • Neg: Must handle MIN value overflow.
  • Zero, One, Default: Return constant values.
  • Into<SourceType>: For converting from smaller types.
  • Hash: Must be consistent with Eq (normalize before hashing).
  • Use #[derive(Copy, Drop, Serde, Debug)] for automatic implementations.

Limb-Based Arithmetic

  • Prefer 64-bit limbs for Cairo (U256 = 4 x u64, U512 = 8 x u64).
  • Use helper functions for limb access instead of copy-paste branches.
  • For division by 2^64, use value / TWOPOW64 (Cairo lacks >> operator).
  • Loop-based operations reduce code size vs unrolled branches.

Code Organization

  • Keep checked arithmetic as the core implementation.
  • Panicking wrappers call checked versions with .expect().
  • Group related operations: constructors, arithmetic, traits, tests.

Cairo-Specific Quirks

  • No >> bit shift operator - use division by power-of-2 constant.
  • Can't index fixed-size arrays with runtime index - use .span().
  • use statements cannot appear inside functions.
  • Unary negation parsing issues - use 0_i32 - value instead of -value in some contexts.
  • Ambiguous unwrap - use OptionTrait::unwrap(result) to disambiguate.

Example Requests

  • "How do I implement a signed integer type in Cairo?"
  • "How do I enforce invariants like no negative zero?"
  • "How do I implement operator overloading for my numeric type?"
  • "How do I handle overflow in custom arithmetic?"

Reference Implementations

  • Ekubo i129 - Production signed integer (the gold standard)

Cairo by Example