Skip to content

Security: Mist-Labs/contracts

Security

Security.md

πŸ”’ Shadow Swap Security & Privacy Documentation

Overview

This document details security measures and privacy guarantees in Shadow Swap's privacy-preserving atomic swap protocol.


πŸ›‘οΈ Security Architecture

Multi-Layer Defense System

Layer Technology Protection
Cryptographic Pedersen Commitments Amount hiding, binding guarantees
Proof Systems Range Proofs Validity without disclosure
Access Control Stealth Ownership Identity-based authorization
Smart Contract OpenZeppelin Guards Reentrancy, state integrity
Time Safety Bounded Timelocks Refund guarantees

πŸ” Privacy Guarantees

1. Amount Confidentiality

Technology: Elliptic Curve Pedersen Commitments

What's Hidden:

  • Exact STRK amount locked in HTLC
  • Token quantities in both redeem and refund operations
  • Historical transaction amounts (even after completion)

How It Works:

User commits: C = amountΒ·G + blindingΒ·H
On-chain sees: C (just a curve point)
To claim: Must prove knowledge of (amount, blinding)

Security Guarantee: Computationally infeasible to extract amount from commitment (ECDLP hardness)


2. Range Proof System

Technology: Bit Decomposition + Commitment Scheme

Purpose: Prove 0 < amount < 2^64 without revealing amount

Protection Against:

  • βœ… Negative amounts (underflow attacks)
  • βœ… Overflow attacks (amount > max supply)
  • βœ… Zero-value exploits

Verification: Contract checks 64 bit commitments without learning amount


3. Stealth Address System

Technology: Hash-based Address Derivation

