| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: field.h,v 1.5 2004/03/23 12:08:26 mdw Exp $ |
| 4 | * |
| 5 | * Definitions for field arithmetic |
| 6 | * |
| 7 | * (c) 2000 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: field.h,v $ |
| 33 | * Revision 1.5 2004/03/23 12:08:26 mdw |
| 34 | * Random field-element selection. |
| 35 | * |
| 36 | * Revision 1.4 2004/03/21 22:52:06 mdw |
| 37 | * Merge and close elliptic curve branch. |
| 38 | * |
| 39 | * Revision 1.3.4.2 2004/03/21 22:39:46 mdw |
| 40 | * Elliptic curves on binary fields work. |
| 41 | * |
| 42 | * Revision 1.3.4.1 2004/03/20 00:13:31 mdw |
| 43 | * Projective coordinates for prime curves |
| 44 | * |
| 45 | * Revision 1.3 2002/01/13 13:48:44 mdw |
| 46 | * Further progress. |
| 47 | * |
| 48 | * Revision 1.2 2001/05/07 17:30:13 mdw |
| 49 | * Add an internal-representation no-op function. |
| 50 | * |
| 51 | * Revision 1.1 2001/04/29 18:12:33 mdw |
| 52 | * Prototype version. |
| 53 | * |
| 54 | */ |
| 55 | |
| 56 | #ifndef CATACOMB_FIELD_H |
| 57 | #define CATACOMB_FIELD_H |
| 58 | |
| 59 | #ifdef __cplusplus |
| 60 | extern "C" { |
| 61 | #endif |
| 62 | |
| 63 | /*----- Header files ------------------------------------------------------*/ |
| 64 | |
| 65 | #ifndef CATACOMB_GRAND_H |
| 66 | # include "grand.h" |
| 67 | #endif |
| 68 | |
| 69 | #ifndef CATACOMB_MP_H |
| 70 | # include "mp.h" |
| 71 | #endif |
| 72 | |
| 73 | /*----- Data structures ---------------------------------------------------*/ |
| 74 | |
| 75 | typedef struct field { |
| 76 | const struct field_ops *ops; /* Field operations */ |
| 77 | mp *zero, *one; /* Identities in the field */ |
| 78 | } field; |
| 79 | |
| 80 | typedef struct field_ops { |
| 81 | |
| 82 | /* --- Universal operations --- */ |
| 83 | |
| 84 | void (*destroy)(field */*f*/); |
| 85 | mp *(*rand)(field */*f*/, mp */*d*/, grand */*r*/); |
| 86 | |
| 87 | mp *(*in)(field */*f*/, mp */*d*/, mp */*x*/); |
| 88 | mp *(*out)(field */*f*/, mp */*d*/, mp */*x*/); |
| 89 | |
| 90 | int (*zerop)(field */*f*/, mp */*x*/); |
| 91 | mp *(*neg)(field */*f*/, mp */*d*/, mp */*x*/); |
| 92 | mp *(*add)(field */*f*/, mp */*d*/, mp */*x*/, mp */*y*/); |
| 93 | mp *(*sub)(field */*f*/, mp */*d*/, mp */*x*/, mp */*y*/); |
| 94 | mp *(*mul)(field */*f*/, mp */*d*/, mp */*x*/, mp */*y*/); |
| 95 | mp *(*sqr)(field */*f*/, mp */*d*/, mp */*x*/); |
| 96 | mp *(*inv)(field */*f*/, mp */*d*/, mp */*x*/); |
| 97 | mp *(*reduce)(field */*f*/, mp */*d*/, mp */*x*/); |
| 98 | mp *(*sqrt)(field */*f*/, mp */*d*/, mp */*x*/); |
| 99 | |
| 100 | /* --- Operations for binary fields only --- */ |
| 101 | |
| 102 | mp *(*quadsolve)(field */*f*/, mp */*d*/, mp */*x*/); |
| 103 | |
| 104 | /* --- Operations for prime fields only --- */ |
| 105 | |
| 106 | mp *(*dbl)(field */*f*/, mp */*d*/, mp */*x*/); |
| 107 | mp *(*tpl)(field */*f*/, mp */*d*/, mp */*x*/); |
| 108 | mp *(*qdl)(field */*f*/, mp */*d*/, mp */*x*/); |
| 109 | mp *(*hlv)(field */*f*/, mp */*d*/, mp */*x*/); |
| 110 | |
| 111 | } field_ops; |
| 112 | |
| 113 | #define F_DESTROY(f) (f)->ops->destroy((f)) |
| 114 | |
| 115 | #define F_IN(f, d, x) (f)->ops->in((f), (d), (x)) |
| 116 | #define F_OUT(f, d, x) (f)->ops->out((f), (d), (x)) |
| 117 | |
| 118 | #define F_ZEROP(f, x) (f)->ops->zerop((f), (x)) |
| 119 | #define F_NEG(f, d, x) (f)->ops->neg((f), (d), (x)) |
| 120 | #define F_ADD(f, d, x, y) (f)->ops->add((f), (d), (x), (y)) |
| 121 | #define F_SUB(f, d, x, y) (f)->ops->sub((f), (d), (x), (y)) |
| 122 | #define F_MUL(f, d, x, y) (f)->ops->mul((f), (d), (x), (y)) |
| 123 | #define F_SQR(f, d, x) (f)->ops->sqr((f), (d), (x)) |
| 124 | #define F_INV(f, d, x) (f)->ops->inv((f), (d), (x)) |
| 125 | #define F_REDUCE(f, d, x) (f)->ops->reduce((f), (d), (x)) |
| 126 | #define F_SQRT(f, d, x) (f)->ops->sqrt((f), (d), (x)) |
| 127 | |
| 128 | #define F_QUADSOLVE(f, d, x) (f)->ops->quadsolve((f), (d), (x)) |
| 129 | |
| 130 | #define F_DBL(f, d, x) (f)->ops->dbl((f), (d), (x)) |
| 131 | #define F_TPL(f, d, x) (f)->ops->tpl((f), (d), (x)) |
| 132 | #define F_QDL(f, d, x) (f)->ops->qdl((f), (d), (x)) |
| 133 | #define F_HLV(f, d, x) (f)->ops->hlv((f), (d), (x)) |
| 134 | |
| 135 | /*----- Helpful field operations ------------------------------------------*/ |
| 136 | |
| 137 | /* --- @field_id@ --- * |
| 138 | * |
| 139 | * Arguments: @field *f@ = pointer to a field |
| 140 | * @mp *d@ = a destination element |
| 141 | * @mp *x@ = a source element |
| 142 | * |
| 143 | * Returns: The result element. |
| 144 | * |
| 145 | * Use: An identity operation which can be used if your field has no |
| 146 | * internal representation. |
| 147 | */ |
| 148 | |
| 149 | extern mp *field_id(field */*f*/, mp */*d*/, mp */*x*/); |
| 150 | |
| 151 | /*----- Creating fields ---------------------------------------------------*/ |
| 152 | |
| 153 | /* --- @field_prime@ --- * |
| 154 | * |
| 155 | * Arguments: @mp *p@ = the characteristic of the field |
| 156 | * |
| 157 | * Returns: A pointer to the field. |
| 158 | * |
| 159 | * Use: Creates a field structure for a prime field of size %$p$%, |
| 160 | * using Montgomery reduction for arithmetic. |
| 161 | */ |
| 162 | |
| 163 | extern field *field_prime(mp */*p*/); |
| 164 | |
| 165 | /* --- @field_binpoly@ --- * |
| 166 | * |
| 167 | * Arguments: @mp *p@ = an irreducible polynomial over %$\gf{2}$% |
| 168 | * |
| 169 | * Returns: A pointer to the field. |
| 170 | * |
| 171 | * Use: Creates a field structure for a binary field using naive |
| 172 | * arithmetic. |
| 173 | */ |
| 174 | |
| 175 | extern field *field_binpoly(mp */*p*/); |
| 176 | |
| 177 | /*----- That's all, folks -------------------------------------------------*/ |
| 178 | |
| 179 | #ifdef __cplusplus |
| 180 | } |
| 181 | #endif |
| 182 | |
| 183 | #endif |