Use auto-version machinery for building.
[u/mdw/catacomb] / f-prime.c
1 /* -*-c-*-
2 *
3 * $Id$
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 "mprand.h"
36 #include "field-guts.h"
37
38 /*----- Main code ---------------------------------------------------------*/
39
40 /* --- Field operations --- */
41
42 static void fdestroy(field *ff) {
43 fctx_prime *f = (fctx_prime *)ff;
44 mpmont_destroy(&f->mm);
45 DESTROY(f);
46 }
47
48 static mp *frand(field *ff, mp *d, grand *r) {
49 fctx_prime *f = (fctx_prime *)ff;
50 return (mprand_range(d, f->mm.m, r, 0));
51 }
52
53 static mp *fin(field *ff, mp *d, mp *x) {
54 fctx_prime *f = (fctx_prime *)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_prime *f = (fctx_prime *)ff;
61 return (mpmont_reduce(&f->mm, d, x));
62 }
63
64 static int fzerop(field *ff, mp *x) { return (MP_ZEROP(x)); }
65
66 static mp *fneg(field *ff, mp *d, mp *x) {
67 fctx_prime *f = (fctx_prime *)ff;
68 return (mp_sub(d, f->mm.m, x));
69 }
70
71 static mp *fadd(field *ff, mp *d, mp *x, mp *y) {
72 fctx_prime *f = (fctx_prime *)ff; d = mp_add(d, x, y);
73 if (MP_NEGP(d)) d = mp_add(d, d, f->mm.m);
74 else if (MP_CMP(d, >, f->mm.m)) d = mp_sub(d, d, f->mm.m);
75 return (d);
76 }
77
78 static mp *fsub(field *ff, mp *d, mp *x, mp *y) {
79 fctx_prime *f = (fctx_prime *)ff; d = mp_sub(d, x, y);
80 if (MP_NEGP(d)) d = mp_add(d, d, f->mm.m);
81 else if (MP_CMP(d, >, f->mm.m)) d = mp_sub(d, d, f->mm.m);
82 return (d);
83 }
84
85 static mp *fmul(field *ff, mp *d, mp *x, mp *y) {
86 fctx_prime *f = (fctx_prime *)ff;
87 return (mpmont_mul(&f->mm, d, x, y));
88 }
89
90 static mp *fsqr(field *ff, mp *d, mp *x) {
91 fctx_prime *f = (fctx_prime *)ff; d = mp_sqr(d, x);
92 return (mpmont_reduce(&f->mm, d, d));
93 }
94
95 static mp *finv(field *ff, mp *d, mp *x) {
96 fctx_prime *f = (fctx_prime *)ff; d = mpmont_reduce(&f->mm, d, x);
97 d = mp_modinv(d, d, f->mm.m); return (mpmont_mul(&f->mm, d, d, f->mm.r2));
98 }
99
100 static mp *freduce(field *ff, mp *d, mp *x) {
101 fctx_prime *f = (fctx_prime *)ff;
102 mp_div(0, &d, x, f->mm.m);
103 return (d);
104 }
105
106 static mp *fsqrt(field *ff, mp *d, mp *x) {
107 fctx_prime *f = (fctx_prime *)ff; d = mpmont_reduce(&f->mm, d, x);
108 d = mp_modsqrt(d, d, f->mm.m); if (!d) return (d);
109 return (mpmont_mul(&f->mm, d, d, f->mm.r2));
110 }
111
112 static mp *fdbl(field *ff, mp *d, mp *x) {
113 fctx_prime *f = (fctx_prime *)ff; d = mp_lsl(d, x, 1);
114 if (MP_CMP(d, >=, f->mm.m)) d = mp_sub(d, d, f->mm.m);
115 return (d);
116 }
117
118 static mp *ftpl(field *ff, mp *d, mp *x) {
119 fctx_prime *f = (fctx_prime *)ff; MP_DEST(d, MP_LEN(x) + 1, x->f);
120 MPX_UMULN(d->v, d->vl, x->v, x->vl, 3); d->f &= ~MP_UNDEF;
121 while (MP_CMP(d, >=, f->mm.m)) d = mp_sub(d, d, f->mm.m);
122 return (d);
123 }
124
125 static mp *fqdl(field *ff, mp *d, mp *x) {
126 fctx_prime *f = (fctx_prime *)ff; d = mp_lsl(d, x, 2);
127 while (MP_CMP(d, >=, f->mm.m)) d = mp_sub(d, d, f->mm.m);
128 return (d);
129 }
130
131 static mp *fhlv(field *ff, mp *d, mp *x) {
132 fctx_prime *f = (fctx_prime *)ff;
133 if (MP_ZEROP(x)) { MP_COPY(x); MP_DROP(d); return (x); }
134 if (x->v[0] & 1) { d = mp_add(d, x, f->mm.m); x = d; }
135 return (mp_lsr(d, x, 1));
136 }
137
138 /* --- Field operations table --- */
139
140 static const field_ops fops = {
141 FTY_PRIME, "prime",
142 fdestroy, frand, field_stdsamep,
143 fin, fout,
144 fzerop, fneg, fadd, fsub, fmul, fsqr, finv, freduce, fsqrt,
145 0,
146 fdbl, ftpl, fqdl, fhlv
147 };
148
149 /* --- @field_prime@ --- *
150 *
151 * Arguments: @mp *p@ = the characteristic of the field
152 *
153 * Returns: A pointer to the field or null.
154 *
155 * Use: Creates a field structure for a prime field of size %$p$%,
156 * using Montgomery reduction for arithmetic.
157 */
158
159 field *field_prime(mp *p)
160 {
161 fctx_prime *f;
162
163 f = CREATE(fctx_prime);
164 f->f.ops = &fops;
165 if (mpmont_create(&f->mm, p)) {
166 DESTROY(f);
167 return (0);
168 }
169 f->f.zero = MP_ZERO;
170 f->f.one = f->mm.r;
171 f->f.m = f->mm.m;
172 f->f.nbits = mp_bits(p);
173 f->f.noctets = (f->f.nbits + 7) >> 3;
174 f->f.q = f->mm.m;
175 return (&f->f);
176 }
177
178 /*----- That's all, folks -------------------------------------------------*/