{"id":6547,"library":"bittensor-drand","title":"Bittensor Drand Library","description":"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.","status":"active","version":"1.3.0","language":"en","source_language":"en","source_url":"https://github.com/opentensor/bittensor-drand","tags":["bittensor","drand","cryptography","blockchain","randomness","commit-reveal","timelock","mlkem"],"install":[{"cmd":"pip install bittensor-drand","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core interaction with the Bittensor network and Subtensor blockchain.","package":"bittensor","optional":false},{"reason":"Used for array manipulation in weight handling.","package":"numpy","optional":false},{"reason":"Required for building the Rust-backed Python package from source during development.","package":"maturin","optional":true}],"imports":[{"symbol":"get_encrypted_commit","correct":"from bittensor_drand import get_encrypted_commit"},{"symbol":"encrypt_at_round","correct":"from bittensor_drand import encrypt_at_round"},{"symbol":"decrypt_with_signature","correct":"from bittensor_drand import decrypt_with_signature"},{"symbol":"encrypt_mlkem768","correct":"from bittensor_drand import encrypt_mlkem768"}],"quickstart":{"code":"import numpy as np\nimport bittensor_drand as drand_lib\nimport bittensor as bt\nfrom bittensor.utils.weight_utils import convert_weights_and_uids_for_emit\nimport os\n\n# NOTE: This quickstart assumes a local Bittensor Subtensor node is running.\n# For full functionality, you need a running Subtensor instance and a configured wallet.\n# Example: subtensor = bt.subtensor(network=\"local\", chain_endpoint=\"ws://127.0.0.1:9944\")\n\n# Mock data for demonstration purposes\nuids = np.array([1, 3], dtype=np.int64)\nweights = np.array([0.3, 0.7], dtype=np.float32)\nversion_key = 843000 # Example version key\nnetuid = 1 # Example network ID\n\n# In a real scenario, you would fetch these from a live or local subtensor instance\n# For local testing, ensure your local subtensor is configured with CRv3 and Drand.\n# tempo = subtensor.get_subnet_hyperparameters(netuid).tempo\n# current_block = subtensor.get_current_block()\n# subnet_reveal_period_epochs = subtensor.get_subnet_reveal_period_epochs(netuid=netuid)\n\ntempo = 10 # Mock tempo\ncurrent_block = 100 # Mock current block\nsubnet_reveal_period_epochs = 20 # Mock reveal period\n\n# Ensure weights are correctly formatted for emit\nuids, weights = convert_weights_and_uids_for_emit(uids, weights)\n\ntry:\n    encrypted_commit = drand_lib.get_encrypted_commit(\n        uids=uids,\n        weights=weights,\n        version_key=version_key,\n        tempo=tempo,\n        current_block=current_block,\n        netuid=netuid,\n        subnet_reveal_period_epochs=subnet_reveal_period_epochs\n    )\n    print(f\"Successfully generated encrypted commit: {encrypted_commit[:60]}...\")\n\n    # Example of MLKEM768 encryption (requires a target Drand round)\n    drand_round = 123456 # A specific Drand round to encrypt for\n    encryption_data = b\"My secret message for Drand round!\"\n    encrypted_mlkem_payload = drand_lib.encrypt_mlkem768(drand_round, encryption_data)\n    print(f\"MLKEM768 encrypted payload: {encrypted_mlkem_payload[:60]}...\")\n\nexcept Exception as e:\n    print(f\"An error occurred: {e}\")\n    print(\"Please ensure you have a local Bittensor Subtensor node running with CRv3 support \")\n    print(\"and that bittensor is installed (pip install bittensor).\")","lang":"python","description":"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."},"warnings":[{"fix":"Uninstall `bittensor-commit-reveal` and install `bittensor-drand`. Update all `import bittensor_commit_reveal` statements to `import bittensor_drand`.","message":"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.","severity":"breaking","affected_versions":"<0.5.0"},{"fix":"Review any direct usage of `WeightsTlockPayload` and ensure the `hotkey` parameter is correctly provided.","message":"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.","severity":"breaking","affected_versions":"<1.0.0"},{"fix":"Always consult the official Bittensor documentation and release notes for both `bittensor-drand` and `Subtensor` to ensure compatibility between library versions and the target Subtensor network (mainnet, testnet, or local). Monitor Subtensor runtime version updates.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure Rust and `maturin` (`pip install maturin`) are installed and configured in your development environment if building from source or contributing.","message":"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.","severity":"gotcha","affected_versions":"All versions (for development/source installs)"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[]}