| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: f-prime.c,v 1.3.4.3 2004/03/21 22:39:46 mdw Exp $ |
| 4 | * |
| 5 | * Prime fields with Montgomery arithmetic |
| 6 | * |
| 7 | * (c) 2001 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-prime.c,v $ |
| 33 | * Revision 1.3.4.3 2004/03/21 22:39:46 mdw |
| 34 | * Elliptic curves on binary fields work. |
| 35 | * |
| 36 | * Revision 1.3.4.2 2004/03/20 00:13:31 mdw |
| 37 | * Projective coordinates for prime curves |
| 38 | * |
| 39 | * Revision 1.3.4.1 2003/06/10 13:43:53 mdw |
| 40 | * Simple (non-projective) curves over prime fields now seem to work. |
| 41 | * |
| 42 | * Revision 1.3 2003/05/15 23:25:59 mdw |
| 43 | * Make elliptic curve stuff build. |
| 44 | * |
| 45 | * Revision 1.2 2002/01/13 13:48:44 mdw |
| 46 | * Further progress. |
| 47 | * |
| 48 | * Revision 1.1 2001/04/29 18:12:33 mdw |
| 49 | * Prototype version. |
| 50 | * |
| 51 | */ |
| 52 | |
| 53 | /*----- Header files ------------------------------------------------------*/ |
| 54 | |
| 55 | #include <mLib/sub.h> |
| 56 | |
| 57 | #include "field.h" |
| 58 | #include "mpmont.h" |
| 59 | |
| 60 | /*----- Data structures ---------------------------------------------------*/ |
| 61 | |
| 62 | typedef struct fctx { |
| 63 | field f; |
| 64 | mpmont mm; |
| 65 | } fctx; |
| 66 | |
| 67 | /*----- Main code ---------------------------------------------------------*/ |
| 68 | |
| 69 | /* --- Field operations --- */ |
| 70 | |
| 71 | static void fdestroy(field *ff) |
| 72 | { |
| 73 | fctx *f = (fctx *)ff; |
| 74 | mpmont_destroy(&f->mm); |
| 75 | DESTROY(f); |
| 76 | } |
| 77 | |
| 78 | static mp *fin(field *ff, mp *d, mp *x) |
| 79 | { |
| 80 | fctx *f = (fctx *)ff; |
| 81 | mp_div(0, &d, x, f->mm.m); |
| 82 | return (mpmont_mul(&f->mm, d, d, f->mm.r2)); |
| 83 | } |
| 84 | |
| 85 | static mp *fout(field *ff, mp *d, mp *x) |
| 86 | { |
| 87 | fctx *f = (fctx *)ff; |
| 88 | return (mpmont_reduce(&f->mm, d, x)); |
| 89 | } |
| 90 | |
| 91 | static int fzerop(field *ff, mp *x) |
| 92 | { |
| 93 | return (!MP_LEN(x)); |
| 94 | } |
| 95 | |
| 96 | static mp *fneg(field *ff, mp *d, mp *x) |
| 97 | { |
| 98 | fctx *f = (fctx *)ff; |
| 99 | return (mp_sub(d, f->mm.m, x)); |
| 100 | } |
| 101 | |
| 102 | static mp *fadd(field *ff, mp *d, mp *x, mp *y) |
| 103 | { |
| 104 | fctx *f = (fctx *)ff; |
| 105 | d = mp_add(d, x, y); |
| 106 | if (d->f & MP_NEG) |
| 107 | d = mp_add(d, d, f->mm.m); |
| 108 | else if (MP_CMP(d, >, f->mm.m)) |
| 109 | d = mp_sub(d, d, f->mm.m); |
| 110 | return (d); |
| 111 | } |
| 112 | |
| 113 | static mp *fsub(field *ff, mp *d, mp *x, mp *y) |
| 114 | { |
| 115 | fctx *f = (fctx *)ff; |
| 116 | d = mp_sub(d, x, y); |
| 117 | if (d->f & MP_NEG) |
| 118 | d = mp_add(d, d, f->mm.m); |
| 119 | else if (MP_CMP(d, >, f->mm.m)) |
| 120 | d = mp_sub(d, d, f->mm.m); |
| 121 | return (d); |
| 122 | } |
| 123 | |
| 124 | static mp *fmul(field *ff, mp *d, mp *x, mp *y) |
| 125 | { |
| 126 | fctx *f = (fctx *)ff; |
| 127 | return (mpmont_mul(&f->mm, d, x, y)); |
| 128 | } |
| 129 | |
| 130 | static mp *fsqr(field *ff, mp *d, mp *x) |
| 131 | { |
| 132 | fctx *f = (fctx *)ff; |
| 133 | d = mp_sqr(d, x); |
| 134 | return (mpmont_reduce(&f->mm, d, d)); |
| 135 | } |
| 136 | |
| 137 | static mp *finv(field *ff, mp *d, mp *x) |
| 138 | { |
| 139 | fctx *f = (fctx *)ff; |
| 140 | d = mpmont_reduce(&f->mm, d, x); |
| 141 | mp_gcd(0, 0, &d, f->mm.m, d); |
| 142 | return (mpmont_mul(&f->mm, d, d, f->mm.r2)); |
| 143 | } |
| 144 | |
| 145 | static mp *freduce(field *ff, mp *d, mp *x) |
| 146 | { |
| 147 | fctx *f = (fctx *)ff; |
| 148 | mp_div(0, &d, x, f->mm.m); |
| 149 | return (d); |
| 150 | } |
| 151 | |
| 152 | static mp *fsqrt(field *ff, mp *d, mp *x) |
| 153 | { |
| 154 | fctx *f = (fctx *)ff; |
| 155 | d = mpmont_reduce(&f->mm, d, x); |
| 156 | d = mp_modsqrt(d, d, f->mm.m); |
| 157 | if (!d) |
| 158 | return (d); |
| 159 | return (mpmont_mul(&f->mm, d, d, f->mm.r2)); |
| 160 | } |
| 161 | |
| 162 | static mp *fdbl(field *ff, mp *d, mp *x) |
| 163 | { |
| 164 | fctx *f = (fctx *)ff; |
| 165 | d = mp_lsl(d, x, 1); |
| 166 | if (MP_CMP(d, >, f->mm.m)) |
| 167 | d = mp_sub(d, d, f->mm.m); |
| 168 | return (d); |
| 169 | } |
| 170 | |
| 171 | static mp *ftpl(field *ff, mp *d, mp *x) |
| 172 | { |
| 173 | fctx *f = (fctx *)ff; |
| 174 | MP_DEST(d, MP_LEN(x) + 1, x->f); |
| 175 | MPX_UMULN(d->v, d->vl, x->v, x->vl, 3); |
| 176 | while (MP_CMP(d, >, f->mm.m)) |
| 177 | d = mp_sub(d, d, f->mm.m); |
| 178 | return (d); |
| 179 | } |
| 180 | |
| 181 | static mp *fqdl(field *ff, mp *d, mp *x) |
| 182 | { |
| 183 | fctx *f = (fctx *)ff; |
| 184 | d = mp_lsl(d, x, 2); |
| 185 | while (MP_CMP(d, >, f->mm.m)) |
| 186 | d = mp_sub(d, d, f->mm.m); |
| 187 | return (d); |
| 188 | } |
| 189 | |
| 190 | static mp *fhlv(field *ff, mp *d, mp *x) |
| 191 | { |
| 192 | fctx *f = (fctx *)ff; |
| 193 | if (!MP_LEN(x)) { |
| 194 | MP_COPY(x); |
| 195 | MP_DROP(d); |
| 196 | return (x); |
| 197 | } |
| 198 | if (x->v[0] & 1) { |
| 199 | d = mp_add(d, x, f->mm.m); |
| 200 | x = d; |
| 201 | } |
| 202 | return (mp_lsr(d, x, 1)); |
| 203 | } |
| 204 | |
| 205 | /* --- Field operations table --- */ |
| 206 | |
| 207 | static field_ops fops = { |
| 208 | fdestroy, |
| 209 | fin, fout, |
| 210 | fzerop, fneg, fadd, fsub, fmul, fsqr, finv, freduce, fsqrt, |
| 211 | 0, |
| 212 | fdbl, ftpl, fqdl, fhlv |
| 213 | }; |
| 214 | |
| 215 | /* --- @field_prime@ --- * |
| 216 | * |
| 217 | * Arguments: @mp *p@ = the characteristic of the field |
| 218 | * |
| 219 | * Returns: A pointer to the field. |
| 220 | * |
| 221 | * Use: Creates a field structure for a prime field of size %$p$%, |
| 222 | * using Montgomery reduction for arithmetic. |
| 223 | */ |
| 224 | |
| 225 | field *field_prime(mp *p) |
| 226 | { |
| 227 | fctx *f = CREATE(fctx); |
| 228 | f->f.ops = &fops; |
| 229 | mpmont_create(&f->mm, p); |
| 230 | f->f.zero = MP_ZERO; |
| 231 | f->f.one = f->mm.r; |
| 232 | return (&f->f); |
| 233 | } |
| 234 | |
| 235 | /*----- That's all, folks -------------------------------------------------*/ |