Skip to main content

Dual-Verifiable Framework for Federated Learning using Zero-Knowledge Proofs

Project description

๐Ÿ” Secure FL: Dual-Verifiable Federated Learning with Zero-Knowledge Proofs

This repository contains a complete implementation of a secure federated learning framework that uses dual zero-knowledge proof verification to ensure both client-side training correctness and server-side aggregation integrity.

๐ŸŽฏ Key Features

  • ๐Ÿ›ก๏ธ Dual ZKP Verification: Client-side zk-STARKs + Server-side zk-SNARKs
  • ๐Ÿš€ FedJSCM Aggregation: Momentum-based federated aggregation for improved convergence
  • ๐Ÿ“Š Dynamic Proof Rigor: Adaptive proof complexity based on training stability
  • ๐Ÿ”— Blockchain Integration: On-chain verification for public auditability
  • ๐Ÿ“ˆ Comprehensive Experiments: Built-in benchmarking and visualization tools

๐Ÿ”ง Prerequisites

Zero-Knowledge Proof Tools

This project requires specific ZKP tools for circuit compilation and proof generation:

  • Circom (Rust-based): Circuit compiler for zero-knowledge proofs
  • SnarkJS: JavaScript library for zk-SNARK operations

Quick Setup

# Install Rust (required for circom)
curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh

# Install circom from source
git clone https://github.com/iden3/circom.git
cd circom && cargo build --release && cargo install --path circom

# Install snarkjs via npm
npm install -g snarkjs

# Verify installation
uv run python -m secure_fl.setup check

๐Ÿ“– Detailed Setup Guide: See docs/ZKP_SETUP.md for comprehensive installation instructions, troubleshooting, and platform-specific guidance.

CI/CD Testing: Automated tests run on Ubuntu and macOS. Windows support is available but requires manual setup and verification.

๐Ÿ—๏ธ Architecture Overview

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Client 1      โ”‚    โ”‚   Client 2      โ”‚    โ”‚   Client N      โ”‚
โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚    โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚    โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚ โ”‚Local Trainingโ”‚ โ”‚    โ”‚ โ”‚Local Trainingโ”‚ โ”‚    โ”‚ โ”‚Local Trainingโ”‚ โ”‚
โ”‚ โ”‚ + zk-STARK  โ”‚ โ”‚    โ”‚ โ”‚ + zk-STARK  โ”‚ โ”‚    โ”‚ โ”‚ + zk-STARK  โ”‚ โ”‚
โ”‚ โ”‚   Proof     โ”‚ โ”‚    โ”‚ โ”‚   Proof     โ”‚ โ”‚    โ”‚ โ”‚   Proof     โ”‚ โ”‚
โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚    โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚    โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
          โ”‚                      โ”‚                      โ”‚
          โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                 โ”‚
                    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                    โ”‚     FL Server           โ”‚
                    โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
                    โ”‚ โ”‚ FedJSCM Aggregation โ”‚ โ”‚
                    โ”‚ โ”‚   + zk-SNARK Proof  โ”‚ โ”‚
                    โ”‚ โ”‚ + Stability Monitor โ”‚ โ”‚
                    โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
                    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                 โ”‚
                    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                    โ”‚   Blockchain Verifier   โ”‚
                    โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
                    โ”‚ โ”‚  Smart Contract     โ”‚ โ”‚
                    โ”‚ โ”‚ Proof Verification  โ”‚ โ”‚
                    โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
                    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ“Œ Contributors


๐Ÿ“‚ Repository Structure

