3 * Abstraction for prime groups
5 * (c) 2004 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
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.
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.
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,
28 /*----- Header files ------------------------------------------------------*/
36 #include "group-guts.h"
38 /*----- Main code ---------------------------------------------------------*/
40 /* --- Group operations --- */
42 static void gdestroygroup(group
*gg
) {
43 gctx_prime
*g
= (gctx_prime
*)gg
;
44 mp_drop(g
->gen
); mp_drop(g
->g
.r
); mp_drop(g
->g
.h
);
45 mpmont_destroy(&g
->mm
);
49 static mp
**gcreate(group
*gg
)
50 { mp
**x
= CREATE(mp
*); *x
= MP_COPY(*gg
->i
); return (x
); }
52 static void gcopy(group
*gg
, mp
**d
, mp
**x
)
53 { mp
*t
= MP_COPY(*x
); MP_DROP(*d
); *d
= t
; }
55 static void gburn(group
*gg
, mp
**x
) { (*x
)->f
|= MP_BURN
; }
57 static void gdestroy(group
*gg
, mp
**x
) { MP_DROP(*x
); DESTROY(x
); }
59 static int gsamep(group
*gg
, group
*hh
) {
60 gctx_prime
*g
= (gctx_prime
*)gg
, *h
= (gctx_prime
*)hh
;
61 return (MP_EQ(g
->mm
.m
, h
->mm
.m
));
64 static int geq(group
*gg
, mp
**x
, mp
**y
) { return (MP_EQ(*x
, *y
)); }
66 static const char *gcheck(group
*gg
, grand
*gr
) {
67 gctx_prime
*g
= (gctx_prime
*)gg
; int rc
; mp
*t
;
68 if (!pgen_primep(g
->mm
.m
, gr
)) return ("p is not prime");
69 t
= mp_mul(MP_NEW
, g
->g
.r
, g
->g
.h
); t
= mp_add(t
, t
, MP_ONE
);
70 rc
= MP_EQ(t
, g
->mm
.m
); MP_DROP(t
); if (!rc
) return ("not a subgroup");
71 return (group_stdcheck(gg
, gr
));
74 static void gmul(group
*gg
, mp
**d
, mp
**x
, mp
**y
)
75 { gctx_prime
*g
= (gctx_prime
*)gg
; *d
= mpmont_mul(&g
->mm
, *d
, *x
, *y
); }
77 static void gsqr(group
*gg
, mp
**d
, mp
**x
) {
78 gctx_prime
*g
= (gctx_prime
*)gg
; mp
*r
= mp_sqr(*d
, *x
);
79 *d
= mpmont_reduce(&g
->mm
, r
, r
);
82 static void ginv(group
*gg
, mp
**d
, mp
**x
) {
83 gctx_prime
*g
= (gctx_prime
*)gg
; mp
*r
= mpmont_reduce(&g
->mm
, *d
, *x
);
84 r
= mp_modinv(r
, r
, g
->mm
.m
); *d
= mpmont_mul(&g
->mm
, r
, r
, g
->mm
.r2
);
87 static void gexp(group
*gg
, mp
**d
, mp
**x
, mp
*n
)
88 { gctx_prime
*g
= (gctx_prime
*)gg
; *d
= mpmont_expr(&g
->mm
, *d
, *x
, n
); }
90 static void gmexp(group
*gg
, mp
**d
, const group_expfactor
*f
, size_t n
) {
91 gctx_prime
*g
= (gctx_prime
*)gg
; size_t i
;
92 mp_expfactor
*ff
= xmalloc(n
* sizeof(mp_expfactor
));
93 for (i
= 0; i
< n
; i
++) { ff
[i
].base
= *f
[i
].base
; ff
[i
].exp
= f
[i
].exp
; }
94 *d
= mpmont_mexpr(&g
->mm
, *d
, ff
, n
); xfree(ff
);
97 static int gread(group
*gg
, mp
**d
, const mptext_ops
*ops
, void *p
) {
98 gctx_prime
*g
= (gctx_prime
*)gg
; mp
*t
;
99 if ((t
= mp_read(MP_NEW
, 0, ops
, p
)) == 0) return (-1);
100 mp_drop(*d
); *d
= mpmont_mul(&g
->mm
, t
, t
, g
->mm
.r2
); return (0);
103 static int gwrite(group
*gg
, mp
**x
, const mptext_ops
*ops
, void *p
) {
104 gctx_prime
*g
= (gctx_prime
*)gg
;
105 mp
*t
= mpmont_reduce(&g
->mm
, MP_NEW
, *x
);
106 int rc
= mp_write(t
, 10, ops
, p
); MP_DROP(t
); return (rc
);
109 static mp
*gtoint(group
*gg
, mp
*d
, mp
**x
) {
110 gctx_prime
*g
= (gctx_prime
*)gg
;
111 return (mpmont_reduce(&g
->mm
, d
, *x
));
114 static int gfromint(group
*gg
, mp
**d
, mp
*x
) {
115 gctx_prime
*g
= (gctx_prime
*)gg
; mp_div(0, d
, x
, g
->mm
.m
);
116 *d
= mpmont_mul(&g
->mm
, *d
, *d
, g
->mm
.r2
); return (0);
119 static int gtobuf(group
*gg
, buf
*b
, mp
**x
) {
120 gctx_prime
*g
= (gctx_prime
*)gg
;
121 mp
*t
= mpmont_reduce(&g
->mm
, MP_NEW
, *x
);
122 int rc
= buf_putmp(b
, t
); MP_DROP(t
); return (rc
);
125 static int gfrombuf(group
*gg
, buf
*b
, mp
**d
) {
126 gctx_prime
* g
= (gctx_prime
*)gg
; mp
*x
;
127 if ((x
= buf_getmp(b
)) == 0) return (-1);
128 mp_div(0, &x
, x
, g
->mm
.m
); mp_drop(*d
);
129 *d
= mpmont_mul(&g
->mm
, x
, x
, g
->mm
.r2
); return(0);
132 static int gtoraw(group
*gg
, buf
*b
, mp
**x
) {
133 gctx_prime
*g
= (gctx_prime
*)gg
; octet
*q
;
134 mp
*t
= mpmont_reduce(&g
->mm
, MP_NEW
, *x
);
135 if ((q
= buf_get(b
, g
->g
.noctets
)) == 0) { MP_DROP(t
); return (-1); }
136 mp_storeb(t
, q
, g
->g
.noctets
); MP_DROP(t
); return (0);
139 static int gfromraw(group
*gg
, buf
*b
, mp
**d
) {
140 gctx_prime
* g
= (gctx_prime
*)gg
; mp
*x
; octet
*q
;
141 if ((q
= buf_get(b
, g
->g
.noctets
)) == 0) return (-1);
142 x
= mp_loadb(MP_NEW
, q
, g
->g
.noctets
);
143 mp_div(0, &x
, x
, g
->mm
.m
); mp_drop(*d
);
144 *d
= mpmont_mul(&g
->mm
, x
, x
, g
->mm
.r2
); return(0);
147 /* --- @group_prime@ --- *
149 * Arguments: @const gprime_param *gp@ = group parameters
151 * Returns: A pointer to the group, or null.
153 * Use: Constructs an abstract group interface for a subgroup of a
154 * prime field. Group elements are @mp *@ pointers.
157 static const group_ops gops
= {
159 gdestroygroup
, gcreate
, gcopy
, gburn
, gdestroy
,
160 gsamep
, geq
, group_stdidentp
,
162 gmul
, gsqr
, ginv
, group_stddiv
, gexp
, gmexp
,
164 gtoint
, gfromint
, group_stdtoec
, group_stdfromec
, gtobuf
, gfrombuf
,
168 group
*group_prime(const gprime_param
*gp
)
172 if (!MP_POSP(gp
->p
) || !MP_ODDP(gp
->p
))
174 g
= CREATE(gctx_prime
);
176 g
->g
.nbits
= mp_bits(gp
->p
);
177 g
->g
.noctets
= (g
->g
.nbits
+ 7) >> 3;
178 mpmont_create(&g
->mm
, gp
->p
);
180 g
->gen
= mpmont_mul(&g
->mm
, MP_NEW
, gp
->g
, g
->mm
.r2
);
182 g
->g
.r
= MP_COPY(gp
->q
);
183 g
->g
.h
= MP_NEW
; mp_div(&g
->g
.h
, 0, gp
->p
, gp
->q
);
187 /*----- That's all, folks -------------------------------------------------*/