Rate This Document
Findability
Accuracy
Completeness
Readability

calculate

Performs calculation using basic operators, including cryptographic addition, subtraction, maximum, minimum, multiplication, division, averaging, summation, sorting, and comparison operators.

int calculate(
	DG_TeeCtx *dgTeeCtx,
        DG_AlgorithmsType type,
        DG_MpcShareSet *shareSet,
        DG_MpcShare **share
)

Parameters

Table 1 Description

Parameter

Description

Value Range

Input/Output

dgTeeCtx

KCAL context

Context initialized in section initTeeCtx.

Input

type

Operator enumeration:

DG_AlgorithmsType

MULTI // Multiplication operator

DIV // Division operator

LT // Less-than operator

LT_EQ // Less-than-or-equal-to operator

GT // Greater-than operator

GT_EQ // Greater-than-or-equal-to operator

EQ // Equal-to operator

NEQ // Not-equal-to operator

AVG // Average operator

SUM // Sum operator

ASCEND_SORT // Ascending sort operator

DESCEND_SORT // Descending sort operator

ADD // Addition operator

SUB // Subtraction operator

MAX // Maximum operator

MIN // Minimum operator

Input

shareSet

The type of the structure after concatenating the shares:

DG_MpcShareSet

The value is not null.

Input

share

The type of the calculation result:

DG_MpcShare

It is null as a single pointer and cannot be null as a double pointer.

Output

Table 2 DG_MpcShareSet structure

Name

Type

Description

*shareSet

DG_MpcShare

Shares of each participant

size

unsigned long

Number of participants

Table 3 DG_MpcShare structure

Name

Type

Description

dataShare

Share*

Share structure

size

unsigned long

Data volume

shareType

DG_ShareType

Share type

Return Values

  • Success: 0 is returned.
  • Failure: The error code is returned.

Error Codes

Table 4 Error codes

Error Code

Value

Description

Remarks

DG_ERR_MPC_TEE_INVALID_PARAM

4501

Invalid parameter

-

DG_ERR_MPC_TEE_INVALID_ALGORITHM_TYPE

4502

Invalid algorithm type

-

DG_ERR_MALLOC_FAIL

51

Memory allocation failure

-

DG_ERR_MPC_TEE_DIV_IS_ZERO

4512

Division-by-zero error

-

DG_FAILURE

1

Operator execution failure

Failed to verify the parameters.

Failed to split data into shares.

Failed to receive data from the other node.

Dependency

  • dgTeeCtx depends on the successful initialization of initTeeCtx.
  • The input parameter must be the result of a successful makeShare call.
  • The dependent header file is data_guard_mpc.h.

Example

  • Call the calculate API using the dgTeeCtx output from the successful initTeeCtx call.
  • The share output indicates the data share of a single party. You need to call the makeShare API multiple times and concatenate the DG_MpcShare data from both parties into DG_MpcShareSet. The concatenation order is crucial. For example, before calling the less-than operator to confirm that a < b (a originates from compute node 1 and b from compute node 0), compute node 0 acquires share a, and then places share a at position 0 and share b at position 1 in DG_MpcShareSet.
  • Two-party and three-party operations are supported. For two-party operations, one party is set to node 0 and the other is set to node 1. For three-party operations, nodes 0, 1, and 2 are used.
  • The calculation uses the multiplication operator as an example, and shares 1 and 2 are for reference and cannot be executed.
  • Running result: The shares are not null and res is 0.
#include  "data_guard_mpc.h" 
// Compute party
DG_AlgorithmsType type = MULTI;
std::unique_ptr<DG_MpcShareSet> shares = std::make_unique<DG_MpcShareSet>();
std::unique_ptr<DG_MpcShare[]> share_datas = std::make_unique<DG_MpcShare[]>(2);
share_datas[0] = *share2;
share_datas[1] = *share1;
shares->shareSet = share_datas.get();
shares->size = 2;   
DG_MpcShare *share_out = nullptr;
res = aritOpts.calculate(dgTee, type, shares.get(), &share_out);