🔐Encryption

The core of the cryptographic functionality is the cipher.encrypt() method. This method takes a plaintext numerical vector and converts it into an encrypted CipherBlock object, which can then be securely stored or used for searching.

The most critical parameter in this process is the encoding_type, which tells the SDK how to prepare the vector for its specific purpose.


Encoding Type

When encrypting data, you must specify its role by setting the encoding_type parameter. This determines how the data will be processed and ensures it is encrypted in the correct format for its intended purpose.

There are two possible values:

  • "item": Choose this when encrypting a vector that represents a record to be inserted and stored in the index on the server. Use this for your dataset entries that will be part of the searchable index.

  • "query": Choose this when encrypting a vector that will be used as a search input against the stored index. Use this for the vector you want to look up or compare against the dataset.


Usage and Examples

Below are examples demonstrating how to use cipher.encrypt() for both index entries and search queries.

1. Encrypting an Index Entry

When you want to add a new encrypted vector to your index, you must set encode_type="item".

from pyenvector import Cipher

# Initialize the Cipher class with the FHE context
# (No keys needed here if providing them on-the-fly)
cipher = Cipher(dim=512)

# This is the vector you want to insert into the index
item_vector = [0.1, 0.2, 0.3] * (512 // 3) + [0.0] * (512 % 3)

# Encrypt the vector for index insertion
# The result is a CipherBlock object ready to be stored
encrypted_item = cipher.encrypt(
    item_vector, 
    encode_type="item", 
    enc_key_path="keys/example/EncKey.json"
)

# Now you can insert 'encrypted_item' into your index
# index.insert(encrypted_item)

2. Encrypting a Search Query

When you want to search for a vector in your index, you must encrypt your search vector with encode_type="query".

from pyenvector import Cipher

# Initialize the Cipher class
cipher = Cipher(dim=512)

# This is the vector you want to search with
query_vector = [0.9, 0.8, 0.7] * (512 // 3) + [0.0] * (512 % 3)

# Encrypt the vector for querying
encrypted_query = cipher.encrypt(
    query_vector, 
    encode_type="query", 
    enc_key_path="keys/example/EncKey.json"
)

# Now you can use 'encrypted_query' to perform a search
# search_results = index.search(encrypted_query)

Bulk Encryption with encrypt_multiple

For situations where you need to encrypt many vectors at once, such as during a bulk index insertion, the SDK provides the highly efficient cipher.encrypt_multiple() method. Using this method is significantly faster than calling cipher.encrypt() in a loop.

encrypt_multiple() is specifically designed for preparing large batches of data for storage. Because of this, it only supports the "item" encoding type. You must always set encoding_type="item" when using this method.


Usage and Example

The method takes a list of vectors and returns a CipherBlock object.

Encrypting a Batch of Index Entries

When you need to insert a large number of vectors into your index, prepare them in a list and pass them to encrypt_multiple.

from pyenvector import Cipher

# Initialize the Cipher class
cipher = Cipher(dim=512)

# Create a list of vectors that you want to bulk insert
list_of_item_vectors = [
    [0.1, 0.2, 0.3] * (512 // 3) + [0.0] * (512 % 3),  # Vector 1
    [0.4, 0.5, 0.6] * (512 // 3) + [0.0] * (512 % 3),  # Vector 2
    [0.7, 0.8, 0.9] * (512 // 3) + [0.0] * (512 % 3)   # Vector 3
]

# Encrypt the entire list of vectors in a single, efficient operation
encrypted_items = cipher.encrypt_multiple(
    list_of_item_vectors, 
    encode_type="item",
    enc_key_path="keys/example/EncKey.json",
)

# The result is a CipherBlock object, ready for a bulk insert operation
# index.insert(encrypted_items) ✅

Last updated