Skip to content

Mobile-IoT-Security-Lab/medchain-avitabile

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

280 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MedChain: Redactable Blockchain with Smart Contracts

Tests Python Coverage Contracts Solidity Coverage

A Python blockchain simulator implementing chameleon hash-based redaction with smart contract governance, zero-knowledge proofs, and medical data GDPR compliance.

Research Papers:

  • Ateniese et al. (2017) - Chameleon hash-based blockchain redaction
  • Avitabile et al. - Smart contract governance with zero-knowledge proofs

Key Innovation: Rewrite blockchain history without breaking the chain using cryptographic trapdoors.

Research Papers:

  • Ateniese et al. (2017) - Chameleon hash-based blockchain redaction
  • Avitabile et al. - Smart contract governance with zero-knowledge proofs

Quick Start

```bash git clone https://github.com/Mobile-IoT-Security-Lab/medchain-avitabile.git cd medchain-avitabile pip install -r requirements.txt python Main.py ```

Core Concepts

Chameleon Hash Redaction

``` CH(m, r) = g^m · PK^r mod p ```

  1. Select historical block for modification
  2. Compute new message (updated transactions + previous hash)
  3. Forge new randomness: `r2 = forge(SK, m1, r1, m2)`
  4. Result: `CH(m1, r1) = CH(m2, r2)` - same hash, different content
  5. Chain remains valid

Features

Feature Description
Redaction Types DELETE, MODIFY, ANONYMIZE
Smart Contracts Governance, audit, privacy compliance
ZK Proofs SNARKs for private verification
IPFS Integration Distributed storage with AES-GCM encryption
Medical Use Case GDPR Article 17 compliance
Roles ADMIN, REGULATOR, MINER, USER, OBSERVER

Usage

Basic Simulation

```bash python Main.py # Standard mode TESTING_MODE=1 python Main.py # Fast mode DRY_RUN=1 python Main.py # Preview config ```

Demos

```bash python -m demo.medchain_demo # Full workflow python -m demo.medical_redaction_demo # Medical redaction python -m demo.ipfs_demo # IPFS integration python ZK/SNARKs.py # SNARK proofs python ZK/ProofOfConsistency.py # Consistency verification ```

Configuration

Edit `InputsConfig.py`:

```python Nodes = 20 # Number of nodes Binterval = 10 # Block interval (seconds) Tn = 5 # Transactions per second hasRedact = True minRedactionApprovals = 2 hasSmartContracts = True hasPermissions = True ```

Environment Variables

```bash TESTING_MODE=1 # Fast mode USE_REAL_IPFS=1 # Real IPFS daemon USE_REAL_EVM=1 # Real blockchain
IPFS_ENC_KEY=KEY_HERE # AES-256 encryption key (base64) ```

Medical Use Case

GDPR Right to be Forgotten

engine = MedicalRedactionEngine()
record_id = engine.store_patient_data(patient_data)

request_id = engine.request_redaction(
    record_id=record_id,
    redaction_type="DELETE",
    reason="GDPR Article 17"
)

engine.approve_redaction(request_id, approver="REGULATOR")
engine.approve_redaction(request_id, approver="ADMIN")

proof = engine.execute_redaction(request_id)
assert engine.verify_consistency(proof)

Zero-Knowledge Proof Implementation

Overview

Phase 1 Complete: Real zero-knowledge proofs for data redaction are now implemented using Groth16 SNARKs.

Key Components

1. Medical Data Circuit Mapper (medical/circuit_mapper.py)

Bridges medical records and circom circuit inputs:

  • Converts medical data to BN254-compatible field elements
  • Splits 256-bit hashes into two 128-bit limbs
  • Applies redaction transformations (DELETE, ANONYMIZE, MODIFY)
  • Validates circuit inputs before proof generation
  • Ensures deterministic, reproducible proofs
from medical.circuit_mapper import MedicalDataCircuitMapper

mapper = MedicalDataCircuitMapper()
inputs = mapper.prepare_circuit_inputs(
    medical_record_dict={"patient_id": "PAT_001", "diagnosis": "..."},
    redaction_type="ANONYMIZE",
    policy_hash="policy_gdpr"
)

if mapper.validate_circuit_inputs(inputs):
    # Use inputs for proof generation
    pass

2. Enhanced Hybrid SNARK Manager (medical/my_snark_manager.py)

SNARK manager dedicated to real Groth16 proof generation:

  • Uses snarkjs with the prepared circuit inputs from the mapper
  • Requires compiled artifacts (redaction.wasm, redaction_final.zkey, verification_key.json)
  • Integrates medical circuit mapper for deterministic witness construction
  • Emits detailed logging and diagnostics
from medical.my_snark_manager import EnhancedHybridSNARKManager

manager = EnhancedHybridSNARKManager(snark_client)
proof = manager.create_redaction_proof(redaction_data)

# Check operation mode
mode_info = manager.get_mode_info()
print(f"Operating in {mode_info['mode']} mode")

