Fix bit operations. Test them (a bit) better.
[u/mdw/catacomb] / f-prime.c
1 /* -*-c-*-
2 *
3 * $Id: f-prime.c,v 1.2 2002/01/13 13:48:44 mdw Exp $
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 /*----- Revision history --------------------------------------------------*
31 *
32 * $Log: f-prime.c,v $
33 * Revision 1.2 2002/01/13 13:48:44 mdw
34 * Further progress.
35 *
36 * Revision 1.1 2001/04/29 18:12:33 mdw
37 * Prototype version.
38 *
39 */
40
41 /*----- Header files ------------------------------------------------------*/
42
43 #include <mLib/sub.h>
44
45 #include "field.h"
46 #include "mpmont.h"
47
48 /*----- Data structures ---------------------------------------------------*/
49
50 typedef struct fctx {
51 field f;
52 mpmont mm;
53 } fctx;
54
55 /*----- Main code ---------------------------------------------------------*/
56
57 /* --- Field operations --- */
58
59 static void fdestroy(field *ff)
60 {
61 fctx *f = (fctx *)ff;
62 mpmont_destroy(&f->mm);
63 DESTROY(f);
64 }
65
66 static mp *fin(field *ff, mp *d, mp *x)
67 {
68 fctx *f = (fctx *)ff;
69 return (mpmont_mul(&f->mm, d, x, f->mm.r2));
70 }
71
72 static mp *fout(field *ff, mp *d, mp *x)
73 {
74 fctx *f = (fctx *)ff;
75 return (mpmont_reduce(&f->mm, d, x));
76 }
77
78 static mp *fneg(field *ff, mp *d, mp *x)
79 {
80 fctx *f = (fctx *)ff;
81 return (mp_sub(d, f->mm.m, x));
82 }
83
84 static mp *fadd(field *ff, mp *d, mp *x, mp *y)
85 {
86 return (mp_add(d, x, y));
87 }
88
89 static mp *fsub(field *ff, mp *d, mp *x, mp *y)
90 {
91 return (mp_sub(d, x, y));
92 }
93
94 static mp *fmul(field *ff, mp *d, mp *x, mp *y)
95 {
96 fctx *f = (fctx *)ff;
97 return (mpmont_mul(&f->mm, d, x, y));
98 }
99
100 static mp *fsqr(field *ff, mp *d, mp *x)
101 {
102 fctx *f = (fctx *)ff;
103 d = mp_sqr(d, x);
104 return (mpmont_reduce(&f->mm, d, d));
105 }
106
107 static mp *finv(field *ff, mp *d, mp *x)
108 {
109 fctx *f = (fctx *)ff;
110 d = mpmont_reduce(&f->mm, x);
111 mp_gcd(0, 0, &d, f->mm.m, d);
112 return (mpmont_mul(&f->mm, d, d, f->mm.r2));
113 }
114
115 static mp *freduce(field *ff, mp *d, mp *x)
116 {
117 fctx *f = (fctx *)ff;
118 mp_div(0, &d, x, f->mm.m);
119 return (d);
120 }
121
122 static mp *fdbl(field *ff, mp *d, mp *x)
123 {
124 fctx *f = (fctx *)ff;
125 return (mp_lsl(d, x, 1));
126 }
127
128 static mp *ftpl(field *ff, mp *d, mp *x)
129 {
130 fctx *f = (fctx *)ff;
131 MP_DEST(d, MP_LEN(x) + 1, x->f);
132 MPX_UMULN(d->v, d->vl, x->v, x->vl, 3);
133 return (d);
134 }
135
136 static mp *fsqrt(field *ff, mp *d, mp *x)
137 {
138 fctx *f = (fctx *)ff;
139 d = mpmont_reduce(&f->mm, x);
140 d = mp_modsqrt(d, d, f->mm.m);
141 return (mpmont_mul(&f->mm, d, d, f->mm.r2));
142 }
143
144 /* --- Field operations table --- */
145
146 static field_ops fops = {
147 fdestroy,
148 fin, fout,
149 fneg, fadd, fsub, fmul, fsqr, finv, freduce,
150 fdbl, ftpl, fsqrt
151 };
152
153 /* --- @field_prime@ --- *
154 *
155 * Arguments: @mp *p@ = the characteristic of the field
156 *
157 * Returns: A pointer to the field.
158 *
159 * Use: Creates a field structure for a prime field of size %$p$%,
160 * using Montgomery reduction for arithmetic.
161 */
162
163 field *field_prime(mp *p)
164 {
165 ftcx *f = CREATE(fctx);
166 f->f.ops = &fops;
167 mpmont_create(&f->mm, p);
168 f->zero = MP_ZERO;
169 f->one = &f->mm.r;
170 return (&f->f);
171 }
172
173 /*----- That's all, folks -------------------------------------------------*/