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