What's Hidden:

  • Real initiator identity (user's main address)
  • Real participant identity (bridge operator address)
  • Address linkability across multiple swaps

How It Works:

Real Address: 0xABC...
Stealth Address: H(0xABC...)
On-chain sees: H(0xABC...) only

Security Guarantee: Cannot reverse hash to find real address (preimage resistance)


πŸ›‘οΈ Smart Contract Security

1. Reentrancy Protection

Attack Prevented: Malicious callback during token transfer

Implementation:

component!(path: ReentrancyGuardComponent, storage: reentrancy, event: ReentrancyGuardEvent);

fn redeem(...) {
    self.reentrancy.start();  // Blocks reentrant calls
    // ... operations ...
    self.reentrancy.end();
}

Test Coverage: βœ… Verified with malicious ERC20 mock


2. Checks-Effects-Interactions Pattern

Attack Prevented: State inconsistency on external call failure

Pattern:

fn redeem(...) {
    // 1. CHECKS
    assert(verify_ownership(caller, stealth_participant));
    assert(verify_commitment(amount, blinding));
    assert(verify_secret(secret));
    
    // 2. EFFECTS
    self.state.write(REDEEMED);
    
    // 3. INTERACTIONS
    token.transfer(recipient, amount);
}

3. Commitment Verification

Attack Prevented: Invalid amount claims

Implementation:

fn redeem(secret, amount, blinding_factor) {
    // Recompute commitment
    let computed = generate_amount_commitment(amount, blinding_factor);
    
    // Must match stored commitment
    assert(computed == self.amount_commitment.read(), 'Invalid commitment');
}

Why Secure: Pedersen commitments are computationally binding (cannot find different values for same commitment)


4. Ownership Verification

Attack Prevented: Unauthorized redemption/refund

Implementation:

fn verify_ownership(claimer: ContractAddress, stealth: felt252) -> bool {
    // Recompute stealth from real address
    let computed_stealth = generate_stealth_address(claimer);
    
    // Must match stored stealth
    computed_stealth == stealth
}

5. State Machine Protection

Attack Prevented: Double-spending, invalid transitions

States:

  • 0 = INITIATED β†’ Can redeem (before timelock) or refund (after timelock)
  • 1 = REDEEMED β†’ Final state (cannot refund)
  • 2 = REFUNDED β†’ Final state (cannot redeem)

Enforcement:

fn redeem(...) {
    assert(self.state.read() == 0, 'Not initiated');
    self.state.write(1); // Atomic transition
}

6. Timelock Safety

Attack Prevented: Immediate refund, indefinite lock

Boundaries:

  • Minimum: 1 hour (prevents front-running refunds)
  • Maximum: 7 days (prevents permanent locks)
  • Recommended: 48 hours (Shadow Swap default)

Validation:

// Constructor
assert(timelock > now + 3600, 'Timelock too short');
assert(timelock < now + 604800, 'Timelock too long');

// Refund
assert(get_block_timestamp() >= timelock, 'Timelock active');

🎯 Attack Vectors Mitigated

βœ… Cryptographic Attacks

Attack Mitigation Security Level
Amount Extraction Pedersen ECDLP hardness 128-bit
Commitment Forgery Binding property Computational
Range Proof Bypass 64-bit decomposition Provable
Stealth Reversal Hash preimage resistance 128-bit

βœ… Smart Contract Attacks

Attack Prevention Test Status
Reentrancy Guard component βœ… Tested
Double Redemption State machine βœ… Tested
Unauthorized Claim Ownership verification βœ… Tested
Invalid Amount Commitment mismatch βœ… Tested
Premature Refund Timelock enforcement βœ… Tested
Zero-Value Exploit Constructor validation βœ… Tested

βœ… Privacy Attacks

Attack Protection Effectiveness
Amount Linkage Unique blinding per HTLC High
Address Correlation Fresh stealth per swap High
Timing Analysis Optional delay mixing Medium
Metadata Leakage Minimal on-chain data High

πŸ” Known Limitations

1. ERC20 Transfer Visibility

Issue: Initial deposit shows Transfer event
What's Leaked: Transfer(user β†’ htlc, ???)
Amount Visible: NO (commitment only)
Mitigation (V2): Privacy pool eliminates this leak


2. Timing Correlation

Issue: Swap duration might reveal patterns
What's Leaked: "Swap took 2 hours"
Amount Visible: NO
Mitigation (V2): Random delays, decoy transactions


3. Factory Deployment Event

Issue: HTLCDeployed event reveals "someone swapping"
What's Leaked: Timestamp, commitment, stealth addresses
Amount Visible: NO
Mitigation: Cannot be eliminated (on-chain requirement)


πŸ§ͺ Security Testing

Key Test Cases

βœ… test_commitment_binding          // Cannot forge commitment
βœ… test_range_proof_validation      // Rejects invalid proofs  
βœ… test_stealth_ownership          // Only owner can claim
βœ… test_amount_privacy             // Amount never on-chain
βœ… test_reentrancy_protection      // Blocks reentrant calls
βœ… test_double_spend_prevention    // State machine enforced
βœ… test_unauthorized_redemption    // Access control works
βœ… test_timelock_boundaries        // Timing enforced

πŸ“‹ Security Checklist

Pre-Deployment

  • All cryptographic primitives use audited libraries
  • Range proofs cover full 64-bit space
  • Stealth addresses use Pedersen hash
  • Commitments use proper blinding
  • Reentrancy guards on all state changes
  • CEI pattern in all external calls
  • Access control on privileged functions
  • State transitions validated
  • Timelock boundaries enforced
  • Events emit minimal data
  • 100% test coverage achieved
  • Fuzz testing passed

Post-Deployment

  • Contract verified on Starkscan
  • Testnet validation (48 hours minimum)
  • Security audit scheduled
  • Bug bounty program active
  • Incident response plan ready
  • Monitoring dashboard deployed


πŸ“š References

Cryptography:

  • Pedersen Commitments: [Pedersen, 1991]
  • Range Proofs: [Bulletproofs, BΓΌnz et al., 2018]
  • Stealth Addresses: [CryptoNote Protocol]

Smart Contract Security:

  • OpenZeppelin Cairo Contracts
  • Starknet Security Best Practices
  • Cairo Security Guidelines

Privacy Research:

  • Zcash Protocol Specification
  • Monero Research Lab Papers
  • Tornado Cash Technical Documentation

Last Updated: November 2025
Version: 0.2.0 (Privacy Update)
Audit Status: Pre-audit (testnet validation in progress)


There aren’t any published security advisories