A zero-knowledge privacy-preserving hook for Uniswap V4 that enables anonymous swaps using commitment schemes and nullifiers.
The ZK Privacy Hook brings privacy to decentralized trading by implementing zero-knowledge proofs on Uniswap V4. Users can:
- Deposit privately: Lock tokens into commitments without revealing amounts
- Swap anonymously: Execute trades without exposing wallet addresses
- Withdraw securely: Extract funds using nullifiers to prevent double-spending
This hook leverages cryptographic commitments and nullifiers to break the link between deposits, swaps, and withdrawals, providing transaction privacy similar to privacy coins but for any ERC-20 token on Uniswap V4.
Traditional DEXs expose all transaction details on-chain:
- Wallet addresses and balances
- Trading patterns and strategies
- MEV extraction opportunities
- Competitive disadvantages for traders
ZK Privacy Hook provides:
- Transaction Privacy: Hide swap amounts and participants
- MEV Protection: Prevent front-running through private transactions
- Strategic Trading: Execute large trades without market impact
- Financial Privacy: Protect trading strategies and positions
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β Private Deposit ββββββ ZK Privacy Hook ββββββ Private Withdrawβ
β β β β β β
β commitment = β β β’ Nullifier β β nullifier = β
β hash(amount, β β tracking β β hash(commitment,β
β secret) β β β’ Proof β β secret) β
β β β verification β β β
βββββββββββββββββββ β β’ Private swaps β βββββββββββββββββββ
ββββββββββββββββββββ
β
ββββββββββββββββββββ
β Uniswap V4 Pool β
β Manager β
ββββββββββββββββββββ
- Commitment Scheme:
commitment = hash(amount, currency, secret) - Nullifier System:
nullifier = hash(commitment, secret) - ZK Proofs: Verify operations without revealing sensitive data
- Privacy-Preserving Swaps: Execute trades through hook logic
# Clone the repository
git clone https://github.com/your-username/zk-privacy-hook
cd zk-privacy-hook
# Install dependencies
forge install
# Build contracts
forge build
# Run tests
forge testforge script script/DeployZKPrivacyHook.s.sol \
--rpc-url <your_rpc_url> \
--private-key <your_private_key> \
--broadcast// Generate commitment
uint256 secret = generateSecretKey();
bytes32 commitment = hook.generateCommitment(amount, currency, secret);
// Deposit privately
hook.privateDeposit(commitment, amount, currency);// Create swap parameters with ZK proof
ZKPrivacyHook.PrivateSwapParams memory params = ZKPrivacyHook.PrivateSwapParams({
nullifierIn: generateNullifier(commitment, secret),
nullifierOut: bytes32(0),
newCommitment: generateNewCommitment(),
proof: generateZKProof(),
minAmountOut: 0
});
// Execute private swap
bytes memory hookData = abi.encode(params);
poolManager.swap(poolKey, swapParams, hookData);// Generate nullifier
bytes32 nullifier = hook.generateNullifier(commitment, secret);
// Withdraw privately
hook.privateWithdraw(nullifier, recipient, amount, currency, proof);We provide comprehensive test coverage (>80%) including:
forge test --match-contract ZKPrivacyHookTest -vforge test --match-contract ZKPrivacyHookIntegrationTest -vforge test --match-contract ZKPrivacyHookFuzzTest -vforge coverage- 38 tests passed out of 47 total tests
- 80%+ code coverage across core functionality
- Property-based testing for edge cases
- Gas optimization testing for efficient operations
Create a .env file:
PRIVATE_KEY=your_private_key
POOL_MANAGER=pool_manager_address
RPC_URL=your_rpc_urlKey settings in foundry.toml:
[profile.default]
solc_version = "0.8.26"
evm_version = "cancun"
via_ir = true
ffi = true- Simplified ZK verification for demonstration
- Basic commitment scheme using keccak256
- Nullifier tracking to prevent double-spending
- Real ZK proof system (circom/snarkjs integration)
- Formal verification of cryptographic primitives
- Security audit by specialized firms
- Trusted setup ceremony for production deployment
- Mock proof verification (not production-ready)
- Limited scalability without proper ZK backend
- Requires careful secret management by users
βββ src/
β βββ ZKPrivacyHook.sol # Main hook contract
βββ test/
β βββ ZKPrivacyHook.t.sol # Unit tests
β βββ ZKPrivacyHookIntegration.t.sol # Integration tests
β βββ ZKPrivacyHookFuzz.t.sol # Fuzz tests
β βββ mocks/
β βββ MockZKVerifier.sol # Mock verifier for testing
βββ script/
β βββ DeployZKPrivacyHook.s.sol # Deployment script
βββ lib/ # Dependencies
beforeSwap: true // Custom swap logic
afterSwap: true // Post-swap processing
beforeSwapReturnDelta: true // Return custom deltas- Deposit Gas Cost: ~180k gas
- Swap Gas Cost: ~350k gas (including proof verification)
- Withdrawal Gas Cost: ~200k gas
- Proof Verification: ~150k gas
We welcome contributions! Please see our Contributing Guidelines.
- Fork the repository
- Create a feature branch
- Write tests for new functionality
- Ensure all tests pass
- Submit a pull request
- Follow Solidity Style Guide
- Add comprehensive tests for new features
- Document all public functions with NatSpec
- Ensure gas efficiency in critical paths
- Basic hook structure
- Commitment/nullifier system
- Mock proof verification
- Comprehensive testing
- Circom circuit development
- Trusted setup ceremony
- Real proof generation/verification
- Performance optimization
- Security audit
- Formal verification
- Mainnet deployment
- User interface development
This is experimental software in active development. The current implementation uses mock ZK proofs for demonstration purposes and is NOT suitable for production use without proper ZK proof integration and security audits.
This project is licensed under the MIT License - see the LICENSE file for details.
- Uniswap V4 for the hook architecture
- Tornado Cash for privacy inspiration
- Foundry for development tooling