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