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