34e4f738 |
1 | /* -*-c-*- |
2 | * |
b817bfc6 |
3 | * $Id: g-prime.c,v 1.4 2004/04/08 01:36:15 mdw Exp $ |
34e4f738 |
4 | * |
5 | * Abstraction for prime groups |
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 | |
34e4f738 |
30 | /*----- Header files ------------------------------------------------------*/ |
31 | |
32 | #include <mLib/sub.h> |
33 | |
34 | #include "mpmont.h" |
35 | #include "pgen.h" |
36 | |
37 | #define ge mp * |
38 | #include "group.h" |
39 | |
40 | /*----- Data structures ---------------------------------------------------*/ |
41 | |
42 | typedef struct gctx { |
43 | group g; |
44 | mp *gen; |
45 | mpmont mm; |
46 | } gctx; |
47 | |
48 | /*----- Main code ---------------------------------------------------------*/ |
49 | |
50 | /* --- Group operations --- */ |
51 | |
52 | static void gdestroygroup(group *gg) { |
53 | gctx *g = (gctx *)gg; |
54 | mp_drop(g->gen); mp_drop(g->g.r); mp_drop(g->g.h); |
55 | mpmont_destroy(&g->mm); |
56 | DESTROY(g); |
57 | } |
58 | |
59 | static mp **gcreate(group *gg) |
60 | { mp **x = CREATE(mp *); *x = MP_COPY(*gg->i); return (x); } |
61 | |
62 | static void gcopy(group *gg, mp **d, mp **x) |
63 | { mp *t = MP_COPY(*x); MP_DROP(*d); *d = t; } |
64 | |
65 | static void gburn(group *gg, mp **x) { (*x)->f |= MP_BURN; } |
66 | |
67 | static void gdestroy(group *gg, mp **x) { MP_DROP(*x); DESTROY(x); } |
68 | |
02d7884d |
69 | static int gsamep(group *gg, group *hh) { |
70 | gctx *g = (gctx *)gg, *h = (gctx *)hh; |
71 | return (MP_EQ(g->mm.m, h->mm.m)); |
72 | } |
34e4f738 |
73 | |
74 | static int geq(group *gg, mp **x, mp **y) { return (MP_EQ(*x, *y)); } |
75 | |
76 | static const char *gcheck(group *gg, grand *gr) { |
77 | gctx *g = (gctx *)gg; int rc; mp *t; |
78 | if (!pgen_primep(g->mm.m, gr)) return ("p is not prime"); |
79 | t = mp_mul(MP_NEW, g->g.r, g->g.h); t = mp_add(t, t, MP_ONE); |
80 | rc = MP_EQ(t, g->mm.m); MP_DROP(t); if (!rc) return ("not a subgroup"); |
81 | return (group_stdcheck(gg, gr)); |
82 | } |
83 | |
84 | static void gmul(group *gg, mp **d, mp **x, mp **y) |
85 | { gctx *g = (gctx *)gg; *d = mpmont_mul(&g->mm, *d, *x, *y); } |
86 | |
87 | static void gsqr(group *gg, mp **d, mp **x) { |
88 | gctx *g = (gctx *)gg; mp *r = mp_sqr(*d, *x); |
89 | *d = mpmont_reduce(&g->mm, r, r); |
90 | } |
91 | |
92 | static void ginv(group *gg, mp **d, mp **x) { |
93 | gctx *g = (gctx *)gg; mp *r = mpmont_reduce(&g->mm, *d, *x); |
b817bfc6 |
94 | r = mp_modinv(r, r, g->mm.m); *d = mpmont_mul(&g->mm, r, r, g->mm.r2); |
34e4f738 |
95 | } |
96 | |
97 | static void gexp(group *gg, mp **d, mp **x, mp *n) |
98 | { gctx *g = (gctx *)gg; *d = mpmont_expr(&g->mm, *d, *x, n); } |
99 | |
100 | static void gmexp(group *gg, mp **d, const group_expfactor *f, size_t n) { |
101 | gctx *g = (gctx *)gg; size_t i; |
102 | mp_expfactor *ff = xmalloc(n * sizeof(mp_expfactor)); |
103 | for (i = 0; i < n; i++) { ff[i].base = *f[i].base; ff[i].exp = f[i].exp; } |
104 | *d = mpmont_mexpr(&g->mm, *d, ff, n); xfree(ff); |
105 | } |
106 | |
107 | static int gread(group *gg, mp **d, const mptext_ops *ops, void *p) { |
108 | gctx *g = (gctx *)gg; mp *t; |
109 | if ((t = mp_read(MP_NEW, 0, ops, p)) == 0) return (-1); |
110 | mp_drop(*d); *d = mpmont_mul(&g->mm, t, t, g->mm.r2); return (0); |
111 | } |
112 | |
113 | static int gwrite(group *gg, mp **x, const mptext_ops *ops, void *p) { |
114 | gctx *g = (gctx *)gg; mp *t = mpmont_reduce(&g->mm, MP_NEW, *x); |
115 | int rc = mp_write(t, 10, ops, p); MP_DROP(t); return (rc); |
116 | } |
117 | |
118 | static mp *gtoint(group *gg, mp *d, mp **x) |
119 | { gctx *g = (gctx *)gg; return (mpmont_reduce(&g->mm, d, *x)); } |
120 | |
121 | static int gfromint(group *gg, mp **d, mp *x) { |
b817bfc6 |
122 | gctx *g = (gctx *)gg; mp_div(0, d, x, g->mm.m); |
123 | *d = mpmont_mul(&g->mm, *d, *d, g->mm.r2); return (0); |
34e4f738 |
124 | } |
125 | |
126 | static int gtobuf(group *gg, buf *b, mp **x) { |
127 | gctx *g = (gctx *)gg; mp *t = mpmont_reduce(&g->mm, MP_NEW, *x); |
128 | int rc = buf_putmp(b, t); MP_DROP(t); return (rc); |
129 | } |
130 | |
131 | static int gfrombuf(group *gg, buf *b, mp **d) { |
132 | gctx * g = (gctx *)gg; mp *x; if ((x = buf_getmp(b)) == 0) return (-1); |
02d7884d |
133 | mp_div(0, &x, x, g->mm.m); mp_drop(*d); |
34e4f738 |
134 | *d = mpmont_mul(&g->mm, x, x, g->mm.r2); return(0); |
135 | } |
136 | |
0f3faccd |
137 | static int gtoraw(group *gg, buf *b, mp **x) { |
138 | gctx *g = (gctx *)gg; octet *q; mp *t = mpmont_reduce(&g->mm, MP_NEW, *x); |
139 | if ((q = buf_get(b, g->g.noctets)) == 0) { MP_DROP(t); return (-1); } |
140 | mp_storeb(t, q, g->g.noctets); MP_DROP(t); return (0); |
141 | } |
142 | |
143 | static int gfromraw(group *gg, buf *b, mp **d) { |
144 | gctx * g = (gctx *)gg; mp *x; octet *q; |
145 | if ((q = buf_get(b, g->g.noctets)) == 0) return (-1); |
146 | x = mp_loadb(MP_NEW, q, g->g.noctets); |
147 | mp_div(0, &x, x, g->mm.m); mp_drop(*d); |
148 | *d = mpmont_mul(&g->mm, x, x, g->mm.r2); return(0); |
149 | } |
150 | |
34e4f738 |
151 | /* --- @group_prime@ --- * |
152 | * |
153 | * Arguments: @const gprime_param *gp@ = group parameters |
154 | * |
02d7884d |
155 | * Returns: A pointer to the group, or null. |
34e4f738 |
156 | * |
157 | * Use: Constructs an abstract group interface for a subgroup of a |
158 | * prime field. Group elements are @mp *@ pointers. |
159 | */ |
160 | |
161 | static const group_ops gops = { |
162 | GTY_PRIME, |
163 | gdestroygroup, gcreate, gcopy, gburn, gdestroy, |
164 | gsamep, geq, group_stdidentp, |
165 | gcheck, |
166 | gmul, gsqr, ginv, group_stddiv, gexp, gmexp, |
167 | gread, gwrite, |
0f3faccd |
168 | gtoint, gfromint, group_stdtoec, group_stdfromec, gtobuf, gfrombuf, |
169 | gtoraw, gfromraw |
34e4f738 |
170 | }; |
171 | |
172 | group *group_prime(const gprime_param *gp) |
173 | { |
02d7884d |
174 | gctx *g; |
34e4f738 |
175 | |
02d7884d |
176 | if (!MP_ISPOS(gp->p) || !MP_ISODD(gp->p)) |
177 | return (0); |
178 | g = CREATE(gctx); |
34e4f738 |
179 | g->g.ops = &gops; |
180 | g->g.nbits = mp_bits(gp->p); |
181 | g->g.noctets = (g->g.nbits + 7) >> 3; |
182 | mpmont_create(&g->mm, gp->p); |
183 | g->g.i = &g->mm.r; |
184 | g->gen = mpmont_mul(&g->mm, MP_NEW, gp->g, g->mm.r2); |
185 | g->g.g = &g->gen; |
186 | g->g.r = MP_COPY(gp->q); |
187 | g->g.h = MP_NEW; mp_div(&g->g.h, 0, gp->p, gp->q); |
188 | return (&g->g); |
189 | } |
190 | |
191 | /*----- That's all, folks -------------------------------------------------*/ |