AgentSkillsCN

compact-reviewer:performance-review

当您审查Compact电路,关注性能问题、优化约束数量、分析证明生成时间,或发掘Gas与成本优化的潜在机会时,可选用此功能。

SKILL.md
--- frontmatter
name: compact-reviewer:performance-review
description: Use when reviewing Compact circuits for performance issues, optimizing constraint counts, analyzing proof generation time, or identifying gas and cost optimization opportunities.

Performance Review Skill

Analyze circuit efficiency and identify optimization opportunities in Compact contracts.

When to Use

This skill activates for queries about:

  • Circuit performance and efficiency
  • Constraint count optimization
  • Proof generation time
  • Gas/cost optimization
  • Performance bottlenecks

Trigger words: performance, optimization, constraints, circuit efficiency, proof generation, gas, cost

Quick Reference

Constraint Cost Table

OperationCostNotes
Addition (+)0Free in R1CS
Subtraction (-)0Free
Multiplication (*)1Single constraint
Division (/)~1Includes inverse
Equality (==)~1Direct check
Inequality (<, >)~254Bit decomposition
Hash (Pedersen)~1,000Optimized
SHA256~25,000Expensive
Merkle proof (depth N)~N×1,000Per-level hash

Complexity Estimation

code
Total Constraints ≈
  (Hash Ops × 1,000) +
  (SHA256 Ops × 25,000) +
  (Comparisons × 254) +
  (Merkle Depth × 1,000) +
  (Loop Iterations × Body Cost)

Quick Wins

OptimizationSavingsEffort
Replace SHA256 → Pedersen25x per hashLow
Use == instead of <~253 constraintsLow
Reduce Merkle depth~1,000 per levelMedium
Move computation to witnessVariableMedium

Review Process

1. Count Expensive Operations

Scan the contract for:

compact
// High-cost operations
persistentHash()      // ~1,000 constraints
persistentCommit()    // ~1,000 constraints
sha256()              // ~25,000 constraints ❌ Avoid
ecMul()               // ~5,000-10,000 constraints

// Medium-cost operations
if x < y { }          // ~254 constraints (bit decomposition)
for i in 0..N { }     // Multiplies inner constraints by N

// Low-cost operations
x + y                 // Free
x * y                 // 1 constraint
x == y                // ~1 constraint

2. Analyze Loops

For each loop:

code
1. What operations are inside?
2. How many iterations?
3. Can any operations move outside?
4. Is the loop necessary?

Example:

compact
// ❌ Inefficient: hash inside loop
for i in 0..10 {
    hashes[i] = hash(data[i]);  // 10 × 1,000 = 10,000 constraints
}

// ✅ Consider: can this be done in witness?

3. Check Type Choices

Smaller types mean cheaper comparisons:

TypeComparison Cost
Uint<8>~8 constraints
Uint<64>~64 constraints
Uint<254>~254 constraints

4. Evaluate Merkle Usage

compact
// Merkle tree with depth 20
const proof = get_merkle_proof();  // ~20,000 constraints

// Consider: Is depth 20 necessary?
// Depth 10 would be ~10,000 constraints

References

Related Skills