Skip to content

Commit 39429cd

Browse files
committed
.
1 parent 61a2c59 commit 39429cd

31 files changed

+296
-494
lines changed

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
[WORK IN PROGRESS]
2+
13
# 🔑 ZKK: Zero-Knowledge Key
24

35
ZKK is a proof-of-concept project demonstrating the use of **Zero-Knowledge Proofs (ZKP)** to validate a Bitcoin private key without exposing its value. The project generates a ZKP proving that a given private key corresponds to a derived public key and encodes the proof in a QR code for easy sharing and validation.
@@ -6,7 +8,7 @@ ZKK is a proof-of-concept project demonstrating the use of **Zero-Knowledge Proo
68

79
- Validate Bitcoin private keys.
810
- Derive public keys and addresses from private keys.
9-
- Generate Zero-Knowledge Proofs (ZKP) using `binius`.
11+
- Generate Zero-Knowledge Proofs (ZKP) using `zk-SNARKs`.
1012
- Encode ZKP into a QR code for secure sharing.
1113
- Display and save QR codes as SVG images.
1214

@@ -21,10 +23,11 @@ ZKK is a proof-of-concept project demonstrating the use of **Zero-Knowledge Proo
2123
3. **Dependencies**:
2224
- `bitcoinlib`: For Bitcoin key derivation.
2325
- `qrcode`: For QR code generation.
26+
- `snarkjs`: For zk-SNARK proof generation.
2427

2528
Install dependencies using:
2629
```bash
27-
pip install bitcoinlib qrcode
30+
pip install bitcoinlib qrcode snarkjs
2831
```
2932

3033
## Installation
@@ -42,9 +45,9 @@ zkk/
4245
├── bin/
4346
│ └── cli.py # CLI script for ZKP generation and verification
4447
├── src/
45-
│ ├── binius/ # Binius proof-related code
48+
│ ├── snarks/ # zk-SNARK proof-related code
4649
│ │ ├── proof.py # Proof generation and verification logic
47-
│ │ ├── binary_fields.py # Binary field operations
50+
│ │ ├── constraints.circom # Circom circuit for zk-SNARKs
4851
│ │ ├── utils.py # Utility functions for evaluation
4952
│ │ └── merkle.py # Merkle tree implementation
5053
│ ├── bitcoin/
@@ -64,8 +67,8 @@ zkk/
6467
- It derives the corresponding public key and Bitcoin address.
6568

6669
2. **Generate ZKP**:
67-
- Using `binius`, the private key is input as a binary evaluation.
68-
- The proof is generated based on a securely chosen evaluation point.
70+
- Using `zk-SNARKs`, the private key is input into a Circom circuit.
71+
- The proof is generated via `snarkjs` based on the circuit constraints.
6972
- The output is a ZKP that can be validated without exposing the private key.
7073

7174
3. **Encode in QR Code**:

bin/cli.py

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,71 @@
11
import sys
22
import os
33
import argparse
4-
from bitcoin.key_utils import derive_public_key, derive_address
5-
from binius.proof import packed_binius_proof, verify_packed_binius_proof
6-
from binius.binary_fields import BinaryFieldElement
7-
from qr.generate_qr import generate_qr_code
4+
import json
5+
import qrcode
86

9-
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../src"))
7+
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
108

11-
# Function to generate a proof and QR code
12-
def generate_proof(private_key_raw):
13-
print("Deriving public key and address...")
14-
public_key = derive_public_key(private_key_raw)
15-
address = derive_address(private_key_raw)
9+
from src.bitcoin.key_utils import validate_private_key_raw, derive_public_key, derive_address
10+
from src.snarks.proof import generate_zk_proof, verify_zk_proof
1611

12+
def generate_proof(private_key):
13+
print("Validating and deriving public key and address...")
14+
public_key = derive_public_key(private_key)
15+
address = derive_address(private_key)
1716
print(f"Public Key: {public_key}")
1817
print(f"Address: {address}")
1918

20-
# Convert the raw private key to binary evaluations
21-
evaluations = [BinaryFieldElement(int(bit)) for bit in bin(int(private_key_raw, 16))[2:]]
19+
print("Generating ZKP...")
20+
proof = generate_zk_proof(private_key, public_key)
21+
proof_data = {
22+
"proof": proof,
23+
"public_key": public_key,
24+
"address": address
25+
}
26+
27+
qr = qrcode.QRCode()
28+
qr.add_data(json.dumps(proof_data))
29+
qr.make(fit=True)
30+
img = qr.make_image(fill_color="black", back_color="white")
31+
img.save(f"zkp_qr_{public_key[:8]}.svg")
32+
print("ZKP Generated Successfully.")
33+
print(f"QR Code saved as zkp_qr_{public_key[:8]}.svg")
2234

