Skip to content
Newer
Older
100644 122 lines (101 sloc) 2.93 KB
e549ccf added libtommath-0.32
Tom St Denis authored
1 #include <tommath.h>
2 #ifdef BN_MP_KARATSUBA_SQR_C
57354e1 added libtommath-0.12
Tom St Denis authored
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis
4 *
c1da6aa added libtommath-0.25
Tom St Denis authored
5 * LibTomMath is a library that provides multiple-precision
57354e1 added libtommath-0.12
Tom St Denis authored
6 * integer arithmetic as well as number theoretic functionality.
7 *
c1da6aa added libtommath-0.25
Tom St Denis authored
8 * The library was designed directly after the MPI library by
57354e1 added libtommath-0.12
Tom St Denis authored
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 *
333aebc added libtommath-0.41
Tom St Denis authored
15 * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
57354e1 added libtommath-0.12
Tom St Denis authored
16 */
17
0ef44ce added libtommath-0.18
Tom St Denis authored
18 /* Karatsuba squaring, computes b = a*a using three
19 * half size squarings
57354e1 added libtommath-0.12
Tom St Denis authored
20 *
e549ccf added libtommath-0.32
Tom St Denis authored
21 * See comments of karatsuba_mul for details. It
0ef44ce added libtommath-0.18
Tom St Denis authored
22 * is essentially the same algorithm but merely
23 * tuned to perform recursive squarings.
57354e1 added libtommath-0.12
Tom St Denis authored
24 */
350578d added libtommath-0.30
Tom St Denis authored
25 int mp_karatsuba_sqr (mp_int * a, mp_int * b)
57354e1 added libtommath-0.12
Tom St Denis authored
26 {
b66471f added libtommath-0.13
Tom St Denis authored
27 mp_int x0, x1, t1, t2, x0x0, x1x1;
82f4858 added libtommath-0.14
Tom St Denis authored
28 int B, err;
57354e1 added libtommath-0.12
Tom St Denis authored
29
30 err = MP_MEM;
31
32 /* min # of digits */
33 B = a->used;
34
35 /* now divide in two */
350578d added libtommath-0.30
Tom St Denis authored
36 B = B >> 1;
57354e1 added libtommath-0.12
Tom St Denis authored
37
38 /* init copy all the temps */
39 if (mp_init_size (&x0, B) != MP_OKAY)
40 goto ERR;
41 if (mp_init_size (&x1, a->used - B) != MP_OKAY)
42 goto X0;
43
44 /* init temps */
82f4858 added libtommath-0.14
Tom St Denis authored
45 if (mp_init_size (&t1, a->used * 2) != MP_OKAY)
57354e1 added libtommath-0.12
Tom St Denis authored
46 goto X1;
82f4858 added libtommath-0.14
Tom St Denis authored
47 if (mp_init_size (&t2, a->used * 2) != MP_OKAY)
57354e1 added libtommath-0.12
Tom St Denis authored
48 goto T1;
82f4858 added libtommath-0.14
Tom St Denis authored
49 if (mp_init_size (&x0x0, B * 2) != MP_OKAY)
57354e1 added libtommath-0.12
Tom St Denis authored
50 goto T2;
82f4858 added libtommath-0.14
Tom St Denis authored
51 if (mp_init_size (&x1x1, (a->used - B) * 2) != MP_OKAY)
57354e1 added libtommath-0.12
Tom St Denis authored
52 goto X0X0;
53
82f4858 added libtommath-0.14
Tom St Denis authored
54 {
55 register int x;
56 register mp_digit *dst, *src;
57
58 src = a->dp;
59
60 /* now shift the digits */
61 dst = x0.dp;
62 for (x = 0; x < B; x++) {
63 *dst++ = *src++;
64 }
57354e1 added libtommath-0.12
Tom St Denis authored
65
82f4858 added libtommath-0.14
Tom St Denis authored
66 dst = x1.dp;
67 for (x = B; x < a->used; x++) {
68 *dst++ = *src++;
69 }
57354e1 added libtommath-0.12
Tom St Denis authored
70 }
71
72 x0.used = B;
73 x1.used = a->used - B;
74
75 mp_clamp (&x0);
76
77 /* now calc the products x0*x0 and x1*x1 */
78 if (mp_sqr (&x0, &x0x0) != MP_OKAY)
0ef44ce added libtommath-0.18
Tom St Denis authored
79 goto X1X1; /* x0x0 = x0*x0 */
57354e1 added libtommath-0.12
Tom St Denis authored
80 if (mp_sqr (&x1, &x1x1) != MP_OKAY)
0ef44ce added libtommath-0.18
Tom St Denis authored
81 goto X1X1; /* x1x1 = x1*x1 */
57354e1 added libtommath-0.12
Tom St Denis authored
82
9294e79 added libtommath-0.36
Tom St Denis authored
83 /* now calc (x1+x0)**2 */
84 if (s_mp_add (&x1, &x0, &t1) != MP_OKAY)
0ef44ce added libtommath-0.18
Tom St Denis authored
85 goto X1X1; /* t1 = x1 - x0 */
57354e1 added libtommath-0.12
Tom St Denis authored
86 if (mp_sqr (&t1, &t1) != MP_OKAY)
0ef44ce added libtommath-0.18
Tom St Denis authored
87 goto X1X1; /* t1 = (x1 - x0) * (x1 - x0) */
57354e1 added libtommath-0.12
Tom St Denis authored
88
89 /* add x0y0 */
82f4858 added libtommath-0.14
Tom St Denis authored
90 if (s_mp_add (&x0x0, &x1x1, &t2) != MP_OKAY)
0ef44ce added libtommath-0.18
Tom St Denis authored
91 goto X1X1; /* t2 = x0x0 + x1x1 */
9294e79 added libtommath-0.36
Tom St Denis authored
92 if (s_mp_sub (&t1, &t2, &t1) != MP_OKAY)
93 goto X1X1; /* t1 = (x1+x0)**2 - (x0x0 + x1x1) */
57354e1 added libtommath-0.12
Tom St Denis authored
94
95 /* shift by B */
96 if (mp_lshd (&t1, B) != MP_OKAY)
0ef44ce added libtommath-0.18
Tom St Denis authored
97 goto X1X1; /* t1 = (x0x0 + x1x1 - (x1-x0)*(x1-x0))<<B */
57354e1 added libtommath-0.12
Tom St Denis authored
98 if (mp_lshd (&x1x1, B * 2) != MP_OKAY)
0ef44ce added libtommath-0.18
Tom St Denis authored
99 goto X1X1; /* x1x1 = x1x1 << 2*B */
57354e1 added libtommath-0.12
Tom St Denis authored
100
101 if (mp_add (&x0x0, &t1, &t1) != MP_OKAY)
0ef44ce added libtommath-0.18
Tom St Denis authored
102 goto X1X1; /* t1 = x0x0 + t1 */
57354e1 added libtommath-0.12
Tom St Denis authored
103 if (mp_add (&t1, &x1x1, b) != MP_OKAY)
0ef44ce added libtommath-0.18
Tom St Denis authored
104 goto X1X1; /* t1 = x0x0 + t1 + x1x1 */
57354e1 added libtommath-0.12
Tom St Denis authored
105
106 err = MP_OKAY;
107
108 X1X1:mp_clear (&x1x1);
109 X0X0:mp_clear (&x0x0);
110 T2:mp_clear (&t2);
111 T1:mp_clear (&t1);
112 X1:mp_clear (&x1);
113 X0:mp_clear (&x0);
114 ERR:
115 return err;
116 }
e549ccf added libtommath-0.32
Tom St Denis authored
117 #endif
9294e79 added libtommath-0.36
Tom St Denis authored
118
119 /* $Source$ */
120 /* $Revision$ */
121 /* $Date$ */
Something went wrong with that request. Please try again.