插件低风险未认领

dimensional-analysis

Audits code for unit and decimal-scaling bugs and annotates it with dimensional comments recording the units of each value. Use when someone asks to audit or review code for unit confusion, decimals or scaling mismatches, WAD/RAY scale errors, or the wrong token's decimals; to annotate units in a codebase; or to perform a dimensional analysis of a DeFi protocol, offchain code, or other arithmetic-heavy codebase. Catches formulas that combine values carrying different units or scales.

trailofbitstrailofbits/dimensional-analysis-plugin★ 8.3k版本 3.2.0更新于 2026年11月12日

说明

Dimensional Analysis Plugin

Add dimensional annotations to codebases and detect dimensional bugs. Uses an annotation format inspired by Reserve Protocol's Solidity conventions, but applicable to any language or protocol performing numeric arithmetic with mixed units, precisions, or scaling factors.

Overview

This plugin runs one automatic workflow:

  1. Discover the dimensional vocabulary in your codebase (tokens, shares, prices, etc.)
  2. Annotate your code with dimensional comments like D18{tok}, D27{UoA/tok}
  3. Propagate dimensions through arithmetic and call paths
  4. Validate dimensional consistency and detect bugs

Annotation Format

Based on Reserve Protocol's format (shown here in Solidity, but adaptable to any language):

// State variables
uint256 public tvlFee; // D18{1/s} demurrage fee on AUM
uint256 public lastPoke; // {s}

// Struct fields
struct RebalanceLimits {
    uint256 low;  // D18{BU/share} (0, 1e27]
    uint256 spot; // D18{BU/share} (0, 1e27]
    uint256 high; // D18{BU/share} (0, 1e27]
}

// Function parameters (NatSpec)
/// @param weights D27{tok/BU} Basket weight ranges
/// @param prices D27{UoA/tok} Prices for each token
/// @return price D27{buyTok/sellTok}

// Inline arithmetic
// D27{buyTok/sellTok} = D27{UoA/sellTok} * D27 / D27{UoA/buyTok}
uint256 startPrice = Math.mulDiv(sellPrices.high, D27, buyPrices.low);

Usage

The skill always executes in full-auto mode. Any supplied mode argument is ignored.

Workflow phases are implemented in:

  • skills/dimensional-analysis/references/step1.md
  • skills/dimensional-analysis/references/step2.md
  • skills/dimensional-analysis/references/step3.md
  • skills/dimensional-analysis/references/step4.md

Automatic Behavior

  • Uses existing DIMENSIONAL_UNITS.md if present; otherwise auto-generates and saves it
  • Persists DIMENSIONAL_SCOPE.json as a source-of-truth manifest for large repos
  • Applies annotations directly without approval gates
  • Uses best-guess inference for uncertainties and flags them in output
  • Reports results in a single summary at the end

Coverage Guarantees

  • All in-scope arithmetic files from scanner output are required scope (CRITICAL/HIGH/MEDIUM/LOW)
  • Discoverer narrowing (for vocabulary speed) does not reduce annotation or validation scope
  • Each in-scope file must be marked as completed in Phase 2, Phase 3, and Phase 4 before finalization

Agents

Agent Purpose
arithmetic-scanner Scans repo for files with dimensional arithmetic to scope discovery
dimension-discoverer Discovers dimensional vocabulary from naming, interfaces, and patterns
dimension-annotator Adds dimensional annotations at anchor points (comments only)
dimension-propagator Propagates dimensional annotations through arithmetic and call chains, reporting mismatches
dimension-validator Validates dimensional consistency and detects bugs

Tests

DETERMINISTIC_ONLY=1 tests/smoke-test.sh   # free: validator self-test
tests/smoke-test.sh                        # also runs the semantic eval (paid)

tests/fixtures/src/ is a cross-margin borrowing protocol with five planted dimensional bugs, three of which only surface by tracing dimensions across files (a RAY borrow index consumed as WAD, per-asset decimals from a registry, and Uniswap v3 Q96 encoding). StakingRewards.sol is a dimensionally correct control for measuring false positives. Ground truth is in tests/fixtures/ANSWER-KEY.md.

The eval copies fixtures to /tmp/dimensional-analysis-eval before running. This is deliberate: arithmetic-scanner post-filters any path containing /tests/, so analysing the fixtures in place would exclude every file and score a vacuous pass.

The harness requires uv and runs each eval arm against pristine fixtures. Use the smoke-test script for ablation comparisons: the built-in --ablation option shares annotated sources between arms and contaminates the baseline.

Requirements

  • A codebase performing numeric arithmetic with mixed units, precisions, or scaling factors
  • Most effective for DeFi protocols (Solidity, Rust/Anchor, CosmWasm, etc.) but works with any language
  • Optional for Solidity projects: slither-mcp for enhanced static analysis

References

See the references/ directory for:

  • dimension-algebra.md - Rules for dimensional arithmetic
  • common-dimensions.md - DeFi dimension vocabulary
  • bug-patterns.md - Dimensional bug patterns with examples
  • annotate.md - Full annotated protocol examples (ERC-4626, AMM, Lending)
  • step1.md - Phase 1 workflow instructions
  • step2.md - Phase 2 workflow instructions
  • step3.md - Phase 3 workflow instructions
  • step4.md - Phase 4 workflow instructions

Author

Coriolan Pinhas & Benjamin Samuels - Trail of Bits

Codex support

