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