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