### Individual Decrypt Example Source: https://fhe.desilo.dev/latest/api/engine/individual_decrypt Demonstrates how to use `individual_decrypt` to obtain a decrypted share. This is part of a multiparty computation setup. ```python from desilofhe import Engine engine = Engine(use_multiparty=True) secret_key1 = engine.create_secret_key() secret_key2 = engine.create_secret_key() public_key_a = engine.create_public_key_a() public_key_b1 = engine.create_public_key_b(secret_key1, public_key_a) public_key_b2 = engine.create_public_key_b(secret_key2, public_key_a) public_key = engine.create_multiparty_public_key( [public_key_b1, public_key_b2], public_key_a ) message = [1, 2, 3] ciphertext = engine.encrypt(message, public_key) decrypted_share1 = engine.individual_decrypt(ciphertext, secret_key1) ``` -------------------------------- ### Get Bootstrap Key Serialized Size Source: https://fhe.desilo.dev/latest/api/data-type/bootstrap_key/serialized_nbytes Use this snippet to create a bootstrap key and retrieve its serialized size in bytes. Ensure the `desilofhe` library is installed and the engine is initialized with `use_bootstrap=True`. ```python from desilofhe import Engine engine = Engine(use_bootstrap=True) secret_key = engine.create_secret_key() bootstrap_key = engine.create_bootstrap_key(secret_key) serialized_nbytes = bootstrap_key.serialized_nbytes ``` -------------------------------- ### Install DESILO FHE (CPU Version) with pip Source: https://fhe.desilo.dev/latest/install Use this command to install the CPU-only version of the DESILO FHE library. Ensure your system meets the specified OS and Python version requirements. ```bash pip install desilofhe ``` -------------------------------- ### Accessing nbytes Source: https://fhe.desilo.dev/latest/api/data-type/small_bootstrap_key/nbytes Demonstrates how to create a small bootstrap key and access its `nbytes` attribute to get its memory size. ```APIDOC ## Accessing nbytes ### Description Retrieves the memory size of a small bootstrap key. ### Method Attribute access ### Endpoint N/A (Object attribute) ### Parameters None ### Request Example ```python from desilofhe import Engine engine = Engine(use_bootstrap=True) secret_key = engine.create_secret_key() small_bootstrap_key = engine.create_small_bootstrap_key(secret_key) nbytes = small_bootstrap_key.nbytes print(f"The small bootstrap key uses {nbytes} bytes.") ``` ### Response #### Success Response - **nbytes** (int) - The memory size of the small bootstrap key in bytes. ``` -------------------------------- ### Get Small Bootstrap Key Size Source: https://fhe.desilo.dev/latest/api/data-type/small_bootstrap_key/nbytes Instantiate the engine, create a secret key, then a small bootstrap key, and finally access its `nbytes` attribute to get its memory size. ```python from desilofhe import Engine engine = Engine(use_bootstrap=True) secret_key = engine.create_secret_key() small_bootstrap_key = engine.create_small_bootstrap_key(secret_key) nbytes = small_bootstrap_key.nbytes ``` -------------------------------- ### Install DESILO FHE with CUDA 12.9 Support (poetry) Source: https://fhe.desilo.dev/latest/install Add the DESILO FHE library with CUDA 12.9 support to your project using poetry. This version requires a compatible CUDA toolkit installed on your system. Not available for musllinux or macOS. ```bash poetry add desilofhe-cu129 ``` -------------------------------- ### Install DESILO FHE with CUDA 12.1 Support (poetry) Source: https://fhe.desilo.dev/latest/install Add the DESILO FHE library with CUDA 12.1 support to your project using poetry. This version requires a compatible CUDA toolkit installed on your system. Not available for musllinux or macOS. ```bash poetry add desilofhe-cu121 ``` -------------------------------- ### Install DESILO FHE with CUDA 13.0 Support (poetry) Source: https://fhe.desilo.dev/latest/install Add the DESILO FHE library with CUDA 13.0 support to your project using poetry. This version requires a compatible CUDA toolkit installed on your system. Not available for musllinux or macOS. ```bash poetry add desilofhe-cu130 ``` -------------------------------- ### Install DESILO FHE with CUDA 12.8 Support (poetry) Source: https://fhe.desilo.dev/latest/install Add the DESILO FHE library with CUDA 12.8 support to your project using poetry. This version requires a compatible CUDA toolkit installed on your system. Not available for musllinux or macOS. ```bash poetry add desilofhe-cu128 ``` -------------------------------- ### Install DESILO FHE with CUDA 12.6 Support (poetry) Source: https://fhe.desilo.dev/latest/install Add the DESILO FHE library with CUDA 12.6 support to your project using poetry. This version requires a compatible CUDA toolkit installed on your system. Not available for musllinux or macOS. ```bash poetry add desilofhe-cu126 ``` -------------------------------- ### Install DESILO FHE with CUDA 12.4 Support (poetry) Source: https://fhe.desilo.dev/latest/install Add the DESILO FHE library with CUDA 12.4 support to your project using poetry. This version requires a compatible CUDA toolkit installed on your system. Not available for musllinux or macOS. ```bash poetry add desilofhe-cu124 ``` -------------------------------- ### Get Public Key Serialized Size Source: https://fhe.desilo.dev/latest/api/data-type/public_key/serialized_nbytes Demonstrates how to create a public key and retrieve its serialized size in bytes using the `serialized_nbytes` attribute. ```python from desilofhe import Engine engine = Engine() secret_key = engine.create_secret_key() public_key = engine.create_public_key(secret_key) serialized_nbytes = public_key.serialized_nbytes ``` -------------------------------- ### Get Relinearization Key Level Source: https://fhe.desilo.dev/latest/api/data-type/relinearization_key/level Demonstrates how to create a relinearization key and access its maximum relinearization level. ```python from desilofhe import Engine engine = Engine() secret_key = engine.create_secret_key() relinearization_key = engine.create_relinearization_key(secret_key) level = relinearization_key.level ``` -------------------------------- ### Create Engine with Bootstrap Enabled Source: https://fhe.desilo.dev/latest/create_engine Enable bootstrapping for advanced users. This configuration uses `log_coeff_count=16` and `special_prime_count=4`. Bootstrapping is not available for `log_coeff_count` below 15 or at 17 and above due to key size limitations. ```python from desilofhe import Engine bootstrap_engine = Engine(use_bootstrap=True) ``` -------------------------------- ### Get PlainMatrix Level Source: https://fhe.desilo.dev/latest/api/data-type/plain_matrix/level This snippet demonstrates how to encode a message into a plain matrix and then retrieve its level. Ensure the desilofhe library and numpy are installed. ```python import numpy as np from desilofhe import Engine engine = Engine(slot_count=64) message = np.arange(64 * 64).reshape(64, 64) plain_matrix = engine.encode_to_plain_matrix(message) level = plain_matrix.level ``` -------------------------------- ### Create Engine with Custom Parameters Source: https://fhe.desilo.dev/latest/create_engine For advanced users, specify `log_coeff_count` (logarithm of polynomial size) and `special_prime_count` (key switching partition size) for custom configurations. ```python from desilofhe import Engine # Multiplication count 0 ~ 2: tiny_engine = Engine(log_coeff_count=13, special_prime_count=1) # Multiplication count 3 ~ 7: small_engine = Engine(log_coeff_count=14, special_prime_count=1) # Multiplication count 8 ~ 16: medium_engine = Engine(log_coeff_count=15, special_prime_count=2) # Multiplication count 17 ~ 34: large_engine = Engine(log_coeff_count=16, special_prime_count=4) # Multiplication count 35 ~ 72: huge_engine = Engine(log_coeff_count=17, special_prime_count=6) ``` -------------------------------- ### Create Lossy Bootstrap Key with Different Configurations Source: https://fhe.desilo.dev/latest/api/engine/lossy_bootstrap_key/create_lossy_bootstrap_key Demonstrates creating a lossy bootstrap key with default settings, custom stage counts, and custom sizes. The `for_bootstrap` parameter must be set to `True` when initializing the `Engine` for bootstrapping operations. ```python from desilofhe import Engine engine = Engine(for_bootstrap=True) secret_key = engine.create_secret_key() lossy_bootstrap_key = engine.create_lossy_bootstrap_key(secret_key) medium_lossy_bootstrap_key_with_5_stages = engine.create_lossy_bootstrap_key( secret_key, stage_count=5 ) large_lossy_bootstrap_key = engine.create_lossy_bootstrap_key( secret_key, size="large" ) large_lossy_bootstrap_key_with_4_stages = engine.create_lossy_bootstrap_key( secret_key, stage_count=4, size="large" ) ``` -------------------------------- ### Create and Use Bootstrap Key Source: https://fhe.desilo.dev/latest/bootstrap Demonstrates the creation of a standard bootstrap key and its use in bootstrapping a ciphertext. Ensure the Engine is initialized with `use_bootstrap=True`. ```python from desilofhe import Engine engine = Engine(slot_count=1024, use_bootstrap=True) secret_key = engine.create_secret_key() public_key = engine.create_public_key(secret_key) relinearization_key = engine.create_relinearization_key(secret_key) conjugation_key = engine.create_conjugation_key(secret_key) bootstrap_key = engine.create_bootstrap_key(secret_key, stage_count=3) message = [-1, 0, 1, 0] * 256 ciphertext = engine.encrypt(message, public_key, level=0) bootstrapped = engine.bootstrap( ciphertext, relinearization_key, conjugation_key, bootstrap_key ) ``` -------------------------------- ### Install DESILO FHE with CUDA 13.0 Support (pip) Source: https://fhe.desilo.dev/latest/install Install the DESILO FHE library with CUDA 13.0 support using pip. This version requires a compatible CUDA toolkit installed on your system. Not available for musllinux or macOS. ```bash pip install desilofhe-cu130 ``` -------------------------------- ### Get PublicKey Level Source: https://fhe.desilo.dev/latest/api/data-type/public_key/level Demonstrates how to create a PublicKey and access its level attribute. The level indicates the maximum encryption level the key supports. ```python from desilofhe import Engine engine = Engine() secret_key = engine.create_secret_key() public_key = engine.create_public_key(secret_key) level = public_key.level ``` -------------------------------- ### Install DESILO FHE with CUDA 12.9 Support (pip) Source: https://fhe.desilo.dev/latest/install Install the DESILO FHE library with CUDA 12.9 support using pip. This version requires a compatible CUDA toolkit installed on your system. Not available for musllinux or macOS. ```bash pip install desilofhe-cu129 ``` -------------------------------- ### Create Default Engine Source: https://fhe.desilo.dev/latest/api/engine/engine Instantiate an Engine with default settings or specify mode, multiparty, bootstrap, thread count, and GPU device ID. Note that only one of bootstrap, bootstrap_to_14_levels, bootstrap_to_17_levels, or multiparty can be enabled. ```python from desilofhe import Engine engine1 = Engine() engine2 = Engine(mode="parallel", use_multiparty=True, thread_count=256) engine3 = Engine(mode="gpu", use_bootstrap=True, thread_count=512, device_id=1) ``` -------------------------------- ### Sign Bootstrap Example Source: https://fhe.desilo.dev/latest/bootstrap Performs a sign bootstrap operation and calculates the resulting average noise. This is useful for understanding the noise amplification or reduction characteristics of the bootstrapping process. ```python relinearization_key = engine.create_relinearization_key(secret_key) conjugation_key = engine.create_conjugation_key(secret_key) lossy_bootstrap_key = engine.create_lossy_bootstrap_key(secret_key) message = [-1, 1] * (engine.slot_count // 2) ciphertext = engine.encrypt(message, secret_key) bootstrapped = engine.sign_bootstrap( ciphertext, relinearization_key, conjugation_key, lossy_bootstrap_key ) decrypted = engine.decrypt(bootstrapped, secret_key) average_noise = np.mean(abs(message - decrypted)) print(f"Sign Bootstrap Average Noise :{average_noise}") print(f"Sign Bootstrap Average Noise (Bits) :{np.log2(average_noise)}") ``` -------------------------------- ### Install DESILO FHE with CUDA 12.8 Support (pip) Source: https://fhe.desilo.dev/latest/install Install the DESILO FHE library with CUDA 12.8 support using pip. This version requires a compatible CUDA toolkit installed on your system. Not available for musllinux or macOS. ```bash pip install desilofhe-cu128 ``` -------------------------------- ### Install DESILO FHE with CUDA 12.6 Support (pip) Source: https://fhe.desilo.dev/latest/install Install the DESILO FHE library with CUDA 12.6 support using pip. This version requires a compatible CUDA toolkit installed on your system. Not available for musllinux or macOS. ```bash pip install desilofhe-cu126 ``` -------------------------------- ### Create a Basic Engine Source: https://fhe.desilo.dev/latest/create_engine Use this for the default engine creation. It supports a multiplicative depth of 7. ```python from desilofhe import Engine engine = Engine() ``` -------------------------------- ### Install DESILO FHE with CUDA 12.4 Support (pip) Source: https://fhe.desilo.dev/latest/install Install the DESILO FHE library with CUDA 12.4 support using pip. This version requires a compatible CUDA toolkit installed on your system. Not available for musllinux or macOS. ```bash pip install desilofhe-cu124 ``` -------------------------------- ### Check PublicKey Storage Location (GPU vs CPU) Source: https://fhe.desilo.dev/latest/api/data-type/public_key/is_cuda Demonstrates how to create public keys using both GPU and CPU engines and assert their respective storage locations using the `is_cuda` attribute. This helps in verifying that keys are correctly allocated to the intended hardware. ```python from desilofhe import Engine gpu_engine = Engine(mode="gpu") gpu_secret_key = gpu_engine.create_secret_key() gpu_public_key = gpu_engine.create_public_key(gpu_secret_key) assert gpu_public_key.is_cuda cpu_engine = Engine(mode="cpu") cpu_secret_key = cpu_engine.create_secret_key() cpu_public_key = cpu_engine.create_public_key(cpu_secret_key) assert not cpu_public_key.is_cuda ``` -------------------------------- ### Install DESILO FHE with CUDA 12.1 Support (pip) Source: https://fhe.desilo.dev/latest/install Install the DESILO FHE library with CUDA 12.1 support using pip. This version requires a compatible CUDA toolkit installed on your system. Not available for musllinux or macOS. ```bash pip install desilofhe-cu121 ``` -------------------------------- ### Check PublicKeyB CUDA Storage Source: https://fhe.desilo.dev/latest/api/data-type/public_key_b/is_cuda Demonstrates how to create PublicKeyB instances on both GPU and CPU and assert their respective `is_cuda` status. Use this to verify device placement. ```python from desilofhe import Engine gpu_engine = Engine(use_multiparty=True, mode="gpu") gpu_secret_key = gpu_engine.create_secret_key() gpu_public_key_a = gpu_engine.create_public_key_a() gpu_public_key_b = gpu_engine.create_public_key_b( gpu_secret_key, gpu_public_key_a ) assert gpu_public_key_b.is_cuda cpu_engine = Engine(use_multiparty=True, mode="cpu") cpu_secret_key = cpu_engine.create_secret_key() cpu_public_key_a = cpu_engine.create_public_key_a() cpu_public_key_b = cpu_engine.create_public_key_b( cpu_secret_key, cpu_public_key_a ) assert not cpu_public_key_b.is_cuda ``` -------------------------------- ### Get Plaintext Length Source: https://fhe.desilo.dev/latest/api/data-type/plaintext/__len__ Demonstrates how to get the length of a plaintext object after encoding. The length is calculated based on the engine's slot count. ```python from desilofhe import Engine engine = Engine() message = [1] * 2 * engine.slot_count plaintext = engine.encode(message) length = len(plaintext) # 2 ``` -------------------------------- ### Create Engine with Bootstrap to 17 Levels Source: https://fhe.desilo.dev/latest/create_engine Enable bootstrapping that results in 17 levels remaining after the operation. This is for advanced users requiring a specific number of levels post-bootstrapping. ```python from desilofhe import Engine bootstrap_engine = Engine(use_bootstrap_to_17_levels=True) ``` -------------------------------- ### Get Public Key Size in Bytes Source: https://fhe.desilo.dev/latest/api/data-type/public_key/nbytes Demonstrates how to create a public key and retrieve its size in bytes using the `nbytes` attribute. This is useful for estimating memory usage. ```python from desilofhe import Engine engine = Engine() secret_key = engine.create_secret_key() public_key = engine.create_public_key(secret_key) nbytes = public_key.nbytes ``` -------------------------------- ### Get Plaintext Serialized Bytes Source: https://fhe.desilo.dev/latest/api/data-type/plaintext/serialized_nbytes Use the `serialized_nbytes` attribute on a plaintext object to get its size in bytes after serialization. This is useful for understanding data size before transmission or storage. ```python from desilofhe import Engine engine = Engine() message = [1, 2, 3] plaintext = engine.encode(message) serialized_nbytes = plaintext.serialized_nbytes ``` -------------------------------- ### Create Bootstrap Key with Default Settings Source: https://fhe.desilo.dev/latest/api/engine/bootstrap_key/create_bootstrap_key Creates a bootstrap key with default stage count (3) and size (medium). Requires an initialized Engine and a secret key. ```python from desilofhe import Engine engine = Engine(for_bootstrap=True) secret_key = engine.create_secret_key() bootstrap_key = engine.create_bootstrap_key(secret_key) ``` -------------------------------- ### Get Rotation Key Level Source: https://fhe.desilo.dev/latest/api/data-type/rotation_key/level This snippet demonstrates how to create an Engine, generate a secret key and a rotation key, and then access the `level` attribute to get the maximum rotation level. ```APIDOC ## Get Rotation Key Level ### Description Retrieves the maximum level of a ciphertext that the rotation key can rotate. ### Method Attribute Access ### Endpoint N/A (Object Attribute) ### Parameters None ### Request Example ```python from desilofhe import Engine engine = Engine() secret_key = engine.create_secret_key() rotation_key = engine.create_rotation_key(secret_key) level = rotation_key.level print(level) ``` ### Response #### Success Response - **level** (int) - The maximum rotation level. ``` -------------------------------- ### Sign Bootstrap with Small Key and Stage Count Source: https://fhe.desilo.dev/latest/api/engine/sign_bootstrap Illustrates bootstrapping sign values using the small bootstrap key, which is slower but more memory-efficient. This example also shows how to specify a custom stage count for bootstrapping. The ciphertext level must be at least the specified stage count. ```python from desilofhe import Engine engine = Engine(use_bootstrap=True) secret_key = engine.create_secret_key() public_key = engine.create_public_key(secret_key) relinearization_key = engine.create_relinearization_key(secret_key) conjugation_key = engine.create_conjugation_key(secret_key) rotation_key = engine.create_rotation_key(secret_key) small_bootstrap_key = engine.create_small_bootstrap_key(secret_key) message = [-1, 1] ciphertext_level_3 = engine.encrypt(message, public_key, level=3) bootstrapped = engine.sign_bootstrap( ciphertext_level_3, relinearization_key, conjugation_key, rotation_key, small_bootstrap_key, ) ciphertext_level_5 = engine.encrypt(message, public_key, level=5) bootstrapped_stage_count_5 = engine.sign_bootstrap( ciphertext_level_5, relinearization_key, conjugation_key, rotation_key, small_bootstrap_key, stage_count=5, ) ``` -------------------------------- ### Get Merge Bootstrap Key Size Source: https://fhe.desilo.dev/latest/api/data-type/merge_bootstrap_key/nbytes Instantiate an Engine, create a secret key, then a merge bootstrap key, and finally access its `nbytes` attribute to get its size in memory. ```python from desilofhe import Engine engine = Engine(use_bootstrap=True) secret_key = engine.create_secret_key() merge_bootstrap_key = engine.create_merge_bootstrap_key(secret_key) nbytes = merge_bootstrap_key.nbytes ``` -------------------------------- ### Running Argmax on Encrypted Data Source: https://fhe.desilo.dev/latest/advanced_examples Demonstrates how to encrypt data, compute its argmax using the previously defined function, and decrypt the result. Requires setting up the engine and keys. ```python from desilofhe import Engine import math engine = Engine(use_bootstrap=True) secret_key = engine.create_secret_key() rotation_key = engine.create_rotation_key(secret_key) relinearization_key = engine.create_relinearization_key(secret_key) conjugation_key = engine.create_conjugation_key(secret_key) bootstrap_key = engine.create_bootstrap_key(secret_key, stage_count=3) # data data = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8] n = len(data) # encrypt encrypted = engine.encrypt(data, secret_key) # compute argmax encrypted_argmax = argmax( encrypted, n, rotation_key, relinearization_key, conjugation_key, bootstrap_key, ) # decrypt and print the result decrypted = engine.decrypt(encrypted_argmax, secret_key) print(decrypted[:16]) # [~0 ~0 ~0 ~0 ~0 ~0 ~0 ~1 ~0 ...] ``` -------------------------------- ### Check SmallBootstrapKey CUDA Storage Source: https://fhe.desilo.dev/latest/api/data-type/small_bootstrap_key/is_cuda Demonstrates how to create small bootstrap keys for both GPU and CPU modes and assert their respective CUDA storage status using `is_cuda`. ```python from desilofhe import Engine gpu_engine = Engine(use_bootstrap=True, mode="gpu") gpu_secret_key = gpu_engine.create_secret_key() gpu_small_bootstrap_key = gpu_engine.create_small_bootstrap_key(gpu_secret_key) assert gpu_small_bootstrap_key.is_cuda cpu_engine = Engine(use_bootstrap=True, mode="cpu") cpu_secret_key = cpu_engine.create_secret_key() cpu_small_bootstrap_key = cpu_engine.create_small_bootstrap_key(cpu_secret_key) assert not cpu_small_bootstrap_key.is_cuda ``` -------------------------------- ### Get Serialized Size of Light Plaintext Source: https://fhe.desilo.dev/latest/api/data-type/light_plaintext/serialized_nbytes Use `serialized_nbytes` to get the size of a light plaintext object after encoding a message. This property returns an integer representing the size in bytes. ```python from desilofhe import Engine engine = Engine() message = [1, 2, 3] light_plaintext = engine.encode_to_light_plaintext(message) serialized_nbytes = light_plaintext.serialized_nbytes ``` -------------------------------- ### Get Lossy Bootstrap Key Size Source: https://fhe.desilo.dev/latest/api/data-type/lossy_bootstrap_key/nbytes Instantiate the engine, create a secret key, then a lossy bootstrap key, and finally access its `nbytes` attribute to get its memory size. ```python from desilofhe import Engine engine = Engine(use_bootstrap=True) secret_key = engine.create_secret_key() lossy_bootstrap_key = engine.create_lossy_bootstrap_key(secret_key) nbytes = lossy_bootstrap_key.nbytes ``` -------------------------------- ### Get Decrypted Share Length Source: https://fhe.desilo.dev/latest/api/data-type/decrypted_share/__len__ This snippet demonstrates how to obtain the length of a decrypted share. It requires setting up an engine, keys, encrypting a message, and then decrypting it to get the `DecryptedShare` object. ```python from desilofhe import Engine engine = Engine(use_multiparty=True) secret_key_1 = engine.create_secret_key() secret_key_2 = engine.create_secret_key() public_key_a = engine.create_public_key_a() public_key_b1 = engine.create_public_key_b(secret_key1, public_key_a) public_key_b2 = engine.create_public_key_b(secret_key2, public_key_a) public_key = engine.create_multiparty_public_key( [public_key_b1, public_key_b2], public_key_a ) message = [1] * 2 * engine.slot_count ciphertext = engine.encrypt(message, public_key) decrypted_share = engine.individual_decrypt(ciphertext, secret_key_1) length = len(decrypted_share) # 2 ``` -------------------------------- ### Create PublicKeyB with and without Level - Python Source: https://fhe.desilo.dev/latest/api/engine/public_key_b/create_public_key_b Demonstrates creating a PublicKeyB using the `create_public_key_b` function. Shows both a default creation and one with a specified multiplication level. ```python from desilofhe import Engine engine = Engine(use_multiparty=True) secret_key = engine.create_secret_key() public_key_a = engine.create_public_key_a() public_key_b = engine.create_public_key_b(secret_key, public_key_a) level_3_public_key_b = engine.create_public_key_b( secret_key, public_key_a, level=3 ) ``` -------------------------------- ### Verify GPU vs CPU Storage for Merge Bootstrap Key Source: https://fhe.desilo.dev/latest/api/data-type/merge_bootstrap_key/is_cuda Instantiate engines for both GPU and CPU modes, create secret keys, and then create merge bootstrap keys. Assert that `is_cuda` is true for the GPU key and false for the CPU key to confirm correct storage. ```python from desilofhe import Engine gpu_engine = Engine(use_bootstrap=True, mode="gpu") gpu_secret_key = gpu_engine.create_secret_key() gpu_merge_bootstrap_key = gpu_engine.create_merge_bootstrap_key(gpu_secret_key) assert gpu_merge_bootstrap_key.is_cuda cpu_engine = Engine(use_bootstrap=True, mode="cpu") cpu_secret_key = cpu_engine.create_secret_key() cpu_merge_bootstrap_key = cpu_engine.create_merge_bootstrap_key(cpu_secret_key) assert not cpu_merge_bootstrap_key.is_cuda ``` -------------------------------- ### Lossy Bootstrap with Lossy Bootstrap Key Source: https://fhe.desilo.dev/latest/api/engine/lossy_bootstrap Use this method for faster bootstrapping when memory is not a constraint. The engine must be initialized with `use_bootstrap=True`. The ciphertext level must be at least the stage count of the lossy bootstrap key. ```python from desilofhe import Engine engine = Engine(use_bootstrap=True) secret_key = engine.create_secret_key() public_key = engine.create_public_key(secret_key) relinearization_key = engine.create_relinearization_key(secret_key) conjugation_key = engine.create_conjugation_key(secret_key) lossy_bootstrap_key = engine.create_lossy_bootstrap_key( secret_key, stage_count=3 ) message = [-1, 0, 1] ciphertext = engine.encrypt(message, public_key, level=3) bootstrapped = engine.lossy_bootstrap( ciphertext, relinearization_key, conjugation_key, lossy_bootstrap_key ) ``` -------------------------------- ### Get DecryptedShare Serialized Size Source: https://fhe.desilo.dev/latest/api/data-type/decrypted_share/serialized_nbytes Access the `serialized_nbytes` attribute of a `DecryptedShare` object to get its size in bytes. This requires setting up the FHE engine, keys, and encrypting/decrypting a message to obtain a `DecryptedShare` object. ```python from desilofhe import Engine engine = Engine(use_multiparty=True) secret_key_1 = engine.create_secret_key() secret_key_2 = engine.create_secret_key() public_key_a = engine.create_public_key_a() public_key_b1 = engine.create_public_key_b(secret_key1, public_key_a) public_key_b2 = engine.create_public_key_b(secret_key2, public_key_a) public_key = engine.create_multiparty_public_key( [public_key_b1, public_key_b2], public_key_a ) message = [1] * 2 * engine.slot_count ciphertext = engine.encrypt(message, public_key) decrypted_share = engine.individual_decrypt(ciphertext, secret_key_1) serialized_nbytes = decrypted_share.serialized_nbytes ``` -------------------------------- ### Get Conjugation Key Size Source: https://fhe.desilo.dev/latest/api/data-type/conjugation_key/nbytes Demonstrates how to create a conjugation key and retrieve its size in bytes using the .nbytes attribute. Requires the desilofhe library. ```python from desilofhe import Engine engine = Engine() secret_key = engine.create_secret_key() conjugation_key = engine.create_conjugation_key(secret_key) nbytes = conjugation_key.nbytes ``` -------------------------------- ### Create Conjugation Key with and without Level Source: https://fhe.desilo.dev/latest/api/engine/conjugation_key/create_conjugation_key Demonstrates creating a default conjugation key and one with a specified multiplication level. Requires an initialized Engine and a secret key. ```python from desilofhe import Engine engine = Engine() secret_key = engine.create_secret_key() conjugation_key = engine.create_conjugation_key(secret_key) level_3_conjugation_key = engine.create_conjugation_key(secret_key, level=3) ``` -------------------------------- ### Create and Use Small Bootstrap Key Source: https://fhe.desilo.dev/latest/bootstrap Illustrates the creation and usage of a small bootstrap key, which is more efficient for bootstrapping operations. This variant requires a rotation key as well. ```python from desilofhe import Engine engine = Engine(slot_count=1024, use_bootstrap=True) secret_key = engine.create_secret_key() public_key = engine.create_public_key(secret_key) relinearization_key = engine.create_relinearization_key(secret_key) conjugation_key = engine.create_conjugation_key(secret_key) rotation_key = engine.create_rotation_key(secret_key) small_bootstrap_key = engine.create_small_bootstrap_key(secret_key) message = [-1, 0, 1, 0] * 256 ciphertext = engine.encrypt(message, public_key, level=0) bootstrapped = engine.bootstrap( ciphertext, relinearization_key, conjugation_key, rotation_key, small_bootstrap_key, stage_count=1, ) ``` -------------------------------- ### Merge Bootstrap with Merge Bootstrap Key Source: https://fhe.desilo.dev/latest/api/engine/merge_bootstrap Use this snippet when faster bootstrapping is preferred and memory is not a constraint. Ensure the engine is initialized for bootstrapping. ```python from desilofhe import Engine engine = Engine(use_bootstrap=True) secret_key = engine.create_secret_key() public_key = engine.create_public_key(secret_key) relinearization_key = engine.create_relinearization_key(secret_key) conjugation_key = engine.create_conjugation_key(secret_key) merge_bootstrap_key = engine.create_merge_bootstrap_key( secret_key, stage_count=3 ) message1 = [-1, 0, 1] message2 = [1, 0, -1] ciphertext1 = engine.encrypt(message1, public_key, level=0) ciphertext2 = engine.encrypt(message2, public_key, level=0) bootstrapped1, bootstrapped2 = engine.merge_bootstrap( ciphertext1, ciphertext2, relinearization_key, conjugation_key, merge_bootstrap_key, ) ``` -------------------------------- ### Create Merge Bootstrap Key with Default Settings Source: https://fhe.desilo.dev/latest/api/engine/merge_bootstrap_key/create_merge_bootstrap_key Creates a merge bootstrap key using default settings for stage count and size. Requires a secret key. ```python from desilofhe import Engine engine = Engine(for_bootstrap=True) secret_key = engine.create_secret_key() merge_bootstrap_key = engine.create_merge_bootstrap_key(secret_key) ``` -------------------------------- ### Create PublicKeyA with Engine Source: https://fhe.desilo.dev/latest/api/engine/public_key_a/create_public_key_a Instantiate a PublicKeyA object using the Engine. Use the `use_multiparty=True` flag when initializing the Engine for multiparty computation. The `create_public_key_a` method can be called without arguments for a default key, or with a `level` argument to generate a key with a reduced size for a specific number of multiplication levels. ```python from desilofhe import Engine engine = Engine(use_multiparty=True) public_key_a = engine.create_public_key_a() level_3_public_key_a = engine.create_public_key_a(level=3) ``` -------------------------------- ### Subtraction between two ciphertexts Source: https://fhe.desilo.dev/latest/api/engine/subtract Example demonstrating subtraction between two ciphertexts. ```APIDOC ## Subtraction between two ciphertexts ### Description Subtracts one ciphertext from another. ### Code Example ```python from desilofhe import Engine engine = Engine() secret_key = engine.create_secret_key() public_key = engine.create_public_key(secret_key) message1 = [1, 2, 3] ciphertext1 = engine.encrypt(message1, public_key) message2 = [4, 5, 6] ciphertext2 = engine.encrypt(message2, public_key) subtracted = engine.subtract(ciphertext1, ciphertext2) ``` ``` -------------------------------- ### Create Engine with Bootstrap to 14 Levels Source: https://fhe.desilo.dev/latest/create_engine Enable bootstrapping that results in 14 levels remaining after the operation. This differs from the standard bootstrap option which leaves 10 levels but supports more bootstrapping operation varieties. ```python from desilofhe import Engine bootstrap_engine = Engine(use_bootstrap_to_14_levels=True) ``` -------------------------------- ### RotationKey.nbytes Source: https://fhe.desilo.dev/latest/api/data-type/rotation_key/nbytes Access the `nbytes` attribute to get the memory size of the rotation key. ```APIDOC ## RotationKey.nbytes ### Description Shows the memory size of a rotation key. ### Output - **nbytes** (int) - The memory size of the rotation key in bytes. ``` -------------------------------- ### RelinearizationKey.nbytes Source: https://fhe.desilo.dev/latest/api/data-type/relinearization_key/nbytes Access the `nbytes` attribute to get the memory size of a relinearization key. ```APIDOC ## RelinearizationKey.nbytes ### Description Shows the memory size of a relinearization key. ### Method Attribute Access ### Endpoint N/A ### Parameters None ### Request Example ```python from desilofhe import Engine engine = Engine() secret_key = engine.create_secret_key() relinearization_key = engine.create_relinearization_key(secret_key) nbytes = relinearization_key.nbytes print(nbytes) ``` ### Response #### Success Response (int) - **nbytes** (int) - The memory size of the relinearization key in bytes. ``` -------------------------------- ### Bootstrap Ciphertext with Small Bootstrap Key Source: https://fhe.desilo.dev/latest/api/engine/bootstrap Initializes the engine for bootstrapping, creates necessary keys, encrypts a message, and then bootstraps the ciphertext using the small bootstrap key. This method uses less memory but is slower. The stage_count parameter can be adjusted. ```python from desilofhe import Engine engine = Engine(use_bootstrap=True) secret_key = engine.create_secret_key() public_key = engine.create_public_key(secret_key) relinearization_key = engine.create_relinearization_key(secret_key) conjugation_key = engine.create_conjugation_key(secret_key) rotation_key = engine.create_rotation_key(secret_key) small_bootstrap_key = engine.create_small_bootstrap_key(secret_key) message = [-1, 0, 1] ciphertext = engine.encrypt(message, public_key, level=0) bootstrapped = engine.bootstrap( ciphertext, relinearization_key, conjugation_key, rotation_key, small_bootstrap_key, ) bootstrapped_stage_count_5 = engine.bootstrap( ciphertext, relinearization_key, conjugation_key, rotation_key, small_bootstrap_key, stage_count=5, ) ``` -------------------------------- ### Sum along the 1-axis Source: https://fhe.desilo.dev/latest/api/engine/sum Example demonstrating how to sum a ciphertext along the 1-axis. ```APIDOC ## Sum along the 1-axis ### Description This example shows how to compute the sum of a ciphertext specifically along the 1-axis. ### Code Example ```python from desilofhe import Engine engine = Engine(slot_count=4) secret_key = engine.create_secret_key() rotation_key = engine.create_rotation_key(secret_key) message_4 = [1, 2, 3, 4] ciphertext_4 = engine.encrypt(message_4, secret_key) # contents of ciphertext_4: # [[1, 2, 3, 4]] sum_of_ciphertext_4 = engine.sum(ciphertext_4, rotation_key, axis=1) # contents of sum_of_ciphertext_4: # [[10, 10, 10, 10]] message_8 = [1, 2, 3, 4, 5, 6, 7, 8] ciphertext_8 = engine.encrypt(message_8, secret_key) # contents of ciphertext_8: # [[1, 2, 3, 4], # [5, 6, 7, 8]] sum_of_ciphertext_8 = engine.sum(ciphertext_8, rotation_key, axis=1) # contents of sum_of_ciphertext_8: # [[10, 10, 10, 10], # [26, 26, 26, 26]] ``` ``` -------------------------------- ### Create Bootstrap Key with Custom Stage Count Source: https://fhe.desilo.dev/latest/api/engine/bootstrap_key/create_bootstrap_key Creates a bootstrap key with a specified stage count. This example uses 5 stages, which affects bootstrapping speed and result level. ```python medium_bootstrap_key_with_5_stages = engine.create_bootstrap_key( secret_key, stage_count=5 ) ``` -------------------------------- ### Bootstrap Ciphertext with Bootstrap Key Source: https://fhe.desilo.dev/latest/api/engine/bootstrap Initializes the engine for bootstrapping, creates necessary keys, encrypts a message, and then bootstraps the ciphertext using the bootstrap key. This method is faster but uses more memory. ```python from desilofhe import Engine engine = Engine(use_bootstrap=True) secret_key = engine.create_secret_key() public_key = engine.create_public_key(secret_key) relinearization_key = engine.create_relinearization_key(secret_key) conjugation_key = engine.create_conjugation_key(secret_key) bootstrap_key = engine.create_bootstrap_key(secret_key, stage_count=3) message = [-1, 0, 1] ciphertext = engine.encrypt(message, public_key, level=0) bootstrapped = engine.bootstrap( ciphertext, relinearization_key, conjugation_key, bootstrap_key ) ``` -------------------------------- ### Sum along the 0-axis Source: https://fhe.desilo.dev/latest/api/engine/sum Example demonstrating how to sum a ciphertext along the 0-axis. ```APIDOC ## Sum along the 0-axis ### Description This example shows how to compute the sum of a ciphertext specifically along the 0-axis. ### Code Example ```python from desilofhe import Engine engine = Engine(slot_count=4) secret_key = engine.create_secret_key() rotation_key = engine.create_rotation_key(secret_key) message_4 = [1, 2, 3, 4] ciphertext_4 = engine.encrypt(message_4, secret_key) # contents of ciphertext_4: # [[1, 2, 3, 4]] sum_of_ciphertext_4 = engine.sum(ciphertext_4, rotation_key, axis=0) # contents of sum_of_ciphertext_4: # [[1, 2, 3, 4]] message_8 = [1, 2, 3, 4, 5, 6, 7, 8] ciphertext_8 = engine.encrypt(message_8, secret_key) # contents of ciphertext_8: # [[1, 2, 3, 4], # [5, 6, 7, 8]] sum_of_ciphertext_8 = engine.sum(ciphertext_8, rotation_key, axis=0) # contents of sum_of_ciphertext_8: # [[6, 8, 10, 12]] ``` ``` -------------------------------- ### Create Rotation Key with GLEngine Source: https://fhe.desilo.dev/latest/api/gl-engine/create_rotation_key Demonstrates creating a secret key and then a default rotation key. Use this for standard 3D rotations. ```python from desilofhe import GLEngine engine = GLEngine() secret_key = engine.create_secret_key() rotation_key = engine.create_rotation_key(secret_key) ``` -------------------------------- ### LightPlainMatrix.serialized_nbytes Source: https://fhe.desilo.dev/latest/api/data-type/light_plain_matrix/serialized_nbytes Access the serialized_nbytes attribute to get the size of the serialized light plain matrix. ```APIDOC ## LightPlainMatrix.serialized_nbytes ### Description Shows the size of a light plain matrix when serialized. ### Output - `serialized_nbytes` (int): The size of the serialized matrix in bytes. ### Example ```python import numpy as np from desilofhe import Engine engine = Engine(slot_count=64) message = np.arange(64 * 64).reshape(64, 64) light_plain_matrix = engine.encode_to_light_plain_matrix(message) serialized_nbytes = light_plain_matrix.serialized_nbytes print(f"Serialized size: {serialized_nbytes} bytes") ``` ``` -------------------------------- ### Subtraction of a plaintext from a ciphertext in-place Source: https://fhe.desilo.dev/latest/api/engine/subtract Example demonstrating in-place subtraction of a plaintext from a ciphertext. ```APIDOC ## Subtraction of a plaintext from a ciphertext in-place ### Description Performs subtraction in-place, modifying the ciphertext with the result of subtracting a plaintext from it. ### Code Example ```python from desilofhe import Engine engine = Engine() secret_key = engine.create_secret_key() public_key = engine.create_public_key(secret_key) message1 = [1, 2, 3] plaintext1 = engine.encode(message1) ciphertext1 = engine.encrypt(plaintext1, public_key) message2 = [4, 5, 6] plaintext2 = engine.encode(message2) engine.subtract(ciphertext1, plaintext2, out=ciphertext1) decrypted = engine.decrypt(ciphertext1, secret_key) # [-3, -3, -3] ``` ``` -------------------------------- ### Lossy Bootstrap with Small Bootstrap Key Source: https://fhe.desilo.dev/latest/api/engine/lossy_bootstrap Use this method when memory is a constraint, as it uses less memory than the lossy bootstrap key but is slower. The engine must be initialized with `use_bootstrap=True`. The ciphertext level must be at least the specified stage count. ```python from desilofhe import Engine engine = Engine(use_bootstrap=True) secret_key = engine.create_secret_key() public_key = engine.create_public_key(secret_key) relinearization_key = engine.create_relinearization_key(secret_key) conjugation_key = engine.create_conjugation_key(secret_key) rotation_key = engine.create_rotation_key(secret_key) small_bootstrap_key = engine.create_small_bootstrap_key(secret_key) message = [-1, 0, 1] ciphertext_level_3 = engine.encrypt(message, public_key, level=3) bootstrapped = engine.lossy_bootstrap( ciphertext_level_3, relinearization_key, conjugation_key, rotation_key, small_bootstrap_key, ) ciphertext_level_5 = engine.encrypt(message, public_key, level=5) bootstrapped_stage_count_5 = engine.lossy_bootstrap( ciphertext_level_5, relinearization_key, conjugation_key, rotation_key, small_bootstrap_key, stage_count=5, ) ``` -------------------------------- ### Create Individual Fixed Rotation Keys Source: https://fhe.desilo.dev/latest/api/engine/fixed_rotation_key/create_individual_fixed_rotation_key Demonstrates the creation of individual fixed rotation keys with and without specifying a multiplication level. Ensure you have initialized the Engine and created the necessary secret and public keys beforehand. ```python from desilofhe import Engine engine = Engine() secret_key1 = engine.create_secret_key() secret_key2 = engine.create_secret_key() public_key_a = engine.create_public_key_a() public_key_b1 = engine.create_public_key_b(secret_key1, public_key_a) public_key_b2 = engine.create_public_key_b(secret_key2, public_key_a) public_key = engine.create_multiparty_public_key( [public_key_b1, public_key_b2], public_key_a ) fixed_rotation_key1 = engine.create_individual_fixed_rotation_key( secret_key1, public_key, delta=1 ) level_3_fixed_rotation_key1 = engine.create_individual_fixed_rotation_key( secret_key1, public_key, delta=1, level=3 ) ``` -------------------------------- ### Subtraction in-place between two ciphertexts Source: https://fhe.desilo.dev/latest/api/engine/subtract Example demonstrating in-place subtraction between two ciphertexts. ```APIDOC ## Subtraction in-place between two ciphertexts ### Description Performs subtraction in-place, modifying the first ciphertext with the result of subtracting the second ciphertext from it. ### Code Example ```python from desilofhe import Engine engine = Engine() secret_key = engine.create_secret_key() public_key = engine.create_public_key(secret_key) message1 = [1, 2, 3] ciphertext1 = engine.encrypt(message1, public_key) message2 = [4, 5, 6] ciphertext2 = engine.encrypt(message2, public_key) engine.subtract(ciphertext1, ciphertext2, out=ciphertext1) decrypted = engine.decrypt(ciphertext1, secret_key) # [-3, -3, -3] ``` ``` -------------------------------- ### Subtraction of a plaintext from a ciphertext Source: https://fhe.desilo.dev/latest/api/engine/subtract Example demonstrating subtraction of a plaintext from a ciphertext, and vice versa. ```APIDOC ## Subtraction of a plaintext from a ciphertext ### Description Subtracts a plaintext from a ciphertext, or a ciphertext from a plaintext. ### Code Example ```python from desilofhe import Engine engine = Engine() secret_key = engine.create_secret_key() public_key = engine.create_public_key(secret_key) message1 = [1, 2, 3] plaintext1 = engine.encode(message1) ciphertext1 = engine.encrypt(plaintext1, public_key) message2 = [4, 5, 6] plaintext2 = engine.encode(message2) # ciphertext2 = engine.encrypt(plaintext2, public_key) # This line is not used in the example but is present in the source subtracted1 = engine.subtract(ciphertext1, plaintext2) subtracted2 = engine.subtract(plaintext1, ciphertext2) ``` ``` -------------------------------- ### Execute ModRaise with Bootstrap Key Source: https://fhe.desilo.dev/latest/api/engine/mod_raise Use this snippet to execute the ModRaise function with a full bootstrap key. Ensure the engine is initialized with use_bootstrap=True and that a bootstrap key is generated. ```python from desilofhe import Engine engine = Engine(use_bootstrap=True) secret_key = engine.create_secret_key() public_key = engine.create_public_key(secret_key) bootstrap_key = engine.create_bootstrap_key(secret_key, stage_count=3) message = [-1, 0, 1] ciphertext = engine.encrypt(message, public_key, level=0) mod_raised = engine.mod_raise(ciphertext, bootstrap_key) ``` -------------------------------- ### Bootstrap to 14 Levels with Small Bootstrap Key Source: https://fhe.desilo.dev/latest/bootstrap Initializes the FHE engine for bootstrapping to 14 levels and generates necessary keys, including a small bootstrap key. This is useful when a smaller key size is preferred for bootstrapping. ```python from desilofhe import Engine engine = Engine(use_bootstrap_to_14_levels=True) secret_key = engine.create_secret_key() public_key = engine.create_public_key(secret_key) relinearization_key = engine.create_relinearization_key(secret_key) conjugation_key = engine.create_conjugation_key(secret_key) rotation_key = engine.create_rotation_key(secret_key) small_bootstrap_key = engine.create_small_bootstrap_key(secret_key) message = [-1, 0, 1] ciphertext = engine.encrypt(message, public_key, level=0) bootstrapped = engine.bootstrap( ciphertext, relinearization_key, conjugation_key, rotation_key, small_bootstrap_key, ) ``` -------------------------------- ### Sum along all axes Source: https://fhe.desilo.dev/latest/api/engine/sum Example demonstrating how to sum all elements of a ciphertext when no axis is specified. ```APIDOC ## Sum along all axes ### Description This example shows how to compute the sum of all elements in a ciphertext by omitting the `axis` parameter. ### Code Example ```python from desilofhe import Engine engine = Engine(slot_count=4) secret_key = engine.create_secret_key() rotation_key = engine.create_rotation_key(secret_key) message_4 = [1, 2, 3, 4] ciphertext_4 = engine.encrypt(message_4, secret_key) # contents of ciphertext_4: # [[1, 2, 3, 4]] sum_of_ciphertext_4 = engine.sum(ciphertext_4, rotation_key) # contents of sum_of_ciphertext_4: # [[10, 10, 10, 10]] message_8 = [1, 2, 3, 4, 5, 6, 7, 8] ciphertext_8 = engine.encrypt(message_8, secret_key) # contents of ciphertext_8: # [[1, 2, 3, 4], # [5, 6, 7, 8]] sum_of_ciphertext_8 = engine.sum(ciphertext_8, rotation_key) # contents of sum_of_ciphertext_8: # [[36, 36, 36, 36]] ``` ``` -------------------------------- ### LightPlainMatrix.level Source: https://fhe.desilo.dev/latest/api/data-type/light_plain_matrix/level Accessing the level property of a LightPlainMatrix object to get the maximum possible number of multiplications. ```APIDOC ## LightPlainMatrix.level ### Description Shows the maximum possible number of multiplications for a light plain matrix. ### Output - **level** (int) - The maximum possible number of multiplications. ``` -------------------------------- ### Get Matrix Multiplication Key Size Source: https://fhe.desilo.dev/latest/api/data-type/matrix_multiplication_key/nbytes Demonstrates how to create a matrix multiplication key and retrieve its size in bytes using the `.nbytes` attribute. Requires the `desilofhe` library. ```python from desilofhe import Engine engine = Engine(slot_count=64) secret_key = engine.create_secret_key() matrix_multiplication_key = engine.create_matrix_multiplication_key(secret_key) nbytes = matrix_multiplication_key.nbytes ``` -------------------------------- ### Get ConjugationKey Size Source: https://fhe.desilo.dev/latest/api/data-type/conjugation_key/nbytes The `nbytes` attribute of a `ConjugationKey` object returns its size in bytes as an integer. ```APIDOC ## ConjugationKey.nbytes ### Description Shows the size of a conjugation key in bytes. ### Method Attribute Access ### Endpoint N/A (Object Attribute) ### Parameters None ### Response #### Success Response - **nbytes** (int) - The size of the conjugation key in bytes. ```