OpenVector
  • OpenVector
  • Introduction
    • Our Thesis
    • Our Solution
    • Other Approaches
    • Benchmarks
  • Network Architecture
    • Overview
    • Networking
    • Deploy Compute Node
    • Deploy CoFHE Node
    • Deploy Client Node
  • Quick start
    • Overview
    • Setting up Client Node
    • Encrypting Data
    • Tensor Multiplication
    • Decrypting the Ouput
    • Verifying the Output
    • Running the Program
  • Tutorials
    • Building MLP Block
  • Use Cases
    • Confidential LLM Inference
    • Training on Encrypted Data
    • Vector Search on Encrypted Data
    • Encrypted Data Access Control
  • API references
    • CryptoSystem Interface
    • ClientNode Class
    • ComputeRequest Class
    • ComputeResponse Class
  • PYTHON API REFERENCES
    • Overview
    • Tensor
    • nn.Module
    • nn.Functional
  • Contribution
    • Call for Research
    • CoFHE Library
Powered by GitBook
On this page
  1. Quick start

Encrypting Data

In this article we continue our quick start series. We assume that you are following from the previous article.

3. Create Tensors of Encrypted Data

We will create two tensors (ct1 and ct2) of encrypted data to perform matrix multiplication.

    // Define dimensions for the matrices
    size_t n = 8, m = 8, p = 8;

    size_t n = 8, m = 8, p = 8;
    Tensor<CPUCryptoSystem::PlainText*> pt1(n, m, nullptr);
    pt1.flatten();
    for (size_t i = 0; i < n * m; i++) {
        pt1.at(i) = new CPUCryptoSystem::PlainText(cs.make_plaintext(i + 1));
    }

    Tensor<CPUCryptoSystem::PlainText*> pt2(m, p, nullptr);
    pt2.flatten();
    for (size_t i = 0; i < m * p; i++) {
        pt2.at(i) = new CPUCryptoSystem::PlainText(cs.make_plaintext(i + 1));
    }

    pt1.reshape({n, m});
    pt2.reshape({m, p});
    auto ct1 = cs.encrypt_tensor(client_node.network_public_key(), pt1);
    auto ct2 = cs.encrypt_tensor(client_node.network_public_key(), pt2);

Explanation:

  • We define the dimensions for our matrices.

  • We create two tensors ct1 and ct2 initialized with nullptr.

  • The flatten method simplifies the traversal of all elements.

  • We use lambda functions init_ct1 and init_ct2 to initialize each element:

    • Create a plaintext with an incremented float value.

    • Encrypt the plaintext using the network's public key.

    • Store the ciphertext in the tensor.

  • After initialization, we reshape the tensors back to their matrix dimensions.

4. Serialize Tensors

Before creating a compute request, we need to serialize the tensors.

    // Serialize the tensors of ciphertexts
    std::string serialized_ct1 = cs.serialize_ciphertext_tensor(ct1);
    std::string serialized_ct2 = cs.serialize_ciphertext_tensor(ct2);

Explanation:

  • The serialize_ciphertext_tensor method converts the tensor of ciphertexts into a string for transmission.

PreviousSetting up Client NodeNextTensor Multiplication

Last updated 6 months ago