Skip to content
Back to skills

2537 Formal Verification 752d3796

ASecurity

Formal verification provides mathematical guarantees about semantic equivalence through rigorous proof techniques. Use these methods when high confidence is required or when testing alone is insufficient.

  • 9 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added October 11, 2026
testingpythongoexpresstestingrefactoringsecurity

Security analysis

A100/100

Scanned October 11, 2026

npx -y skills add tools-only/X-Skills --skill 2537-formal_verification_752d3796 --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of 2537 Formal Verification 752d3796?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for 2537 Formal Verification 752d3796
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/tools-only-2537-formal-verification-752d3796/badge)](https://www.skillsdirectory.com/skills/tools-only-2537-formal-verification-752d3796)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

SKILL.md
# Formal Verification Techniques for Equivalence Checking

## Overview

Formal verification provides mathematical guarantees about semantic equivalence through rigorous proof techniques. Use these methods when high confidence is required or when testing alone is insufficient.

## Proof-Based Approaches

### Hoare Logic

Use pre/postconditions to prove equivalence:

1. Define precondition P (input constraints)
2. Define postcondition Q (expected output relation)
3. Prove {P} Artifact_A {Q} and {P} Artifact_B {Q}
4. If both hold, artifacts are equivalent under P

**Example:**
```
Precondition: n >= 0
Postcondition: result = sum of squares from 1 to n

Prove both implementations satisfy this specification
```

### Weakest Precondition Calculus

Work backwards from postconditions:
- Compute wp(Artifact_A, Q) and wp(Artifact_B, Q)
- If weakest preconditions are logically equivalent, artifacts are equivalent

### Invariant-Based Reasoning

For loops and recursive functions:
1. Identify loop/recursion invariants
2. Prove invariants hold for both artifacts
3. Show invariants imply equivalence

## SMT-Based Verification

### Using Z3 Solver

Encode programs as logical formulas:

```python
from z3 import *

# Define symbolic variables
x = Int('x')
n = Int('n')

# Encode Artifact A
result_A = Sum([i*i for i in range(1, n+1)])

# Encode Artifact B
result_B = n * (n + 1) * (2*n + 1) / 6

# Check equivalence
solver = Solver()
solver.add(result_A != result_B)
solver.add(n >= 0)

if solver.check() == unsat:
    print("Equivalent for n >= 0")
else:
    print("Counterexample:", solver.model())
```

### Bounded Model Checking

For finite state spaces:
- Unroll loops up to bound k
- Check equivalence for all paths up to depth k
- Increase k until confidence threshold met

## Translation Validation

Verify compiler optimizations and transformations:

1. **Source-to-source**: Compare original and refactored code
2. **Compiler correctness**: Verify compiled code matches source semantics
3. **Optimization validation**: Prove optimizations preserve behavior

**Workflow:**
- Generate verification conditions (VCs) from both artifacts
- Use automated theorem provers to discharge VCs
- Report any VCs that cannot be proven

## Denotational Semantics

Define mathematical meaning of programs:

1. Map each construct to mathematical function
2. Compute denotation [[Artifact_A]] and [[Artifact_B]]
3. Prove [[Artifact_A]] = [[Artifact_B]]

**Useful for:**
- Functional programs
- Expression-level equivalence
- Mathematical transformations

## Relational Verification

Prove properties relating two programs:

**Product Programs:**
- Construct product program executing both artifacts in lockstep
- Prove output relation holds at end
- More efficient than separate verification

**Coupling Invariants:**
- Define relation between states of both artifacts
- Prove relation maintained throughout execution
- Show relation implies output equivalence

## Proof Assistants

### Coq

Interactive theorem proving:
```coq
Theorem equivalence : forall n,
  sum_squares_A n = sum_squares_B n.
Proof.
  induction n.
  - reflexivity.
  - simpl. rewrite IHn. ring.
Qed.
```

### Isabelle/HOL

Higher-order logic verification:
```isabelle
lemma "sum_squares_A n = sum_squares_B n"
  apply (induction n)
  apply auto
  done
```

## Practical Guidelines

**When to use formal verification:**
- Safety-critical systems (medical, aerospace)
- Security-sensitive code (cryptography, authentication)
- Complex algorithms with subtle correctness properties
- Refactoring with high risk of introducing bugs

**Limitations:**
- Requires formal specifications
- Can be time-intensive
- May need expert knowledge
- Not all properties are decidable

**Best practices:**
- Start with automated tools (SMT solvers)
- Use interactive provers for complex cases
- Combine with testing for comprehensive validation
- Document assumptions and proof obligations

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.