|
1 | 1 | import sys
|
2 | 2 | import os
|
3 | 3 | 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 |
8 | 6 |
|
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__), ".."))) |
10 | 8 |
|
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 |
16 | 11 |
|
| 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) |
17 | 16 | print(f"Public Key: {public_key}")
|
18 | 17 | print(f"Address: {address}")
|
19 | 18 |
|
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") |
22 | 34 |
|
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 | + |
37 | 42 | if is_valid:
|
38 |
| - print("Proof verification successful.") |
| 43 | + print("Proof is valid!") |
39 | 44 | else:
|
40 | 45 | print("Proof verification failed.")
|
41 | 46 |
|
42 |
| -# Main CLI function |
43 | 47 | 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") |
46 | 50 |
|
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") |
50 | 53 |
|
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") |
54 | 56 |
|
55 | 57 | args = parser.parse_args()
|
56 | 58 |
|
57 | 59 | 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}") |
59 | 65 | 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() |
63 | 69 |
|
64 | 70 | if __name__ == "__main__":
|
65 | 71 | main()
|
0 commit comments