---
title: calculateByIndex
description: 调用此函数对基础算子进行计算，结果输出索引。目前支持密态最大值、密态最小值、密态排序。
url: "https://www.hikunpeng.com/document/detail/zh/kunpengcctrustzone/tee/kcal/kunpeng_kcal_16_0097.html"
---

# calculateByIndex

调用此函数对基础算子进行计算，结果输出索引。目前支持密态最大值、密态最小值、密态排序。
```
int calculateByIndex(
DG_TeeCtx *dgTeeCtx,
DG_AlgorithmsType type,
DG_MpcShareSet *shareSet,
DG_MpcShare **share
)
```


**表1 参数说明**

| 参数名 | 描述 | 取值范围 | 输入/输出 |
| --- | --- | --- | --- |
| dgTeeCtx | KCAL上下文信息 | 为initTeeCtx成功初始化的结果 | 输入 |
| type | 算子枚举 DG\_AlgorithmsType | ASCEND\_SORT ： 升序排序 DESCEND\_SORT ：降序排序 MAX：最大值 MIN：最小值 | 输入 |
| shareSet | 分片数据组装后的结构体 类型为DG\_MpcShareSet | 不能输入为NULL | 输入 |
| share | 计算结果 类型为：DG\_MpcShare | 一级指针为NULL，二级指针不为NULL | 输出 |


**表2 DG_MpcShareSet结构体**

| 名称 | 类型 | 说明 |
| --- | --- | --- |
| \*shareSet | DG\_MpcShare | 每个参与方的分片 |
| size | unsigned long | 参与方数量 |


**表3 DG_MpcShare结构体**

| 名称 | 类型 | 说明 |
| --- | --- | --- |
| dataShare | Share\* | 分片结构 |
| size | unsigned long | 数据规模 |
| shareType | DG\_ShareType | 分片类型 |


#### 返回值

- 成功：返回0。
- 失败：返回错误码。


#### 错误码


**表4 错误码**

| 错误码 | 错误码值 | 描述 | 特殊说明 |
| --- | --- | --- | --- |
| DG\_ERR\_MPC\_TEE\_INVALID\_PARAM | 4501 | 无效参数 | \- |
| DG\_ERR\_MPC\_TEE\_INVALID\_ALGORITHM\_TYPE | 4502 | 无效算法类型 | \- |
| DG\_ERR\_MALLOC\_FAIL | 51 | 内存分配失败 | \- |
| DG\_ERR\_MPC\_TEE\_DIV\_IS\_ZERO | 4512 | 除0异常 | \- |
| DG\_FAILURE | 1 | 算子执行失败 | 参数校验失败 分片失败 接收对方数据失败 |


#### 依赖

- dgTeeCtx依赖  initTeeCtx
的成功初始化。
- 入参需为调用makeShare后的结果。
- 依赖的头文件为"data_guard_mpc.h"。


#### 示例

- 需使用调用initTeeCtx成功后的dgTeeCtx来调用calculateByIndex接口。
- 通过分片获取的是单个数据方的分片结果，需要用户多次调用makeShare，当前仅支持最大值、最小值、排序算子，故仅有0节点数据参与计算。
- 仅支持2方运算，2方需设置一方为0节点，另一方为1节点。
- 计算的为最大值算子，样例只做参考，share1为分片的结果，不能真正运行。

- 运行结果：shares不为NULL且res为0。


```
#include  "data_guard_mpc.h"
//计算方
DG_AlgorithmsType type = MAX;
std::unique_ptr<DG_MpcShareSet> shares = std::make_unique<DG_MpcShareSet>();
std::unique_ptr<DG_MpcShare[]> share_datas = std::make_unique<DG_MpcShare[]>(1);
share_datas[0] = *share1;
shares->shareSet = share_datas.get();
shares->size = 1;
DG_MpcShare *share_out = nullptr;
res = aritOpts.calculateByIndex(dgTee, type, shares.get(), &share_out);
```
