Tensor Multiplication

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

5. Create a Compute Request for Matrix Multiplication

We will create a ComputeRequest to perform homomorphic matrix multiplication.

    // Create operands for the compute request
    ComputeRequest::ComputeOperationOperand operand1(
        ComputeRequest::DataType::TENSOR,
        ComputeRequest::DataEncrytionType::CIPHERTEXT,
        serialized_ct1
    );

    ComputeRequest::ComputeOperationOperand operand2(
        ComputeRequest::DataType::TENSOR,
        ComputeRequest::DataEncrytionType::CIPHERTEXT,
        serialized_ct2
    );

    // Define the compute operation instance
    ComputeRequest::ComputeOperationInstance operation(
        ComputeRequest::ComputeOperationType::BINARY,
        ComputeRequest::ComputeOperation::MULTIPLY,
        { operand1, operand2 }
    );

    // Create the compute request
    ComputeRequest req(operation);

Explanation:

  • We create two ComputeOperationOperand objects representing the serialized tensors.

  • We specify that the data type is TENSOR and the encryption type is CIPHERTEXT.

  • We define a ComputeOperationInstance for the multiplication operation with two operands.

  • The ComputeRequest is created with this operation instance.

6. Send the Compute Request and Receive the Response

We send the request to the network and receive the encrypted result.

    // Measure the start time
    auto start = std::chrono::high_resolution_clock::now();

    // Perform the computation
    ComputeResponse* res = nullptr;
    client_node.compute(req, &res);

    // Measure the end time
    auto stop = std::chrono::high_resolution_clock::now();

    // Check if the computation was successful
    if (res && res->status() == ComputeResponse::Status::OK) {
        std::cout << "Encrypted result tensor received." << std::endl;
    } else {
        std::cerr << "Computation failed." << std::endl;
        return 1;
    }

Explanation:

  • We measure the time before and after the computation to calculate the duration.

  • The compute method sends the request and populates the res pointer with the response.

  • We check the status of the response to ensure the computation was successful.

Last updated