ComputeRequest Class
The ComputeRequest class represents a request sent to the OpenVector network to perform a computation. It encapsulates the operation to be performed, along with the operands required for the computation.
Public Member Classes and Enums
enum class ComputeOperationType
enum class ComputeOperationTypeSpecifies the parity of the compute operation.
Values:
UNARY: Operation with one operand.BINARY: Operation with two operands.TERNARY: Operation with three operands.
enum class ComputeOperation
enum class ComputeOperationSpecifies the type of computation to perform.
Values:
DECRYPTADDSUBTRACTMULTIPLYDIVIDE
enum class DataType
enum class DataTypeSpecifies the type of data being used.
Values:
SINGLE: Single data element.TENSOR: Tensor data.TENSOR_ID: Reference to a tensor ID (data encryption type is ignored).
enum class DataEncrytionType
enum class DataEncrytionTypeSpecifies whether the data is in plaintext or ciphertext form.
Values:
PLAINTEXTCIPHERTEXT
class ComputeOperationOperand
class ComputeOperationOperandRepresents an operand in a compute operation.
Constructors
ComputeOperationOperand(DataType data_type, DataEncrytionType encryption_type, std::string data);Creates an operand with the specified data type, encryption type, and data.
ComputeOperationOperand operand( ComputeRequest::DataType::SINGLE, ComputeRequest::DataEncrytionType::CIPHERTEXT, serializedData );
Member Functions
DataType& data_type();Returns a reference to the data type.
const DataType& data_type() const;Returns a constant reference to the data type.
DataEncrytionType& encryption_type();Returns a reference to the encryption type.
const DataEncrytionType& encryption_type() const;Returns a constant reference to the encryption type.
std::string& data();Returns a reference to the data string.
const std::string& data() const;Returns a constant reference to the data string.
std::string to_string() const;Serializes the operand to a string.
static ComputeOperationOperand from_string(const std::string& str);Deserializes an operand from a string.
static std::vector<ComputeOperationOperand> from_string(const std::string& str, size_t num_operands);Deserializes multiple operands from a string.
class ComputeOperationInstance
class ComputeOperationInstanceRepresents an instance of a compute operation, including its type and operands.
Constructors
ComputeOperationInstance(const ComputeOperationType& operation_type, const ComputeOperation& operation, const std::vector<ComputeOperationOperand>& operands);Creates a compute operation instance.
ComputeOperationInstance operation( ComputeOperationType::BINARY, ComputeOperation::ADD, { operand1, operand2 } );
Member Functions
ComputeOperationType& operation_type();Returns a reference to the operation type.
const ComputeOperationType& operation_type() const;Returns a constant reference to the operation type.
ComputeOperation& operation();Returns a reference to the operation.
const ComputeOperation& operation() const;Returns a constant reference to the operation.
std::vector<ComputeOperationOperand>& operands();Returns a reference to the vector of operands.
const std::vector<ComputeOperationOperand>& operands() const;Returns a constant reference to the vector of operands.
std::string to_string() const;Serializes the operation instance to a string.
static ComputeOperationInstance from_string(const std::string& str);Deserializes an operation instance from a string.
Constructors
ComputeRequest(const ComputeOperationInstance& operation);
ComputeRequest(const ComputeOperationInstance& operation);Creates a compute request with the specified operation instance.
ComputeRequest request(operationInstance);Member Functions
ComputeOperationInstance& operation();Returns a reference to the compute operation instance.
const ComputeOperationInstance& operation() const;Returns a constant reference to the compute operation instance.
std::string to_string() const;Serializes the compute request to a string.
static ComputeRequest from_string(const std::string& str);Deserializes a compute request from a string.
Usage Example
// Assuming cs is an instance of CryptoSystem and pk is the network public key
// Encrypt two plaintexts
PlainTextImpl pt1 = cs.make_plaintext(5.0f);
PlainTextImpl pt2 = cs.make_plaintext(7.0f);
CipherTextImpl ct1 = cs.encrypt(pk, pt1);
CipherTextImpl ct2 = cs.encrypt(pk, pt2);
// Serialize the ciphertexts
std::string serialized_ct1 = cs.serialize_ciphertext(ct1);
std::string serialized_ct2 = cs.serialize_ciphertext(ct2);
// Create operands
ComputeRequest::ComputeOperationOperand operand1(
ComputeRequest::DataType::SINGLE,
ComputeRequest::DataEncrytionType::CIPHERTEXT,
serialized_ct1
);
ComputeRequest::ComputeOperationOperand operand2(
ComputeRequest::DataType::SINGLE,
ComputeRequest::DataEncrytionType::CIPHERTEXT,
serialized_ct2
);
// Create compute operation instance for addition
ComputeRequest::ComputeOperationInstance operation(
ComputeRequest::ComputeOperationType::BINARY,
ComputeRequest::ComputeOperation::ADD,
{ operand1, operand2 }
);
// Create the compute request
ComputeRequest request(operation);
// Send the request using ClientNode
ComputeResponse* response = nullptr;
clientNode.compute(request, &response);Notes
When creating a
ComputeRequest, ensure that the data in operands is correctly serialized and corresponds to the expected encryption and data types.The
ComputeOperationenum defines basic arithmetic operations and decryption. More operations can be added as needed.
See Also
Last updated