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
```bash git clone https://github.com/Mobile-IoT-Security-Lab/medchain-avitabile.git cd medchain-avitabile pip install -r requirements.txt python Main.py ```
``` CH(m, r) = g^m · PK^r mod p ```
- Select historical block for modification
- Compute new message (updated transactions + previous hash)
- Forge new randomness: `r2 = forge(SK, m1, r1, m2)`
- Result: `CH(m1, r1) = CH(m2, r2)` - same hash, different content
- Chain remains valid
| 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 |
```bash python Main.py # Standard mode TESTING_MODE=1 python Main.py # Fast mode DRY_RUN=1 python Main.py # Preview config ```
```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 ```
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 ```
```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)
```
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)Phase 1 Complete: Real zero-knowledge proofs for data redaction are now implemented using Groth16 SNARKs.
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
passSNARK 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")# 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-
Compile the circuit:
cd circuits && ./scripts/compile.sh
-
Run Groth16 setup:
PTAU=../tools/pot14_final.ptau ./scripts/setup.sh
-
Verify setup:
ls circuits/build/ # Should see: redaction.wasm, redaction_final.zkey, verification_key.json
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
- Wire
RedactionVerifier_groth16.soltoMedicalDataManager.sol - Update
EVMBackendto submit proofs on-chain - Add on-chain verification tests
- Implement Merkle tree for blockchain state
- Compute Merkle paths for redaction requests
- Enable
enforceMerkle=1in circuit inputs
- Profile circuit constraints
- Consider Poseidon hash for better performance
- Implement proof caching
- Add parallel proof generation
| Metric | Simulation Mode | Real SNARK Mode |
|---|---|---|
| Proof Generation | <10ms | ~5-10s |
| Verification | <5ms | ~50-100ms |
| Gas Cost | N/A | ~250k gas |
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
``` 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 ```
```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 ```
| 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) |
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.
- Ateniese et al. (2017) - Redactable Blockchain
- Avitabile et al. - Data Redaction in Smart-Contract-Enabled Permissioned Blockchains
- Deuber et al. (2019) - Permissionless Setting
- Puddu et al. (2017) - μchain
- Botta et al. (2022) - Towards Data Redaction
- Fork the repository
- Create feature branch
- Run tests (`pytest tests/`)
- Commit changes
- Open Pull Request
Academic research project. See institution policies for usage rights.
- University of Genoa - Decentralized Systems Course
- BlockSim - Blockchain simulator framework
For implementation details, see `todo.md`