API参考
载入中...
搜索中...
未找到
bn_bincal_x8664.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#ifndef BN_BINCAL_X8664_H
16#define BN_BINCAL_X8664_H
17
18#include "hitls_build.h"
19#ifdef HITLS_CRYPTO_BN
20
21#include <stdint.h>
22#include "bn_basic.h"
23
24#ifdef __cplusplus
25extern "c" {
26#endif
27
28// wh | wl = u * v
29#define MUL_AB(wh, wl, u, v) \
30 { \
31 __asm("mulq %3" : "=d"(wh), "=a"(wl) : "a"(u), "r"(v) : "cc"); \
32 }
33// wh | wl = u ^ 2
34#define SQR_A(wh, wl, u) \
35 { \
36 __asm("mulq %2 " : "=d"(wh), "=a"(wl) : "a"(u) : "cc"); \
37 }
38
39// nh | nl / d = q...r
40#define DIV_ND(q, r, nh, nl, d) \
41 { \
42 __asm("divq %4" : "=a"(q), "=d"(r) : "d"(nh), "a"(nl), "r"(d) : "cc"); \
43 }
44
45/* r += c
46 * c = carry
47 */
48#define ADD_CARRY(carry, r) \
49 do { \
50 __asm("addq %1, %0 \n\t " \
51 "adcq %4, %1 \n\t " \
52 "adcq $0, %2 \n\t " \
53 : "+m"(l), "+r"(carry) \
54 : \
55 : "cc"); \
56 } while (0)
57
58/* h|m|l = h|m|l + u * v. Ensure that the value of h is not too large to avoid carry. */
59#define MULXADD_AB(h, m, l, u, v) \
60 do { \
61 BN_UINT hi, lo; \
62 __asm("mulq %0, %1, %2" : "=a"(lo), "=d"(hi) : "a"(u), "m"(v) : "cc"); \
63 __asm("addq %3, %0 \n\t " \
64 "adcq %4, %1 \n\t " \
65 "adcq $0, %2 \n\t " \
66 : "+r"(l), "+r"(m), "+r"(h) \
67 : "r"(lo), "r"(hi) \
68 : "cc"); \
69 } while (0)
70
71// (hi, lo) = a * b
72// r += lo + carry
73// carry = hi + c
74#define MULADC_AB(r, a, b, carry) \
75 do { \
76 BN_UINT hi, lo; \
77 __asm("mulq %3" : "=a"(lo), "=d"(hi) : "a"(a), "g"(b) : "cc"); \
78 __asm("addq %3, %1 \n\t" \
79 "adcq $0, %2 \n\t" \
80 "addq %1, %0 \n\t" \
81 "adcq $0, %2 \n\t" \
82 "movq %2, %1 \n\t" \
83 : "+r"(r), "+r"(carry), "+r"(hi) \
84 : "r"(lo) \
85 : "cc"); \
86 } while (0)
87
88/* h|m|l = h|m|l + u * v. Ensure that the value of h is not too large to avoid carry. */
89#define MULADD_AB(h, m, l, u, v) \
90 do { \
91 BN_UINT hi, lo; \
92 __asm("mulq %3" : "=a"(lo), "=d"(hi) : "a"(u), "m"(v) : "cc"); \
93 __asm("addq %3, %0 \n\t " \
94 "adcq %4, %1 \n\t " \
95 "adcq $0, %2 \n\t " \
96 : "+r"(l), "+r"(m), "+r"(h) \
97 : "r"(lo), "r"(hi) \
98 : "cc"); \
99 } while (0)
100
101/* h|m|l = h|m|l + 2 * u * v. Ensure that the value of h is not too large to avoid carry. */
102#define MULADD_AB2(h, m, l, u, v) \
103 do { \
104 BN_UINT hi, lo; \
105 __asm("mulq %3" : "=a"(lo), "=d"(hi) : "a"(u), "m"(v) : "cc"); \
106 __asm("addq %3, %0 \n\t " \
107 "adcq %4, %1 \n\t " \
108 "adcq $0, %2 \n\t " \
109 "addq %3, %0 \n\t " \
110 "adcq %4, %1 \n\t " \
111 "adcq $0, %2 \n\t " \
112 : "+r"(l), "+r"(m), "+r"(h) \
113 : "r"(lo), "r"(hi) \
114 : "cc"); \
115 } while (0)
116
117/* h|m|l = h|m|l + u * u. Ensure that the value of h is not too large to avoid carry. */
118#define SQRADD_A(h, m, l, u) MULADD_AB(h, m, l, u, u)
119
120// Modular operation, satisfy d < (1 << BN_UINT_HALF_BITS) r = nh | nl % d
121#define MOD_HALF(r, nh, nl, d) \
122 do { \
123 BN_UINT tmp = 0; \
124 __asm("divq %4" : "=a"(tmp), "=d"(r) : "d"(nh), "a"(nl), "r"(d) : "cc"); \
125 } while (0)
126
127#ifdef __cplusplus
128}
129#endif
130
131#endif /* HITLS_CRYPTO_BN */
132
133#endif