{"id":10285,"library":"tenseal","title":"TenSEAL","description":"TenSEAL (v0.3.16) is a Python library providing homomorphic encryption operations on tensors, built on Microsoft SEAL. It enables computations on encrypted data without decrypting it, enhancing privacy for machine learning and other applications. Releases are frequent, often incorporating updates from the underlying SEAL library and expanding Python version support.","status":"active","version":"0.3.16","language":"en","source_language":"en","source_url":"https://github.com/OpenMined/TenSEAL","tags":["homomorphic encryption","privacy","machine learning","cryptography","tensor"],"install":[{"cmd":"pip install tenseal","lang":"bash","label":"Install TenSEAL"}],"dependencies":[],"imports":[{"note":"While `from tenseal import context` might work for some IDEs, the idiomatic and most stable way to access core components like `context`, `ckks_vector`, etc., is via `import tenseal as ts` due to the library's structure.","wrong":"from tenseal import context","symbol":"context","correct":"import tenseal as ts\nctx = ts.context(...)"},{"symbol":"CKKSVector","correct":"import tenseal as ts\nciph = ts.ckks_vector(...)"},{"symbol":"BFVVector","correct":"import tenseal as ts\nciph = ts.bfv_vector(...)"}],"quickstart":{"code":"import tenseal as ts\n\n# 1. Setup TenSEAL Context for CKKS scheme\n# These parameters are critical for security and functionality.\npoly_mod_degree = 8192\ncoeff_mod_bit_sizes = [60, 40, 40, 60]\n\n# Create a TenSEAL context\ncontext = ts.context(\n    ts.SCHEME_TYPE.CKKS,\n    poly_mod_degree=poly_mod_degree,\n    coeff_mod_bit_sizes=coeff_mod_bit_sizes\n)\n\n# Generate keys for homomorphic operations (e.g., rotations, relinearization for multiplication)\ncontext.generate_galois_keys()\ncontext.generate_relin_keys()\n\n# Set the scale for CKKS scheme\n# The scale defines the precision of computations.\nscale = 2**40\ncontext.global_scale = scale\n\n# 2. Encrypt some data\nvector1 = [0, 1, 2, 3, 4]\nvector2 = [5, 6, 7, 8, 9]\n\nencrypted_vector1 = ts.ckks_vector(context, vector1)\nencrypted_vector2 = ts.ckks_vector(context, vector2)\n\n# 3. Perform homomorphic operations (e.g., addition)\nencrypted_sum = encrypted_vector1 + encrypted_vector2\n\n# 4. Decrypt and verify\n# The decryption operation uses the secret key from the context.\ndecrypted_sum = encrypted_sum.decrypt()\n\nprint(f\"Original vector 1: {vector1}\")\nprint(f\"Original vector 2: {vector2}\")\nprint(f\"Encrypted sum (truncated): {decrypted_sum[:5]}\") # Print first 5 elements\n\n# Expected result: [5.0, 7.0, 9.0, 11.0, 13.0]\n","lang":"python","description":"This quickstart demonstrates setting up a TenSEAL context for the CKKS scheme, encrypting two vectors, performing homomorphic addition on them, and then decrypting the result. It highlights the critical steps of context initialization, key generation, and scale setting for encrypted computations."},"warnings":[{"fix":"Upgrade to Python 3.8 or newer, or pin TenSEAL to version <=0.3.14.","message":"Support for Python 3.7 was dropped in TenSEAL v0.3.15. Projects requiring Python 3.7 must use TenSEAL v0.3.14 or earlier.","severity":"breaking","affected_versions":">=0.3.15"},{"fix":"Carefully consult TenSEAL and SEAL documentation for appropriate parameter selection based on security requirements, desired precision, and the complexity of operations. Start with recommended parameters for common schemes.","message":"The choice of homomorphic encryption parameters (e.g., `poly_mod_degree`, `coeff_mod_bit_sizes`, `global_scale`) is crucial for both security and functionality. Incorrect parameters can lead to insecure encryption, operations failing, or insufficient precision.","severity":"gotcha","affected_versions":"All"},{"fix":"Optimize vector sizes, choose the smallest secure parameters, and consider algorithmic changes to reduce the number or complexity of encrypted operations. Use TenSEAL's built-in optimized operations where possible.","message":"Performance of homomorphic encryption operations is significantly lower than plaintext operations. Large vectors, high polynomial degrees, or complex operations can be very resource-intensive.","severity":"gotcha","affected_versions":"All"},{"fix":"After upgrading TenSEAL to a new minor version (especially one that updates the underlying SEAL version), review and re-validate your encryption context parameters and test critical operations.","message":"Updates to the underlying Microsoft SEAL library (e.g., in v0.3.9, v0.3.13, v0.3.14) can subtly change behavior, default parameters, or introduce new features. While TenSEAL aims for abstraction, parameter-sensitive operations might be affected.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Ensure the `context` object used for decryption (or the `secret_context` argument in `decrypt()`) is the exact same one (or a properly reconstructed one with the same secret key) that was used to create the encrypted data.","cause":"The context used for decryption does not contain the correct secret key matching the one used for encryption, or the ciphertext itself is corrupted.","error":"RuntimeError: TenSEAL decryption error: input ciphertext does not have a matching public key"},{"fix":"Verify the length/shape of all input vectors involved in an operation. Ensure they are compatible according to TenSEAL's rules for the specific operation (e.g., element-wise addition requires same size).","cause":"Attempting an operation between two TenSEAL vectors (or a vector and a plaintext array) where their dimensions are incompatible or do not match the expected size for the operation.","error":"ValueError: input vector has wrong size"},{"fix":"Ensure `tenseal` is installed and use `import tenseal as ts`, then access components via `ts.context(...)`, `ts.ckks_vector(...)`, etc.","cause":"This usually indicates attempting to access `context` or other core TenSEAL components directly as a top-level module attribute without proper import aliasing, or `tenseal` is not correctly installed.","error":"AttributeError: module 'tenseal' has no attribute 'context'"}]}