| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * Karatsuba-based squaring algorithm |
| 4 | * |
| 5 | * (c) 1999 Straylight/Edgeware |
| 6 | */ |
| 7 | |
| 8 | /*----- Licensing notice --------------------------------------------------* |
| 9 | * |
| 10 | * This file is part of Catacomb. |
| 11 | * |
| 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. |
| 16 | * |
| 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. |
| 21 | * |
| 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, |
| 25 | * MA 02111-1307, USA. |
| 26 | */ |
| 27 | |
| 28 | /*----- Header files ------------------------------------------------------*/ |
| 29 | |
| 30 | #include <assert.h> |
| 31 | #include <stdio.h> |
| 32 | |
| 33 | #include "mpx.h" |
| 34 | #include "karatsuba.h" |
| 35 | |
| 36 | /*----- Tweakables --------------------------------------------------------*/ |
| 37 | |
| 38 | #ifdef TEST_RIG |
| 39 | # undef MPK_THRESH |
| 40 | # define MPK_THRESH 4 |
| 41 | #endif |
| 42 | |
| 43 | /*----- Main code ---------------------------------------------------------*/ |
| 44 | |
| 45 | /* --- @mpx_ksqr@ --- * |
| 46 | * |
| 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 |
| 50 | * |
| 51 | * Returns: --- |
| 52 | * |
| 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. |
| 58 | * |
| 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. |
| 62 | */ |
| 63 | |
| 64 | void mpx_ksqr(mpw *dv, mpw *dvl, |
| 65 | const mpw *av, const mpw *avl, |
| 66 | mpw *sv, mpw *svl) |
| 67 | { |
| 68 | const mpw *avm; |
| 69 | size_t m; |
| 70 | |
| 71 | /* --- Dispose of easy cases to @mpx_usqr@ --- * |
| 72 | * |
| 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... |
| 77 | */ |
| 78 | |
| 79 | MPX_SHRINK(av, avl); |
| 80 | |
| 81 | if (avl - av <= MPK_THRESH) { |
| 82 | mpx_usqr(dv, dvl, av, avl); |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | /* --- How the algorithm works --- * |
| 87 | * |
| 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 |
| 91 | * squarings. |
| 92 | */ |
| 93 | |
| 94 | /* --- First things --- * |
| 95 | * |
| 96 | * Sort out where to break the factor in half. |
| 97 | */ |
| 98 | |
| 99 | m = (avl - av + 1) >> 1; |
| 100 | avm = av + m; |
| 101 | |
| 102 | /* --- Sort out everything --- */ |
| 103 | |
| 104 | { |
| 105 | mpw *svm = sv + m, *svn = svm + m, *ssv = svn + 4; |
| 106 | mpw *tdv = dv + m; |
| 107 | mpw *rdv = tdv + m; |
| 108 | |
| 109 | assert(rdv + m + 4 < dvl); |
| 110 | assert(ssv < svl); |
| 111 | UADD2(sv, svm, av, avm, avm, avl); |
| 112 | if (m > MPK_THRESH) |
| 113 | mpx_ksqr(tdv, rdv + m + 4, sv, svm + 1, ssv, svl); |
| 114 | else |
| 115 | mpx_usqr(tdv, rdv + m + 4, sv, svm + 1); |
| 116 | |
| 117 | if (m > MPK_THRESH) |
| 118 | mpx_ksqr(sv, ssv, avm, avl, ssv, svl); |
| 119 | else |
| 120 | mpx_usqr(sv, ssv, avm, avl); |
| 121 | MPX_COPY(rdv + m + 1, dvl, svm + 1, svn); |
| 122 | UADD(rdv, sv, svm + 1); |
| 123 | USUB(tdv, sv, svn); |
| 124 | |
| 125 | if (m > MPK_THRESH) |
| 126 | mpx_ksqr(sv, ssv, av, avm, ssv, svl); |
| 127 | else |
| 128 | mpx_usqr(sv, ssv, av, avm); |
| 129 | MPX_COPY(dv, tdv, sv, svm); |
| 130 | UADD(tdv, svm, svn); |
| 131 | USUB(tdv, sv, svn); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /*----- Test rig ----------------------------------------------------------*/ |
| 136 | |
| 137 | #ifdef TEST_RIG |
| 138 | |
| 139 | #include <mLib/alloc.h> |
| 140 | #include <mLib/testrig.h> |
| 141 | |
| 142 | #define ALLOC(v, vl, sz) do { \ |
| 143 | size_t _sz = (sz); \ |
| 144 | mpw *_vv = xmalloc(MPWS(_sz)); \ |
| 145 | mpw *_vvl = _vv + _sz; \ |
| 146 | (v) = _vv; \ |
| 147 | (vl) = _vvl; \ |
| 148 | } while (0) |
| 149 | |
| 150 | #define LOAD(v, vl, d) do { \ |
| 151 | const dstr *_d = (d); \ |
| 152 | mpw *_v, *_vl; \ |
| 153 | ALLOC(_v, _vl, MPW_RQ(_d->len)); \ |
| 154 | mpx_loadb(_v, _vl, _d->buf, _d->len); \ |
| 155 | (v) = _v; \ |
| 156 | (vl) = _vl; \ |
| 157 | } while (0) |
| 158 | |
| 159 | #define MAX(x, y) ((x) > (y) ? (x) : (y)) |
| 160 | |
| 161 | static void dumpmp(const char *msg, const mpw *v, const mpw *vl) |
| 162 | { |
| 163 | fputs(msg, stderr); |
| 164 | MPX_SHRINK(v, vl); |
| 165 | while (v < vl) |
| 166 | fprintf(stderr, " %08lx", (unsigned long)*--vl); |
| 167 | fputc('\n', stderr); |
| 168 | } |
| 169 | |
| 170 | static int usqr(dstr *v) |
| 171 | { |
| 172 | mpw *a, *al; |
| 173 | mpw *c, *cl; |
| 174 | mpw *d, *dl; |
| 175 | mpw *s, *sl; |
| 176 | size_t m; |
| 177 | int ok = 1; |
| 178 | |
| 179 | LOAD(a, al, &v[0]); |
| 180 | LOAD(c, cl, &v[1]); |
| 181 | m = al - a + 1; |
| 182 | ALLOC(d, dl, 3 * m); |
| 183 | ALLOC(s, sl, 5 * m); |
| 184 | |
| 185 | mpx_ksqr(d, dl, a, al, s, sl); |
| 186 | if (!mpx_ueq(d, dl, c, cl)) { |
| 187 | fprintf(stderr, "\n*** usqr failed\n"); |
| 188 | dumpmp(" a", a, al); |
| 189 | dumpmp("expected", c, cl); |
| 190 | dumpmp(" result", d, dl); |
| 191 | ok = 0; |
| 192 | } |
| 193 | |
| 194 | xfree(a); xfree(c); xfree(d); xfree(s); |
| 195 | return (ok); |
| 196 | } |
| 197 | |
| 198 | static test_chunk defs[] = { |
| 199 | { "usqr", usqr, { &type_hex, &type_hex, 0 } }, |
| 200 | { 0, 0, { 0 } } |
| 201 | }; |
| 202 | |
| 203 | int main(int argc, char *argv[]) |
| 204 | { |
| 205 | test_run(argc, argv, defs, SRCDIR"/t/mpx"); |
| 206 | return (0); |
| 207 | } |
| 208 | |
| 209 | #endif |
| 210 | |
| 211 | /*----- That's all, folks -------------------------------------------------*/ |