comparison libtommath/bn_mp_karatsuba_sqr.c @ 1655:f52919ffd3b1

update ltm to 1.1.0 and enable FIPS 186.4 compliant key-generation (#79) * make key-generation compliant to FIPS 186.4 * fix includes in tommath_class.h * update fuzzcorpus instead of error-out * fixup fuzzing make-targets * update Makefile.in * apply necessary patches to ltm sources * clean-up not required ltm files * update to vanilla ltm 1.1.0 this already only contains the required files * remove set/get double
author Steffen Jaeckel <s_jaeckel@gmx.de>
date Mon, 16 Sep 2019 15:50:38 +0200
parents 8bba51a55704
children
comparison
equal deleted inserted replaced
1654:cc0fc5131c5c 1655:f52919ffd3b1
1 #include <tommath_private.h> 1 #include "tommath_private.h"
2 #ifdef BN_MP_KARATSUBA_SQR_C 2 #ifdef BN_MP_KARATSUBA_SQR_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis 3 /* LibTomMath, multiple-precision integer library -- Tom St Denis
4 * 4 *
5 * LibTomMath is a library that provides multiple-precision 5 * LibTomMath is a library that provides multiple-precision
6 * integer arithmetic as well as number theoretic functionality. 6 * integer arithmetic as well as number theoretic functionality.
7 * 7 *
8 * The library was designed directly after the MPI library by 8 * The library was designed directly after the MPI library by
9 * Michael Fromberger but has been written from scratch with 9 * Michael Fromberger but has been written from scratch with
10 * additional optimizations in place. 10 * additional optimizations in place.
11 * 11 *
12 * The library is free for all purposes without any express 12 * SPDX-License-Identifier: Unlicense
13 * guarantee it works.
14 *
15 * Tom St Denis, [email protected], http://libtom.org
16 */ 13 */
17 14
18 /* Karatsuba squaring, computes b = a*a using three 15 /* Karatsuba squaring, computes b = a*a using three
19 * half size squarings 16 * half size squarings
20 * 17 *
21 * See comments of karatsuba_mul for details. It 18 * See comments of karatsuba_mul for details. It
22 * is essentially the same algorithm but merely 19 * is essentially the same algorithm but merely
23 * tuned to perform recursive squarings. 20 * tuned to perform recursive squarings.
24 */ 21 */
25 int mp_karatsuba_sqr (mp_int * a, mp_int * b) 22 int mp_karatsuba_sqr(const mp_int *a, mp_int *b)
26 { 23 {
27 mp_int x0, x1, t1, t2, x0x0, x1x1; 24 mp_int x0, x1, t1, t2, x0x0, x1x1;
28 int B, err; 25 int B, err;
29 26
30 err = MP_MEM; 27 err = MP_MEM;
31 28
32 /* min # of digits */ 29 /* min # of digits */
33 B = a->used; 30 B = a->used;
34 31
35 /* now divide in two */ 32 /* now divide in two */
36 B = B >> 1; 33 B = B >> 1;
37 34
38 /* init copy all the temps */ 35 /* init copy all the temps */
39 if (mp_init_size (&x0, B) != MP_OKAY) 36 if (mp_init_size(&x0, B) != MP_OKAY)
40 goto ERR; 37 goto LBL_ERR;
41 if (mp_init_size (&x1, a->used - B) != MP_OKAY) 38 if (mp_init_size(&x1, a->used - B) != MP_OKAY)
42 goto X0; 39 goto X0;
43 40
44 /* init temps */ 41 /* init temps */
45 if (mp_init_size (&t1, a->used * 2) != MP_OKAY) 42 if (mp_init_size(&t1, a->used * 2) != MP_OKAY)
46 goto X1; 43 goto X1;
47 if (mp_init_size (&t2, a->used * 2) != MP_OKAY) 44 if (mp_init_size(&t2, a->used * 2) != MP_OKAY)
48 goto T1; 45 goto T1;
49 if (mp_init_size (&x0x0, B * 2) != MP_OKAY) 46 if (mp_init_size(&x0x0, B * 2) != MP_OKAY)
50 goto T2; 47 goto T2;
51 if (mp_init_size (&x1x1, (a->used - B) * 2) != MP_OKAY) 48 if (mp_init_size(&x1x1, (a->used - B) * 2) != MP_OKAY)
52 goto X0X0; 49 goto X0X0;
53 50
54 { 51 {
55 int x; 52 int x;
56 mp_digit *dst, *src; 53 mp_digit *dst, *src;
57 54
58 src = a->dp; 55 src = a->dp;
59 56
60 /* now shift the digits */ 57 /* now shift the digits */
61 dst = x0.dp; 58 dst = x0.dp;
62 for (x = 0; x < B; x++) { 59 for (x = 0; x < B; x++) {
63 *dst++ = *src++; 60 *dst++ = *src++;
64 } 61 }
65 62
66 dst = x1.dp; 63 dst = x1.dp;
67 for (x = B; x < a->used; x++) { 64 for (x = B; x < a->used; x++) {
68 *dst++ = *src++; 65 *dst++ = *src++;
69 } 66 }
70 } 67 }
71 68
72 x0.used = B; 69 x0.used = B;
73 x1.used = a->used - B; 70 x1.used = a->used - B;
74 71
75 mp_clamp (&x0); 72 mp_clamp(&x0);
76 73
77 /* now calc the products x0*x0 and x1*x1 */ 74 /* now calc the products x0*x0 and x1*x1 */
78 if (mp_sqr (&x0, &x0x0) != MP_OKAY) 75 if (mp_sqr(&x0, &x0x0) != MP_OKAY)
79 goto X1X1; /* x0x0 = x0*x0 */ 76 goto X1X1; /* x0x0 = x0*x0 */
80 if (mp_sqr (&x1, &x1x1) != MP_OKAY) 77 if (mp_sqr(&x1, &x1x1) != MP_OKAY)
81 goto X1X1; /* x1x1 = x1*x1 */ 78 goto X1X1; /* x1x1 = x1*x1 */
82 79
83 /* now calc (x1+x0)**2 */ 80 /* now calc (x1+x0)**2 */
84 if (s_mp_add (&x1, &x0, &t1) != MP_OKAY) 81 if (s_mp_add(&x1, &x0, &t1) != MP_OKAY)
85 goto X1X1; /* t1 = x1 - x0 */ 82 goto X1X1; /* t1 = x1 - x0 */
86 if (mp_sqr (&t1, &t1) != MP_OKAY) 83 if (mp_sqr(&t1, &t1) != MP_OKAY)
87 goto X1X1; /* t1 = (x1 - x0) * (x1 - x0) */ 84 goto X1X1; /* t1 = (x1 - x0) * (x1 - x0) */
88 85
89 /* add x0y0 */ 86 /* add x0y0 */
90 if (s_mp_add (&x0x0, &x1x1, &t2) != MP_OKAY) 87 if (s_mp_add(&x0x0, &x1x1, &t2) != MP_OKAY)
91 goto X1X1; /* t2 = x0x0 + x1x1 */ 88 goto X1X1; /* t2 = x0x0 + x1x1 */
92 if (s_mp_sub (&t1, &t2, &t1) != MP_OKAY) 89 if (s_mp_sub(&t1, &t2, &t1) != MP_OKAY)
93 goto X1X1; /* t1 = (x1+x0)**2 - (x0x0 + x1x1) */ 90 goto X1X1; /* t1 = (x1+x0)**2 - (x0x0 + x1x1) */
94 91
95 /* shift by B */ 92 /* shift by B */
96 if (mp_lshd (&t1, B) != MP_OKAY) 93 if (mp_lshd(&t1, B) != MP_OKAY)
97 goto X1X1; /* t1 = (x0x0 + x1x1 - (x1-x0)*(x1-x0))<<B */ 94 goto X1X1; /* t1 = (x0x0 + x1x1 - (x1-x0)*(x1-x0))<<B */
98 if (mp_lshd (&x1x1, B * 2) != MP_OKAY) 95 if (mp_lshd(&x1x1, B * 2) != MP_OKAY)
99 goto X1X1; /* x1x1 = x1x1 << 2*B */ 96 goto X1X1; /* x1x1 = x1x1 << 2*B */
100 97
101 if (mp_add (&x0x0, &t1, &t1) != MP_OKAY) 98 if (mp_add(&x0x0, &t1, &t1) != MP_OKAY)
102 goto X1X1; /* t1 = x0x0 + t1 */ 99 goto X1X1; /* t1 = x0x0 + t1 */
103 if (mp_add (&t1, &x1x1, b) != MP_OKAY) 100 if (mp_add(&t1, &x1x1, b) != MP_OKAY)
104 goto X1X1; /* t1 = x0x0 + t1 + x1x1 */ 101 goto X1X1; /* t1 = x0x0 + t1 + x1x1 */
105 102
106 err = MP_OKAY; 103 err = MP_OKAY;
107 104
108 X1X1:mp_clear (&x1x1); 105 X1X1:
109 X0X0:mp_clear (&x0x0); 106 mp_clear(&x1x1);
110 T2:mp_clear (&t2); 107 X0X0:
111 T1:mp_clear (&t1); 108 mp_clear(&x0x0);
112 X1:mp_clear (&x1); 109 T2:
113 X0:mp_clear (&x0); 110 mp_clear(&t2);
114 ERR: 111 T1:
115 return err; 112 mp_clear(&t1);
113 X1:
114 mp_clear(&x1);
115 X0:
116 mp_clear(&x0);
117 LBL_ERR:
118 return err;
116 } 119 }
117 #endif 120 #endif
118 121
119 /* ref: $Format:%D$ */ 122 /* ref: HEAD -> master, tag: v1.1.0 */
120 /* git commit: $Format:%H$ */ 123 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
121 /* commit time: $Format:%ai$ */ 124 /* commit time: 2019-01-28 20:32:32 +0100 */