ceb3f0c0 |
1 | /* -*-c-*- |
2 | * |
bc985cef |
3 | * $Id: f-binpoly.c,v 1.4 2004/03/23 15:19:32 mdw Exp $ |
ceb3f0c0 |
4 | * |
5 | * Binary fields with polynomial basis representation |
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-binpoly.c,v $ |
bc985cef |
33 | * Revision 1.4 2004/03/23 15:19:32 mdw |
34 | * Test elliptic curves more thoroughly. |
35 | * |
9b8b6877 |
36 | * Revision 1.3 2004/03/23 12:08:26 mdw |
37 | * Random field-element selection. |
38 | * |
c3caa2fa |
39 | * Revision 1.2 2004/03/21 22:52:06 mdw |
40 | * Merge and close elliptic curve branch. |
41 | * |
ceb3f0c0 |
42 | * Revision 1.1.2.1 2004/03/21 22:39:46 mdw |
43 | * Elliptic curves on binary fields work. |
44 | * |
45 | */ |
46 | |
47 | /*----- Header files ------------------------------------------------------*/ |
48 | |
49 | #include <mLib/sub.h> |
50 | |
51 | #include "field.h" |
52 | #include "gf.h" |
53 | #include "gfreduce.h" |
9b8b6877 |
54 | #include "mprand.h" |
ceb3f0c0 |
55 | |
56 | /*----- Data structures ---------------------------------------------------*/ |
57 | |
58 | typedef struct fctx { |
59 | field f; |
60 | gfreduce r; |
61 | } fctx; |
62 | |
63 | /*----- Main code ---------------------------------------------------------*/ |
64 | |
65 | /* --- Field operations --- */ |
66 | |
67 | static void fdestroy(field *ff) |
68 | { |
69 | fctx *f = (fctx *)ff; |
70 | gfreduce_destroy(&f->r); |
71 | DESTROY(f); |
72 | } |
73 | |
bc985cef |
74 | static mp *frand(field *ff, mp *d, grand *r) |
9b8b6877 |
75 | { |
76 | fctx *f = (fctx *)ff; |
77 | return (mprand(d, mp_octets(f->r.p) - 1, r, 0)); |
78 | } |
79 | |
ceb3f0c0 |
80 | static int fzerop(field *ff, mp *x) |
81 | { |
82 | return (!MP_LEN(x)); |
83 | } |
84 | |
85 | static mp *fadd(field *ff, mp *d, mp *x, mp *y) |
86 | { |
87 | return (gf_add(d, x, y)); |
88 | } |
89 | |
90 | static mp *fmul(field *ff, mp *d, mp *x, mp *y) |
91 | { |
92 | fctx *f = (fctx *)ff; |
93 | d = gf_mul(d, x, y); |
94 | return (gfreduce_do(&f->r, d, d)); |
95 | } |
96 | |
97 | static mp *fsqr(field *ff, mp *d, mp *x) |
98 | { |
99 | fctx *f = (fctx *)ff; |
100 | d = gf_sqr(d, x); |
101 | return (gfreduce_do(&f->r, d, d)); |
102 | } |
103 | |
104 | static mp *finv(field *ff, mp *d, mp *x) |
105 | { |
106 | fctx *f = (fctx *)ff; |
107 | gf_gcd(0, 0, &d, f->r.p, x); |
108 | return (d); |
109 | } |
110 | |
111 | static mp *freduce(field *ff, mp *d, mp *x) |
112 | { |
113 | fctx *f = (fctx *)ff; |
114 | return (gfreduce_do(&f->r, d, x)); |
115 | } |
116 | |
117 | static mp *fsqrt(field *ff, mp *d, mp *x) |
118 | { |
119 | fctx *f = (fctx *)ff; |
120 | return (gfreduce_sqrt(&f->r, d, x)); |
121 | } |
122 | |
123 | static mp *fquadsolve(field *ff, mp *d, mp *x) |
124 | { |
125 | fctx *f = (fctx *)ff; |
126 | return (gfreduce_quadsolve(&f->r, d, x)); |
127 | } |
128 | |
129 | /* --- Field operations table --- */ |
130 | |
131 | static field_ops fops = { |
bc985cef |
132 | FTY_BINARY, "binpoly", |
9b8b6877 |
133 | fdestroy, frand, |
ceb3f0c0 |
134 | freduce, field_id, |
135 | fzerop, field_id, fadd, fadd, fmul, fsqr, finv, freduce, fsqrt, |
136 | fquadsolve, |
137 | 0, 0, 0, 0 |
138 | }; |
139 | |
140 | /* --- @field_binpoly@ --- * |
141 | * |
142 | * Arguments: @mp *p@ = the reduction polynomial |
143 | * |
144 | * Returns: A pointer to the field. |
145 | * |
146 | * Use: Creates a field structure for a binary field mod @p@. |
147 | */ |
148 | |
149 | field *field_binpoly(mp *p) |
150 | { |
151 | fctx *f = CREATE(fctx); |
152 | f->f.ops = &fops; |
153 | f->f.zero = MP_ZERO; |
154 | f->f.one = MP_ONE; |
155 | gfreduce_create(&f->r, p); |
156 | return (&f->f); |
157 | } |
158 | |
159 | /*----- That's all, folks -------------------------------------------------*/ |