templates.randomized_encryption_scheme module

Generic classes for creating an EncryptionScheme that allows for precomputed, or stored randomness.

class templates.randomized_encryption_scheme.RandomizableCiphertext(raw_value, scheme)[source]

Bases: Ciphertext[KM, PT, RP, CV], ABC

Ciphertext that can be rerandomized. Subclass of Ciphertext.

__init__(raw_value, scheme)[source]

Construct a RandomizableCiphertext, with the given value for the given EncryptionScheme.

Parameters:
  • raw_value (Any) – Ciphertext value.

  • scheme (RandomizedEncryptionScheme[TypeVar(KM), TypeVar(PT), TypeVar(RP), TypeVar(CV), TypeVar(RC, bound= RandomizableCiphertext[Any, Any, Any, Any])]) – RandomizedEncryptionScheme that is used to encrypt this ciphertext.

Raises:

TypeError – When scheme has the incorrect type.

abstract apply_randomness(randomization_value)[source]

Apply a random value to rerandomize this ciphertext.

Parameters:

randomization_value (Any) – Random value used to rerandomize this ciphertext.

Raises:

NotImplementedError – When scheme does not support rerandomization.

Return type:

None

randomize()[source]

Rerandomize this ciphertext object.

Return type:

None

class templates.randomized_encryption_scheme.RandomizedEncryptionScheme(randomizations=None, max_size=100, nr_of_threads=1, path=None, separator=',', start_generation=True, debug=False)[source]

Bases: EncryptionScheme[KM, PT, RP, CV, CT], ABC

Abstract base class for a RandomizedEncryptionScheme. Subclass of EncryptionScheme

__init__(randomizations=None, max_size=100, nr_of_threads=1, path=None, separator=',', start_generation=True, debug=False)[source]

Initiate a Randomness variable with the given parameters to be used for randomizing ciphertexts.

Parameters:
  • debug (bool) – flag to determine whether debug information should be displayed (Default: False)

  • start_generation (bool) – flag that determines whether the scheme starts generating randomness immediately (Default: True)

  • randomizations (Optional[Queue[int]]) – queue with randomizations. If no queue is given, it creates a fresh one (Default: None)

  • max_size (int) – maximum size of the queue (Default: 100)

  • nr_of_threads (int) – number of generation worker threads that should be started (Default: 1)

  • path (Optional[str]) – path (including filename) to the file that contains randomizations. By default no path is given and no randomness is extracted from any files. (Default: “”)

  • separator (str) – separator for the random values in the given file (Default: “,”)

boot_generation(nr_of_threads=None, path=None, start_generation=True)[source]

calls the boot_generation method of the internal randomness

Parameters:
  • nr_of_threads (Optional[int]) – number of generation threads. (Default: None, the nr_of_threads parameter is taken from the original __init__)

  • path (Optional[str]) – path to the file containing randomizations. (Default: None, the path parameter is taken from the original __init__)

  • start_generation (bool) – flag that determines whether the threads start generating immediately (Default: True).

Return type:

None

abstract generate_randomness()[source]

Method to generate randomness for this particular scheme.

Raises:

NotImplementedError – When scheme does not support randomness generation.

Return type:

Any

Returns:

A list containing number_of_randomizations random numbers.

get_randomness()[source]

Get new randomness from the randomness source.

Return type:

Any

Returns:

One random value.

shut_down()[source]

Give the shut down signal to the scheme’s randomness. This tells the threads to gracefully shut down.

Return type:

None

class templates.randomized_encryption_scheme.Randomness(generation_function, randomizations=None, max_size=100, nr_of_threads=1, path=None, separator=',', start_generation=True, debug=False)[source]

Bases: object

Object containing precomputed randomness. This randomness is either stored internally or in a specified file. Allows for functionality to create randomness on-the-fly.

__init__(generation_function, randomizations=None, max_size=100, nr_of_threads=1, path=None, separator=',', start_generation=True, debug=False)[source]

Construct a Randomness object. This construction starts generation workers and a file worker that generate new randomness using the given generation function and abstract random from a file respectively. This happens in separate threads and the random elements are placed in a (thread-safe) queue. This queue can then be used to request random elements at encryption time.

Parameters:
  • debug (bool) – flag to determine whether debug information should be displayed (Default: False)

  • start_generation (bool) – flag to determine whether all threads should immediately start generating randomness (Default: True)

  • max_size (int) – maximum size of the buffer of randomizations. (Default: 100)

  • nr_of_threads (int) – number of threads that generate randomizations in parallel.

  • generation_function (Callable[[], int]) – Function that generates one random value.

  • randomizations (Optional[Queue[int]]) – Precomputed in-memory randomness to be stored in this object for usage in an encryption scheme. (Default: None)

  • path (Optional[str]) – Optional path to a file containing randomness. (Default: None)

  • separator (str) – Separator between random numbers in the file. (Default: “,”)

__len__()[source]

Determine number of randomizations currently present in the queue.

Return type:

int

Returns:

Number of randomizations currently present in the queue.

add_generation_worker()[source]

Add an extra thread that generates randomness.

Return type:

None

boot_generation(nr_of_threads=None, path=None, start_generation=True)[source]

shut down the generation threads and file thread if they are still running. Then, initialise new generation threads and a file thread.

Parameters:
  • nr_of_threads (Optional[int]) – number of generation threads. (Default: None, the nr_of_threads parameter is taken from the original __init__)

  • path (Optional[str]) – path to the file containing randomizations. (Default: None, the path parameter is taken from the original __init__)

  • start_generation (bool) – flag that determines whether the threads start generating immediately (Default: True).

Return type:

None

default_shutdown_timeout = 0.01
file_worker()[source]

Function to be run by the file worker. The file is read in chunks and these chunks are then used to extract random elements.

Return type:

None

generation_worker(identifier)[source]

Function to be run by each generation worker. These workers keep generating random elements and try to add them to the queue until a stop sign is given.

Parameters:

identifier (int) – identifier used for debugging

Return type:

None

get_one()[source]

Checks the buffer for random value. If the buffer is empty, it waits for a random value from the generation thread.

Return type:

Any

Returns:

One random element.

safe_print(message)[source]

Atomic print. It does nothing if the debug flag is False

Parameters:

message (str) – message to be printed.

Return type:

None

shut_down()[source]

Turn on the signal that tells the threads to drop what they’re doing and shut down.

Return type:

None

start_generating()[source]

Turn on the signal that tells the threads to start generating randomness.

Return type:

None

stop_generating()[source]

Tell the threads that they can stop generating randomness.

Return type:

None