API参考
载入中...
搜索中...
未找到
bn_bincal.h
1/*
2 * This file is part of the openHiTLS project.
3 *
4 * openHiTLS is licensed under the Mulan PSL v2.
5 * You can use this software according to the terms and conditions of the Mulan PSL v2.
6 * You may obtain a copy of Mulan PSL v2 at:
7 *
8 * http://license.coscl.org.cn/MulanPSL2
9 *
10 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13 * See the Mulan PSL v2 for more details.
14 */
15
16#ifndef BN_BINCAL_H
17#define BN_BINCAL_H
18
19#include "hitls_build.h"
20#ifdef HITLS_CRYPTO_BN
21
22#include <stdint.h>
23#include "bn_basic.h"
24
25#if defined(HITLS_CRYPTO_BN_X8664)
26 #include "bn_bincal_x8664.h"
27#elif defined(HITLS_CRYPTO_BN_ARMV8)
28 #include "bn_bincal_armv8.h"
29#else
30 #include "bn_bincal_noasm.h"
31#endif
32
33#ifdef __cplusplus
34extern "c" {
35#endif
36
37/* r = a + b, input 'carry' means carry */
38#define ADD_AB(carry, r, a, b) \
39 do { \
40 BN_UINT macroTmpT = (a) + (b); \
41 (carry) = macroTmpT < (a) ? 1 : 0; \
42 (r) = macroTmpT; \
43 } while (0)
44
45/* r = a - b, input 'borrow' means borrow digit */
46#define SUB_AB(borrow, r, a, b) \
47 do { \
48 BN_UINT macroTmpT = (a) - (b); \
49 (borrow) = ((a) < (b)) ? 1 : 0; \
50 (r) = macroTmpT; \
51 } while (0)
52
53/* r = a - b - c, input 'borrow' means borrow digit */
54#define SUB_ABC(borrow, r, a, b, c) \
55 do { \
56 BN_UINT macroTmpS = (a) - (b); \
57 BN_UINT macroTmpB = ((a) < (b)) ? 1 : 0; \
58 macroTmpB += (macroTmpS < (c)) ? 1 : 0; \
59 (r) = macroTmpS - (c); \
60 borrow = macroTmpB; \
61 } while (0)
62
63#define BN_UINT_HALF_BITS (BN_UINT_BITS >> 1)
64
65/* carry value of the upper part */
66#define BN_UINT_HC ((BN_UINT)1 << BN_UINT_HALF_BITS)
67
68/* Takes the low bit and assigns it to the high bit. */
69#define BN_UINT_LO_TO_HI(t) ((t) << BN_UINT_HALF_BITS)
70
71/* Takes the high bit and assigns it to the high bit. */
72#define BN_UINT_HI_TO_HI(t) ((t) & ((BN_UINT)0 - BN_UINT_HC))
73
74/* Takes the low bit and assigns it to the low bit. */
75#define BN_UINT_LO(t) ((t) & (BN_UINT_HC - 1))
76
77/* Takes the high bit and assigns it to the low bit. */
78#define BN_UINT_HI(t) ((t) >> BN_UINT_HALF_BITS)
79
80/* copy bytes, ensure that dstLen >= srcLen */
81#define BN_COPY_BYTES(dst, dstlen, src, srclen) \
82 do { \
83 uint32_t macroTmpI; \
84 for (macroTmpI = 0; macroTmpI < (srclen); macroTmpI++) { (dst)[macroTmpI] = (src)[macroTmpI]; } \
85 for (; macroTmpI < (dstlen); macroTmpI++) { (dst)[macroTmpI] = 0; } \
86 } while (0)
87
88/* r = a * b + r + c, where c is refreshed as the new carry value */
89#define MULADD_ABC(c, r, a, b) \
90do { \
91 BN_UINT macroTmpAl = BN_UINT_LO(a); \
92 BN_UINT macroTmpAh = BN_UINT_HI(a); \
93 BN_UINT macroTmpBl = BN_UINT_LO(b); \
94 BN_UINT macroTmpBh = BN_UINT_HI(b); \
95 BN_UINT macroTmpX3 = macroTmpAh * macroTmpBh; \
96 BN_UINT macroTmpX2 = macroTmpAh * macroTmpBl; \
97 BN_UINT macroTmpX1 = macroTmpAl * macroTmpBh; \
98 BN_UINT macroTmpX0 = macroTmpAl * macroTmpBl; \
99 (r) += (c); \
100 (c) = ((r) < (c)) ? 1 : 0; \
101 macroTmpX1 += macroTmpX2; \
102 (c) += (macroTmpX1 < macroTmpX2) ? BN_UINT_HC : 0; \
103 macroTmpX2 = macroTmpX0; \
104 macroTmpX0 += macroTmpX1 << BN_UINT_HALF_BITS; \
105 (c) += (macroTmpX0 < macroTmpX2) ? 1 : 0; \
106 (c) += BN_UINT_HI(macroTmpX1); \
107 (c) += macroTmpX3; \
108 (r) += macroTmpX0; \
109 (c) += ((r) < macroTmpX0) ? 1 : 0; \
110} while (0)
111
112/* r = a + b + c, input 'carry' means carry. Note that a and carry cannot be the same variable. */
113#define ADD_ABC(carry, r, a, b, c) \
114 do { \
115 BN_UINT macroTmpS = (b) + (c); \
116 carry = (macroTmpS < (c)) ? 1 : 0; \
117 (r) = macroTmpS + (a); \
118 carry += ((r) < macroTmpS) ? 1 : 0; \
119 } while (0)
120
121BN_UINT BinAdd(BN_UINT *r, const BN_UINT *a, const BN_UINT *b, uint32_t n);
122
123BN_UINT BinSub(BN_UINT *r, const BN_UINT *a, const BN_UINT *b, uint32_t n);
124
125BN_UINT BinInc(BN_UINT *r, const BN_UINT *a, uint32_t size, BN_UINT w);
126
127BN_UINT BinDec(BN_UINT *r, const BN_UINT *a, uint32_t n, BN_UINT w);
128
129uint32_t BinRshift(BN_UINT *r, const BN_UINT *a, uint32_t n, uint32_t bits);
130
131BN_UINT BinSubMul(BN_UINT *r, const BN_UINT *a, BN_UINT aSize, BN_UINT m);
132
133uint32_t BinLshift(BN_UINT *r, const BN_UINT *a, uint32_t n, uint32_t bits);
134
135BN_UINT BinMulAcc(BN_UINT *r, const BN_UINT *a, uint32_t aSize, BN_UINT b);
136
137uint32_t BinMul(BN_UINT *r, uint32_t rRoom, const BN_UINT *a, uint32_t aSize, const BN_UINT *b, uint32_t bSize);
138
139uint32_t BinSqr(BN_UINT *r, uint32_t rRoom, const BN_UINT *a, uint32_t aSize);
140
141uint32_t GetZeroBitsUint(BN_UINT x);
142
143uint32_t BinFixSize(const BN_UINT *data, uint32_t size);
144
145int32_t BinCmp(const BN_UINT *a, uint32_t aSize, const BN_UINT *b, uint32_t bSize);
146
147uint32_t BinBits(const BN_UINT *data, uint32_t size);
148
149uint32_t BinDiv(BN_UINT *q, uint32_t *qSize, BN_UINT *x, uint32_t xSize, BN_UINT *y, uint32_t ySize);
150
151#ifdef HITLS_CRYPTO_BN_COMBA
152uint32_t SpaceSize(uint32_t size);
153
154// Perform a multiplication calculation of 4 blocks of data, r = a^2,
155// where the length of r is 8, and the length of a is 4.
156void MulComba4(BN_UINT *r, const BN_UINT *a, const BN_UINT *b);
157
158// Calculate the square of 4 blocks of data, r = a^2, where the length of r is 8, and the length of a is 4.
159void SqrComba4(BN_UINT *r, const BN_UINT *a);
160
161// Perform a multiplication calculation of 6 blocks of data, r = a*b,
162// where the length of r is 12, the length of a and b is 6.
163void MulComba6(BN_UINT *r, const BN_UINT *a, const BN_UINT *b);
164
165// Calculate the square of 6 blocks of data, r = a^2, where the length of r is 12, and the length of a is 6.
166void SqrComba6(BN_UINT *r, const BN_UINT *a);
167
168void MulConquer(BN_UINT *r, const BN_UINT *a, const BN_UINT *b, uint32_t size, BN_UINT *space, bool consttime);
169
170void SqrConquer(BN_UINT *r, const BN_UINT *a, uint32_t size, BN_UINT *space, bool consttime);
171#endif
172
173int32_t MontSqrBinCore(BN_UINT *r, BN_Mont *mont, BN_Optimizer *opt, bool consttime);
174
175int32_t MontMulBinCore(BN_UINT *r, const BN_UINT *a, const BN_UINT *b, BN_Mont *mont,
176 BN_Optimizer *opt, bool consttime);
177
178int32_t MontEncBinCore(BN_UINT *r, BN_Mont *mont, BN_Optimizer *opt, bool consttime);
179
180void ReduceCore(BN_UINT *r, BN_UINT *x, const BN_UINT *m, uint32_t mSize, BN_UINT m0);
181
182#ifdef __cplusplus
183}
184#endif
185
186#endif /* HITLS_CRYPTO_BN */
187
188#endif