X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb/blobdiff_plain/343509982ee8c88ddafd0129b4dcf97e3c7a672d..ceb3f0c0a3b7bb3fa3250d31b04c382894095e52:/f-binpoly.c?ds=sidebyside diff --git a/f-binpoly.c b/f-binpoly.c new file mode 100644 index 00000000..509efc4f --- /dev/null +++ b/f-binpoly.c @@ -0,0 +1,142 @@ +/* -*-c-*- + * + * $Id: f-binpoly.c,v 1.1.2.1 2004/03/21 22:39:46 mdw Exp $ + * + * Binary fields with polynomial basis representation + * + * (c) 2004 Straylight/Edgeware + */ + +/*----- Licensing notice --------------------------------------------------* + * + * This file is part of Catacomb. + * + * Catacomb is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * Catacomb is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with Catacomb; if not, write to the Free + * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, + * MA 02111-1307, USA. + */ + +/*----- Revision history --------------------------------------------------* + * + * $Log: f-binpoly.c,v $ + * Revision 1.1.2.1 2004/03/21 22:39:46 mdw + * Elliptic curves on binary fields work. + * + */ + +/*----- Header files ------------------------------------------------------*/ + +#include + +#include "field.h" +#include "gf.h" +#include "gfreduce.h" + +/*----- Data structures ---------------------------------------------------*/ + +typedef struct fctx { + field f; + gfreduce r; +} fctx; + +/*----- Main code ---------------------------------------------------------*/ + +/* --- Field operations --- */ + +static void fdestroy(field *ff) +{ + fctx *f = (fctx *)ff; + gfreduce_destroy(&f->r); + DESTROY(f); +} + +static int fzerop(field *ff, mp *x) +{ + return (!MP_LEN(x)); +} + +static mp *fadd(field *ff, mp *d, mp *x, mp *y) +{ + return (gf_add(d, x, y)); +} + +static mp *fmul(field *ff, mp *d, mp *x, mp *y) +{ + fctx *f = (fctx *)ff; + d = gf_mul(d, x, y); + return (gfreduce_do(&f->r, d, d)); +} + +static mp *fsqr(field *ff, mp *d, mp *x) +{ + fctx *f = (fctx *)ff; + d = gf_sqr(d, x); + return (gfreduce_do(&f->r, d, d)); +} + +static mp *finv(field *ff, mp *d, mp *x) +{ + fctx *f = (fctx *)ff; + gf_gcd(0, 0, &d, f->r.p, x); + return (d); +} + +static mp *freduce(field *ff, mp *d, mp *x) +{ + fctx *f = (fctx *)ff; + return (gfreduce_do(&f->r, d, x)); +} + +static mp *fsqrt(field *ff, mp *d, mp *x) +{ + fctx *f = (fctx *)ff; + return (gfreduce_sqrt(&f->r, d, x)); +} + +static mp *fquadsolve(field *ff, mp *d, mp *x) +{ + fctx *f = (fctx *)ff; + return (gfreduce_quadsolve(&f->r, d, x)); +} + +/* --- Field operations table --- */ + +static field_ops fops = { + fdestroy, + freduce, field_id, + fzerop, field_id, fadd, fadd, fmul, fsqr, finv, freduce, fsqrt, + fquadsolve, + 0, 0, 0, 0 +}; + +/* --- @field_binpoly@ --- * + * + * Arguments: @mp *p@ = the reduction polynomial + * + * Returns: A pointer to the field. + * + * Use: Creates a field structure for a binary field mod @p@. + */ + +field *field_binpoly(mp *p) +{ + fctx *f = CREATE(fctx); + f->f.ops = &fops; + f->f.zero = MP_ZERO; + f->f.one = MP_ONE; + gfreduce_create(&f->r, p); + return (&f->f); +} + +/*----- That's all, folks -------------------------------------------------*/