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