API参考
载入中...
搜索中...
未找到
sm9_bn.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 __HEADER_BN_H__
17#define __HEADER_BN_H__
18
19#include "hitls_build.h"
20#ifdef HITLS_CRYPTO_SM9
21
22#include <stdint.h>
23
24// macro for common bn
25#define WordLen 32
26#define ByteLen 8
27#define WordByteLen (WordLen/ByteLen)
28#define LSBOfWord 0x00000001
29#define MSBOfWord 0x80000000
30
31// macro for BN in SM9
32#define BNBitLen 256
33#define BNByteLen (BNBitLen/ByteLen)
34#define BNWordLen (BNBitLen/WordLen)
35
36#define WORDBITS 32
37#define WORDBYTES (WORDBITS/8)
38#define BN_MAX_WORDSIZE 16
39
40#define BN_MSB(x, w) (((x)[w] >> (WORDBITS - 1)) & 1)
41#define BN_LSB(x, w) ((x)[0] & 1)
42#define BN_BIT(x, i) (((x)[(i) / WORDBITS] >> ((i) % WORDBITS)) & 1)
43
44#ifdef __cplusplus
45extern "C" {
46#endif
47
48/*============================Part_1: Basic Functions=========================*/
49
50// x <= 0
51void bn_reset(uint32_t *x, int32_t wsize);
52
53// x <= n
54void bn_set_int(uint32_t *x, int32_t n, int32_t wsize);
55
56// y <= x
57void bn_assign(uint32_t *y, const uint32_t *x, int32_t wsize);
58
59int32_t bn_get_bitlen(const uint32_t *x, int32_t wsize);
60
61int32_t bn_get_wordlen(const uint32_t *x, int32_t wsize);
62
63/*================== Section: Comparison Operations ======================
64@Brief
65==============================================================================*/
66
67int32_t bn_equal(const uint32_t *x, const uint32_t *y, int32_t wsize);
68
69// Big number compare function 1(x > y) 0(x = y) -1(x < y)
70int32_t bn_cmp(const uint32_t *x, const uint32_t *y, int32_t wsize);
71
72// if x equal 0 return 1, else return 0
73int32_t bn_is_zero(uint32_t *x, int32_t wsize);
74
75int32_t bn_is_nonzero(uint32_t *x, int32_t wsize);
76
77/*==============================================================================
78@Section Logical Operations
79@Brief Logical operations are operations that can be performed either with
80 simple shifts or boolean operators such as AND, XOR and OR directly.
81==============================================================================*/
82
83// y = x / 2 or y = x >> 1
84int32_t bn_div_2(uint32_t *y, const uint32_t *x, int32_t wsize);
85
86// Addition: r = x + y
87uint32_t bn_add(uint32_t *r, const uint32_t *x, const uint32_t *y, int32_t wsize);
88
89// Subtraction: r = x - y
90uint32_t bn_sub(uint32_t *r, const uint32_t *x, const uint32_t *y, int32_t wsize);
91
92// r = x + n
93uint32_t bn_add_int(uint32_t *r, const uint32_t *x, uint32_t n, int32_t wsize);
94
95// r = x - n
96uint32_t bn_sub_int(uint32_t *r, const uint32_t *x, uint32_t n, int32_t wsize);
97
98/*============================Part_2: Mod Functions============================*/
99
100// r = x + y mod m
101void bn_mod_add(uint32_t *r, const uint32_t *x, const uint32_t *y, const uint32_t *m, int32_t wsize);
102
103// r = x - y mod m
104void bn_mod_sub(uint32_t *r, const uint32_t *x, const uint32_t *y, const uint32_t *m, int32_t wsize);
105
106// r = - y mod m
107void bn_mod_neg(uint32_t *r, const uint32_t *x, const uint32_t *m, int32_t wsize);
108
109// r = y ^ -1 mod m
110void bn_mod_inv(uint32_t *r, uint32_t *x, uint32_t *m, int32_t wsize);
111
112// r = x >> 1 mod m
113void bn_mod_div_2(uint32_t *r, const uint32_t *x, const uint32_t *m, int32_t wsize);
114
115// x = x mod m
116void bn_get_res(uint32_t *x, const uint32_t *m, int32_t wsize);
117
118/*==================____Section: Montgomery Reduction____========================
119@Brief Montgomery is a specialized reduction algorithm for any odd moduli.
120----Before using montgomery reduction, integers should be normalized by multiplying
121----it by R, where the pre-computed value R = b ^ n, n is the n number of digits in m
122----and b is radix used (default is 2^32).
123==============================================================================*/
124
125/* Montgomery multiplication: r = x * y * R^-1 mod m (HAC 14.36) */
126void bn_mont_mul(uint32_t *r, const uint32_t *x, const uint32_t *y, const uint32_t *m, uint32_t mc, int32_t wsize);
127
128/* Montgomery reduction: r = x^2 * R^-1 mod m */
129void bn_mont_redc(uint32_t *r, const uint32_t *x, const uint32_t *m, uint32_t mc, int32_t wsize);
130/*============================================================================*/
131
132void BN_GetInv_Mont(uint32_t *r, uint32_t *x, uint32_t *m, uint32_t wModuleConst, uint32_t *pwRRModule, int32_t wsize);
133
134int32_t BN_Mod_Basic(uint32_t *rem, int32_t iBNWordLen_r, uint32_t *pwBNX,
135 int32_t iBNWordLen_X, uint32_t *pwBNM, int32_t iBNWordLen_M);
136
137int32_t ByteToBN(const uint8_t *pByteBuf, int32_t bytelen, uint32_t *pwBN, int32_t wsize);
138int32_t BNToByte(uint32_t *pwBN, int32_t wsize, uint8_t *pByteBuf, int32_t *bytelen);
139
140#ifdef __cplusplus
141}
142#endif
143
144#endif // HITLS_CRYPTO_SM9
145
146#endif /* __HEADER_BN_H__ */
147