| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: f-binpoly.c,v 1.6 2004/04/01 12:50:09 mdw Exp $ |
| 4 | * |
| 5 | * Binary fields with polynomial basis representation |
| 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: f-binpoly.c,v $ |
| 33 | * Revision 1.6 2004/04/01 12:50:09 mdw |
| 34 | * Add cyclic group abstraction, with test code. Separate off exponentation |
| 35 | * functions for better static linking. Fix a buttload of bugs on the way. |
| 36 | * Generally ensure that negative exponents do inversion correctly. Add |
| 37 | * table of standard prime-field subgroups. (Binary field subgroups are |
| 38 | * currently unimplemented but easy to add if anyone ever finds a good one.) |
| 39 | * |
| 40 | * Revision 1.5 2004/03/27 17:54:11 mdw |
| 41 | * Standard curves and curve checking. |
| 42 | * |
| 43 | * Revision 1.4 2004/03/23 15:19:32 mdw |
| 44 | * Test elliptic curves more thoroughly. |
| 45 | * |
| 46 | * Revision 1.3 2004/03/23 12:08:26 mdw |
| 47 | * Random field-element selection. |
| 48 | * |
| 49 | * Revision 1.2 2004/03/21 22:52:06 mdw |
| 50 | * Merge and close elliptic curve branch. |
| 51 | * |
| 52 | * Revision 1.1.2.1 2004/03/21 22:39:46 mdw |
| 53 | * Elliptic curves on binary fields work. |
| 54 | * |
| 55 | */ |
| 56 | |
| 57 | /*----- Header files ------------------------------------------------------*/ |
| 58 | |
| 59 | #include <mLib/sub.h> |
| 60 | |
| 61 | #include "field.h" |
| 62 | #include "gf.h" |
| 63 | #include "gfreduce.h" |
| 64 | #include "mprand.h" |
| 65 | |
| 66 | /*----- Data structures ---------------------------------------------------*/ |
| 67 | |
| 68 | typedef struct fctx { |
| 69 | field f; |
| 70 | gfreduce r; |
| 71 | } fctx; |
| 72 | |
| 73 | /*----- Main code ---------------------------------------------------------*/ |
| 74 | |
| 75 | /* --- Field operations --- */ |
| 76 | |
| 77 | static void fdestroy(field *ff) |
| 78 | { |
| 79 | fctx *f = (fctx *)ff; |
| 80 | gfreduce_destroy(&f->r); |
| 81 | DESTROY(f); |
| 82 | } |
| 83 | |
| 84 | static mp *frand(field *f, mp *d, grand *r) |
| 85 | { |
| 86 | return (mprand(d, f->nbits, r, 0)); |
| 87 | } |
| 88 | |
| 89 | static int fzerop(field *ff, mp *x) |
| 90 | { |
| 91 | return (!MP_LEN(x)); |
| 92 | } |
| 93 | |
| 94 | static mp *fadd(field *ff, mp *d, mp *x, mp *y) |
| 95 | { |
| 96 | return (gf_add(d, x, y)); |
| 97 | } |
| 98 | |
| 99 | static mp *fmul(field *ff, mp *d, mp *x, mp *y) |
| 100 | { |
| 101 | fctx *f = (fctx *)ff; |
| 102 | d = gf_mul(d, x, y); |
| 103 | return (gfreduce_do(&f->r, d, d)); |
| 104 | } |
| 105 | |
| 106 | static mp *fsqr(field *ff, mp *d, mp *x) |
| 107 | { |
| 108 | fctx *f = (fctx *)ff; |
| 109 | d = gf_sqr(d, x); |
| 110 | return (gfreduce_do(&f->r, d, d)); |
| 111 | } |
| 112 | |
| 113 | static mp *finv(field *ff, mp *d, mp *x) |
| 114 | { |
| 115 | fctx *f = (fctx *)ff; |
| 116 | gf_gcd(0, 0, &d, f->r.p, x); |
| 117 | return (d); |
| 118 | } |
| 119 | |
| 120 | static mp *freduce(field *ff, mp *d, mp *x) |
| 121 | { |
| 122 | fctx *f = (fctx *)ff; |
| 123 | return (gfreduce_do(&f->r, d, x)); |
| 124 | } |
| 125 | |
| 126 | static mp *fsqrt(field *ff, mp *d, mp *x) |
| 127 | { |
| 128 | fctx *f = (fctx *)ff; |
| 129 | return (gfreduce_sqrt(&f->r, d, x)); |
| 130 | } |
| 131 | |
| 132 | static mp *fquadsolve(field *ff, mp *d, mp *x) |
| 133 | { |
| 134 | fctx *f = (fctx *)ff; |
| 135 | return (gfreduce_quadsolve(&f->r, d, x)); |
| 136 | } |
| 137 | |
| 138 | /* --- Field operations table --- */ |
| 139 | |
| 140 | static field_ops fops = { |
| 141 | FTY_BINARY, "binpoly", |
| 142 | fdestroy, frand, field_stdsamep, |
| 143 | freduce, field_id, |
| 144 | fzerop, field_id, fadd, fadd, fmul, fsqr, finv, freduce, fsqrt, |
| 145 | fquadsolve, |
| 146 | 0, 0, 0, 0 |
| 147 | }; |
| 148 | |
| 149 | /* --- @field_binpoly@ --- * |
| 150 | * |
| 151 | * Arguments: @mp *p@ = the reduction polynomial |
| 152 | * |
| 153 | * Returns: A pointer to the field. |
| 154 | * |
| 155 | * Use: Creates a field structure for a binary field mod @p@. |
| 156 | */ |
| 157 | |
| 158 | field *field_binpoly(mp *p) |
| 159 | { |
| 160 | fctx *f = CREATE(fctx); |
| 161 | f->f.ops = &fops; |
| 162 | f->f.zero = MP_ZERO; |
| 163 | f->f.one = MP_ONE; |
| 164 | f->f.nbits = mp_bits(p) - 1; |
| 165 | f->f.noctets = (f->f.nbits + 7) >> 3; |
| 166 | gfreduce_create(&f->r, p); |
| 167 | f->f.m = f->r.p; |
| 168 | return (&f->f); |
| 169 | } |
| 170 | |
| 171 | /*----- That's all, folks -------------------------------------------------*/ |