• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Winner Microelectronics Co., Ltd. All rights reserved.
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  */
15 
16 #ifndef __LIBTOMMATH_H__
17 #define __LIBTOMMATH_H__
18 
19 typedef signed short int16;
20 
21 #ifndef CHAR_BIT
22 #define CHAR_BIT 8
23 #endif
24 
25 #ifndef NULL
26 #define NULL ((void *)0)
27 #endif
28 
29 #define BN_MP_INVMOD_C
30 #define BN_S_MP_EXPTMOD_C /* Note: #undef in tommath_superclass.h; this would
31                * require BN_MP_EXPTMOD_FAST_C instead */
32 #define BN_S_MP_MUL_DIGS_C
33 #define BN_MP_INVMOD_SLOW_C
34 #define BN_S_MP_SQR_C
35 #define BN_S_MP_MUL_HIGH_DIGS_C /* Note: #undef in tommath_superclass.h; this
36                  * would require other than mp_reduce */
37 
38 #ifdef LTM_FAST
39 
40 /* Use faster div at the cost of about 1 kB */
41 #define BN_MP_MUL_D_C
42 
43 /* Include faster exptmod (Montgomery) at the cost of about 2.5 kB in code */
44 #define BN_MP_EXPTMOD_FAST_C
45 #define BN_MP_MONTGOMERY_SETUP_C
46 #define BN_FAST_MP_MONTGOMERY_REDUCE_C
47 #define BN_MP_MONTGOMERY_CALC_NORMALIZATION_C
48 #define BN_MP_MUL_2_C
49 
50 /* Include faster sqr at the cost of about 0.5 kB in code */
51 #define BN_FAST_S_MP_SQR_C
52 
53 #else /* LTM_FAST */
54 
55 #define BN_MP_DIV_SMALL
56 #define BN_MP_INIT_MULTI_C
57 #define BN_MP_CLEAR_MULTI_C
58 #define BN_MP_ABS_C
59 #endif /* LTM_FAST */
60 
61 /* Current uses do not require support for negative exponent in exptmod, so we
62  * can save about 1.5 kB in leaving out invmod. */
63 #define LTM_NO_NEG_EXP
64 
65 /* from tommath.h */
66 
67 #ifndef MIN
68 #define MIN(x, y) ((x)<(y)?(x):(y))
69 #endif
70 
71 #ifndef MAX
72 #define MAX(x, y) ((x)>(y)?(x):(y))
73 #endif
74 
75 #define  OPT_CAST(x)
76 
77 typedef unsigned int     mp_digit;
78 typedef unsigned long long mp_word;
79 
80 #define XMALLOC  tls_mem_alloc
81 #define XFREE    tls_mem_free
82 #define XREALLOC os_realloc
83 
84 #define MP_MASK          ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1))
85 
86 #define MP_LT        (-1)   /* less than */
87 #define MP_EQ         0   /* equal to */
88 #define MP_GT         1   /* greater than */
89 
90 #define MP_ZPOS       0   /* positive integer */
91 #define MP_NEG        1   /* negative */
92 
93 #define MP_OKAY       0   /* ok result */
94 #define MP_MEM        (-2)  /* out of mem */
95 #define MP_VAL        (-3)  /* invalid input */
96 
97 #define MP_YES        1   /* yes response */
98 #define MP_NO         0   /* no response */
99 
100 typedef int           mp_err;
101 
102 /* define this to use lower memory usage routines (exptmods mostly) */
103 #define MP_LOW_MEM
104 
105 /* default precision */
106 #ifndef MP_PREC
107 
108 #ifndef MP_LOW_MEM
109 #define MP_PREC                 32     /* default digits of precision */
110 #else
111 #define MP_PREC                 8      /* default digits of precision */
112 #endif
113 
114 #endif
115 
116 /* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */
117 #define MP_WARRAY               (1 << (sizeof(mp_word) *CHAR_BIT - 2 *DIGIT_BIT + 1))
118 
119 /* the infamous mp_int structure */
120 typedef struct  {
121     int16 used, alloc, sign;
122     mp_digit *dp;
123 } mp_int;
124 
125 /* ---> Basic Manipulations <--- */
126 #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)
127 #define mp_iseven(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? MP_YES : MP_NO)
128 #define mp_isodd(a)  (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? MP_YES : MP_NO)
129 
130  void mp_reverse (unsigned char *s, int len);
131 #ifdef BN_MP_INIT_MULTI_C
132  int mp_init_multi(mp_int *mp, ...);
133 #endif
134 #ifdef BN_MP_CLEAR_MULTI_C
135  void mp_clear_multi(mp_int *mp, ...);
136 #endif
137  int mp_lshd(mp_int *a, int b);
138  void mp_set(mp_int *a, mp_digit b);
139  void mp_clamp(mp_int *a);
140  void mp_exch(mp_int *a, mp_int *b);
141  void mp_rshd(mp_int *a, int b);
142  void mp_zero(mp_int *a);
143  int mp_mod_2d(mp_int *a, int b, mp_int *c);
144  int mp_div_2d(mp_int *a, int b, mp_int *c, mp_int *d);
145  int mp_init_copy(mp_int *a, mp_int *b);
146  int mp_mul_2d(mp_int *a, int b, mp_int *c);
147 #ifndef LTM_NO_NEG_EXP
148  int mp_div_2(mp_int *a, mp_int *b);
149  int mp_invmod(mp_int *a, mp_int *b, mp_int *c);
150  int mp_invmod_slow(mp_int *a, mp_int *b, mp_int *c);
151 #endif /* LTM_NO_NEG_EXP */
152  int mp_copy(mp_int *a, mp_int *b);
153  int mp_count_bits(mp_int *a);
154  int mp_div(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
155  int mp_mod(mp_int *a, mp_int *b, mp_int *c);
156  int mp_grow(mp_int *a, int size);
157  int mp_cmp_mag(mp_int *a, mp_int *b);
158 #ifdef BN_MP_ABS_C
159  int mp_abs(mp_int *a, mp_int *b);
160 #endif
161  int mp_sqr(mp_int *a, mp_int *b);
162  int mp_reduce_2k_l(mp_int *a, mp_int *n, mp_int *d);
163  int mp_reduce_2k_setup_l(mp_int *a, mp_int *d);
164  int mp_2expt(mp_int *a, int b);
165  int mp_reduce_setup(mp_int *a, mp_int *b);
166  int mp_reduce(mp_int *x, mp_int *m, mp_int *mu);
167  int mp_init_size(mp_int *a, int size);
168 #ifdef BN_MP_EXPTMOD_FAST_C
169  int mp_exptmod_fast (mp_int *G, mp_int *X, mp_int *P, mp_int *Y, int redmode);
170 #endif /* BN_MP_EXPTMOD_FAST_C */
171 #ifdef BN_FAST_S_MP_SQR_C
172  int fast_s_mp_sqr (mp_int *a, mp_int *b);
173 #endif /* BN_FAST_S_MP_SQR_C */
174 #ifdef BN_MP_MUL_D_C
175  int mp_mul_d (mp_int *a, mp_digit b, mp_int *c);
176 #endif /* BN_MP_MUL_D_C */
177 #ifdef BN_MP_MUL_2_C
178 /* b = a*2 */
179 int mp_mul_2(mp_int *a, mp_int *b);
180 #endif
181  int mp_init_for_read_unsigned_bin(mp_int *a, mp_digit len);
182  void mp_clear (mp_int *a);
183 int mp_exptmod (mp_int *G, mp_int *X, mp_int *P, mp_int *Y);
184 int mp_init (mp_int *a);
185 int mp_read_unsigned_bin (mp_int *a, const unsigned char *b, int c);
186 int mp_to_unsigned_bin_nr (mp_int *a, unsigned char *b);
187 int mp_to_unsigned_bin (mp_int *a, unsigned char *b);
188 int mp_unsigned_bin_size (mp_int *a);
189 int mp_add (mp_int *a, mp_int *b, mp_int *c);
190 int mp_cmp (mp_int *a, mp_int *b);
191 int mp_sub (mp_int *a, mp_int *b, mp_int *c);
192 int mp_mulmod (mp_int *a, mp_int *b, mp_int *c, mp_int *d);
193 int mp_cmp_d(mp_int *a, mp_digit b);
194 
195 #ifdef BN_MP_MONTGOMERY_SETUP_C
196 int mp_montgomery_setup (mp_int *n, mp_digit *rho);
197 #endif
198 #ifdef BN_MP_MONTGOMERY_CALC_NORMALIZATION_C
199 int mp_montgomery_calc_normalization (mp_int *a, mp_int *b);
200 #endif
201 #ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C
202 int fast_mp_montgomery_reduce (mp_int *x, mp_int *n, mp_digit rho);
203 #endif
204 
205 #endif // __LIBTOMMATH_H__
206 
207