comparison libtommath/bn_mp_sqr.c @ 1739:13d834efc376 fuzz

merge from main
author Matt Johnston <matt@ucc.asn.au>
date Thu, 15 Oct 2020 19:55:15 +0800
parents 1051e4eea25a
children
comparison
equal deleted inserted replaced
1562:768ebf737aa0 1739:13d834efc376
1 #include <tommath_private.h> 1 #include "tommath_private.h"
2 #ifdef BN_MP_SQR_C 2 #ifdef BN_MP_SQR_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis 3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 * 4 /* SPDX-License-Identifier: Unlicense */
5 * LibTomMath is a library that provides multiple-precision
6 * integer arithmetic as well as number theoretic functionality.
7 *
8 * The library was designed directly after the MPI library by
9 * Michael Fromberger but has been written from scratch with
10 * additional optimizations in place.
11 *
12 * The library is free for all purposes without any express
13 * guarantee it works.
14 *
15 * Tom St Denis, [email protected], http://libtom.org
16 */
17 5
18 /* computes b = a*a */ 6 /* computes b = a*a */
19 int 7 mp_err mp_sqr(const mp_int *a, mp_int *b)
20 mp_sqr (mp_int * a, mp_int * b)
21 { 8 {
22 int res; 9 mp_err err;
23 10 if (MP_HAS(S_MP_TOOM_SQR) && /* use Toom-Cook? */
24 #ifdef BN_MP_TOOM_SQR_C 11 (a->used >= MP_TOOM_SQR_CUTOFF)) {
25 /* use Toom-Cook? */ 12 err = s_mp_toom_sqr(a, b);
26 if (a->used >= TOOM_SQR_CUTOFF) { 13 } else if (MP_HAS(S_MP_KARATSUBA_SQR) && /* Karatsuba? */
27 res = mp_toom_sqr(a, b); 14 (a->used >= MP_KARATSUBA_SQR_CUTOFF)) {
28 /* Karatsuba? */ 15 err = s_mp_karatsuba_sqr(a, b);
29 } else 16 } else if (MP_HAS(S_MP_SQR_FAST) && /* can we use the fast comba multiplier? */
30 #endif 17 (((a->used * 2) + 1) < MP_WARRAY) &&
31 #ifdef BN_MP_KARATSUBA_SQR_C 18 (a->used < (MP_MAXFAST / 2))) {
32 if (a->used >= KARATSUBA_SQR_CUTOFF) { 19 err = s_mp_sqr_fast(a, b);
33 res = mp_karatsuba_sqr (a, b); 20 } else if (MP_HAS(S_MP_SQR)) {
34 } else 21 err = s_mp_sqr(a, b);
35 #endif 22 } else {
36 { 23 err = MP_VAL;
37 #ifdef BN_FAST_S_MP_SQR_C 24 }
38 /* can we use the fast comba multiplier? */ 25 b->sign = MP_ZPOS;
39 if ((((a->used * 2) + 1) < MP_WARRAY) && 26 return err;
40 (a->used <
41 (1 << (((sizeof(mp_word) * CHAR_BIT) - (2 * DIGIT_BIT)) - 1)))) {
42 res = fast_s_mp_sqr (a, b);
43 } else
44 #endif
45 {
46 #ifdef BN_S_MP_SQR_C
47 res = s_mp_sqr (a, b);
48 #else
49 res = MP_VAL;
50 #endif
51 }
52 }
53 b->sign = MP_ZPOS;
54 return res;
55 } 27 }
56 #endif 28 #endif
57
58 /* ref: $Format:%D$ */
59 /* git commit: $Format:%H$ */
60 /* commit time: $Format:%ai$ */