https://github.com/yujidong/Risk-HedgingProtocol
Trustworthy data trading with game-theoretic pricing, blockchain settlement, and TEE validation. Official implementation for IEEE IoT Journal submission.
https://github.com/yujidong/Risk-HedgingProtocol
blockchain data-trading ethereum game-theory hardhat iot machine-learning pytorch sgx smart-contracts trusted-execution-environment
Last synced: 5 months ago
JSON representation
Trustworthy data trading with game-theoretic pricing, blockchain settlement, and TEE validation. Official implementation for IEEE IoT Journal submission.
- Host: GitHub
- URL: https://github.com/yujidong/Risk-HedgingProtocol
- Owner: yujidong
- License: other
- Created: 2025-12-15T11:52:02.000Z (7 months ago)
- Default Branch: master
- Last Pushed: 2025-12-15T12:44:15.000Z (7 months ago)
- Last Synced: 2025-12-26T23:39:31.253Z (6 months ago)
- Topics: blockchain, data-trading, ethereum, game-theory, hardhat, iot, machine-learning, pytorch, sgx, smart-contracts, trusted-execution-environment
- Language: Python
- Size: 116 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Security: SECURITY.md
Awesome Lists containing this project
- Awesome-SGX-Open-Source - https://github.com/yujidong/Risk-HedgingProtocol
README
# Risk-Hedging Equity Protocol for IoT Data Trading
A complete blockchain-based implementation of the **Trustworthy Data Equity Protocol** combining smart contracts, TEE-secured computation, and AI-powered data analysis for secure IoT data trading.
> **๐ Paper**: This repository contains the official implementation for the paper *"Trustworthy Data Equity: A Retrospective Risk-Hedging Protocol for High-Entropy IoT Data Assets"* submitted to **IEEE Internet of Things Journal**.
## Overview
This project implements an end-to-end decentralized data trading system with three integrated components:
### ๐ 1. Blockchain Smart Contracts
- **Smart Contract Settlement**: Ethereum-compatible contracts handle order creation, escrow, and atomic payment
- **Multi-role Architecture**: Separate accounts for Owner, Buyer, Seller, and TEE Signer
- **Testnet Deployment**: Production-ready deployment on Sepolia, Arbitrum, and Optimism testnets
- **Comprehensive Benchmarks**: Gas cost and performance analysis across L1/L2 networks
### ๐ 2. TEE (Trusted Execution Environment)
- **Intel SGX Integration**: Secure model inference in hardware-protected enclaves via Gramine
- **Performance Benchmarking**: Native vs TEE overhead analysis (~17% inference overhead)
- **Cloud Deployment**: Automated Azure VM setup scripts for SGX-capable infrastructure
- **Cryptographic Attestation**: ECDSA signature generation for on-chain verification
### ๐ค 3. AI/ML Data Analysis
- **LSTM Traffic Prediction**: Deep learning models trained on PeMS traffic dataset
- **Noise Robustness Testing**: Validates protocol resilience to sensor noise and data quality issues
- **Game Theory Experiments**: Data scarcity and random drop scenario analysis
- **Automated Visualization**: Comprehensive plotting and results analysis tools
## ๐๏ธ Project Structure
```
Risk-HedgingProtocol/
โโโ contracts/ # ๐ Smart Contracts
โ โโโ protocol.sol # DataEquityProtocol (Solidity)
โ
โโโ test/ # ๐งช Contract Testing
โ โโโ DataEquityProtocol.test.js # Functional tests
โ โโโ DataEquityProtocol.benchmark.js # L1 performance benchmarks
โ โโโ DataEquityProtocol.benchmark-simple.js
โ
โโโ scripts/ # ๐ ๏ธ Utility Scripts
โ โโโ check-all-balances.js # Multi-account balance checker
โ โโโ compare_benchmarks.py # Benchmark results comparison
โ โโโ run_all_benchmarks.ps1 # Automated test runner
โ
โโโ tee/ # ๐ TEE Component (NEW!)
โ โโโ tee_benchmark.py # Main TEE benchmark script
โ โโโ Dockerfile.gramine # SGX container configuration
โ โโโ tee_benchmark.manifest.template # Gramine SGX manifest
โ โโโ create_azure_vm.ps1 # Azure SGX VM provisioning
โ โโโ run_cloud_test.sh # Automated TEE testing
โ โโโ data/ # PEMS08 dataset (shared with ML)
โ โโโ models/ # Trained LSTM models
โ โโโ results/ # TEE benchmark results
โ โโโ docs/ # TEE architecture documentation
โ
โโโ input/ # ๐ ML Datasets
โ โโโ pems-dataset/ # PeMS traffic dataset (PEMS03/04/07/08)
โ
โโโ output/ # ๐ Experiment Results
โ โโโ data/ # ML experiment results (JSON)
โ โโโ figures/ # Visualization plots (PNG)
โ โโโ benchmark/ # Blockchain benchmark results
โ
โโโ noise_robustness_experiment.py # ๐ค Main ML experiments
โโโ game_theory_data_scarcity.py # Game theory: data scarcity
โโโ game_theory_random_drop.py # Game theory: random drops
โโโ visualize_results.py # Results visualization
โ
โโโ hardhat.config.ts # Hardhat configuration (3 networks)
โโโ ACCOUNTS.md # Multi-account setup guide
โโโ DEPLOYMENT.md # Deployment instructions
โโโ README.md # This file
```
## Smart Contract Features
### DataEquityProtocol Contract
**Core Functions:**
- `createOrder()`: Buyer locks funds in escrow with pricing parameters
- `settleTransaction()`: TEE-signed utility score triggers atomic payment
- `refund()`: Timeout protection for buyers
- `setTEESigner()`: Admin function to update TEE public key
**Pricing Model:**
```
Final Payment = p_base + ฮฑ * k * u
Where:
- p_base: Base fee (fixed)
- ฮฑ: Equity share coefficient (0 โค ฮฑ โค 1)
- k: Utility-to-money conversion factor
- u: Data utility score (0 โค u โค 1, TEE-verified)
```
**Security:**
- ECDSA signature verification for TEE attestation
- ReentrancyGuard protection
- Ownable access control
- Nonce-based replay attack prevention
## ๐ Quick Start Guide
### Prerequisites
- **Node.js** 22+ and npm 11+ (blockchain development)
- **Python** 3.9+ with conda (ML experiments)
- **Docker** (optional, for TEE testing)
- **Azure Account** (optional, for SGX cloud deployment)
### 1๏ธโฃ Blockchain Setup
```bash
# Install dependencies
npm install
# Compile contracts
npx hardhat compile
# Run functional tests
npx hardhat test
# Run performance benchmarks (requires testnet ETH)
npx hardhat test test/DataEquityProtocol.benchmark.js --network sepolia
```
### 2๏ธโฃ ML/AI Setup
```bash
# Create conda environment
conda env create -f environment.yml
conda activate risk-hedging
# Run noise robustness experiments
python noise_robustness_experiment.py
# Run game theory analysis
python game_theory_data_scarcity.py
python game_theory_random_drop.py
# Generate visualizations
python visualize_results.py
```
### 3๏ธโฃ TEE Setup (Advanced)
```bash
cd tee/
# Option A: Local testing with dummy data
python tee_benchmark.py --mode native --iterations 10
# Option B: Cloud SGX deployment
# 1. Create Azure VM with SGX support
.\create_azure_vm.ps1
# 2. Deploy and run tests
.\redeploy_all.ps1
# 3. SSH to VM and execute
ssh azureuser@
chmod +x run_cloud_test.sh
./run_cloud_test.sh
# Results will be in tee/results/*.json
```
## ๐ Complete Workflow Example
### End-to-End Data Trading Simulation
**Step 1: Generate Training Data**
```bash
# ML experiments produce utility scores and model performance
python noise_robustness_experiment.py
# Output: output/data/*.json
```
**Step 2: Deploy Smart Contract**
```bash
# Set up accounts and deploy to testnet
npx hardhat keystore set SEPOLIA_PRIVATE_KEY --dev
npx hardhat ignition deploy ignition/modules/DataEquityProtocol.ts --network sepolia
# Contract address: 0xE0aa880da6822A26C946f9417F7F6380FDf9799F (example)
```
**Step 3: TEE Utility Evaluation**
```bash
cd tee/
# TEE validates data and signs utility score
python tee_benchmark.py --mode native
# Output: Utility score (0.80) + ECDSA signature
```
**Step 4: On-Chain Settlement**
```bash
# Buyer creates order with locked funds
# TEE submits signed utility score
# Smart contract verifies signature and executes payment
npx hardhat test test/DataEquityProtocol.test.js --network sepolia
# โ
Order settled: Seller receives payment based on utility
```
**Step 5: Analyze Results**
```bash
# Compare blockchain benchmarks
python scripts/compare_benchmarks.py
# Visualize all experimental data
python visualize_results.py
```
## ๐ Detailed Usage
## ๐ Detailed Usage
### Blockchain Component
#### Deploy to Public Testnet
```bash
# Configure accounts (Owner, Buyer, Seller, TEE Signer)
# See ACCOUNTS.md for multi-account setup guide
npx hardhat keystore set SEPOLIA_PRIVATE_KEY
npx hardhat keystore set SEPOLIA_PRIVATE_KEY_2
npx hardhat keystore set SEPOLIA_PRIVATE_KEY_3
npx hardhat keystore set SEPOLIA_PRIVATE_KEY_4
# Check balances
npx hardhat run scripts/check-all-balances.js --network sepolia
# Deploy contract
npx hardhat ignition deploy ignition/modules/DataEquityProtocol.ts --network sepolia
# Run comprehensive benchmarks
npx hardhat test test/DataEquityProtocol.benchmark.js --network sepolia
```
**Benchmark Results** (Sepolia L1 @ 3 gwei):
- Deployment: 21,000 gas (~$0.06)
- Order Creation: 273,077 gas (~$2.46)
- Settlement: 73,708 gas (~$0.63)
- Refund: 44,745 gas (~$0.41)
See [DEPLOYMENT.md](DEPLOYMENT.md) for complete deployment guide.
### AI/ML Component
#### LSTM Noise Robustness Experiments
```bash
# Train and evaluate LSTM models with noise injection
python noise_robustness_experiment.py
# Key parameters:
# - Noise levels: 0%, 10%, 20%, 30%, 40%, 50%
# - Datasets: PEMS03, PEMS04, PEMS07, PEMS08
# - Model: 2-layer LSTM (hidden_size=256)
# Output: output/data/*_results.json
# Metrics: MAE, RMSE, R2 score per noise level
```
#### Game Theory Experiments
```bash
# Data scarcity scenarios (50-100% data availability)
python game_theory_data_scarcity.py
# Random drop scenarios (probability-based data loss)
python game_theory_random_drop.py
# Generate comparison plots
python visualize_results.py
```
### TEE Component
#### Local Testing (Without SGX Hardware)
```bash
cd tee/
# Create dummy data for testing
python tee_benchmark.py --mode native --iterations 10 --use-dummy-data
# Output: results/native_*.json
```
#### Cloud SGX Deployment (Production)
**Prerequisites**:
- Azure account with quota for DC-series VMs
- Azure CLI installed and authenticated
- SSH key pair configured
**Deployment Steps**:
```bash
cd tee/
# 1. Provision Azure SGX VM
.\create_azure_vm.ps1
# Creates: Standard_DC2s_v3 VM with 8GB SGX EPC
# 2. Deploy code and dependencies
.\redeploy_all.ps1
# Uploads: tee_benchmark.py, data/, models/, configs
# 3. SSH to VM and run tests
ssh azureuser@
# 4. Run native baseline
docker run --rm -v $(pwd):/app tee-benchmark:native python tee_benchmark.py
# 5. Run SGX enclave test
docker run --device /dev/sgx_enclave --device /dev/sgx_provision \
-v $(pwd):/app tee-benchmark:sgx gramine-sgx python
# 6. Retrieve results
exit
scp azureuser@:~/results/*.json ./results/
# 7. Stop VM to avoid charges
az vm deallocate --resource-group web3 --name tee-benchmark-vm
```
**TEE Performance Results**:
- Native Inference: 547ms ยฑ 18ms
- SGX Inference: 642ms ยฑ 5ms (+17.3% overhead)
- Model Accuracy: Identical (Rยฒ = 0.8063)
- Memory Overhead: +18.2%
See [tee/docs/ARCHITECTURE.md](tee/docs/ARCHITECTURE.md) for technical details.
## ๐งช Experimental Results
## ๐งช Experimental Results Summary
### 1. Blockchain Performance (Sepolia L1 Testnet)
| Operation | Gas Used | Cost (@3000 gwei) | Time |
|-----------|----------|-------------------|------|
| Contract Deployment | 21,000 | $0.06 | ~11s |
| Order Creation | 273,077 | $2.46 | ~2s |
| Order Settlement | 73,708 | $0.63 | ~2s |
| Refund | 44,745 | $0.41 | ~2s |
| **Complete Trade** | **346,785** | **$3.09** | **~4s** |
๐ก **L2 Recommendation**: Deploying to Arbitrum/Optimism can reduce costs by 95% (~$0.15/trade)
### 2. TEE Performance (Azure DC2s_v3 + Intel SGX)
| Metric | Native Docker | Gramine SGX | Overhead |
|--------|---------------|-------------|----------|
| Inference Time | 547ms ยฑ 18ms | 642ms ยฑ 5ms | **+17.3%** |
| Model Accuracy (Rยฒ) | 0.8063 | 0.8063 | **Identical** |
| Memory Usage | 2047 MB | 2420 MB | +18.2% |
| Signature Generation | 13ms ยฑ 3ms | 8ms ยฑ 2ms | -38% |
๐ **Key Finding**: Only **17% inference overhead** in real SGX hardware with **identical accuracy**
### 3. AI/ML Noise Robustness
**LSTM Model Performance under Noise:**
| Noise Level | MAE | RMSE | Rยฒ Score | Utility Score |
|-------------|-----|------|----------|---------------|
| 0% (Clean) | 2.87 | 4.12 | 0.953 | 1.00 |
| 10% | 3.02 | 4.31 | 0.945 | 0.95 |
| 20% | 3.24 | 4.58 | 0.932 | 0.89 |
| 30% | 3.51 | 4.91 | 0.915 | 0.81 |
| 40% | 3.89 | 5.34 | 0.891 | 0.72 |
| 50% | 4.42 | 5.98 | 0.852 | 0.61 |
โ
**Protocol Stability**: Maintains reliable utility scores up to 40% noise level
**Game Theory Results**:
- Data scarcity: Linear degradation from 100% โ 50% data availability
- Random drops: Exponential impact on utility with drop probability > 0.3
All results available in `output/data/*.json` and `tee/results/*.json`
## ๐๏ธ System Architecture
## ๐งฉ Key Components Integration
### Data Flow
1. **Data Generation** (ML Component)
- IoT sensors โ PeMS traffic dataset
- LSTM training & validation
- Noise injection experiments
- Output: Utility metrics (Rยฒ, MAE, RMSE)
2. **TEE Validation** (Security Component)
- Load trained LSTM model into SGX enclave
- Secure inference on encrypted data
- Generate utility score (u = 0.0 to 1.0)
- Sign with TEE private key โ ECDSA signature
3. **Blockchain Settlement** (Smart Contract)
- Buyer creates order: locks `maxDeposit` ETH
- Sets pricing: `p_base`, `ฮฑ`, `k`
- TEE submits: `(orderId, utility, signature)`
- Contract verifies signature
- Calculates: `payment = p_base + ฮฑ * k * u`
- Transfers to Seller, refunds excess to Buyer
### Pricing Formula
```
P = p_base + ฮฑ ร k ร u
Where:
- p_base: Base fee (guaranteed minimum payment)
- ฮฑ: Equity share [0, 1] (risk-sharing coefficient)
- k: Utility-to-money conversion factor (scaling parameter)
- u: Data utility score [0, 1] (TEE-verified quality metric)
```
**Example**:
- `p_base = 0.01 ETH`, `ฮฑ = 0.5`, `k = 0.005 ETH`, `u = 0.95`
- **Payment** = 0.01 + 0.5 ร 0.005 ร 0.95 = **0.012375 ETH**
## ๐ ๏ธ Technology Stack
**Blockchain:**
- Solidity 0.8.28 - Smart contract language
- Hardhat 3.1.0 - Development framework
- Viem 2.41.2 - Type-safe Ethereum library
- OpenZeppelin - Security-audited contracts
- Sepolia/Arbitrum/Optimism - Multi-testnet support
**TEE:**
- Intel SGX - Hardware-based trusted execution
- Gramine 1.7+ - SGX library OS
- Docker - Containerization
- Azure DC-series - SGX-capable VMs
**AI/ML:**
- PyTorch 2.5.1 - Deep learning framework
- CUDA 12.4 - GPU acceleration
- scikit-learn - ML utilities
- NumPy/Pandas - Data processing
- Matplotlib - Visualization
## ๐ Security Features
1. **TEE Signature Verification**: ECDSA signature validation for utility scores
2. **Escrow Protection**: Funds locked until settlement or timeout refund
3. **Replay Attack Prevention**: Unique nonce per transaction
4. **Reentrancy Guard**: Protection against recursive call attacks
5. **Access Control**: Owner-only admin functions (Ownable pattern)
6. **Atomic Settlement**: All-or-nothing payment execution
## ๐ง Future Enhancements
- [ ] Multi-TEE consensus mechanism
- [ ] Layer 2 deployment (Arbitrum/Optimism mainnet)
- [ ] Real-time IoT device integration
- [ ] Dynamic pricing with market-based ฮฑ
- [ ] Cross-chain settlement support
- [ ] Advanced data quality metrics
## ๐ License
MIT License - See [LICENSE](LICENSE)
## ๐ Useful Links
**Testnet Faucets:**
- Sepolia: https://sepoliafaucet.com/
- Arbitrum Sepolia: https://bridge.arbitrum.io/
- Optimism Sepolia: https://app.optimism.io/bridge
**Block Explorers:**
- Sepolia: https://sepolia.etherscan.io/
- Arbitrum: https://sepolia.arbiscan.io/
- Optimism: https://sepolia-optimism.etherscan.io/
**Datasets:**
- PeMS Traffic: http://pems.dot.ca.gov/
- Azure SGX Docs: https://learn.microsoft.com/en-us/azure/virtual-machines/dcv3-series
---