bittensor.core.timelock#

This module provides functionality for TimeLock Encryption (TLE), a mechanism that encrypts data such that it can only be decrypted after a specific amount of time (expressed in the form of Drand rounds). It includes functions for encryption, decryption, and handling the decryption process by waiting for the reveal round. The logic is based on Drand QuickNet.

Main Functions:
  • encrypt: Encrypts data and returns the encrypted data along with the reveal round.

  • decrypt: Decrypts the provided encrypted data when the reveal round is reached.

  • wait_reveal_and_decrypt: Waits for the reveal round and decrypts the encrypted data.

Usage Example:

`python from bittensor import timelock data = "From Cortex to Bittensor" encrypted_data, reveal_round = timelock.encrypt(data, n_blocks=5) decrypted_data = timelock.wait_reveal_and_decrypt(encrypted_data) `

Usage Example with custom data:

```python import pickle from dataclasses import dataclass

from bittensor import timelock

@dataclass class Person:

name: str age: int

# get instance of your data x_person = Person(“X Lynch”, 123)

# get bytes of your instance byte_data = pickle.dumps(x_person)

# get TLE encoded bytes encrypted, reveal_round = timelock.encrypt(byte_data, 1)

# wait when reveal round appears in Drand QuickNet and get decrypted data decrypted = timelock.wait_reveal_and_decrypt(encrypted_data=encrypted)

# convert bytes into your instance back x_person_2 = pickle.loads(decrypted)

# make sure initial and decoded instances are the same assert x_person == x_person_2 ```

Note: For handling fast-block nodes, set the block_time parameter to 0.25 seconds during encryption.

Functions#

decrypt(encrypted_data[, no_errors, return_str])

Decrypts encrypted data using TimeLock Decryption

encrypt(data, n_blocks[, block_time])

Encrypts data using TimeLock Encryption

wait_reveal_and_decrypt(encrypted_data[, ...])

Waits for reveal round and decrypts data using TimeLock Decryption.

Module Contents#

bittensor.core.timelock.decrypt(encrypted_data, no_errors=True, return_str=False)#

Decrypts encrypted data using TimeLock Decryption

Parameters:
  • encrypted_data (bytes) – Encrypted data to be decrypted.

  • no_errors (bool) – If True, no errors will be raised during decryption.

  • return_str (bool) – convert decrypted data to string if True. Default is False.

Returns:

Decrypted data, when reveled round is reached.

Return type:

decrypted_data

Usage:

# default usage decrypted_data = decrypt(encrypted_data)

# passing no_errors=False for raising errors during decryption decrypted_data = decrypt(encrypted_data, no_errors=False)

# passing return_str=True for returning decrypted data as string decrypted_data = decrypt(encrypted_data, return_str=True)

bittensor.core.timelock.encrypt(data, n_blocks, block_time=12.0)#

Encrypts data using TimeLock Encryption

Parameters:
  • data (Union[bytes, str]) – Any bytes data to be encrypted.

  • n_blocks (int) – Number of blocks to encrypt.

  • block_time (Union[int, float]) – Time in seconds for each block. Default is 12.0 seconds.

Returns:

A tuple containing the encrypted data and reveal TimeLock reveal round.

Return type:

tuple

Raises:

PyValueError – If failed to encrypt data.

Usage:

data = “From Cortex to Bittensor”

# default usage encrypted_data, reveal_round = encrypt(data, 10)

# passing block_time for fast-blocks node encrypted_data, reveal_round = encrypt(data, 15, block_time=0.25)

encrypted_data, reveal_round = encrypt(data, 5)

Note

For using this function with fast-blocks node you need to set block_time to 0.25 seconds. data, round = encrypt(data, n_blocks, block_time=0.25)

bittensor.core.timelock.wait_reveal_and_decrypt(encrypted_data, reveal_round=None, no_errors=True, return_str=False)#

Waits for reveal round and decrypts data using TimeLock Decryption.

Parameters:
  • encrypted_data (bytes) – Encrypted data to be decrypted.

  • reveal_round (Optional[int]) – Reveal round to wait for. If None, will be parsed from encrypted data.

  • no_errors (bool) – If True, no errors will be raised during decryption.

  • return_str (bool) – convert decrypted data to string if True. Default is False.

Raises:
  • struct.error – If failed to parse reveal round from encrypted data.

  • TypeError – If reveal_round is None or wrong type.

  • IndexError – If provided encrypted_data does not contain reveal round.

Returns:

Decrypted data.

Return type:

bytes

Usage:

import bittensor as bt encrypted, reveal_round = bt.timelock.encrypt(“Cortex is power”, 3)