secure-fl/
โ”œโ”€โ”€ ๐Ÿ“ docs/              # Research papers and documentation
โ”‚   โ”œโ”€โ”€ concept-note/     # Initial concept and motivation
โ”‚   โ”œโ”€โ”€ project-proposal/ # Detailed project proposal
โ”‚   โ””โ”€โ”€ proposal-defense/ # Defense materials
โ”œโ”€โ”€ ๐Ÿ“ fl/               # Core federated learning implementation
โ”‚   โ”œโ”€โ”€ server.py        # FL server with FedJSCM and ZKP integration
โ”‚   โ”œโ”€โ”€ client.py        # FL client with zk-STARK proof generation
โ”‚   โ”œโ”€โ”€ aggregation.py   # FedJSCM momentum-based aggregation
โ”‚   โ”œโ”€โ”€ proof_manager.py # ZKP proof generation and verification
โ”‚   โ”œโ”€โ”€ stability_monitor.py # Dynamic proof rigor adjustment
โ”‚   โ”œโ”€โ”€ quantization.py  # Parameter quantization for circuits
โ”‚   โ””โ”€โ”€ utils.py         # Utility functions
โ”œโ”€โ”€ ๐Ÿ“ proofs/           # Zero-knowledge proof circuits
โ”‚   โ”œโ”€โ”€ client/          # zk-STARK circuits (Cairo)
โ”‚   โ”‚   โ””โ”€โ”€ sgd_full_trace.cairo
โ”‚   โ””โ”€โ”€ server/          # zk-SNARK circuits (Circom)
โ”‚       โ””โ”€โ”€ fedjscm_aggregation.circom
โ”œโ”€โ”€ ๐Ÿ“ blockchain/       # Smart contracts for verification
โ”‚   โ””โ”€โ”€ FLVerifier.sol   # Solidity contract for proof verification
โ”œโ”€โ”€ ๐Ÿ“ experiments/      # Experiment scripts and configs
โ”‚   โ”œโ”€โ”€ train_secure_fl.py # Main training experiment
โ”‚   โ””โ”€โ”€ config.yaml      # Experiment configuration
โ”œโ”€โ”€ ๐Ÿ“ k8s/             # Kubernetes deployment manifests
โ”œโ”€โ”€ ๐Ÿ“ infra/           # Infrastructure as Code configs
โ”œโ”€โ”€ requirements.txt     # Python dependencies
โ”œโ”€โ”€ .gitignore
โ””โ”€โ”€ README.md

๐Ÿš€ Quick Start

Prerequisites

  • Python 3.8+
  • Node.js (for Circom/SnarkJS)
  • Cairo compiler (for zk-STARKs)
  • CUDA-capable GPU (optional, for acceleration)

Installation

Option 1: Install from PyPI (Recommended)

# Install the package
pip install secure-fl

# Setup ZKP tools (optional but recommended)
secure-fl setup zkp

# Run a quick demo
secure-fl demo

Option 2: Install with PDM (For Development)

# Clone the repository
git clone https://github.com/krishantt/secure-fl.git
cd secure-fl

# Install PDM if you don't have it
pip install pdm

# Install dependencies
pdm install

# Setup ZKP tools
pdm run setup-zkp

# Run tests
pdm run test

Option 3: Install from Source

# Clone the repository
git clone https://github.com/krishantt/secure-fl.git
cd secure-fl

# Install in development mode
pip install -e .

# Setup environment
secure-fl setup full

Research and Development

For research purposes, the project includes a comprehensive experiments directory with multi-dataset benchmarking:

# Run multi-dataset benchmark (development only)
cd secure-fl
python experiments/benchmark.py --datasets mnist cifar10 synthetic

# Quick benchmark
python experiments/benchmark.py --quick --configs baseline_iid

# See experiments/README.md for full documentation

Note: The experiments/ directory is excluded from package distribution and contains standalone research scripts.

Basic Usage

Command Line Interface

# Run a quick demo
secure-fl demo

# Run a federated learning experiment
secure-fl experiment --num-clients 3 --rounds 5 --dataset synthetic

# Start a server
secure-fl server --rounds 10 --enable-zkp

# Connect a client
secure-fl client --client-id client_1 --dataset mnist

# Check system requirements
secure-fl setup check

Python API

from secure_fl import SecureFlowerServer, create_client, create_server_strategy
import torch.nn as nn

# Define your model
class MyModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc = nn.Linear(784, 10)
    
    def forward(self, x):
        return self.fc(x.flatten(1))

# Create server
strategy = create_server_strategy(
    model_fn=lambda: MyModel(),
    enable_zkp=True,
    proof_rigor="medium"
)
server = SecureFlowerServer(strategy=strategy)

# Create clients
client = create_client(
    client_id="client_1",
    model_fn=lambda: MyModel(),
    train_data=your_train_data,
    enable_zkp=True
)

๐Ÿ”ฌ Technical Details

Core Algorithms

FedJSCM Aggregation

Our momentum-based aggregation algorithm:

m^{(t+1)} = ฮณ ร— m^{(t)} + ฮฃ(p_i ร— ฮ”_i)
w^{(t+1)} = w^{(t)} + m^{(t+1)}

