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.

Last updated