ZKP-License-System/setup.sh

296 lines
No EOL
7.4 KiB
Bash

#!/bin/bash
# License Verification ZKP System - Setup Script
set -e
echo "======================================"
echo "License Verification ZKP System Setup"
echo "======================================"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Check prerequisites
echo -e "\n${YELLOW}Checking prerequisites...${NC}"
if ! command -v docker &> /dev/null; then
echo -e "${RED}Docker is not installed. Please install Docker first.${NC}"
exit 1
fi
if ! command -v docker compose &> /dev/null; then
echo -e "${RED}Docker Compose is not installed. Please install Docker Compose first.${NC}"
exit 1
fi
echo -e "${GREEN}✓ Docker and Docker Compose found${NC}"
# Create directory structure
echo -e "\n${YELLOW}Creating directory structure...${NC}"
mkdir -p db/init
mkdir -p zkp-service/src
mkdir -p zkp-service/circuits
mkdir -p zkp-service/keys
mkdir -p zkp-service/scripts
mkdir -p merkle-service/src
mkdir -p test-frontend/src
mkdir -p monitoring
echo -e "${GREEN}✓ Directories created${NC}"
# Create environment file
echo -e "\n${YELLOW}Creating environment configuration...${NC}"
cat > .env << 'EOF'
# Database Configuration
POSTGRES_USER=license_admin
POSTGRES_PASSWORD=secure_license_pass_123
POSTGRES_DB=license_verification
DATABASE_URL=postgresql://license_admin:secure_license_pass_123@postgres:5432/license_verification
# ZKP Service Configuration
CIRCUIT_PATH=/app/circuits
PROVING_KEYS_PATH=/app/keys
NODE_ENV=development
LOG_LEVEL=info
# Merkle Tree Configuration
TREE_DEPTH=17
UPDATE_INTERVAL=3600
# Redis Configuration
REDIS_URL=redis://redis:6379
# Service Ports
ZKP_PORT=8080
MERKLE_PORT=8082
FRONTEND_PORT=3000
EOF
echo -e "${GREEN}✓ Environment file created${NC}"
# Create simple Circom circuit for testing
echo -e "\n${YELLOW}Creating test circuit...${NC}"
cat > zkp-service/circuits/license_verification.circom << 'EOF'
pragma circom 2.0.0;
include "../../node_modules/circomlib/circuits/poseidon.circom";
include "../../node_modules/circomlib/circuits/comparators.circom";
template LicenseVerification() {
// Simple version for testing - expand for production
signal input licenseNumber;
signal input expiryDate;
signal input currentDate;
// Public inputs
signal input merkleRoot;
// Check if license is not expired
component notExpired = GreaterThan(32);
notExpired.in[0] <== expiryDate;
notExpired.in[1] <== currentDate;
// Output validity
signal output isValid;
isValid <== notExpired.out;
}
component main = LicenseVerification();
EOF
echo -e "${GREEN}✓ Test circuit created${NC}"
# Create circuit compilation script
echo -e "\n${YELLOW}Creating circuit compilation script...${NC}"
cat > zkp-service/scripts/compile-circuits.js << 'EOF'
#!/usr/bin/env node
const { exec } = require('child_process');
const path = require('path');
const fs = require('fs');
async function compileCircuit() {
console.log('Compiling circuit...');
const circuitPath = path.join(__dirname, '../circuits/license_verification.circom');
const outputDir = path.join(__dirname, '../circuits');
// Compile circuit
exec(`circom ${circuitPath} --r1cs --wasm --sym -o ${outputDir}`, (error, stdout, stderr) => {
if (error) {
console.error(`Compilation error: ${error}`);
return;
}
console.log('Circuit compiled successfully');
console.log(stdout);
});
}
compileCircuit();
EOF
chmod +x zkp-service/scripts/compile-circuits.js
echo -e "${GREEN}✓ Compilation script created${NC}"
# Create Makefile for easy commands
echo -e "\n${YELLOW}Creating Makefile...${NC}"
cat > Makefile << 'EOF'
.PHONY: help up down logs benchmark clean reset
help:
@echo "License Verification ZKP System - Available Commands:"
@echo " make up - Start all services"
@echo " make down - Stop all services"
@echo " make logs - View logs"
@echo " make benchmark - Run performance benchmark"
@echo " make clean - Clean up containers and volumes"
@echo " make reset - Full reset (removes all data)"
@echo " make db-init - Initialize database with test data"
@echo " make compile - Compile Circom circuits"
up:
docker compose up -d
@echo "Services starting... Check status with 'docker compose ps'"
down:
docker compose down
logs:
docker compose logs -f
benchmark:
docker compose exec zkp-engine npm run benchmark
clean:
docker compose down -v
rm -rf postgres_data redis_data proving_keys
reset: clean
rm -rf zkp-service/circuits/*.r1cs
rm -rf zkp-service/circuits/*.wasm
rm -rf zkp-service/circuits/*.sym
rm -rf zkp-service/keys/*
db-init:
docker compose exec postgres psql -U license_admin -d license_verification -c "SELECT populate_test_licenses(1000);"
@echo "Database populated with 1000 test licenses"
compile:
docker compose exec zkp-engine node scripts/compile-circuits.js
monitor:
@echo "Opening monitoring dashboard at http://localhost:9090"
@xdg-open http://localhost:9090 2>/dev/null || open http://localhost:9090 2>/dev/null || echo "Please open http://localhost:9090 in your browser"
EOF
echo -e "${GREEN}✓ Makefile created${NC}"
# Create README
echo -e "\n${YELLOW}Creating README...${NC}"
cat > README.md << 'EOF'
# License Verification ZKP System - Test Environment
## Quick Start
1. **Start the system:**
```bash
make up
```
2. **Initialize test data:**
```bash
make db-init
```
3. **Run benchmarks:**
```bash
make benchmark
```
## Architecture
- **PostgreSQL**: Stores licenses and Merkle tree structure
- **ZKP Engine**: Handles proof generation and verification
- **Redis**: Caches Merkle proofs for performance
- **Merkle Service**: Manages Merkle tree updates
## Performance Expectations
Based on your simple age circuit benchmarks:
- **Proof Generation**: 3,500-4,500ms (browser)
- **Proof Verification**: 25-40ms (server)
- **Memory Usage**: 150-250MB peak during generation
## Monitoring
View real-time metrics:
```bash
curl http://localhost:8081/metrics
```
View benchmark results:
```bash
curl http://localhost:8080/api/benchmark
```
## Database Access
```bash
docker compose exec postgres psql -U license_admin -d license_verification
```
Useful queries:
```sql
-- View benchmark results
SELECT * FROM benchmark_results ORDER BY created_at DESC LIMIT 10;
-- Check Merkle tree status
SELECT * FROM merkle_tree_stats;
-- View recent verifications
SELECT * FROM verification_audit ORDER BY created_at DESC LIMIT 10;
```
## Troubleshooting
If proof generation is too slow:
1. Check memory allocation: `docker stats`
2. Reduce tree depth in `.env` (affects max licenses)
3. Consider server-side proof generation
## Clean Up
```bash
make down # Stop services
make clean # Remove all data
make reset # Full system reset
```
EOF
echo -e "${GREEN}✓ README created${NC}"
# Final instructions
echo -e "\n${GREEN}======================================"
echo "Setup Complete!"
echo "======================================${NC}"
echo ""
echo "Next steps:"
echo "1. Start the services: ${YELLOW}make up${NC}"
echo "2. Wait for startup: ${YELLOW}docker compose ps${NC}"
echo "3. Initialize test data: ${YELLOW}make db-init${NC}"
echo "4. Run benchmarks: ${YELLOW}make benchmark${NC}"
echo ""
echo "Monitor logs: ${YELLOW}make logs${NC}"
echo "View metrics: ${YELLOW}curl http://localhost:8081/metrics${NC}"
echo ""
echo -e "${GREEN}Ready to test your license verification system!${NC}"