math/gfreduce.[ch]: Fix out-of-bounds memory access.
[u/mdw/catacomb] / math / f-niceprime.c
1 /* -*-c-*-
2 *
3 * Prime fields with efficient reduction for special-form primes
4 *
5 * (c) 2004 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of Catacomb.
11 *
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
16 *
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
21 *
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
26 */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <mLib/sub.h>
31
32 #include "field.h"
33 #include "field-guts.h"
34 #include "mprand.h"
35
36 /*----- Main code ---------------------------------------------------------*/
37
38 /* --- Field operations --- */
39
40 static void fdestroy(field *ff) {
41 fctx_niceprime *f = (fctx_niceprime *)ff;
42 mpreduce_destroy(&f->r);
43 DESTROY(f);
44 }
45
46 static mp *frand(field *ff, mp *d, grand *r) {
47 fctx_niceprime *f = (fctx_niceprime *)ff;
48 return (mprand_range(d, f->r.p, r, 0));
49 }
50
51 static int fzerop(field *ff, mp *x) { return (MP_ZEROP(x)); }
52
53 static mp *fneg(field *ff, mp *d, mp *x) {
54 fctx_niceprime *f = (fctx_niceprime *)ff;
55 return (mp_sub(d, f->r.p, x));
56 }
57
58 static mp *fadd(field *ff, mp *d, mp *x, mp *y) {
59 fctx_niceprime *f = (fctx_niceprime *)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_niceprime *f = (fctx_niceprime *)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_niceprime *f = (fctx_niceprime *)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_niceprime *f = (fctx_niceprime *)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_niceprime *f = (fctx_niceprime *)ff;
84 d = mp_modinv(d, x, f->r.p);
85 return (d);
86 }
87
88 static mp *freduce(field *ff, mp *d, mp *x) {
89 fctx_niceprime *f = (fctx_niceprime *)ff;
90 return (mpreduce_do(&f->r, d, x));
91 }
92
93 static mp *fsqrt(field *ff, mp *d, mp *x) {
94 fctx_niceprime *f = (fctx_niceprime *)ff;
95 return (mp_modsqrt(d, x, f->r.p));
96 }
97
98 static mp *fdbl(field *ff, mp *d, mp *x) {
99 fctx_niceprime *f = (fctx_niceprime *)ff; d = mp_lsl(d, x, 1);
100 if (MP_CMP(d, >=, f->r.p)) d = mp_sub(d, d, f->r.p);
101 return (d);
102 }
103
104 static mp *ftpl(field *ff, mp *d, mp *x) {
105 fctx_niceprime *f = (fctx_niceprime *)ff; MP_DEST(d, MP_LEN(x) + 1, x->f);
106 MPX_UMULN(d->v, d->vl, x->v, x->vl, 3); d->f &= ~MP_UNDEF;
107 while (MP_CMP(d, >=, f->r.p)) d = mp_sub(d, d, f->r.p);
108 return (d);
109 }
110
111 static mp *fqdl(field *ff, mp *d, mp *x) {
112 fctx_niceprime *f = (fctx_niceprime *)ff; d = mp_lsl(d, x, 2);
113 while (MP_CMP(d, >=, f->r.p)) d = mp_sub(d, d, f->r.p);
114 return (d);
115 }
116
117 static mp *fhlv(field *ff, mp *d, mp *x) {
118 fctx_niceprime *f = (fctx_niceprime *)ff;
119 if (MP_ZEROP(x)) { MP_COPY(x); MP_DROP(d); return (x); }
120 if (x->v[0] & 1) { d = mp_add(d, x, f->r.p); x = d; }
121 return (mp_lsr(d, x, 1));
122 }
123
124 /* --- Field operations table --- */
125
126 static const field_ops fops = {
127 FTY_PRIME, "niceprime",
128 fdestroy, frand, field_stdsamep,
129 freduce, field_id,
130 fzerop, fneg, fadd, fsub, fmul, fsqr, finv, freduce, fsqrt,
131 0,
132 fdbl, ftpl, fqdl, fhlv
133 };
134
135 /* --- @field_niceprime@ --- *
136 *
137 * Arguments: @mp *p@ = the characteristic of the field
138 *
139 * Returns: A pointer to the field, or null.
140 *
141 * Use: Creates a field structure for a prime field of size %$p$%,
142 * using efficient reduction for nice primes.
143 */
144
145 field *field_niceprime(mp *p)
146 {
147 fctx_niceprime *f = CREATE(fctx_niceprime);
148 f->f.ops = &fops;
149 f->f.zero = MP_ZERO;
150 f->f.one = MP_ONE;
151 f->f.nbits = mp_bits(p);
152 f->f.noctets = (f->f.nbits + 7) >> 3;
153 if (mpreduce_create(&f->r, p)) {
154 DESTROY(f);
155 return (0);
156 }
157 f->f.m = f->r.p;
158 f->f.q = f->r.p;
159 return (&f->f);
160 }
161
162 /*----- That's all, folks -------------------------------------------------*/