Package: tno.mpc.encryption-schemes.templates
TNO MPC Lab - Encryption Schemes - TemplatesThe TNO MPC lab consists of generic software components, procedures, and
functionalities developed and maintained on a regular basis to facilitate and
aid in the development of MPC solutions. The lab is a cross-project initiative
allowing us to integrate and reuse previously developed MPC functionalities to
boost the development of new protocols and solutions.The package tno.mpc.encryption_schemes.templates is part of the TNO Python
Toolbox.Generic frameworks for encryption schemes. Currently includes support for:Generic encryption scheme (encryption_scheme.py);Asymmetric encryption scheme (asymmetric_encryption_scheme.py);Symmetric encryption scheme (symmetric_encryption_scheme.py);Support for precomputation of randomness (randomized_encryption_scheme.py).Limitations in (end-)use: the content of this software package may solely be
used for applications that comply with international export control laws.This implementation of cryptographic software has not been audited. Use at your
own risk.DocumentationDocumentation of the tno.mpc.encryption_schemes.templates package can be foundhere.InstallEasily install the tno.mpc.encryption_schemes.templates package using pip:$python-mpipinstalltno.mpc.encryption_schemes.templatesNote:A significant performance improvement can be achieved by installing the GMPY2
library.$python-mpipinstall'tno.mpc.encryption_schemes.templates[gmpy]'If you wish to run the tests you can use:$python-mpipinstall'tno.mpc.encryption_schemes.templates[tests]'Generating prepared randomness for RandomizedEncryptionSchemesIn the encryption process,RandomizedEncryptionSchemes require a random
number. A typical encryption process generates a large random number, processes
it in a way that is specific for (the private key of) the encryption scheme, and
used the prepared random value to encrypt a plaintext message. A similarly
prepared random value is required to rerandomize a ciphertext. This process of
generating and preparing randomness consumes a lot of time and processing power.Fortunately, the randomness depends only on the encryption scheme and not on the
message that is to be encrypted. This makes it possible to prepare randomness a
priori. Our library facilitates this process by the concept of "randomness
sources". Three specific sources are shipped with the library, but you may
define your ownRandomnessSourceby implementing thetno.mpc.encryption_schemes.templates._randomness_manager.RandomnessSourceprotocol. The default sources are:ContextlessSource, which yields randomness from a source that does not need
to be opened or closed (e.g. a list, tuple, function-call, ...).FileSource, which creates a thread to read a file, store the values in RAM
and yield values on request.ProcessSource, which creates one or more processes that all perform function
calls to store a predefined number of return values in RAM and yield on
request.ARandomizedEncryptionSchemeis configured with aContextlessSourcethat
calls its generating function. However, it is often more efficient to start
generating a known amount of randomness earlier in the program execution. For
this, one may callRandomizedEncryptionScheme.boot_generationwhich internally
initializes and starts aProcessSource, or requests an existing source to
generate more randomness (see also below). Other sources can be configured
throughRandomizedEncryptionScheme.register_randomness_source.Storing randomness for later useNaturally, a maximum speed-up during runtime of your main protocol is achieved
by generating random values a priori. This looks as follows. First, the
key-generating party generates a public-private keypair and shares the public
key with the other participants. Now, every player pregenerates the amount of
randomness needed for her part of the protocol and stores it in a file. For
example, this can be done overnight or during the weekend. When the main
protocol is executed, every player uses the same scheme (public key) as
communicated before, configures the scheme to use the pregenerated randomness
from file, and runs the main protocol without the need to generate randomness
for encryption at that time. A minimal example is provided below.fromitertoolsimportcountfrompathlibimportPathfromtno.mpc.encryption_schemes.templates.random_sourcesimportFileSourcefromtno.mpc.encryption_schemes.templates.randomized_encryption_schemeimport(RandomizedEncryptionScheme,)counter=count()classMyRandomizedEncryptionScheme(RandomizedEncryptionScheme):@staticmethoddef_generate_randomness()->int:"""Dummy randomness generator + preprocessing."""returnnext(counter)# --- empty definitions for all abstract methods ---@classmethoddeffrom_security_parameter(cls,*args,**kwargs):pass@classmethoddefgenerate_key_material(cls,*args,**kwargs):pass@classmethoddefid_from_arguments(cls,*args,**kwargs):passdefencode(self,plaintext):passdefdecode(self,encoded_plaintext):passdef_unsafe_encrypt_raw(self,plaintext):passdef_decrypt_raw(self,ciphertext):passdef__eq__(self,*args,**kwargs):passdefpregenerate_randomness_in_weekend(amount:int,path:Path):# Initialize scheme, usually with parametersgenerating_scheme=MyRandomizedEncryptionScheme()# Generate randomness with two processesgenerating_scheme.boot_randomness_generation(amount,max_workers=2)# Save randomness to comma-separated csvwithopen(path,"w")asfile:for_inrange(amount):file.write(f"{generating_scheme.get_randomness()},")# Shut down scheme to gracefully close sourcegenerating_scheme.shut_down()defuse_pregenerated_randomness(amount:int,path:Path):# Initialize scheme WITH THE SAME PARAMETERS!consuming_scheme=MyRandomizedEncryptionScheme()# Configure file as randomness sourceconsuming_scheme.register_randomness_source(FileSource(path))# Consume randomness from filefor_inrange(amount):print(consuming_scheme.get_randomness())# Shut down scheme to gracefully close sourceconsuming_scheme.shut_down()if__name__=="__main__":AMOUNT=24FILE_PATH=Path("randomness.csv")pregenerate_randomness_in_weekend(AMOUNT,FILE_PATH)use_pregenerated_randomness(AMOUNT,FILE_PATH)# result (possibly not in this order): 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11