Bittensor Drand Library
bittensor-drand is a Python library that provides functionality for interacting with the Drand (distributed randomness) mechanism within the Bittensor ecosystem. It facilitates secure, verifiable randomness for applications like commit-reveal schemes and timelock encryption of weights on the Subtensor blockchain. The library is currently at version 1.3.0 and receives regular updates to support new cryptographic features and Bittensor protocol changes.
Warnings
- breaking The package was renamed from `bittensor-commit-reveal` to `bittensor-drand` in version 0.5.0. Users upgrading from `0.4.x` or earlier must update their import statements and package name.
- breaking In v1.0.0, the `hotkey` parameter was added to the `WeightsTlockPayload` structure. Integrations that directly construct or interact with this payload might need to be updated.
- gotcha The `bittensor-drand` library heavily relies on the functionality provided by the Subtensor blockchain (Bittensor's L1). Runtime upgrades on Subtensor can introduce breaking API changes related to Drand pulse writes, commit-reveal mechanisms, and other core features. Compatibility with specific Subtensor runtime versions is crucial.
- gotcha For local development and building from source, the Rust toolchain and `maturin` (Rust-based Python binding tool) are required to compile the underlying Rust components.
Install
-
pip install bittensor-drand
Imports
- get_encrypted_commit
from bittensor_drand import get_encrypted_commit
- encrypt_at_round
from bittensor_drand import encrypt_at_round
- decrypt_with_signature
from bittensor_drand import decrypt_with_signature
- encrypt_mlkem768
from bittensor_drand import encrypt_mlkem768
Quickstart
import numpy as np
import bittensor_drand as drand_lib
import bittensor as bt
from bittensor.utils.weight_utils import convert_weights_and_uids_for_emit
import os
# NOTE: This quickstart assumes a local Bittensor Subtensor node is running.
# For full functionality, you need a running Subtensor instance and a configured wallet.
# Example: subtensor = bt.subtensor(network="local", chain_endpoint="ws://127.0.0.1:9944")
# Mock data for demonstration purposes
uids = np.array([1, 3], dtype=np.int64)
weights = np.array([0.3, 0.7], dtype=np.float32)
version_key = 843000 # Example version key
netuid = 1 # Example network ID
# In a real scenario, you would fetch these from a live or local subtensor instance
# For local testing, ensure your local subtensor is configured with CRv3 and Drand.
# tempo = subtensor.get_subnet_hyperparameters(netuid).tempo
# current_block = subtensor.get_current_block()
# subnet_reveal_period_epochs = subtensor.get_subnet_reveal_period_epochs(netuid=netuid)
tempo = 10 # Mock tempo
current_block = 100 # Mock current block
subnet_reveal_period_epochs = 20 # Mock reveal period
# Ensure weights are correctly formatted for emit
uids, weights = convert_weights_and_uids_for_emit(uids, weights)
try:
encrypted_commit = drand_lib.get_encrypted_commit(
uids=uids,
weights=weights,
version_key=version_key,
tempo=tempo,
current_block=current_block,
netuid=netuid,
subnet_reveal_period_epochs=subnet_reveal_period_epochs
)
print(f"Successfully generated encrypted commit: {encrypted_commit[:60]}...")
# Example of MLKEM768 encryption (requires a target Drand round)
drand_round = 123456 # A specific Drand round to encrypt for
encryption_data = b"My secret message for Drand round!"
encrypted_mlkem_payload = drand_lib.encrypt_mlkem768(drand_round, encryption_data)
print(f"MLKEM768 encrypted payload: {encrypted_mlkem_payload[:60]}...")
except Exception as e:
print(f"An error occurred: {e}")
print("Please ensure you have a local Bittensor Subtensor node running with CRv3 support ")
print("and that bittensor is installed (pip install bittensor).")