1 /* 2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 * Description: 多精度整数(Multi-precision integer)配层接口(此文件为DEMO,需集成方适配修改) 15 */ 16 #ifndef HILINK_SAL_MPI_H 17 #define HILINK_SAL_MPI_H 18 19 #include <stdint.h> 20 #include "hilink_sal_defines.h" 21 22 #ifdef __cplusplus 23 extern "C" { 24 #endif 25 26 /** @brief 标识多精度整数MPI */ 27 typedef void* HiLinkMpi; 28 29 /** 30 * @brief 大数初始化 31 * 32 * @return 非NULL大数指针,NULL失败 33 * @attention 返回的大数使用完毕调用HILINK_SAL_MpiFree释放 34 */ 35 HiLinkMpi HILINK_SAL_MpiInit(void); 36 37 /** 38 * @brief 释放大数资源 39 * 40 * @param mpi [IN] 已初始化的MPI 41 */ 42 void HILINK_SAL_MpiFree(HiLinkMpi mpi); 43 44 /** 45 * @brief 模指数运算 x = a^e mod n 46 * 47 * @param x [OUT] 运算结果,应为已初始化的MPI 48 * @param a [IN] 待求幂的数,应为已初始化的MPI 49 * @param e [IN] 指数,应为已初始化的MPI 50 * @param n [IN] 模数,应为已初始化的MPI 51 * @return 0表示成功,非0失败 52 */ 53 int HILINK_SAL_MpiExpMod(HiLinkMpi x, HiLinkMpi a, HiLinkMpi e, HiLinkMpi n); 54 55 /** 56 * @brief MPI与整数比较 57 * 58 * @param x [IN] 待比较的MPI,应为已初始化的MPI 59 * @param z [IN] 待比较的整数 60 * @retval 0 x与z相等 61 * @retval 1 x大于z 62 * @retval -1 x小于z 63 * @retval 其他 失败 64 */ 65 int HILINK_SAL_MpiCmpInt(HiLinkMpi x, int64_t z); 66 67 /** 68 * @brief MPI与整数相减 x = a - b 69 * 70 * @param x [OUT] 运算结果,应为已初始化的MPI 71 * @param a [IN] 被减数 72 * @param b [IN] 减数 73 * @return 0成功,非0失败 74 */ 75 int HILINK_SAL_MpiSubInt(HiLinkMpi x, HiLinkMpi a, int64_t b); 76 77 /** 78 * @brief 两个MPI比较大小 79 * 80 * @param x [IN] 待比较的MPI,应为已初始化的MPI 81 * @param y [IN] 待比较的MPI,应为已初始化的MPI 82 * @retval 0 x与Y相等 83 * @retval 1 x大于Y 84 * @retval -1 x小于Y 85 * @retval 其他 失败 86 */ 87 int HILINK_SAL_MpiCmpMpi(HiLinkMpi x, HiLinkMpi y); 88 89 /** 90 * @brief 从字符串读导入MPI 91 * 92 * @param mpi [OUT] 导入结果,应为已初始化的MPI 93 * @param radix [IN] 进制,范围应为[2,16] 94 * @param s [IN] 待读取的字符串 95 * @return 0成功,非0失败 96 */ 97 int HILINK_SAL_MpiReadString(HiLinkMpi mpi, unsigned char radix, const char *s); 98 99 /** 100 * @brief 从MPI导出字符串 101 * 102 * @param mpi [IN] 待导出字符串的MPI 103 * @param radix [IN] 进制,范围应为[2,16] 104 * @param buf [OUT] 输出缓冲区,有效可写长度至少为*bufLen 105 * @param bufLen [IN,OUT] 输入为输出缓冲区长度,输出为包括\0的字符串长度 106 * @return 0成功,非0失败 107 */ 108 int HILINK_SAL_MpiWriteString(HiLinkMpi mpi, unsigned int radix, char *buf, unsigned int *bufLen); 109 110 /** 111 * @brief 从无符号大端二进制数据导入MPI 112 * 113 * @param mpi [OUT] 导入结果,应为已初始化的MPI 114 * @param buf [IN] 二进制数据缓冲区,有效可读长度至少为bufLen 115 * @param bufLen [IN] 二进制数据长度 116 * @return 0成功,非0失败 117 */ 118 int HILINK_SAL_MpiReadBinary(HiLinkMpi mpi, const unsigned char *buf, unsigned int bufLen); 119 120 /** 121 * @brief 从MPI导出无符号大端二进制数据 122 * 123 * @param mpi [IN] 待导出二进制数据的MPI 124 * @param buf [OUT] 二进制数据缓冲区,有效可写长度至少为bufLen 125 * @param bufLen [IN] 缓冲区长度 126 * @return 0成功,非0失败 127 */ 128 int HILINK_SAL_MpiWriteBinary(HiLinkMpi mpi, unsigned char *buf, unsigned int bufLen); 129 130 #ifdef __cplusplus 131 } 132 #endif 133 134 #endif /* HILINK_SAL_MPI_H */ 135