Optimizes Cairo code for lower gas usage and steps after tests pass. Profiles hotspots, applies arithmetic, BoundedInt, and storage packing optimizations.
npx claudepluginhub keep-starknet-strange/starknet-agentic --plugin starknet-agentic-skillsThis skill is limited to using the following tools:
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.
Scans Cairo/StarkNet smart contracts for vulnerabilities including felt252 arithmetic overflow, L1-L2 messaging issues, address conversion problems, and signature replay. Use when auditing StarkNet projects.
Audits Cairo/Starknet smart contracts for security vulnerabilities. Discovers in-scope files, runs preflight scans, spawns agents, and merges findings into reports with default, deep, or file-specific modes.
Autonomously optimizes code performance using CodSpeed benchmarks, flamegraph analysis, and iterative measure-analyze-change loops for Rust, Python, Node.js projects.
Share bugs, ideas, or general feedback.
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).