f46efa79 |
1 | /* -*-c-*- |
2 | * |
4edc47b8 |
3 | * $Id: f-niceprime.c,v 1.4 2004/04/01 21:28:41 mdw Exp $ |
f46efa79 |
4 | * |
5 | * Prime fields with efficient reduction for special-form primes |
6 | * |
7 | * (c) 2004 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-niceprime.c,v $ |
4edc47b8 |
33 | * Revision 1.4 2004/04/01 21:28:41 mdw |
34 | * Normal basis support (translates to poly basis internally). Rewrite |
35 | * EC and prime group table generators in awk, so that they can reuse data |
36 | * for repeated constants. |
37 | * |
34e4f738 |
38 | * Revision 1.3 2004/04/01 12:50:09 mdw |
39 | * Add cyclic group abstraction, with test code. Separate off exponentation |
40 | * functions for better static linking. Fix a buttload of bugs on the way. |
41 | * Generally ensure that negative exponents do inversion correctly. Add |
42 | * table of standard prime-field subgroups. (Binary field subgroups are |
43 | * currently unimplemented but easy to add if anyone ever finds a good one.) |
44 | * |
432c4e18 |
45 | * Revision 1.2 2004/03/27 17:54:11 mdw |
46 | * Standard curves and curve checking. |
47 | * |
f46efa79 |
48 | * Revision 1.1 2004/03/27 00:04:46 mdw |
49 | * Implement efficient reduction for pleasant-looking primes. |
50 | * |
51 | */ |
52 | |
53 | /*----- Header files ------------------------------------------------------*/ |
54 | |
55 | #include <mLib/sub.h> |
56 | |
57 | #include "field.h" |
58 | #include "mpreduce.h" |
59 | #include "mprand.h" |
60 | |
4edc47b8 |
61 | /*----- Main code ---------------------------------------------------------*/ |
f46efa79 |
62 | |
63 | typedef struct fctx { |
64 | field f; |
65 | mpreduce r; |
66 | } fctx; |
67 | |
f46efa79 |
68 | /* --- Field operations --- */ |
69 | |
70 | static void fdestroy(field *ff) |
4edc47b8 |
71 | { fctx *f = (fctx *)ff; mpreduce_destroy(&f->r); DESTROY(f); } |
f46efa79 |
72 | |
73 | static mp *frand(field *ff, mp *d, grand *r) |
4edc47b8 |
74 | { fctx *f = (fctx *)ff; return (mprand_range(d, f->r.p, r, 0)); } |
f46efa79 |
75 | |
4edc47b8 |
76 | static int fzerop(field *ff, mp *x) { return (!MP_LEN(x)); } |
f46efa79 |
77 | |
78 | static mp *fneg(field *ff, mp *d, mp *x) |
4edc47b8 |
79 | { fctx *f = (fctx *)ff; return (mp_sub(d, f->r.p, x)); } |
f46efa79 |
80 | |
4edc47b8 |
81 | static mp *fadd(field *ff, mp *d, mp *x, mp *y) { |
82 | fctx *f = (fctx *)ff; d = mp_add(d, x, y); |
83 | if (d->f & MP_NEG) d = mp_add(d, d, f->r.p); |
84 | else if (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p); |
f46efa79 |
85 | return (d); |
86 | } |
87 | |
4edc47b8 |
88 | static mp *fsub(field *ff, mp *d, mp *x, mp *y) { |
89 | fctx *f = (fctx *)ff; d = mp_sub(d, x, y); |
90 | if (d->f & MP_NEG) d = mp_add(d, d, f->r.p); |
91 | else if (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p); |
f46efa79 |
92 | return (d); |
93 | } |
94 | |
4edc47b8 |
95 | static mp *fmul(field *ff, mp *d, mp *x, mp *y) { |
96 | fctx *f = (fctx *)ff; d = mp_mul(d, x, y); |
f46efa79 |
97 | return (mpreduce_do(&f->r, d, d)); |
98 | } |
99 | |
4edc47b8 |
100 | static mp *fsqr(field *ff, mp *d, mp *x) { |
101 | fctx *f = (fctx *)ff; d = mp_sqr(d, x); |
f46efa79 |
102 | return (mpreduce_do(&f->r, d, d)); |
103 | } |
104 | |
105 | static mp *finv(field *ff, mp *d, mp *x) |
4edc47b8 |
106 | { fctx *f = (fctx *)ff; mp_gcd(0, 0, &d, f->r.p, x); return (d); } |
f46efa79 |
107 | |
108 | static mp *freduce(field *ff, mp *d, mp *x) |
4edc47b8 |
109 | { fctx *f = (fctx *)ff; return (mpreduce_do(&f->r, d, x)); } |
f46efa79 |
110 | |
111 | static mp *fsqrt(field *ff, mp *d, mp *x) |
4edc47b8 |
112 | { fctx *f = (fctx *)ff; return (mp_modsqrt(d, x, f->r.p)); } |
f46efa79 |
113 | |
4edc47b8 |
114 | static mp *fdbl(field *ff, mp *d, mp *x) { |
115 | fctx *f = (fctx *)ff; d = mp_lsl(d, x, 1); |
116 | if (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p); |
f46efa79 |
117 | return (d); |
118 | } |
119 | |
4edc47b8 |
120 | static mp *ftpl(field *ff, mp *d, mp *x) { |
121 | fctx *f = (fctx *)ff; MP_DEST(d, MP_LEN(x) + 1, x->f); |
f46efa79 |
122 | MPX_UMULN(d->v, d->vl, x->v, x->vl, 3); |
4edc47b8 |
123 | while (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p); |
f46efa79 |
124 | return (d); |
125 | } |
126 | |
4edc47b8 |
127 | static mp *fqdl(field *ff, mp *d, mp *x) { |
128 | fctx *f = (fctx *)ff; d = mp_lsl(d, x, 2); |
129 | while (MP_CMP(d, >, f->r.p)) d = mp_sub(d, d, f->r.p); |
f46efa79 |
130 | return (d); |
131 | } |
132 | |
4edc47b8 |
133 | static mp *fhlv(field *ff, mp *d, mp *x) { |
f46efa79 |
134 | fctx *f = (fctx *)ff; |
4edc47b8 |
135 | if (!MP_LEN(x)) { MP_COPY(x); MP_DROP(d); return (x); } |
136 | if (x->v[0] & 1) { d = mp_add(d, x, f->r.p); x = d; } |
f46efa79 |
137 | return (mp_lsr(d, x, 1)); |
138 | } |
139 | |
140 | /* --- Field operations table --- */ |
141 | |
142 | static field_ops fops = { |
143 | FTY_PRIME, "niceprime", |
34e4f738 |
144 | fdestroy, frand, field_stdsamep, |
f46efa79 |
145 | freduce, field_id, |
146 | fzerop, fneg, fadd, fsub, fmul, fsqr, finv, freduce, fsqrt, |
147 | 0, |
148 | fdbl, ftpl, fqdl, fhlv |
149 | }; |
150 | |
151 | /* --- @field_niceprime@ --- * |
152 | * |
153 | * Arguments: @mp *p@ = the characteristic of the field |
154 | * |
155 | * Returns: A pointer to the field. |
156 | * |
157 | * Use: Creates a field structure for a prime field of size %$p$%, |
158 | * using efficient reduction for nice primes. |
159 | */ |
160 | |
161 | field *field_niceprime(mp *p) |
162 | { |
163 | fctx *f = CREATE(fctx); |
164 | f->f.ops = &fops; |
165 | f->f.zero = MP_ZERO; |
166 | f->f.one = MP_ONE; |
432c4e18 |
167 | f->f.nbits = mp_bits(p); |
168 | f->f.noctets = (f->f.nbits + 7) >> 3; |
f46efa79 |
169 | mpreduce_create(&f->r, p); |
432c4e18 |
170 | f->f.m = f->r.p; |
f46efa79 |
171 | return (&f->f); |
172 | } |
173 | |
174 | /*----- That's all, folks -------------------------------------------------*/ |