Requires a writable target and native agent dispatch for the scanner, discovery, annotation, propagation and validation stages. Coverage, retry and blocked-file gates remain mandatory. Invoke the named skill in Codex; existing Claude entry points and metadata remain supported.

Calcifier recipe for dimensional-analysis

The dimensional-analysis Python recipe was generated with Calcifier 0.1.0. It pins the source skill and its packaged resources so source changes can be detected before execution.

Install Calcifier with Python 3.13 or later, and configure the worker role(s) in an agent-config v1 file following the provider setup. Review the generated Python before granting trust.

Run the portable check from this plugin directory:

calcifier lint skills/dimensional-analysis/.calcifier/recipe.py --portable

Then use calcifier exec with the recipe path, --agent-config agents.json, and one --input NAME=VALUE for each required input below. Add --trust only after reviewing the recipe; execution records consent for those exact bytes.

Input Type Default
project_root str Required

Depending on the execution path, tool providers may also be needed for Write. See the provider setup for --tool-config.

宿主

宿主装上后有
Claude Code技能 1助手 5
Codex只装技能
OpenCode只装技能

成员1 个技能 · 5 个助手

  • dimensional-analysisAudits code for unit and decimal-scaling bugs and annotates it with dimensional comments recording the units of each value. Use when someone asks to audit or review code for unit confusion, decimals or scaling mismatches, WAD/RAY scale errors, or the wrong token's decimals; to annotate units in a codebase; or to perform a dimensional analysis of a DeFi protocol, offchain code, or other arithmetic-heavy codebase. Catches formulas that combine values carrying different units or scales.
  • arithmetic-scanner助手Scans repo for files with dimensional arithmetic to scope discovery
  • dimension-annotator助手Adds dimensional annotations to source code at anchor points using Reserve Protocol's format
  • dimension-discoverer助手Discovers dimensional vocabulary for codebases by analyzing naming conventions and protocol patterns
  • dimension-propagator助手Propagates dimensional annotations through arithmetic and call chains, reporting mismatches found during propagation
  • dimension-validator助手Validates dimensional consistency and detects dimensional bugs in annotated code

权限

完整安装
3.2.0
声明检测
运行代码—pythonshell
安装—无
安装时运行脚本—无
网络—github.com
需要的凭据—无
工作区外的路径—无
智能体工具—无

检查

低风险 · 没有发现需要提醒的地方。

未经人工审核 · 已做规则检查;模型审核尚未开启。

另有 5 处低风险标记:常见命令之类,只记录、不提醒
  • 规则 · command_injectiontests/smoke-test.sh:27
  • 规则 · command_injectiontests/smoke-test.sh:64
  • 规则 · command_injectiontests/smoke-test.sh:193
  • 规则 · command_injectiontests/trigger-test.sh:39
  • 规则 · command_injectiontests/trigger-test.sh:48

文件44 个文件 · 197.3 KB

  • README.md6.3 KB
.claude-plugin/1
  • plugin.json724 B
agents/5
  • arithmetic-scanner.md10.8 KB
  • dimension-annotator.md8.6 KB
  • dimension-discoverer.md7.8 KB
  • dimension-propagator.md9.6 KB
  • dimension-validator.md12.6 KB
evals/planted-bugs/1
  • prompt.md1006 B
evals/planted-bugs/graders/1
  • criteria.md2.0 KB
evals/trigger-annotate/1
  • prompt.md300 B
evals/trigger-annotate/graders/1
  • criteria.md233 B
evals/trigger-audit/1
  • prompt.md278 B
evals/trigger-audit/graders/1
  • criteria.md233 B
evals/trigger-explicit/1
  • prompt.md295 B
evals/trigger-explicit/graders/1
  • criteria.md233 B
evals/trigger-oblique/1
  • prompt.md302 B
evals/trigger-oblique/graders/1
  • criteria.md233 B
skills/dimensional-analysis/1
  • SKILL.md9.7 KB
skills/dimensional-analysis/.calcifier/1
  • recipe.py15.1 KB
skills/dimensional-analysis/agents/1
  • openai.yaml245 B
skills/dimensional-analysis/assets/1
  • trail-of-bits-mark.svg3.0 KB
skills/dimensional-analysis/references/8
  • annotate.md11.7 KB
  • bug-patterns.md11.7 KB
  • common-dimensions.md7.3 KB
  • dimension-algebra.md6.3 KB
  • step1.md5.1 KB
  • step2.md3.6 KB
  • step3.md3.9 KB
  • step4.md6.5 KB
tests/5
  • smoke-test.shx8.3 KB
  • summarize_arms.pyx4.5 KB
  • trigger-test.shx3.6 KB
  • validate_eval_result.pyx1.4 KB
  • validate_findings.pyx6.5 KB
tests/fixtures/1
  • ANSWER-KEY.md6.5 KB
tests/fixtures/selftest/1
  • findings-complete.json2.5 KB
tests/fixtures/src/5
  • AssetRegistry.sol2.1 KB
  • InterestIndex.sol1.8 KB
  • MarginVault.sol5.5 KB
  • StakingRewards.sol4.0 KB
  • UniV3Oracle.sol1.6 KB
tests/fixtures/src/libraries/2
  • FixedPointMath.sol779 B
  • PriceLib.sol1.1 KB
tests/fixtures/trigger/src/1
  • Vault.sol1.5 KB

版本

  1. #13.2.0最新2026年11月12日

在 Codeg 中打开

也可以复制地址,在 Codeg 的「发现」里搜索。

trailofbits/dimensional-analysis-plugin

还没有 Codeg?下载