23-
# Generate a dummy evaluation point (replace with a secure point in production)
24-
evaluation_point = [BinaryFieldElement(0), BinaryFieldElement(1)]
25-
26-
print("Generating Zero-Knowledge Proof...")
27-
proof = packed_binius_proof(evaluations, evaluation_point)
28-
29-
print("Proof generated successfully.")
30-
generate_qr_code(str(proof), "zkp_qr.svg")
31-
print("QR Code saved as zkp_qr.svg")
32-
33-
# Function to verify a proof
34-
def verify_proof(proof):
35-
print("Verifying proof...")
36-
is_valid = verify_packed_binius_proof(proof)
35+
def verify_proof(proof_path):
36+
with open(proof_path, "r") as f:
37+
proof_data = json.load(f)
38+
39+
print("Verifying ZKP...")
40+
is_valid = verify_zk_proof(proof_data["proof"], proof_data["public_key"])
41+
3742
if is_valid:
38-
print("Proof verification successful.")
43+
print("Proof is valid!")
3944
else:
4045
print("Proof verification failed.")
4146

42-
# Main CLI function
4347
def main():
44-
parser = argparse.ArgumentParser(description="ZKK: Zero-Knowledge Key CLI")
45-
subparsers = parser.add_subparsers(dest="command", required=True)
48+
parser = argparse.ArgumentParser(description="ZKK CLI Tool")
49+
subparsers = parser.add_subparsers(dest="command")
4650

47-
# Subcommand: Generate Proof
48-
generate_parser = subparsers.add_parser("generate", help="Generate a Zero-Knowledge Proof and QR code")
49-
generate_parser.add_argument("private_key_raw", help="Raw Bitcoin private key in hexadecimal format")
51+
gen_parser = subparsers.add_parser("generate", help="Generate a zk-SNARK proof and QR code")
52+
gen_parser.add_argument("private_key", type=str, help="Bitcoin private key")
5053

51-
# Subcommand: Verify Proof
52-
verify_parser = subparsers.add_parser("verify", help="Verify a Zero-Knowledge Proof")
53-
verify_parser.add_argument("proof_file", help="Path to the proof file to verify")
54+
verify_parser = subparsers.add_parser("verify", help="Verify a zk-SNARK proof")
55+
verify_parser.add_argument("proof_path", type=str, help="Path to the proof JSON file")
5456

5557
args = parser.parse_args()
5658

5759
if args.command == "generate":
58-
generate_proof(args.private_key_raw)
60+
try:
61+
validate_private_key_raw(args.private_key)
62+
generate_proof(args.private_key)
63+
except ValueError as e:
64+
print(f"Error: {e}")
5965
elif args.command == "verify":
60-
with open(args.proof_file, "r") as f:
61-
proof = eval(f.read()) # Replace eval with safer deserialization in production
62-
verify_proof(proof)
66+
verify_proof(args.proof_path)
67+
else:
68+
parser.print_help()
6369

6470
if __name__ == "__main__":
6571
main()

circuit.r1cs

120 Bytes
Binary file not shown.

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
bitcoinlib==0.6.3
2+
python-snarks==0.0.3
23
qrcode==7.4

scripts/deploy.sh

Lines changed: 0 additions & 56 deletions
This file was deleted.

scripts/setup_env.sh

Lines changed: 0 additions & 49 deletions
This file was deleted.

setup.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@
33
setup(
44
name="zkk",
55
version="1.0.0",
6-
description="Zero-Knowledge Key (ZKK): A Python-based implementation of zero-knowledge proofs for Bitcoin private key validation using Binius.",
6+
description="Zero-Knowledge Key (ZKK): A Python-based implementation of zero-knowledge proofs for Bitcoin private key validation using PySNARK.",
77
author="fabohax",
88
author_email="[email protected]",
99
url="https://github.com/fabohax/zkk",
1010
packages=find_packages("src"),
1111
package_dir={"": "src"},
1212
install_requires=[
13-
"bitcoinlib==0.6.3",
13+
"bit",
1414
"qrcode==7.4",
15+
"pysnark",
16+
"numpy>=1.21.4",
1517
],
1618
classifiers=[
1719
"Programming Language :: Python :: 3",
1820
"License :: OSI Approved :: MIT License",
1921
"Operating System :: OS Independent",
2022
],
2123
python_requires=">=3.8",
22-
)
24+
)

src/__init__.py

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/binius/__init__.py

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/binius/binary_fields.py

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)