1 #include "tommath_private.h"
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 /* SPDX-License-Identifier: Unlicense */
7 int mp_get_bit(const mp_int *a, int b)
12 return (s_mp_get_bit(a, (unsigned int)b) == MP_YES) ? MP_YES : MP_NO;
16 mp_err mp_jacobi(const mp_int *a, const mp_int *n, int *c)
18 if (a->sign == MP_NEG) {
21 if (mp_cmp_d(n, 0uL) != MP_GT) {
24 return mp_kronecker(a, n, c);
27 #ifdef BN_MP_PRIME_RANDOM_EX_C
28 mp_err mp_prime_random_ex(mp_int *a, int t, int size, int flags, private_mp_prime_callback cb, void *dat)
30 return s_mp_prime_random_ex(a, t, size, flags, cb, dat);
33 #ifdef BN_MP_RAND_DIGIT_C
34 mp_err mp_rand_digit(mp_digit *r)
36 mp_err err = s_mp_rand_source(r, sizeof(mp_digit));
41 #ifdef BN_FAST_MP_INVMOD_C
42 mp_err fast_mp_invmod(const mp_int *a, const mp_int *b, mp_int *c)
44 return s_mp_invmod_fast(a, b, c);
47 #ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C
48 mp_err fast_mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho)
50 return s_mp_montgomery_reduce_fast(x, n, rho);
53 #ifdef BN_FAST_S_MP_MUL_DIGS_C
54 mp_err fast_s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
56 return s_mp_mul_digs_fast(a, b, c, digs);
59 #ifdef BN_FAST_S_MP_MUL_HIGH_DIGS_C
60 mp_err fast_s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
62 return s_mp_mul_high_digs_fast(a, b, c, digs);
65 #ifdef BN_FAST_S_MP_SQR_C
66 mp_err fast_s_mp_sqr(const mp_int *a, mp_int *b)
68 return s_mp_sqr_fast(a, b);
71 #ifdef BN_MP_BALANCE_MUL_C
72 mp_err mp_balance_mul(const mp_int *a, const mp_int *b, mp_int *c)
74 return s_mp_balance_mul(a, b, c);
77 #ifdef BN_MP_EXPTMOD_FAST_C
78 mp_err mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode)
80 return s_mp_exptmod_fast(G, X, P, Y, redmode);
83 #ifdef BN_MP_INVMOD_SLOW_C
84 mp_err mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c)
86 return s_mp_invmod_slow(a, b, c);
89 #ifdef BN_MP_KARATSUBA_MUL_C
90 mp_err mp_karatsuba_mul(const mp_int *a, const mp_int *b, mp_int *c)
92 return s_mp_karatsuba_mul(a, b, c);
95 #ifdef BN_MP_KARATSUBA_SQR_C
96 mp_err mp_karatsuba_sqr(const mp_int *a, mp_int *b)
98 return s_mp_karatsuba_sqr(a, b);
101 #ifdef BN_MP_TOOM_MUL_C
102 mp_err mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c)
104 return s_mp_toom_mul(a, b, c);
107 #ifdef BN_MP_TOOM_SQR_C
108 mp_err mp_toom_sqr(const mp_int *a, mp_int *b)
110 return s_mp_toom_sqr(a, b);
113 #ifdef S_MP_REVERSE_C
114 void bn_reverse(unsigned char *s, int len)
117 s_mp_reverse(s, (size_t)len);
121 #ifdef BN_MP_TC_AND_C
122 mp_err mp_tc_and(const mp_int *a, const mp_int *b, mp_int *c)
124 return mp_and(a, b, c);
128 mp_err mp_tc_or(const mp_int *a, const mp_int *b, mp_int *c)
130 return mp_or(a, b, c);
133 #ifdef BN_MP_TC_XOR_C
134 mp_err mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c)
136 return mp_xor(a, b, c);
139 #ifdef BN_MP_TC_DIV_2D_C
140 mp_err mp_tc_div_2d(const mp_int *a, int b, mp_int *c)
142 return mp_signed_rsh(a, b, c);
145 #ifdef BN_MP_INIT_SET_INT_C
146 mp_err mp_init_set_int(mp_int *a, unsigned long b)
148 return mp_init_u32(a, (uint32_t)b);
151 #ifdef BN_MP_SET_INT_C
152 mp_err mp_set_int(mp_int *a, unsigned long b)
154 mp_set_u32(a, (uint32_t)b);
158 #ifdef BN_MP_SET_LONG_C
159 mp_err mp_set_long(mp_int *a, unsigned long b)
165 #ifdef BN_MP_SET_LONG_LONG_C
166 mp_err mp_set_long_long(mp_int *a, unsigned long long b)
172 #ifdef BN_MP_GET_INT_C
173 unsigned long mp_get_int(const mp_int *a)
175 return (unsigned long)mp_get_mag_u32(a);
178 #ifdef BN_MP_GET_LONG_C
179 unsigned long mp_get_long(const mp_int *a)
181 return (unsigned long)mp_get_mag_ul(a);
184 #ifdef BN_MP_GET_LONG_LONG_C
185 unsigned long long mp_get_long_long(const mp_int *a)
187 return mp_get_mag_ull(a);
190 #ifdef BN_MP_PRIME_IS_DIVISIBLE_C
191 mp_err mp_prime_is_divisible(const mp_int *a, mp_bool *result)
193 return s_mp_prime_is_divisible(a, result);
196 #ifdef BN_MP_EXPT_D_EX_C
197 mp_err mp_expt_d_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
200 if (b > MP_MIN(MP_DIGIT_MAX, UINT32_MAX)) {
203 return mp_expt_u32(a, (uint32_t)b, c);
206 #ifdef BN_MP_EXPT_D_C
207 mp_err mp_expt_d(const mp_int *a, mp_digit b, mp_int *c)
209 if (b > MP_MIN(MP_DIGIT_MAX, UINT32_MAX)) {
212 return mp_expt_u32(a, (uint32_t)b, c);
215 #ifdef BN_MP_N_ROOT_EX_C
216 mp_err mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
219 if (b > MP_MIN(MP_DIGIT_MAX, UINT32_MAX)) {
222 return mp_root_u32(a, (uint32_t)b, c);
225 #ifdef BN_MP_N_ROOT_C
226 mp_err mp_n_root(const mp_int *a, mp_digit b, mp_int *c)
228 if (b > MP_MIN(MP_DIGIT_MAX, UINT32_MAX)) {
231 return mp_root_u32(a, (uint32_t)b, c);
234 #ifdef BN_MP_UNSIGNED_BIN_SIZE_C
235 int mp_unsigned_bin_size(const mp_int *a)
237 return (int)mp_ubin_size(a);
240 #ifdef BN_MP_READ_UNSIGNED_BIN_C
241 mp_err mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c)
243 return mp_from_ubin(a, b, (size_t) c);
246 #ifdef BN_MP_TO_UNSIGNED_BIN_C
247 mp_err mp_to_unsigned_bin(const mp_int *a, unsigned char *b)
249 return mp_to_ubin(a, b, SIZE_MAX, NULL);
252 #ifdef BN_MP_TO_UNSIGNED_BIN_N_C
253 mp_err mp_to_unsigned_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen)
255 size_t n = mp_ubin_size(a);
256 if (*outlen < (unsigned long)n) {
259 *outlen = (unsigned long)n;
260 return mp_to_ubin(a, b, n, NULL);
263 #ifdef BN_MP_SIGNED_BIN_SIZE_C
264 int mp_signed_bin_size(const mp_int *a)
266 return (int)mp_sbin_size(a);
269 #ifdef BN_MP_READ_SIGNED_BIN_C
270 mp_err mp_read_signed_bin(mp_int *a, const unsigned char *b, int c)
272 return mp_from_sbin(a, b, (size_t) c);
275 #ifdef BN_MP_TO_SIGNED_BIN_C
276 mp_err mp_to_signed_bin(const mp_int *a, unsigned char *b)
278 return mp_to_sbin(a, b, SIZE_MAX, NULL);
281 #ifdef BN_MP_TO_SIGNED_BIN_N_C
282 mp_err mp_to_signed_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen)
284 size_t n = mp_sbin_size(a);
285 if (*outlen < (unsigned long)n) {
288 *outlen = (unsigned long)n;
289 return mp_to_sbin(a, b, n, NULL);
292 #ifdef BN_MP_TORADIX_N_C
293 mp_err mp_toradix_n(const mp_int *a, char *str, int radix, int maxlen)
298 return mp_to_radix(a, str, (size_t)maxlen, NULL, radix);
301 #ifdef BN_MP_TORADIX_C
302 mp_err mp_toradix(const mp_int *a, char *str, int radix)
304 return mp_to_radix(a, str, SIZE_MAX, NULL, radix);
307 #ifdef BN_MP_IMPORT_C
308 mp_err mp_import(mp_int *rop, size_t count, int order, size_t size, int endian, size_t nails,
311 return mp_unpack(rop, count, order, size, endian, nails, op);
314 #ifdef BN_MP_EXPORT_C
315 mp_err mp_export(void *rop, size_t *countp, int order, size_t size,
316 int endian, size_t nails, const mp_int *op)
318 return mp_pack(rop, SIZE_MAX, countp, order, size, endian, nails, op);