skills/cairo-optimization/SKILL.md
Improves Cairo performance after correctness is established. Trigger on "optimize", "gas usage", "reduce steps", "profile", "BoundedInt", "storage packing", "benchmark". Guides profiling, arithmetic optimization, and bounded-int hardening.
npx skillsauth add keep-starknet-strange/starknet-agentic cairo-optimizationInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
You are a Cairo optimization assistant. Your job is to profile existing code, identify hotspots, apply targeted optimizations, and verify no regressions were introduced. Apply only after tests pass and behavior is locked.
cairo-testing).cairo-contract-authoring).cairo-auditor).cairo-deploy).snforge test.python3 skills/cairo-optimization/scripts/profile.py profile.../../evals/cases/contract_skill_benchmark.jsonl to prevent benchmark drift.../references/skill-handoff.md; optimization → testing is optional for regression hardening, but optimization → auditor is mandatory before merge.Turn 1 — Baseline. Before optimizing anything:
(a) Determine mode: profile, arithmetic, bounded-int, or storage.
(b) Verify tests pass. Run snforge test — if any test fails, stop and tell the user to fix tests first.
(c) Read the target code. Use Glob to find .cairo files, then Read to inspect them. Identify:
(d) Profile the baseline. Run python3 {skill_dir}/scripts/profile.py profile with the appropriate arguments. Use the machine-readable table/text summary as the ranking source of truth; treat PNG output as optional visualization only.
(e) Load references based on the optimization type:
| Request involves | Load reference |
|-----------------|---------------|
| Arithmetic rules (DivRem, loops, Poseidon, integer types) | {skill_dir}/references/legacy-full.md (Rules 1-12) |
| BoundedInt types, limb assembly, modular arithmetic | {skill_dir}/references/legacy-full.md (BoundedInt section) |
| Storage packing, StorePacking trait | {skill_dir}/references/legacy-full.md (Rule 9) |
| Profiling CLI, metrics, troubleshooting | {skill_dir}/references/profiling.md |
| Anti-pattern/optimized-pattern pairs | {skill_dir}/references/anti-pattern-pairs.md |
Where {skill_dir} is the directory containing this SKILL.md. Resolve it from the currently loaded SKILL path (preferred), then use references/... and scripts/... relative paths from that directory.
Turn 2 — Plan. Before changing any code, output a brief plan:
Keep the plan under 30 lines. Wait for user confirmation before implementing.
Turn 3 — Optimize. Apply changes following these rules:
Process rules:
snforge test after each change — if any test fails, revert and investigate.Arithmetic rules (from legacy-full.md):
DivRem::div_rem instead of separate / and % (Rule 1).!= instead of < in loop conditions (Rule 2).pow() (Rule 3).pop_front / for / multi_pop_front instead of index loops (Rule 4)..len() before loops (Rule 5).span.slice() instead of manual loop extraction (Rule 6).DivRem for parity checks instead of bitwise ops (Rule 7).hades_permutation for 2-input Poseidon hashes (Rule 11).Storage rules:
StorePacking trait (Rule 9).BoundedInt rules:
u128s_from_felt252 + upcast for bulk felt252 → BoundedInt conversions (Rule 12).python3 {skill_dir}/scripts/bounded_int_calc.py to compute bounds — never calculate manually.bounded_int_div_rem: SHIFT = ceil(|min_possible_value| / modulus) * modulus, then reduce value + SHIFT.After each optimization, run snforge test and python3 {skill_dir}/scripts/profile.py profile to verify improvement.
Turn 4 — Verify. After all optimizations:
snforge test (all tests must pass).cairo-auditor on touched files and update eval cases (contract_skill_benchmark.jsonl, contract_skill_generation_eval.jsonl) to lock gains.For the full execution checklist, use workflows/default.md.
import { Account, Contract, RpcProvider } from "starknet";
const provider = new RpcProvider({ nodeUrl: process.env.STARKNET_RPC! });
const account = new Account(provider, process.env.ACCOUNT_ADDRESS!, process.env.PRIVATE_KEY!);
const contract = new Contract(abi, process.env.CONTRACT_ADDRESS!, provider).connect(account);
try {
const before = await contract.call("hot_path_steps", []);
const tx = await contract.invoke("apply_optimized_path", []);
await provider.waitForTransaction(tx.transaction_hash);
const after = await contract.call("hot_path_steps", []);
console.log({ before, after });
} catch (err) {
console.error("optimization check failed", err);
}
| Code | Condition | Recovery |
| --- | --- | --- |
| OPT-001 | Baseline tests failed before optimization | Stop optimization work, fix failing tests, then rerun baseline profiling. |
| OPT-002 | Profiling artifacts missing (trace/pb.gz) | Re-run profile.py with correct --mode/--package and validate tool availability. |
| OPT-003 | BoundedInt bounds invalid or unsafe | Recompute bounds with bounded_int_calc.py; reject manual bounds and rerun tests. |
| OPT-004 | Post-change profile regressed | Revert the change, isolate one optimization class, and measure again with identical inputs. |
These are non-negotiable. Every optimization you apply must satisfy all of them:
../references/skill-handoff.mdoptimization → auditor (with optimization → testing only as an optional regression-hardening pass before auditor).data-ai
SNIP-36 virtual block proving on Starknet. Trigger on "virtual block", "SNIP-36", "off-chain proof", "anonymous vote", "heavy computation off-chain", "prove a transaction". Covers Cairo virtual contract, proof server, starknet.js integration, and on-chain verification.
development
Reference for integrating or maintaining applications built with keep-starknet-strange/starkzap, including StarkSDK setup, onboarding, wallet lifecycle, sponsored transactions, ERC20 flows, staking, and transaction builder usage.
testing
Create and manage Starknet wallets for AI agents. Transfer tokens, check balances, manage session keys, deploy accounts, and interact with smart contracts using native Account Abstraction.
development
Simple P2P payments on Starknet. Generate QR codes, payment links, invoices, and transfer ETH/STRK/USDC. Like Lightning, but native.