Where:

  • m^{(t)} is server momentum at round t
  • ฮณ is momentum coefficient (0.9 by default)
  • p_i are client weights (proportional to data size)
  • ฮ”_i are client parameter updates

Dynamic Proof Rigor

The system automatically adjusts proof complexity based on training stability:

  • High Rigor: Full SGD trace verification (early rounds, unstable training)
  • Medium Rigor: Single-step verification (moderate stability)
  • Low Rigor: Delta norm verification (stable/converged training)

Zero-Knowledge Proof Systems

Client-side zk-STARKs

  • Language: Cairo
  • Purpose: Prove correct local SGD training
  • Features:
    • Post-quantum secure
    • Transparent (no trusted setup)
    • Scalable verification

Server-side zk-SNARKs

  • Scheme: Groth16
  • Purpose: Prove correct FedJSCM aggregation
  • Features:
    • Succinct proofs (~200 bytes)
    • Fast verification
    • Blockchain-compatible

Security Guarantees

  1. Training Integrity: Clients cannot submit invalid parameter updates
  2. Aggregation Correctness: Server cannot manipulate aggregation process
  3. Data Privacy: No raw data is revealed, only computational correctness
  4. Public Auditability: All proofs can be verified on-chain

๐Ÿ“Š Experimental Results

Performance Benchmarks

Configuration Proof Time Verification Time Communication Overhead
High Rigor ~2.3s ~0.05s +15%
Medium Rigor ~0.8s ~0.02s +8%
Low Rigor ~0.3s ~0.01s +3%

Accuracy Comparison

Method MNIST CIFAR-10 MedMNIST
Standard FL 0.95 0.78 0.82
Secure FL (Ours) 0.94 0.76 0.81
Overhead -1% -2.6% -1.2%

๐Ÿ› ๏ธ Advanced Usage

Custom Model Integration

from secure_fl import create_server_strategy, SecureFlowerServer
import torch.nn as nn

class MyCustomModel(nn.Module):
    def __init__(self):
        super().__init__()
        # Your model definition
        self.conv1 = nn.Conv2d(3, 32, 3)
        self.fc1 = nn.Linear(32 * 30 * 30, 128)
        self.fc2 = nn.Linear(128, 10)
    
    def forward(self, x):
        # Your forward pass
        x = torch.relu(self.conv1(x))
        x = x.flatten(1)
        x = torch.relu(self.fc1(x))
        return self.fc2(x)

# Create server strategy
strategy = create_server_strategy(
    model_fn=lambda: MyCustomModel(),
    enable_zkp=True,
    proof_rigor="medium"
)

# Start server
server = SecureFlowerServer(strategy=strategy, num_rounds=20)
server.start()

Blockchain Deployment

// Deploy the FLVerifier contract
contract MyFLVerifier is FLVerifier {
    constructor() FLVerifier(
        3,  // min clients per round
        300,  // proof timeout (seconds)  
        0x1234...  // STARK verifying key hash
    ) {}
}

Parameter Sweeps

# config.yaml
parameter_sweep:
  enabled: true
  parameters:
    momentum: [0.5, 0.7, 0.9, 0.95]
    proof_rigor: ["low", "medium", "high"]
    num_clients: [3, 5, 10]

๐Ÿ”ง Configuration Reference

Core Parameters

Parameter Type Default Description
num_clients int 5 Number of federated clients
num_rounds int 10 Training rounds
enable_zkp bool true Enable zero-knowledge proofs
proof_rigor str "high" Proof complexity level
momentum float 0.9 FedJSCM momentum coefficient
blockchain_verification bool false Enable on-chain verification

ZKP Parameters

Parameter Type Default Description
quantization_bits int 8 Bits for parameter quantization
max_trace_length int 1024 Maximum STARK trace length
circuit_size int 1000 SNARK circuit constraint count
proof_timeout int 120 Proof generation timeout (seconds)

๐Ÿ› Troubleshooting