Testing ZK Proofs

# Test circuit mapper
pytest tests/test_circuit_mapper.py -v

# Run full pipeline tests (requires compiled circuit artifacts)
pytest tests/test_avitabile_redaction_demo.py -v

Preparing SNARK Artifacts

  1. Compile the circuit:

    cd circuits && ./scripts/compile.sh
  2. Run Groth16 setup:

    PTAU=../tools/pot14_final.ptau ./scripts/setup.sh
  3. Verify setup:

    ls circuits/build/
    # Should see: redaction.wasm, redaction_final.zkey, verification_key.json

What This Achieves

Immediate Benefits:

  • Real Groth16 SNARK proofs (no longer simulated)
  • Proper medical data to circuit input mapping
  • Validation before expensive proof generation
  • Flexible mode switching (real/simulation)
  • Production-ready architecture

Compliance Benefits:

  • GDPR Article 17: Cryptographic proof of lawful redaction
  • Auditability: Complete proof metadata trail
  • Non-repudiation: Cryptographically binding proofs
  • Privacy: Zero-knowledge property protects sensitive data

Technical Achievements:

  • Resolved all TODO placeholders in SNARK flow
  • 95%+ test coverage for new components
  • Complete documentation (see docs/ZK_PROOF_IMPLEMENTATION_PLAN.md)
  • Backward compatible with existing system

Next Steps (Phase 2)

1. On-Chain Verification (1-2 weeks)

  • Wire RedactionVerifier_groth16.sol to MedicalDataManager.sol
  • Update EVMBackend to submit proofs on-chain
  • Add on-chain verification tests

2. Merkle Tree Integration (1 week)

  • Implement Merkle tree for blockchain state
  • Compute Merkle paths for redaction requests
  • Enable enforceMerkle=1 in circuit inputs

3. Performance Optimization (2-3 weeks)

  • Profile circuit constraints
  • Consider Poseidon hash for better performance
  • Implement proof caching
  • Add parallel proof generation

Performance Metrics

Metric Simulation Mode Real SNARK Mode
Proof Generation <10ms ~5-10s
Verification <5ms ~50-100ms
Gas Cost N/A ~250k gas

Documentation

For detailed implementation information, see:

  • Implementation Plan: docs/ZK_PROOF_IMPLEMENTATION_PLAN.md
  • Architecture: Diagrams, component analysis, security notes
  • Testing Guide: Comprehensive test strategy and commands
  • Migration Path: Step-by-step production deployment guide

Project Structure

``` medchain-avitabile/ Main.py # Simulator entry point InputsConfig.py # Configuration Models/ Block.py # Block with chameleon hash Transaction.py # Transaction types SmartContract.py # Contract execution Bitcoin/BlockCommit.py # Mining + redaction CH/ChameleonHash.py # Trapdoor functions ZK/ SNARKs.py # Zero-knowledge proofs ProofOfConsistency.py # Integrity verification medical/ MedicalRedactionEngine.py # GDPR compliance MedicalDataIPFS.py # Distributed storage demo/ # Demo scripts tests/ # Test suite ```

Testing

```bash pytest tests/ # All tests pytest -m "not slow" # Fast tests only pytest tests/test_consistency_system.py # Specific test pytest --cov=. --cov-report=html # With coverage ```

Performance

Metric Value
Nodes 20-1000 (configurable)
Block Time 10-600 seconds
Tx Rate 5-10 tx/s
Redaction Time ~2 seconds (simulated)
Proof Generation ~5 seconds (real SNARKs)

Limitations

Research prototype with simulated components:

  • SNARKs: Real Groth16 proofs (requires compiled circom artifacts)
  • Smart Contracts: In-memory simulation (unless `USE_REAL_EVM=1`)
  • IPFS: Simulated client (unless `USE_REAL_IPFS=1`)
  • Chameleon Hash: Fixed demo parameters
  • Network: No real P2P layer

Not production-ready. For research and education only.

References

  1. Ateniese et al. (2017) - Redactable Blockchain
  2. Avitabile et al. - Data Redaction in Smart-Contract-Enabled Permissioned Blockchains
  3. Deuber et al. (2019) - Permissionless Setting
  4. Puddu et al. (2017) - μchain
  5. Botta et al. (2022) - Towards Data Redaction

Contributing

  1. Fork the repository
  2. Create feature branch
  3. Run tests (`pytest tests/`)
  4. Commit changes
  5. Open Pull Request

License

Academic research project. See institution policies for usage rights.

Acknowledgments

  • University of Genoa - Decentralized Systems Course
  • BlockSim - Blockchain simulator framework

For implementation details, see `todo.md`

About

proof-of-concept benchmark for the the paper 'Data Redaction in Smart-Contract-Enabled Permissioned Blockchains'

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Python 76.9%
  • TeX 14.1%
  • Solidity 3.6%
  • JavaScript 2.9%
  • Shell 1.0%
  • Circom 0.9%
  • Makefile 0.6%