General utilities cleanup. Add signature support to catcrypt. Throw in
[u/mdw/catacomb] / f-prime.c
CommitLineData
b0ab12e6 1/* -*-c-*-
2 *
b817bfc6 3 * $Id: f-prime.c,v 1.12 2004/04/08 01:36:15 mdw Exp $
b0ab12e6 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
b0ab12e6 30/*----- Header files ------------------------------------------------------*/
31
32#include <mLib/sub.h>
33
34#include "field.h"
35#include "mpmont.h"
9b8b6877 36#include "mprand.h"
b0ab12e6 37
4edc47b8 38/*----- Main code ---------------------------------------------------------*/
b0ab12e6 39
40typedef struct fctx {
41 field f;
42 mpmont mm;
43} fctx;
44
b0ab12e6 45/* --- Field operations --- */
46
47static void fdestroy(field *ff)
4edc47b8 48 { fctx *f = (fctx *)ff; mpmont_destroy(&f->mm); DESTROY(f); }
b0ab12e6 49
9b8b6877 50static mp *frand(field *ff, mp *d, grand *r)
4edc47b8 51 { fctx *f = (fctx *)ff; return (mprand_range(d, f->mm.m, r, 0)); }
9b8b6877 52
4edc47b8 53static mp *fin(field *ff, mp *d, mp *x) {
b0ab12e6 54 fctx *f = (fctx *)ff;
dbfee00a 55 mp_div(0, &d, x, f->mm.m);
56 return (mpmont_mul(&f->mm, d, d, f->mm.r2));
b0ab12e6 57}
58
59static mp *fout(field *ff, mp *d, mp *x)
4edc47b8 60 { fctx *f = (fctx *)ff; return (mpmont_reduce(&f->mm, d, x)); }
b0ab12e6 61
4edc47b8 62static int fzerop(field *ff, mp *x) { return (!MP_LEN(x)); }
8823192f 63
b085fd91 64static mp *fneg(field *ff, mp *d, mp *x)
4edc47b8 65 { fctx *f = (fctx *)ff; return (mp_sub(d, f->mm.m, x)); }
b085fd91 66
4edc47b8 67static mp *fadd(field *ff, mp *d, mp *x, mp *y) {
68 fctx *f = (fctx *)ff; d = mp_add(d, x, y);
69 if (d->f & MP_NEG) d = mp_add(d, d, f->mm.m);
70 else if (MP_CMP(d, >, f->mm.m)) d = mp_sub(d, d, f->mm.m);
dbfee00a 71 return (d);
b0ab12e6 72}
73
4edc47b8 74static mp *fsub(field *ff, mp *d, mp *x, mp *y) {
75 fctx *f = (fctx *)ff; d = mp_sub(d, x, y);
76 if (d->f & MP_NEG) d = mp_add(d, d, f->mm.m);
77 else if (MP_CMP(d, >, f->mm.m)) d = mp_sub(d, d, f->mm.m);
dbfee00a 78 return (d);
b0ab12e6 79}
80
81static mp *fmul(field *ff, mp *d, mp *x, mp *y)
4edc47b8 82 { fctx *f = (fctx *)ff; return (mpmont_mul(&f->mm, d, x, y)); }
b0ab12e6 83
4edc47b8 84static mp *fsqr(field *ff, mp *d, mp *x) {
85 fctx *f = (fctx *)ff; d = mp_sqr(d, x);
b0ab12e6 86 return (mpmont_reduce(&f->mm, d, d));
87}
88
4edc47b8 89static mp *finv(field *ff, mp *d, mp *x) {
90 fctx *f = (fctx *)ff; d = mpmont_reduce(&f->mm, d, x);
b817bfc6 91 d = mp_modinv(d, d, f->mm.m); return (mpmont_mul(&f->mm, d, d, f->mm.r2));
b0ab12e6 92}
93
94static mp *freduce(field *ff, mp *d, mp *x)
4edc47b8 95 { fctx *f = (fctx *)ff; mp_div(0, &d, x, f->mm.m); return (d); }
b0ab12e6 96
4edc47b8 97static mp *fsqrt(field *ff, mp *d, mp *x) {
98 fctx *f = (fctx *)ff; d = mpmont_reduce(&f->mm, d, x);
99 d = mp_modsqrt(d, d, f->mm.m); if (!d) return (d);
ceb3f0c0 100 return (mpmont_mul(&f->mm, d, d, f->mm.r2));
101}
102
4edc47b8 103static mp *fdbl(field *ff, mp *d, mp *x) {
104 fctx *f = (fctx *)ff; d = mp_lsl(d, x, 1);
105 if (MP_CMP(d, >, f->mm.m)) d = mp_sub(d, d, f->mm.m);
dbfee00a 106 return (d);
b0ab12e6 107}
108
4edc47b8 109static mp *ftpl(field *ff, mp *d, mp *x) {
110 fctx *f = (fctx *)ff; MP_DEST(d, MP_LEN(x) + 1, x->f);
b0ab12e6 111 MPX_UMULN(d->v, d->vl, x->v, x->vl, 3);
4edc47b8 112 while (MP_CMP(d, >, f->mm.m)) d = mp_sub(d, d, f->mm.m);
b0ab12e6 113 return (d);
114}
115
4edc47b8 116static mp *fqdl(field *ff, mp *d, mp *x) {
117 fctx *f = (fctx *)ff; d = mp_lsl(d, x, 2);
118 while (MP_CMP(d, >, f->mm.m)) d = mp_sub(d, d, f->mm.m);
8823192f 119 return (d);
120}
121
4edc47b8 122static mp *fhlv(field *ff, mp *d, mp *x) {
8823192f 123 fctx *f = (fctx *)ff;
4edc47b8 124 if (!MP_LEN(x)) { MP_COPY(x); MP_DROP(d); return (x); }
125 if (x->v[0] & 1) { d = mp_add(d, x, f->mm.m); x = d; }
8823192f 126 return (mp_lsr(d, x, 1));
b0ab12e6 127}
128
129/* --- Field operations table --- */
130
4e66da02 131static const field_ops fops = {
bc985cef 132 FTY_PRIME, "prime",
34e4f738 133 fdestroy, frand, field_stdsamep,
b0ab12e6 134 fin, fout,
ceb3f0c0 135 fzerop, fneg, fadd, fsub, fmul, fsqr, finv, freduce, fsqrt,
136 0,
137 fdbl, ftpl, fqdl, fhlv
b0ab12e6 138};
139
140/* --- @field_prime@ --- *
141 *
142 * Arguments: @mp *p@ = the characteristic of the field
143 *
02d7884d 144 * Returns: A pointer to the field or null.
b0ab12e6 145 *
146 * Use: Creates a field structure for a prime field of size %$p$%,
147 * using Montgomery reduction for arithmetic.
148 */
149
150field *field_prime(mp *p)
151{
02d7884d 152 fctx *f;
153
154 if (!MP_ISPOS(p) || !MP_ISODD(p))
155 return (0);
156 f = CREATE(fctx);
b0ab12e6 157 f->f.ops = &fops;
158 mpmont_create(&f->mm, p);
41cb1beb 159 f->f.zero = MP_ZERO;
160 f->f.one = f->mm.r;
432c4e18 161 f->f.m = f->mm.m;
162 f->f.nbits = mp_bits(p);
163 f->f.noctets = (f->f.nbits + 7) >> 3;
b0ab12e6 164 return (&f->f);
165}
166
167/*----- That's all, folks -------------------------------------------------*/