| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: f-binpoly.c,v 1.3 2004/03/23 12:08:26 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.3 2004/03/23 12:08:26 mdw |
| 34 | * Random field-element selection. |
| 35 | * |
| 36 | * Revision 1.2 2004/03/21 22:52:06 mdw |
| 37 | * Merge and close elliptic curve branch. |
| 38 | * |
| 39 | * Revision 1.1.2.1 2004/03/21 22:39:46 mdw |
| 40 | * Elliptic curves on binary fields work. |
| 41 | * |
| 42 | */ |
| 43 | |
| 44 | /*----- Header files ------------------------------------------------------*/ |
| 45 | |
| 46 | #include <mLib/sub.h> |
| 47 | |
| 48 | #include "field.h" |
| 49 | #include "gf.h" |
| 50 | #include "gfreduce.h" |
| 51 | #include "mprand.h" |
| 52 | |
| 53 | /*----- Data structures ---------------------------------------------------*/ |
| 54 | |
| 55 | typedef struct fctx { |
| 56 | field f; |
| 57 | gfreduce r; |
| 58 | } fctx; |
| 59 | |
| 60 | /*----- Main code ---------------------------------------------------------*/ |
| 61 | |
| 62 | /* --- Field operations --- */ |
| 63 | |
| 64 | static void fdestroy(field *ff) |
| 65 | { |
| 66 | fctx *f = (fctx *)ff; |
| 67 | gfreduce_destroy(&f->r); |
| 68 | DESTROY(f); |
| 69 | } |
| 70 | |
| 71 | static mp *frand(field *ff, grand *r, mp *d) |
| 72 | { |
| 73 | fctx *f = (fctx *)ff; |
| 74 | return (mprand(d, mp_octets(f->r.p) - 1, r, 0)); |
| 75 | } |
| 76 | |
| 77 | static int fzerop(field *ff, mp *x) |
| 78 | { |
| 79 | return (!MP_LEN(x)); |
| 80 | } |
| 81 | |
| 82 | static mp *fadd(field *ff, mp *d, mp *x, mp *y) |
| 83 | { |
| 84 | return (gf_add(d, x, y)); |
| 85 | } |
| 86 | |
| 87 | static mp *fmul(field *ff, mp *d, mp *x, mp *y) |
| 88 | { |
| 89 | fctx *f = (fctx *)ff; |
| 90 | d = gf_mul(d, x, y); |
| 91 | return (gfreduce_do(&f->r, d, d)); |
| 92 | } |
| 93 | |
| 94 | static mp *fsqr(field *ff, mp *d, mp *x) |
| 95 | { |
| 96 | fctx *f = (fctx *)ff; |
| 97 | d = gf_sqr(d, x); |
| 98 | return (gfreduce_do(&f->r, d, d)); |
| 99 | } |
| 100 | |
| 101 | static mp *finv(field *ff, mp *d, mp *x) |
| 102 | { |
| 103 | fctx *f = (fctx *)ff; |
| 104 | gf_gcd(0, 0, &d, f->r.p, x); |
| 105 | return (d); |
| 106 | } |
| 107 | |
| 108 | static mp *freduce(field *ff, mp *d, mp *x) |
| 109 | { |
| 110 | fctx *f = (fctx *)ff; |
| 111 | return (gfreduce_do(&f->r, d, x)); |
| 112 | } |
| 113 | |
| 114 | static mp *fsqrt(field *ff, mp *d, mp *x) |
| 115 | { |
| 116 | fctx *f = (fctx *)ff; |
| 117 | return (gfreduce_sqrt(&f->r, d, x)); |
| 118 | } |
| 119 | |
| 120 | static mp *fquadsolve(field *ff, mp *d, mp *x) |
| 121 | { |
| 122 | fctx *f = (fctx *)ff; |
| 123 | return (gfreduce_quadsolve(&f->r, d, x)); |
| 124 | } |
| 125 | |
| 126 | /* --- Field operations table --- */ |
| 127 | |
| 128 | static field_ops fops = { |
| 129 | fdestroy, frand, |
| 130 | freduce, field_id, |
| 131 | fzerop, field_id, fadd, fadd, fmul, fsqr, finv, freduce, fsqrt, |
| 132 | fquadsolve, |
| 133 | 0, 0, 0, 0 |
| 134 | }; |
| 135 | |
| 136 | /* --- @field_binpoly@ --- * |
| 137 | * |
| 138 | * Arguments: @mp *p@ = the reduction polynomial |
| 139 | * |
| 140 | * Returns: A pointer to the field. |
| 141 | * |
| 142 | * Use: Creates a field structure for a binary field mod @p@. |
| 143 | */ |
| 144 | |
| 145 | field *field_binpoly(mp *p) |
| 146 | { |
| 147 | fctx *f = CREATE(fctx); |
| 148 | f->f.ops = &fops; |
| 149 | f->f.zero = MP_ZERO; |
| 150 | f->f.one = MP_ONE; |
| 151 | gfreduce_create(&f->r, p); |
| 152 | return (&f->f); |
| 153 | } |
| 154 | |
| 155 | /*----- That's all, folks -------------------------------------------------*/ |