3 * Karatsuba-based squaring algorithm
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
34 #include "karatsuba.h"
36 /*----- Tweakables --------------------------------------------------------*/
43 /*----- Main code ---------------------------------------------------------*/
45 /* --- @mpx_ksqr@ --- *
47 * Arguments: @mpw *dv, *dvl@ = pointer to destination buffer
48 * @const mpw *av, *avl@ = pointer to first argument
49 * @mpw *sv, *svl@ = pointer to scratch workspace
53 * Use: Squares a multiprecision integers using something similar to
54 * Karatsuba's multiplication algorithm. This is rather faster
55 * than traditional long multiplication (e.g., @mpx_umul@) on
56 * large numbers, although more expensive on small ones, and
57 * rather simpler than full-blown Karatsuba multiplication.
59 * The destination must be three times as large as the larger
60 * argument. The scratch space must be five times as large as
61 * the larger argument.
64 void mpx_ksqr(mpw
*dv
, mpw
*dvl
,
65 const mpw
*av
, const mpw
*avl
,
71 /* --- Dispose of easy cases to @mpx_usqr@ --- *
73 * Karatsuba is only a win on large numbers, because of all the
74 * recursiveness and bookkeeping. The recursive calls make a quick check
75 * to see whether to bottom out to @mpx_usqr@ which should help quite a
76 * lot, but sometimes the only way to know is to make sure...
81 if (avl
- av
<= MPK_THRESH
) {
82 mpx_usqr(dv
, dvl
, av
, avl
);
86 /* --- How the algorithm works --- *
88 * The identity for squaring is known to all schoolchildren.
89 * Let %$A = xb + y$%. Then %$A^2 = x^2 b^2 + 2 x y b + y^2$%. Now,
90 * %$(x + y)^2 - x^2 - y^2 = 2 x y$%, which means I only need to do three
94 /* --- First things --- *
96 * Sort out where to break the factor in half.
99 m
= (avl
- av
+ 1) >> 1;
102 /* --- Sort out everything --- */
105 mpw
*svm
= sv
+ m
, *svn
= svm
+ m
, *ssv
= svn
+ 4;
109 assert(rdv
+ m
+ 4 < dvl
);
111 UADD2(sv
, svm
, av
, avm
, avm
, avl
);
113 mpx_ksqr(tdv
, rdv
+ m
+ 4, sv
, svm
+ 1, ssv
, svl
);
115 mpx_usqr(tdv
, rdv
+ m
+ 4, sv
, svm
+ 1);
118 mpx_ksqr(sv
, ssv
, avm
, avl
, ssv
, svl
);
120 mpx_usqr(sv
, ssv
, avm
, avl
);
121 MPX_COPY(rdv
+ m
+ 1, dvl
, svm
+ 1, svn
);
122 UADD(rdv
, sv
, svm
+ 1);
126 mpx_ksqr(sv
, ssv
, av
, avm
, ssv
, svl
);
128 mpx_usqr(sv
, ssv
, av
, avm
);
129 MPX_COPY(dv
, tdv
, sv
, svm
);
135 /*----- Test rig ----------------------------------------------------------*/
139 #include <mLib/alloc.h>
140 #include <mLib/testrig.h>
142 #define ALLOC(v, vl, sz) do { \
144 mpw *_vv = xmalloc(MPWS(_sz)); \
145 mpw *_vvl = _vv + _sz; \
150 #define LOAD(v, vl, d) do { \
151 const dstr *_d = (d); \
153 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
154 mpx_loadb(_v, _vl, _d->buf, _d->len); \
159 #define MAX(x, y) ((x) > (y) ? (x) : (y))
161 static void dumpmp(const char *msg
, const mpw
*v
, const mpw
*vl
)
166 fprintf(stderr
, " %08lx", (unsigned long)*--vl
);
170 static int usqr(dstr
*v
)
185 mpx_ksqr(d
, dl
, a
, al
, s
, sl
);
186 if (!mpx_ueq(d
, dl
, c
, cl
)) {
187 fprintf(stderr
, "\n*** usqr failed\n");
189 dumpmp("expected", c
, cl
);
190 dumpmp(" result", d
, dl
);
194 xfree(a
); xfree(c
); xfree(d
); xfree(s
);
198 static test_chunk defs
[] = {
199 { "usqr", usqr
, { &type_hex
, &type_hex
, 0 } },
203 int main(int argc
, char *argv
[])
205 test_run(argc
, argv
, defs
, SRCDIR
"/t/mpx");
211 /*----- That's all, folks -------------------------------------------------*/