Overview
The Spec-to-Code Compliance plugin performs systematic comparison between specification documents (whitepapers, design docs) and actual implementation code, producing audit-grade compliance reports with:- Zero speculation - Every claim backed by exact quotes or line numbers
- Structured IR workflow - Spec Intent → Code Behavior → Alignment → Divergence
- Confidence scoring - All mappings include 0-1 confidence scores
- Exploit scenarios - Critical findings include attack vectors and economic impact
When to Use
Protocol Audits
Smart contracts being verified against whitepapers or protocol specifications
Compliance Checks
Finding gaps between intended behavior and actual implementation
Spec Divergence
Identifying undocumented code behavior or unimplemented spec claims
Documentation Drift
Discovering where code has evolved beyond original design documents
Concrete Triggers
- User provides both specification documents AND codebase
- Questions like “does this code match the spec?” or “what’s missing from the implementation?”
- Audit engagements requiring spec-to-code alignment analysis
- Protocol implementations being verified against whitepapers
When NOT to Use
Analysis Workflow
The plugin executes a rigorous 7-phase workflow:Documentation Discovery
Identifies all spec sources (whitepapers, READMEs, design notes, Notion exports, meeting transcripts)
Format Normalization
Converts PDF, Markdown, DOCX, HTML, TXT to clean canonical format preserving structure
Spec Intent IR
Extracts all intended behavior (invariants, formulas, flows, security requirements) into structured Spec-IR
Code Behavior IR
Line-by-line semantic analysis of implementation producing Code-IR with full traceability
Alignment IR
Compares Spec-IR to Code-IR, generates alignment records with match types and confidence scores
Divergence Classification
Categorizes misalignments by severity (Critical/High/Medium/Low) with exploit scenarios
Match Types
The plugin classifies spec-to-code alignment into six categories:| Match Type | Description | Example |
|---|---|---|
full_match | Code exactly implements spec | Spec says “revert if balance < amount”, code has exact check |
partial_match | Incomplete implementation | Spec requires 3 checks, code implements 2 |
mismatch | Spec says X, code does Y | Spec says “use safe math”, code uses unchecked arithmetic |
missing_in_code | Spec claim not implemented | Spec documents access control, code has none |
code_stronger_than_spec | Code adds behavior beyond spec | Code has reentrancy guard not in spec |
code_weaker_than_spec | Code misses requirements | Spec requires signature verification, code skips it |
Severity Classification
- Critical
- High
- Medium
- Low
Indicators:
- Spec says X, code does Y
- Missing invariant enabling exploits
- Math divergence involving funds
- Trust boundary mismatches
IR Structure Examples
Spec-IR Record
Code-IR Record
Alignment Record
Divergence Finding (Critical)
Anti-Hallucination Requirements
Rationalizations to Reject
The plugin rejects these common shortcuts:| Rationalization | Why It’s Wrong | Required Action |
|---|---|---|
| ”Spec is clear enough” | Ambiguity hides in plain sight | Extract to IR, classify ambiguity explicitly |
| ”Code obviously matches” | Obvious matches have subtle divergences | Document match_type with evidence |
| ”I’ll note this as partial match” | Partial = potential vulnerability | Investigate until full_match or mismatch |
| ”This undocumented behavior is fine” | Undocumented = untested = risky | Classify as UNDOCUMENTED CODE PATH |
| ”Low confidence is okay here” | Low confidence findings get ignored | Investigate until confidence ≥ 0.8 or classify as AMBIGUOUS |
| ”I’ll infer what the spec meant” | Inference = hallucination | Quote exact text or mark UNDOCUMENTED |
Output Report Structure
The final compliance report includes:- Executive Summary - Finding counts, overall compliance metrics
- Documentation Sources - All spec documents analyzed
- Spec Intent Breakdown - Extracted Spec-IR with full citations
- Code Behavior Summary - Code-IR semantic map
- Full Alignment Matrix - Every spec item mapped to code status
- Divergence Findings - Critical/High/Medium/Low with evidence
- Missing Invariants - Spec requirements not implemented
- Incorrect Logic - Code behavior contradicting spec
- Math Inconsistencies - Formula divergence
- Flow Mismatches - State machine divergence
- Access Control Drift - Permission misalignment
- Undocumented Behavior - Code paths not in spec
- Ambiguity Hotspots - Unclear spec or code sections
- Recommendations - Remediation guidance
- Documentation Updates - Suggested spec improvements
- Final Risk Assessment - Overall compliance rating
Quality Standards
Spec-IR Completeness
Spec-IR Completeness
- All invariants extracted and normalized
- All formulas in canonical symbolic form
- All security requirements documented
- Minimum 80% confidence on critical items
Code-IR Completeness
Code-IR Completeness
- Every function analyzed with control flow
- All state changes tracked
- All external calls documented
- Line numbers for all evidence
Alignment Quality
Alignment Quality
- Every spec item has alignment record
- Match types accurately classified
- Confidence scores justified
- Evidence links verified
Divergence Findings
Divergence Findings
- Exploit scenarios for critical issues
- Economic impact analysis
- Remediation with code examples
- Citations to both spec and code
Agent Usage
Thespec-compliance-checker agent performs the full 7-phase workflow autonomously:
- Structured IR artifacts (Spec-IR, Code-IR, Alignment-IR)
- Divergence findings with severity classification
- Final compliance report in markdown format
Example Use Cases
DeFi Protocol Audit
Whitepaper specifies constant product formula
x * y = k. Code uses x * y = k + fee, enabling economic exploit.Governance Compliance
Spec requires 7-day timelock on proposals. Code implements 3-day timelock, violating governance security model.
Access Control Gap
Documentation describes admin-only emergency pause. Implementation missing
onlyAdmin modifier.Undocumented Upgrade
Code implements proxy upgrade pattern not mentioned in specification, introducing trust assumptions.
Related Resources
The plugin includes detailed reference documentation:resources/IR_EXAMPLES.md- Complete IR workflow examples with DEX patternsresources/OUTPUT_REQUIREMENTS.md- IR production standards and quality thresholdsresources/COMPLETENESS_CHECKLIST.md- Verification checklist for all phases
Integration Points
- Property-Based Testing - Verify extracted invariants hold in practice
- Zeroize Audit - Cross-check sensitive data handling against spec
- Constant-Time Analysis - Validate timing requirements from spec
Author: Omar Inuwa (Trail of Bits)Version: 1.1.0