Common Issues

  1. Cairo Compilation Errors

    # Ensure Cairo is properly installed
    cairo-compile --version
    # Reinstall if needed
    pip uninstall cairo-lang && pip install cairo-lang
    
  2. Circom Circuit Compilation

    # Check Circom installation
    circom --version
    # Compile circuits manually
    cd proofs/server
    circom fedjscm_aggregation.circom --r1cs --wasm --sym
    
  3. Memory Issues with Large Models

    # Reduce model/circuit size in config.yaml
    model:
      hidden_dim: 64  # Reduce from default 128
    zkp:
      client_proof:
        max_trace_length: 512  # Reduce from 1024
    
  4. Client Connection Timeouts

    # Increase timeouts
    networking:
      client_timeout: 600  # Increase from 300
      max_retries: 5       # Increase from 3
    

๐Ÿ“ˆ Monitoring and Visualization

Built-in Metrics

The framework automatically tracks:

  • Training convergence (loss, accuracy)
  • Proof generation/verification times
  • Communication overhead
  • Client participation rates
  • Model parameter stability
  • Resource utilization

Custom Metrics

from secure_fl import StabilityMonitor

monitor = StabilityMonitor()
# Add custom metrics
monitor.update(parameters, round_num, custom_metrics={
    "gradient_norm": grad_norm,
    "privacy_budget": epsilon,
    "custom_score": score
})

๐Ÿค Contributing

We welcome contributions! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Development Setup

# Using PDM (recommended)
pdm install -d
pdm run test
pdm run format
pdm run lint

# Using pip
pip install -e ".[dev]"
pytest
black secure_fl/
isort secure_fl/
mypy secure_fl/

๐Ÿ“ Citation

If you use this work in your research, please cite:

@misc{timilsina2024secure,
  title={Dual-Verifiable Framework for Federated Learning using Zero-Knowledge Proofs},
  author={Timilsina, Krishant and Paudel, Bindu},
  year={2024},
  institution={Tribhuvan University, Institute of Engineering}
}

๐Ÿ“ฆ Package Information

PyPI Installation

pip install secure-fl

Development Installation

git clone https://github.com/krishantt/secure-fl.git
cd secure-fl
pdm install -d

Available Extras

  • dev: Development dependencies (pytest, black, mypy, etc.)
  • medical: Medical dataset support (medmnist, nibabel, etc.)
  • notebook: Jupyter notebook support
  • quantization: Advanced quantization tools
  • blockchain: Blockchain integration tools
  • all: All optional dependencies

Example: pip install "secure-fl[dev,medical,notebook]"

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ™ Acknowledgments

  • Flower for the federated learning framework
  • StarkWare for Cairo and STARK technology
  • iden3 for Circom and zk-SNARK tools
  • Our supervisor, Dr. Arun Kumar Timalsina, for guidance and support
  • Tribhuvan University, Institute of Engineering, Pulchowk Campus

โš ๏ธ Note: This is a research prototype. For production use, additional security audits and optimizations are recommended.

๐Ÿ“ซ Contact: For questions or collaborations, reach out to krishantt@example.com or bigya01@example.com

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

secure_fl-2025.12.11.dev1.tar.gz (4.9 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

secure_fl-2025.12.11.dev1-py3-none-any.whl (85.2 kB view details)

Uploaded Python 3

File details

Details for the file secure_fl-2025.12.11.dev1.tar.gz.

File metadata

  • Download URL: secure_fl-2025.12.11.dev1.tar.gz
  • Upload date:
  • Size: 4.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.16 {"installer":{"name":"uv","version":"0.9.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for secure_fl-2025.12.11.dev1.tar.gz
Algorithm Hash digest
SHA256 1900aa6afd3a51e3a7ce74457767f0bc1fee0b281e4c4b19691db0cce4761042
MD5 16427cce75d7224eda70249752b1b87d
BLAKE2b-256 5ee56750e2c819ecf86d4ce77ec6e13c492ed5459aa01e350f196b97a96a828e

See more details on using hashes here.

File details

Details for the file secure_fl-2025.12.11.dev1-py3-none-any.whl.

File metadata

  • Download URL: secure_fl-2025.12.11.dev1-py3-none-any.whl
  • Upload date:
  • Size: 85.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.16 {"installer":{"name":"uv","version":"0.9.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for secure_fl-2025.12.11.dev1-py3-none-any.whl
Algorithm Hash digest
SHA256 3175075d271ff94d03b6f788dd7d6a4fae6df7e525565f99fbc64881f5ce963d
MD5 e0b780d074c7e028ef28fa41cd313512
BLAKE2b-256 a0513d49f6c232c47357fe2f3824ec05102d269e364df2f54f8e864a56dae2a4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page