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