34e4f738 |
1 | /* -*-c-*- |
34e4f738 |
2 | * |
3 | * Abstraction for prime groups |
4 | * |
5 | * (c) 2004 Straylight/Edgeware |
6 | */ |
7 | |
45c0fd36 |
8 | /*----- Licensing notice --------------------------------------------------* |
34e4f738 |
9 | * |
10 | * This file is part of Catacomb. |
11 | * |
12 | * Catacomb is free software; you can redistribute it and/or modify |
13 | * it under the terms of the GNU Library General Public License as |
14 | * published by the Free Software Foundation; either version 2 of the |
15 | * License, or (at your option) any later version. |
45c0fd36 |
16 | * |
34e4f738 |
17 | * Catacomb is distributed in the hope that it will be useful, |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20 | * GNU Library General Public License for more details. |
45c0fd36 |
21 | * |
34e4f738 |
22 | * You should have received a copy of the GNU Library General Public |
23 | * License along with Catacomb; if not, write to the Free |
24 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
25 | * MA 02111-1307, USA. |
26 | */ |
27 | |
34e4f738 |
28 | /*----- Header files ------------------------------------------------------*/ |
29 | |
30 | #include <mLib/sub.h> |
31 | |
32 | #include "mpmont.h" |
33 | #include "pgen.h" |
34 | |
35 | #define ge mp * |
36 | #include "group.h" |
f94b972d |
37 | #include "group-guts.h" |
34e4f738 |
38 | |
39 | /*----- Main code ---------------------------------------------------------*/ |
40 | |
41 | /* --- Group operations --- */ |
42 | |
43 | static void gdestroygroup(group *gg) { |
f94b972d |
44 | gctx_prime *g = (gctx_prime *)gg; |
34e4f738 |
45 | mp_drop(g->gen); mp_drop(g->g.r); mp_drop(g->g.h); |
46 | mpmont_destroy(&g->mm); |
47 | DESTROY(g); |
48 | } |
49 | |
50 | static mp **gcreate(group *gg) |
51 | { mp **x = CREATE(mp *); *x = MP_COPY(*gg->i); return (x); } |
52 | |
53 | static void gcopy(group *gg, mp **d, mp **x) |
54 | { mp *t = MP_COPY(*x); MP_DROP(*d); *d = t; } |
55 | |
56 | static void gburn(group *gg, mp **x) { (*x)->f |= MP_BURN; } |
57 | |
58 | static void gdestroy(group *gg, mp **x) { MP_DROP(*x); DESTROY(x); } |
59 | |
02d7884d |
60 | static int gsamep(group *gg, group *hh) { |
f94b972d |
61 | gctx_prime *g = (gctx_prime *)gg, *h = (gctx_prime *)hh; |
02d7884d |
62 | return (MP_EQ(g->mm.m, h->mm.m)); |
63 | } |
34e4f738 |
64 | |
65 | static int geq(group *gg, mp **x, mp **y) { return (MP_EQ(*x, *y)); } |
66 | |
67 | static const char *gcheck(group *gg, grand *gr) { |
f94b972d |
68 | gctx_prime *g = (gctx_prime *)gg; int rc; mp *t; |
34e4f738 |
69 | if (!pgen_primep(g->mm.m, gr)) return ("p is not prime"); |
70 | t = mp_mul(MP_NEW, g->g.r, g->g.h); t = mp_add(t, t, MP_ONE); |
71 | rc = MP_EQ(t, g->mm.m); MP_DROP(t); if (!rc) return ("not a subgroup"); |
72 | return (group_stdcheck(gg, gr)); |
73 | } |
74 | |
75 | static void gmul(group *gg, mp **d, mp **x, mp **y) |
f94b972d |
76 | { gctx_prime *g = (gctx_prime *)gg; *d = mpmont_mul(&g->mm, *d, *x, *y); } |
34e4f738 |
77 | |
78 | static void gsqr(group *gg, mp **d, mp **x) { |
f94b972d |
79 | gctx_prime *g = (gctx_prime *)gg; mp *r = mp_sqr(*d, *x); |
34e4f738 |
80 | *d = mpmont_reduce(&g->mm, r, r); |
81 | } |
82 | |
83 | static void ginv(group *gg, mp **d, mp **x) { |
f94b972d |
84 | gctx_prime *g = (gctx_prime *)gg; mp *r = mpmont_reduce(&g->mm, *d, *x); |
b817bfc6 |
85 | r = mp_modinv(r, r, g->mm.m); *d = mpmont_mul(&g->mm, r, r, g->mm.r2); |
34e4f738 |
86 | } |
87 | |
88 | static void gexp(group *gg, mp **d, mp **x, mp *n) |
f94b972d |
89 | { gctx_prime *g = (gctx_prime *)gg; *d = mpmont_expr(&g->mm, *d, *x, n); } |
34e4f738 |
90 | |
91 | static void gmexp(group *gg, mp **d, const group_expfactor *f, size_t n) { |
f94b972d |
92 | gctx_prime *g = (gctx_prime *)gg; size_t i; |
34e4f738 |
93 | mp_expfactor *ff = xmalloc(n * sizeof(mp_expfactor)); |
94 | for (i = 0; i < n; i++) { ff[i].base = *f[i].base; ff[i].exp = f[i].exp; } |
95 | *d = mpmont_mexpr(&g->mm, *d, ff, n); xfree(ff); |
96 | } |
97 | |
98 | static int gread(group *gg, mp **d, const mptext_ops *ops, void *p) { |
f94b972d |
99 | gctx_prime *g = (gctx_prime *)gg; mp *t; |
34e4f738 |
100 | if ((t = mp_read(MP_NEW, 0, ops, p)) == 0) return (-1); |
101 | mp_drop(*d); *d = mpmont_mul(&g->mm, t, t, g->mm.r2); return (0); |
102 | } |
103 | |
104 | static int gwrite(group *gg, mp **x, const mptext_ops *ops, void *p) { |
f94b972d |
105 | gctx_prime *g = (gctx_prime *)gg; |
106 | mp *t = mpmont_reduce(&g->mm, MP_NEW, *x); |
34e4f738 |
107 | int rc = mp_write(t, 10, ops, p); MP_DROP(t); return (rc); |
108 | } |
109 | |
f94b972d |
110 | static mp *gtoint(group *gg, mp *d, mp **x) { |
111 | gctx_prime *g = (gctx_prime *)gg; |
112 | return (mpmont_reduce(&g->mm, d, *x)); |
113 | } |
34e4f738 |
114 | |
115 | static int gfromint(group *gg, mp **d, mp *x) { |
f94b972d |
116 | gctx_prime *g = (gctx_prime *)gg; mp_div(0, d, x, g->mm.m); |
b817bfc6 |
117 | *d = mpmont_mul(&g->mm, *d, *d, g->mm.r2); return (0); |
34e4f738 |
118 | } |
119 | |
120 | static int gtobuf(group *gg, buf *b, mp **x) { |
f94b972d |
121 | gctx_prime *g = (gctx_prime *)gg; |
122 | mp *t = mpmont_reduce(&g->mm, MP_NEW, *x); |
34e4f738 |
123 | int rc = buf_putmp(b, t); MP_DROP(t); return (rc); |
124 | } |
125 | |
126 | static int gfrombuf(group *gg, buf *b, mp **d) { |
f94b972d |
127 | gctx_prime * g = (gctx_prime *)gg; mp *x; |
128 | if ((x = buf_getmp(b)) == 0) return (-1); |
02d7884d |
129 | mp_div(0, &x, x, g->mm.m); mp_drop(*d); |
34e4f738 |
130 | *d = mpmont_mul(&g->mm, x, x, g->mm.r2); return(0); |
131 | } |
132 | |
0f3faccd |
133 | static int gtoraw(group *gg, buf *b, mp **x) { |
f94b972d |
134 | gctx_prime *g = (gctx_prime *)gg; octet *q; |
135 | mp *t = mpmont_reduce(&g->mm, MP_NEW, *x); |
0f3faccd |
136 | if ((q = buf_get(b, g->g.noctets)) == 0) { MP_DROP(t); return (-1); } |
137 | mp_storeb(t, q, g->g.noctets); MP_DROP(t); return (0); |
138 | } |
139 | |
140 | static int gfromraw(group *gg, buf *b, mp **d) { |
f94b972d |
141 | gctx_prime * g = (gctx_prime *)gg; mp *x; octet *q; |
0f3faccd |
142 | if ((q = buf_get(b, g->g.noctets)) == 0) return (-1); |
143 | x = mp_loadb(MP_NEW, q, g->g.noctets); |
144 | mp_div(0, &x, x, g->mm.m); mp_drop(*d); |
145 | *d = mpmont_mul(&g->mm, x, x, g->mm.r2); return(0); |
146 | } |
147 | |
34e4f738 |
148 | /* --- @group_prime@ --- * |
149 | * |
150 | * Arguments: @const gprime_param *gp@ = group parameters |
151 | * |
02d7884d |
152 | * Returns: A pointer to the group, or null. |
34e4f738 |
153 | * |
154 | * Use: Constructs an abstract group interface for a subgroup of a |
155 | * prime field. Group elements are @mp *@ pointers. |
156 | */ |
157 | |
158 | static const group_ops gops = { |
f94b972d |
159 | GTY_PRIME, "prime", |
34e4f738 |
160 | gdestroygroup, gcreate, gcopy, gburn, gdestroy, |
161 | gsamep, geq, group_stdidentp, |
162 | gcheck, |
163 | gmul, gsqr, ginv, group_stddiv, gexp, gmexp, |
164 | gread, gwrite, |
0f3faccd |
165 | gtoint, gfromint, group_stdtoec, group_stdfromec, gtobuf, gfrombuf, |
166 | gtoraw, gfromraw |
34e4f738 |
167 | }; |
168 | |
169 | group *group_prime(const gprime_param *gp) |
170 | { |
f94b972d |
171 | gctx_prime *g; |
34e4f738 |
172 | |
a69a3efd |
173 | if (!MP_POSP(gp->p) || !MP_ODDP(gp->p)) |
02d7884d |
174 | return (0); |
f94b972d |
175 | g = CREATE(gctx_prime); |
34e4f738 |
176 | g->g.ops = &gops; |
177 | g->g.nbits = mp_bits(gp->p); |
178 | g->g.noctets = (g->g.nbits + 7) >> 3; |
179 | mpmont_create(&g->mm, gp->p); |
180 | g->g.i = &g->mm.r; |
181 | g->gen = mpmont_mul(&g->mm, MP_NEW, gp->g, g->mm.r2); |
182 | g->g.g = &g->gen; |
183 | g->g.r = MP_COPY(gp->q); |
184 | g->g.h = MP_NEW; mp_div(&g->g.h, 0, gp->p, gp->q); |
185 | return (&g->g); |
186 | } |
187 | |
188 | /*----- That's all, folks -------------------------------------------------*/ |