5 * Abstraction for prime groups
7 * (c) 2004 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Header files ------------------------------------------------------*/
39 #include "group-guts.h"
41 /*----- Main code ---------------------------------------------------------*/
43 /* --- Group operations --- */
45 static void gdestroygroup(group
*gg
) {
46 gctx_bin
*g
= (gctx_bin
*)gg
;
47 mp_drop(g
->gen
); mp_drop(g
->g
.r
); mp_drop(g
->g
.h
);
48 gfreduce_destroy(&g
->r
);
52 static mp
**gcreate(group
*gg
)
53 { mp
**x
= CREATE(mp
*); *x
= MP_COPY(*gg
->i
); return (x
); }
55 static void gcopy(group
*gg
, mp
**d
, mp
**x
)
56 { mp
*t
= MP_COPY(*x
); MP_DROP(*d
); *d
= t
; }
58 static void gburn(group
*gg
, mp
**x
) { (*x
)->f
|= MP_BURN
; }
60 static void gdestroy(group
*gg
, mp
**x
) { MP_DROP(*x
); DESTROY(x
); }
62 static int gsamep(group
*gg
, group
*hh
) {
63 gctx_bin
*g
= (gctx_bin
*)gg
, *h
= (gctx_bin
*)hh
;
64 return (MP_EQ(g
->r
.p
, h
->r
.p
));
67 static int geq(group
*gg
, mp
**x
, mp
**y
) { return (MP_EQ(*x
, *y
)); }
69 static const char *gcheck(group
*gg
, grand
*gr
) {
70 gctx_bin
*g
= (gctx_bin
*)gg
; int rc
; mp
*t
, *tt
;
71 if (!gf_irreduciblep(g
->r
.p
)) return ("p is not irreducible");
72 t
= mp_mul(MP_NEW
, g
->g
.r
, g
->g
.h
); t
= mp_add(t
, t
, MP_ONE
);
73 tt
= mp_lsl(MP_NEW
, MP_ONE
, g
->g
.nbits
);
74 rc
= MP_EQ(t
, tt
); MP_DROP(t
); MP_DROP(tt
);
75 if (!rc
) return ("not a subgroup");
76 return (group_stdcheck(gg
, gr
));
79 static void gmul(group
*gg
, mp
**d
, mp
**x
, mp
**y
) {
80 gctx_bin
*g
= (gctx_bin
*)gg
; mp
*r
= gf_mul(*d
, *x
, *y
);
81 *d
= gfreduce_do(&g
->r
, r
, r
);
84 static void gsqr(group
*gg
, mp
**d
, mp
**x
) {
85 gctx_bin
*g
= (gctx_bin
*)gg
; mp
*r
= gf_sqr(*d
, *x
);
86 *d
= gfreduce_do(&g
->r
, r
, r
);
89 static void ginv(group
*gg
, mp
**d
, mp
**x
)
90 { gctx_bin
*g
= (gctx_bin
*)gg
; *d
= gf_modinv(*d
, *x
, g
->r
.p
); }
92 static void gexp(group
*gg
, mp
**d
, mp
**x
, mp
*n
)
93 { gctx_bin
*g
= (gctx_bin
*)gg
; *d
= gfreduce_exp(&g
->r
, *d
, *x
, n
); }
95 static int gread(group
*gg
, mp
**d
, const mptext_ops
*ops
, void *p
) {
96 mp
*t
; if ((t
= mp_read(MP_NEW
, 0, ops
, p
)) == 0) return (-1);
97 mp_drop(*d
); *d
= t
; return (0);
100 static int gwrite(group
*gg
, mp
**x
, const mptext_ops
*ops
, void *p
) {
102 if (!ops
->put("0x", 2, p
) && !mp_write(*x
, 16, ops
, p
)) rc
= 0;
106 static mp
*gtoint(group
*gg
, mp
*d
, mp
**x
) { return MP_COPY(*x
); }
108 static int gfromint(group
*gg
, mp
**d
, mp
*x
) { *d
= MP_COPY(x
); return 0; }
110 static int gtobuf(group
*gg
, buf
*b
, mp
**x
)
111 { int rc
= buf_putmp(b
, *x
); return (rc
); }
113 static int gfrombuf(group
*gg
, buf
*b
, mp
**d
) {
114 gctx_bin
*g
= (gctx_bin
*)gg
; mp
*x
;
115 if ((x
= buf_getmp(b
)) == 0) return (-1);
116 MP_DROP(*d
); *d
= gfreduce_do(&g
->r
, x
, x
);
120 static int gtoraw(group
*gg
, buf
*b
, mp
**x
) {
121 gctx_bin
* g
= (gctx_bin
*)gg
; octet
*q
;
122 if ((q
= buf_get(b
, g
->g
.noctets
)) == 0) return (-1);
123 mp_storeb(*x
, q
, g
->g
.noctets
); return (0);
126 static int gfromraw(group
*gg
, buf
*b
, mp
**d
) {
127 gctx_bin
* g
= (gctx_bin
*)gg
; mp
*x
; octet
*q
;
128 if ((q
= buf_get(b
, g
->g
.noctets
)) == 0) return (-1);
129 x
= mp_loadb(MP_NEW
, q
, g
->g
.noctets
);
130 MP_DROP(*d
); *d
= gfreduce_do(&g
->r
, x
, x
);
134 /* --- @group_binary@ --- *
136 * Arguments: @const gbin_param *gb@ = group parameters
138 * Returns: A pointer to the group, or null.
140 * Use: Constructs an abstract group interface for a subgroup of a
141 * prime field. Group elements are @mp *@ pointers.
144 static const group_ops gops
= {
146 gdestroygroup
, gcreate
, gcopy
, gburn
, gdestroy
,
147 gsamep
, geq
, group_stdidentp
,
149 gmul
, gsqr
, ginv
, group_stddiv
, gexp
, group_stdmexp
,
151 gtoint
, gfromint
, group_stdtoec
, group_stdfromec
, gtobuf
, gfrombuf
,
155 group
*group_binary(const gbin_param
*gb
)
162 g
= CREATE(gctx_bin
);
164 g
->g
.nbits
= mp_bits(gb
->p
) - 1;
165 g
->g
.noctets
= (g
->g
.nbits
+ 7) >> 3;
166 gfreduce_create(&g
->r
, gb
->p
);
169 g
->gen
= MP_COPY(gb
->g
);
171 g
->g
.r
= MP_COPY(gb
->q
);
172 t
= mp_lsl(MP_NEW
, MP_ONE
, g
->g
.nbits
);
173 t
= mp_sub(t
, t
, MP_ONE
);
174 g
->g
.h
= MP_NEW
; mp_div(&g
->g
.h
, 0, t
, gb
->q
);
179 /*----- That's all, folks -------------------------------------------------*/