Chia VDF Verification Library
chiavdf is a Python library that provides bindings for a C++ implementation of Verifiable Delay Functions (VDFs), primarily used within the Chia blockchain ecosystem. It allows for the creation of discriminants and verification of Wesolowski proofs. The library is actively maintained with frequent updates, often including improvements to the underlying C++ components and build processes. Current version is 1.1.14.
Common errors
-
ModuleNotFoundError: No module named 'chiavdf'
cause The 'chiavdf' library is not installed in your current Python environment.fixInstall the library using pip: `pip install chiavdf` -
AttributeError: module 'chiavdf' has no attribute 'verify_proof'
cause You are attempting to call a function or access an attribute that does not exist or has been misspelled within the `chiavdf` module.fixCheck the exact function names from the official documentation or the `__init__.pyi` file. Common functions are `verify_wesolowski`, `verify_vdf`, and `create_discriminant`. -
TypeError: argument 'challenge_hash' must be bytes, not str
cause Input parameters like `challenge_hash`, `x_value`, and `proof_bytes` are expected to be byte strings (e.g., `b'...'`), but a regular string was provided.fixEnsure all binary data inputs are properly encoded as `bytes`. For example, use `my_string.encode('utf-8')` or prefix string literals with `b` (e.g., `b'some_data'`).
Warnings
- breaking When compiling `chiavdf` from source (not installing via `pip install` which uses pre-built wheels), the underlying Boost library linkage changed significantly in versions 1.1.12 and 1.1.13. The `boost_system` library was removed from Boost distributions.
- gotcha The `chiavdf` library requires Python 3.9 or newer. Installing with older Python versions will fail or result in compatibility issues.
- gotcha VDF verification is a computationally intensive process. The `num_threads` parameter in `verify_wesolowski` and `verify_vdf` can significantly impact performance. Setting it too high or too low might not yield optimal results.
Install
-
pip install chiavdf
Imports
- verify_wesolowski
from chiavdf import verify_wesolowski
- verify_vdf
from chiavdf import verify_vdf
- create_discriminant
from chiavdf import create_discriminant
Quickstart
import chiavdf
import hashlib
# Example input data (replace with actual challenge, x, and proof from your VDF)
# For a real VDF proof, challenge_hash, x_value, and proof_bytes would be specific outputs
# from a VDF prover.
challenge_seed = b"my_unique_vdf_challenge_seed_001"
challenge_hash = hashlib.sha256(challenge_seed).digest()
form_size = 512 # Standard form size for Chia
num_iterations = 1_000_000 # Number of iterations the VDF ran for
n_wesolowski = 600 # Wesolowski proof parameter
# Dummy x and proof bytes for demonstration. These values will likely result in 'False'
# or an error if not actual valid proof components.
x_value = b"\x01" * 64 # Placeholder for a 64-byte value
proof_bytes = b"\x02" * 128 # Placeholder for a 128-byte proof
try:
# Create the discriminant based on the challenge hash
discriminant = chiavdf.create_discriminant(challenge_hash, form_size)
# Verify the Wesolowski proof
# In a real scenario, x_value and proof_bytes must correspond to a valid VDF computation
is_valid = chiavdf.verify_wesolowski(
challenge_hash,
form_size,
discriminant,
x_value,
proof_bytes,
num_iterations,
n_wesolowski,
1, # num_threads (can be increased for faster verification on multi-core CPUs)
True, # check_input
)
print(f"VDF Wesolowski proof verification result: {is_valid}")
except ValueError as e:
print(f"Input Error during VDF verification: {e}")
except Exception as e:
print(f"An unexpected error occurred during VDF verification: {e}")