@tokamak-network/commit-reveal2-contracts
v1.0.0
Published
Commit-Reveal2 DRB contracts: Solidity source and pre-built artifacts
Keywords
Readme
Commit-Reveal²
A provably secure distributed randomness generation protocol with randomized reveal order for mitigating last-revealer attacks
📋 Table of Contents
- Commit-Reveal²
Overview
Commit-Reveal² is an innovative distributed randomness generation protocol implemented as a smart contract on Ethereum. This protocol extends the traditional Commit-Reveal mechanism by introducing a two-layer reveal process that effectively mitigates the "last revealer attack" - a critical vulnerability in conventional randomness generation systems.
Problem & Solution
🔴 The Last Revealer Problem Traditional Commit-Reveal mechanisms suffer from poor liveness guarantees. When generating randomness for blockchain applications, a malicious actor who reveals last can choose whether to reveal their secret based on the potential result, creating unfair advantages when financial incentives are involved.
✅ The Commit-Reveal² Solution Our protocol employs a dual-phase approach:
- First Layer: Participants commit and reveal their initial values, generating an intermediate random value (Ωᵥ)
- Second Layer: The intermediate randomness determines the reveal order for the final phase, preventing adversaries from positioning themselves as the last revealer
Key Features
- 🛡️ Provably Secure: Cryptographically secure against manipulation attempts
- ⚡ Gas Efficient: Hybrid off-chain/on-chain model reduces gas costs
- 🔄 Randomized Reveal Order: Uses
dᵢ = hash(|Ωᵥ - cᵥ,ᵢ|)to determine reveal sequence - 📝 Signatures: Secure, replay-resistant authentication
- 🚨 Comprehensive Dispute Resolution: Handles participant and leader failures gracefully
- 💰 Economic Incentives: Deposit requirements and slashing mechanisms
Architecture
Contract Hierarchy
CommitReveal2.sol (Main Entry Point)
├── FailLogics.sol (Failure Recovery)
│ ├── DisputeLogics.sol (Dispute Resolution)
│ │ ├── OperatorManager.sol (Node Management)
│ │ └── CommitReveal2Storage.sol (State Management)
│ │ └── EIP712 (Signature Verification)Quick Start
# Clone the repository
git clone <repository-url>
cd Commit-Reveal2
# Run complete setup (recommended for first-time users)
make allThis command automatically:
- 🧹 Cleans the project
- 🗑️ Removes existing dependencies
- 📦 Installs fresh dependencies
- 🔄 Updates dependencies
- 🔨 Builds the project
Installation
Option 1: Standard Install
make install
make buildOption 2: Clean Install (if standard fails)
make install-clean
make buildOption 3: Manual Commands
make clean
make remove
make install
make update
make buildTesting
Run All Tests
make testRun Specific Test Suites
# Gas analysis tests
forge test --match-path "test/gas/*" -vv --gas-limit 9999999999999999999 --isolate
# Manuscript-specific gas tests (see Gas Analysis section)
forge test --match-path "test/gas/ForManuscriptGas.t.sol" -vv --gas-limit 9999999999999999999 --isolate
# Protocol flow tests
forge test --mp test/staging/CommitReveal2Flowchart.t.sol -vvv --gas-limit 9999999999999999999
# Fuzz tests
forge test --match-path "test/fuzz/*"Gas Analysis
Gas Report Files
output/gasreport.json- Main gas analysis resultsoutput/gasreportForManuscript.json- Manuscript-specific analysis
Protocol Flow
The protocol operates in three main phases:
Phase 1: Commit
- Generate secret:
Sᵢ = Gen() - Create commitments:
Cₒ,ᵢ = hash(Sᵢ)Cᵥ,ᵢ = hash(Cₒ,ᵢ)
- Submit Merkle Root (leader)
Phase 2: Reveal-1
- Broadcast
Cₒ,ᵢ - Verify:
hash(Cₒ,ᵢ) = Cᵥ,ᵢ - Calculate reveal order:
Ωᵥ = hash(Cₒ,₁||...||Cₒ,ₙ)dᵢ = hash(|Ωᵥ - Cᵥ,ᵢ|)- Sort by descending
dᵢvalues
Phase 3: Reveal-2
- Broadcast
Sᵢaccording to reveal order - Verify:
hash(Sᵢ) = Cₒ,ᵢandi = π(k) - Generate random number:
Ωₒ = hash(S₁||...||Sₙ)
Deployment
Environment Setup
Create a .env file:
# Deployer Configuration
PRIVATE_KEY=<your-private-key>
DEPLOYER=<your-eoa-address>
# Ethereum Sepolia
ETHERSCAN_API_KEY=<etherscan-api-key>
SEPOLIA_RPC_URL=<sepolia-rpc-url>
# Optimism Sepolia
OP_SEPOLIA_RPC_URL=<op-sepolia-rpc-url>
OP_ETHERSCAN_API_KEY=<op-etherscan-api-key>Deploy to Networks
# Local (Anvil)
make anvil # In terminal 1
make deploy # In terminal 2
# Testnets
make deploy ARGS="--network sepolia"
make deploy ARGS="--network opsepolia"Documentation
Resources
- 📊 Protocol Flowchart - Visual representation of all protocol states
- 📄 ICBC 2025 Paper - Academic publication
- 📖 Medium Article - Non-technical overview
Contributing
We welcome contributions! Please follow these steps:
- Fork the repository
- Create a feature branch:
git checkout -b feature-name - Make changes and test:
make all && make test - Commit changes:
git commit -m "feat: description" - Push to branch:
git push origin feature-name - Submit a pull request
Reporting Issues
Create issues on the GitHub repository
Contact
- Justin: [email protected] | [email protected]
- Suhyeon: [email protected]
