| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: gf-arith.c,v 1.2 2004/03/21 22:52:06 mdw Exp $ |
| 4 | * |
| 5 | * Basic arithmetic on binary polynomials |
| 6 | * |
| 7 | * (c) 2004 Straylight/Edgeware |
| 8 | */ |
| 9 | |
| 10 | /*----- Licensing notice --------------------------------------------------* |
| 11 | * |
| 12 | * This file is part of Catacomb. |
| 13 | * |
| 14 | * Catacomb is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU Library General Public License as |
| 16 | * published by the Free Software Foundation; either version 2 of the |
| 17 | * License, or (at your option) any later version. |
| 18 | * |
| 19 | * Catacomb is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU Library General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU Library General Public |
| 25 | * License along with Catacomb; if not, write to the Free |
| 26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 27 | * MA 02111-1307, USA. |
| 28 | */ |
| 29 | |
| 30 | /*----- Revision history --------------------------------------------------* |
| 31 | * |
| 32 | * $Log: gf-arith.c,v $ |
| 33 | * Revision 1.2 2004/03/21 22:52:06 mdw |
| 34 | * Merge and close elliptic curve branch. |
| 35 | * |
| 36 | * Revision 1.1.2.1 2004/03/21 22:39:46 mdw |
| 37 | * Elliptic curves on binary fields work. |
| 38 | * |
| 39 | */ |
| 40 | |
| 41 | /*----- Header files ------------------------------------------------------*/ |
| 42 | |
| 43 | #include "gf.h" |
| 44 | |
| 45 | /*----- Macros ------------------------------------------------------------*/ |
| 46 | |
| 47 | #define MAX(x, y) ((x) >= (y) ? (x) : (y)) |
| 48 | |
| 49 | /*----- Main code ---------------------------------------------------------*/ |
| 50 | |
| 51 | /* --- @gf_add@ --- * |
| 52 | * |
| 53 | * Arguments: @mp *d@ = destination |
| 54 | * @mp *a, *b@ = sources |
| 55 | * |
| 56 | * Returns: Result, @a@ added to @b@. |
| 57 | */ |
| 58 | |
| 59 | mp *gf_add(mp *d, mp *a, mp *b) |
| 60 | { |
| 61 | MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)), (a->f | b->f) & MP_BURN); |
| 62 | gfx_add(d->v, d->vl, a->v, a->vl, b->v, b->vl); |
| 63 | d->f = (a->f | b->f) & MP_BURN; |
| 64 | MP_SHRINK(d); |
| 65 | return (d); |
| 66 | } |
| 67 | |
| 68 | /* --- @gf_mul@ --- * |
| 69 | * |
| 70 | * Arguments: @mp *d@ = destination |
| 71 | * @mp *a, *b@ = sources |
| 72 | * |
| 73 | * Returns: Result, @a@ multiplied by @b@. |
| 74 | */ |
| 75 | |
| 76 | mp *gf_mul(mp *d, mp *a, mp *b) |
| 77 | { |
| 78 | a = MP_COPY(a); |
| 79 | b = MP_COPY(b); |
| 80 | |
| 81 | if (MP_LEN(a) <= MPK_THRESH || MP_LEN(b) <= GFK_THRESH) { |
| 82 | MP_DEST(d, MP_LEN(a) + MP_LEN(b), a->f | b->f | MP_UNDEF); |
| 83 | gfx_mul(d->v, d->vl, a->v, a->vl, b->v, b->vl); |
| 84 | } else { |
| 85 | size_t m = MAX(MP_LEN(a), MP_LEN(b)); |
| 86 | mpw *s; |
| 87 | MP_DEST(d, 2 * m, a->f | b->f | MP_UNDEF); |
| 88 | s = mpalloc(d->a, 2 * m); |
| 89 | gfx_kmul(d->v, d->vl, a->v, a->vl, b->v, b->vl, s, s + 2 * m); |
| 90 | mpfree(d->a, s); |
| 91 | } |
| 92 | |
| 93 | d->f = (a->f | b->f) & MP_BURN; |
| 94 | MP_SHRINK(d); |
| 95 | MP_DROP(a); |
| 96 | MP_DROP(b); |
| 97 | return (d); |
| 98 | } |
| 99 | |
| 100 | /* --- @gf_sqr@ --- * |
| 101 | * |
| 102 | * Arguments: @mp *d@ = destination |
| 103 | * @mp *a@ = source |
| 104 | * |
| 105 | * Returns: Result, @a@ squared. |
| 106 | */ |
| 107 | |
| 108 | mp *gf_sqr(mp *d, mp *a) |
| 109 | { |
| 110 | MP_COPY(a); |
| 111 | MP_DEST(d, 2 * MP_LEN(a), a->f & MP_BURN); |
| 112 | gfx_sqr(d->v, d->vl, a->v, a->vl); |
| 113 | d->f = a->f & MP_BURN; |
| 114 | MP_SHRINK(d); |
| 115 | MP_DROP(a); |
| 116 | return (d); |
| 117 | } |
| 118 | |
| 119 | /* --- @gf_div@ --- * |
| 120 | * |
| 121 | * Arguments: @mp **qq, **rr@ = destination, quotient and remainder |
| 122 | * @mp *a, *b@ = sources |
| 123 | * |
| 124 | * Use: Calculates the quotient and remainder when @a@ is divided by |
| 125 | * @b@. The destinations @*qq@ and @*rr@ must be distinct. |
| 126 | * Either of @qq@ or @rr@ may be null to indicate that the |
| 127 | * result is irrelevant. (Discarding both results is silly.) |
| 128 | * There is a performance advantage if @a == *rr@. |
| 129 | */ |
| 130 | |
| 131 | void gf_div(mp **qq, mp **rr, mp *a, mp *b) |
| 132 | { |
| 133 | mp *r = rr ? *rr : MP_NEW; |
| 134 | mp *q = qq ? *qq : MP_NEW; |
| 135 | |
| 136 | /* --- Set the remainder up right --- */ |
| 137 | |
| 138 | b = MP_COPY(b); |
| 139 | a = MP_COPY(a); |
| 140 | if (r) |
| 141 | MP_DROP(r); |
| 142 | r = a; |
| 143 | MP_DEST(r, MP_LEN(b) + 2, a->f | b->f); |
| 144 | |
| 145 | /* --- Fix up the quotient too --- */ |
| 146 | |
| 147 | r = MP_COPY(r); |
| 148 | MP_DEST(q, MP_LEN(r), r->f | MP_UNDEF); |
| 149 | MP_DROP(r); |
| 150 | |
| 151 | /* --- Perform the calculation --- */ |
| 152 | |
| 153 | gfx_div(q->v, q->vl, r->v, r->vl, b->v, b->vl); |
| 154 | |
| 155 | /* --- Sort out the sign of the results --- * |
| 156 | * |
| 157 | * If the signs of the arguments differ, and the remainder is nonzero, I |
| 158 | * must add one to the absolute value of the quotient and subtract the |
| 159 | * remainder from @b@. |
| 160 | */ |
| 161 | |
| 162 | q->f = (r->f | b->f) & MP_BURN; |
| 163 | r->f = (r->f | b->f) & MP_BURN; |
| 164 | |
| 165 | /* --- Store the return values --- */ |
| 166 | |
| 167 | MP_DROP(b); |
| 168 | |
| 169 | if (!qq) |
| 170 | MP_DROP(q); |
| 171 | else { |
| 172 | MP_SHRINK(q); |
| 173 | *qq = q; |
| 174 | } |
| 175 | |
| 176 | if (!rr) |
| 177 | MP_DROP(r); |
| 178 | else { |
| 179 | MP_SHRINK(r); |
| 180 | *rr = r; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /*----- Test rig ----------------------------------------------------------*/ |
| 185 | |
| 186 | #ifdef TEST_RIG |
| 187 | |
| 188 | static int verify(const char *op, mp *expect, mp *result, mp *a, mp *b) |
| 189 | { |
| 190 | if (!MP_EQ(expect, result)) { |
| 191 | fprintf(stderr, "\n*** %s failed", op); |
| 192 | fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 16); |
| 193 | fputs("\n*** b = ", stderr); mp_writefile(b, stderr, 16); |
| 194 | fputs("\n*** result = ", stderr); mp_writefile(result, stderr, 16); |
| 195 | fputs("\n*** expect = ", stderr); mp_writefile(expect, stderr, 16); |
| 196 | fputc('\n', stderr); |
| 197 | return (0); |
| 198 | } |
| 199 | return (1); |
| 200 | } |
| 201 | |
| 202 | #define RIG(name, op) \ |
| 203 | static int t##name(dstr *v) \ |
| 204 | { \ |
| 205 | mp *a = *(mp **)v[0].buf; \ |
| 206 | mp *b = *(mp **)v[1].buf; \ |
| 207 | mp *r = *(mp **)v[2].buf; \ |
| 208 | mp *c = op(MP_NEW, a, b); \ |
| 209 | int ok = verify(#name, r, c, a, b); \ |
| 210 | mp_drop(a); mp_drop(b); mp_drop(c); mp_drop(r); \ |
| 211 | assert(mparena_count(MPARENA_GLOBAL) == 0); \ |
| 212 | return (ok); \ |
| 213 | } |
| 214 | |
| 215 | RIG(add, gf_add) |
| 216 | RIG(mul, gf_mul) |
| 217 | |
| 218 | #undef RIG |
| 219 | |
| 220 | static int tsqr(dstr *v) |
| 221 | { |
| 222 | mp *a = *(mp **)v[0].buf; |
| 223 | mp *r = *(mp **)v[1].buf; |
| 224 | mp *c = MP_NEW; |
| 225 | int ok = 1; |
| 226 | c = gf_sqr(MP_NEW, a); |
| 227 | ok &= verify("sqr", r, c, a, MP_ZERO); |
| 228 | mp_drop(a); mp_drop(r); mp_drop(c); |
| 229 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
| 230 | return (ok); |
| 231 | } |
| 232 | |
| 233 | static int tdiv(dstr *v) |
| 234 | { |
| 235 | mp *a = *(mp **)v[0].buf; |
| 236 | mp *b = *(mp **)v[1].buf; |
| 237 | mp *q = *(mp **)v[2].buf; |
| 238 | mp *r = *(mp **)v[3].buf; |
| 239 | mp *c = MP_NEW, *d = MP_NEW; |
| 240 | int ok = 1; |
| 241 | gf_div(&c, &d, a, b); |
| 242 | ok &= verify("div(quotient)", q, c, a, b); |
| 243 | ok &= verify("div(remainder)", r, d, a, b); |
| 244 | mp_drop(a); mp_drop(b); mp_drop(c); mp_drop(d); mp_drop(r); mp_drop(q); |
| 245 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
| 246 | return (ok); |
| 247 | } |
| 248 | |
| 249 | static test_chunk tests[] = { |
| 250 | { "add", tadd, { &type_mp, &type_mp, &type_mp, 0 } }, |
| 251 | { "mul", tmul, { &type_mp, &type_mp, &type_mp, 0 } }, |
| 252 | { "sqr", tsqr, { &type_mp, &type_mp, 0 } }, |
| 253 | { "div", tdiv, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } }, |
| 254 | { 0, 0, { 0 } }, |
| 255 | }; |
| 256 | |
| 257 | int main(int argc, char *argv[]) |
| 258 | { |
| 259 | sub_init(); |
| 260 | test_run(argc, argv, tests, SRCDIR "/tests/gf"); |
| 261 | return (0); |
| 262 | } |
| 263 | |
| 264 | #endif |
| 265 | |
| 266 | /*----- That's all, folks -------------------------------------------------*/ |