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, *, fresh=False)[source]
Bases:
Ciphertext
[KM
,PT
,RP
,CV
],ABC
Ciphertext that can be rerandomized. Subclass of Ciphertext.
- __init__(raw_value, scheme, *, fresh=False)[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.fresh (
bool
) – Indicates whether fresh randomness is already applied to the raw_value.
- 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
- property fresh: bool
Indicate whether the ciphertest has fresh randomness.
Ciphertexts that are send to other parties should generally be fresh. This can be achieved by calling self.randomize().
- Returns:
True if the randomness is fresh, False otherwise.
- get_value()[source]
Get the raw value of the ciphertext.
Accessing this value marks the ciphertext as not fresh. If this is not desired, call the peek_value method.
- Return type:
TypeVar
(CV
)- Returns:
Value of the ciphertext
- peek_value()[source]
Peek at the raw value of the ciphertext.
Accessing this value does not change the freshness of the ciphertext. If this is not desired, call the get_value method.
- Return type:
TypeVar
(CV
)- Returns:
Value of the ciphertext
- randomize()[source]
Rerandomize this ciphertext object.
- Return type:
TypeVar
(RC
, bound= RandomizableCiphertext[Any, Any, Any, Any])- Returns:
The rerandomized object (self).
- property value: CV
Raw value of the ciphertext.
- Returns:
Value of the ciphertext
- class templates.randomized_encryption_scheme.RandomizedEncryptionScheme(randomizations=None, max_size=100, total=None, nr_of_threads=1, path=None, separator=',', start_generation=True, debug=False)[source]
Bases:
EncryptionScheme
[KM
,PT
,RP
,CV
,RC
],ABC
Abstract base class for a RandomizedEncryptionScheme. Subclass of EncryptionScheme
- __init__(randomizations=None, max_size=100, total=None, 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:
randomizations (
Optional
[Queue
[int
]]) – queue with randomizations. If no queue is given, it creates a fresh one.max_size (
int
) – maximum size of the queue.total (
Optional
[int
]) – upper bound on the total amount of randomizations to generate.nr_of_threads (
int
) – number of generation worker threads that should be started.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.separator (
str
) – separator for the random values in the given file.start_generation (
bool
) – flag that determines whether the scheme starts generating randomness immediately.debug (
bool
) – flag to determine whether debug information should be displayed.
- 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.
- 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:
int
- Returns:
A single random element with respect to the scheme.
- get_randomness()[source]
Get new randomness from the randomness source.
- Return type:
int
- Returns:
One random value.
- initialize_randomness(**kwargs)[source]
Initializes a randomness class
- Parameters:
**kwargs (
Any
) – keyword arguments to pass on to Randomness constructor- Return type:
None
- shut_down()[source]
Give the shut down signal to the scheme’s randomness. This tells the threads to gracefully shut down.
- Return type:
None
- unsafe_encrypt(plaintext, apply_encoding=True)[source]
Encrypts the entered (encoded) Plaintext, but does not apply randomness. Also encodes the Plaintext when this is required.
- Parameters:
plaintext (
Union
[TypeVar
(PT
),EncodedPlaintext
[TypeVar
(RP
)]]) – Plaintext or EncodedPlaintext to be encrypted.apply_encoding (
bool
) – Boolean indicating whether a non-encoded plaintext should be encoded. If False, the plaintext is encrypted in raw form.
- Return type:
TypeVar
(RC
, bound= RandomizableCiphertext[Any, Any, Any, Any])- Returns:
Non-randomized RandomizableCiphertext object containing the encrypted value of the plaintext.
- class templates.randomized_encryption_scheme.Randomness(generation_function, randomizations=None, max_size=100, total=None, 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, total=None, 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.start_generation (
bool
) – flag to determine whether all threads should immediately start generating randomness.max_size (
int
) – maximum size of the buffer of randomizations.total (
Optional
[int
]) – upper bound on the total amount of randomizations to generate.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.path (
Optional
[str
]) – Optional path to a file containing randomness.separator (
str
) – Separator between random numbers in the file.
- __len__()[source]
Determine number of randomizations currently present in the queue.
- Return type:
int
- Returns:
Number of randomizations currently present in the queue.
- 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, initialize 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.
- 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_function()[source]
Wrapper around generation function
- Return type:
int
- Returns:
random element
- 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:
int
- 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