Use official names for ANSI curves. Add (names for) the X9.63 curves.
[u/mdw/catacomb] / f-niceprime.c
1 /* -*-c-*-
2 *
3 * $Id$
4 *
5 * Prime fields with efficient reduction for special-form primes
6 *
7 * (c) 2004 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 "mpreduce.h"
36 #include "mprand.h"
37
38 /*----- Main code ---------------------------------------------------------*/
39
40 typedef struct fctx {
41 field f;
42 mpreduce r;
43 } fctx;
44
45 /* --- Field operations --- */
46
47 static void fdestroy(field *ff)
48 { fctx *f = (fctx *)ff; mpreduce_destroy(&f->r); DESTROY(f); }
49
50 static mp *frand(field *ff, mp *d, grand *r)
51 { fctx *f = (fctx *)ff; return (mprand_range(d, f->r.p, r, 0)); }
52
53 static int fzerop(field *ff, mp *x) { return (MP_ZEROP(x)); }
54
55 static mp *fneg(field *ff, mp *d, mp *x)
56 { fctx *f = (fctx *)ff; return (mp_sub(d, f->r.p, x)); }
57
58 static mp *fadd(field *ff, mp *d, mp *x, mp *y) {
59 fctx *f = (fctx *)ff; d = mp_add(d, x, y);
60 if (MP_NEGP(d)) d = mp_add(d, d, f->r.p);
61 else if (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p);
62 return (d);
63 }
64
65 static mp *fsub(field *ff, mp *d, mp *x, mp *y) {
66 fctx *f = (fctx *)ff; d = mp_sub(d, x, y);
67 if (MP_NEGP(d)) d = mp_add(d, d, f->r.p);
68 else if (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p);
69 return (d);
70 }
71
72 static mp *fmul(field *ff, mp *d, mp *x, mp *y) {
73 fctx *f = (fctx *)ff; d = mp_mul(d, x, y);
74 return (mpreduce_do(&f->r, d, d));
75 }
76
77 static mp *fsqr(field *ff, mp *d, mp *x) {
78 fctx *f = (fctx *)ff; d = mp_sqr(d, x);
79 return (mpreduce_do(&f->r, d, d));
80 }
81
82 static mp *finv(field *ff, mp *d, mp *x)
83 { fctx *f = (fctx *)ff; d = mp_modinv(d, x, f->r.p); return (d); }
84
85 static mp *freduce(field *ff, mp *d, mp *x)
86 { fctx *f = (fctx *)ff; return (mpreduce_do(&f->r, d, x)); }
87
88 static mp *fsqrt(field *ff, mp *d, mp *x)
89 { fctx *f = (fctx *)ff; return (mp_modsqrt(d, x, f->r.p)); }
90
91 static mp *fdbl(field *ff, mp *d, mp *x) {
92 fctx *f = (fctx *)ff; d = mp_lsl(d, x, 1);
93 if (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p);
94 return (d);
95 }
96
97 static mp *ftpl(field *ff, mp *d, mp *x) {
98 fctx *f = (fctx *)ff; MP_DEST(d, MP_LEN(x) + 1, x->f);
99 MPX_UMULN(d->v, d->vl, x->v, x->vl, 3);
100 while (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p);
101 return (d);
102 }
103
104 static mp *fqdl(field *ff, mp *d, mp *x) {
105 fctx *f = (fctx *)ff; d = mp_lsl(d, x, 2);
106 while (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p);
107 return (d);
108 }
109
110 static mp *fhlv(field *ff, mp *d, mp *x) {
111 fctx *f = (fctx *)ff;
112 if (MP_ZEROP(x)) { MP_COPY(x); MP_DROP(d); return (x); }
113 if (x->v[0] & 1) { d = mp_add(d, x, f->r.p); x = d; }
114 return (mp_lsr(d, x, 1));
115 }
116
117 /* --- Field operations table --- */
118
119 static const field_ops fops = {
120 FTY_PRIME, "niceprime",
121 fdestroy, frand, field_stdsamep,
122 freduce, field_id,
123 fzerop, fneg, fadd, fsub, fmul, fsqr, finv, freduce, fsqrt,
124 0,
125 fdbl, ftpl, fqdl, fhlv
126 };
127
128 /* --- @field_niceprime@ --- *
129 *
130 * Arguments: @mp *p@ = the characteristic of the field
131 *
132 * Returns: A pointer to the field.
133 *
134 * Use: Creates a field structure for a prime field of size %$p$%,
135 * using efficient reduction for nice primes.
136 */
137
138 field *field_niceprime(mp *p)
139 {
140 fctx *f = CREATE(fctx);
141 f->f.ops = &fops;
142 f->f.zero = MP_ZERO;
143 f->f.one = MP_ONE;
144 f->f.nbits = mp_bits(p);
145 f->f.noctets = (f->f.nbits + 7) >> 3;
146 mpreduce_create(&f->r, p);
147 f->f.m = f->r.p;
148 return (&f->f);
149 }
150
151 /*----- That's all, folks -------------------------------------------------*/