Bittensor Drand Library

1.3.0 · active · verified Wed Apr 15

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

Install

Imports

Quickstart

This quickstart demonstrates how to generate an encrypted commit for Bittensor weights and perform MLKEM768 encryption using the bittensor-drand library. It includes mocking necessary Bittensor network parameters for a runnable example, though in a real application, these would be fetched from a live or local Subtensor instance. A local Subtensor node with commit-reveal v3 (CRv3) and Drand support is required for actual testing.

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).")

view raw JSON →