3 * $Id: gf-gcd.c,v 1.2 2004/03/21 22:52:06 mdw Exp $
5 * Euclidian algorithm on binary polynomials
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 /*----- Revision history --------------------------------------------------*
33 * Revision 1.2 2004/03/21 22:52:06 mdw
34 * Merge and close elliptic curve branch.
36 * Revision 1.1.2.1 2004/03/21 22:39:46 mdw
37 * Elliptic curves on binary fields work.
41 /*----- Header files ------------------------------------------------------*/
45 /*----- Main code ---------------------------------------------------------*/
49 * Arguments: @mp **gcd, **xx, **yy@ = where to write the results
50 * @mp *a, *b@ = sources (must be nonzero)
55 * Use: Calculates @gcd(a, b)@, and two numbers @x@ and @y@ such that
56 * @ax + by = gcd(a, b)@. This is useful for computing modular
60 void gf_gcd(mp
**gcd
, mp
**xx
, mp
**yy
, mp
*a
, mp
*b
)
62 mp
*x
= MP_ONE
, *X
= MP_ZERO
;
63 mp
*y
= MP_ZERO
, *Y
= MP_ONE
;
71 /* --- Sort out some initial flags --- */
76 /* --- Ensure that @a@ is larger than @b@ --- *
78 * If they're the same length we don't care which order they're in, so this
79 * unsigned comparison is fine.
82 if (MPX_UCMP(a
->v
, a
->vl
, <, b
->v
, b
->vl
)) {
83 { mp
*t
= a
; a
= b
; b
= t
; }
87 /* --- Check for zeroness --- */
89 if (MP_EQ(b
, MP_ZERO
)) {
91 /* --- Store %$|a|$% as the GCD --- */
94 if (*gcd
) MP_DROP(*gcd
);
99 /* --- Store %$1$% and %$0$% in the appropriate bins --- */
103 mp
**t
= xx
; xx
= yy
; yy
= t
;
106 if (*xx
) MP_DROP(*xx
);
107 if (MP_EQ(a
, MP_ZERO
))
113 if (*yy
) MP_DROP(*yy
);
120 /* --- Take a reference to the arguments --- */
125 /* --- Make sure @a@ and @b@ are not both even --- */
127 MP_SPLIT(a
); a
->f
&= ~MP_NEG
;
128 MP_SPLIT(b
); b
->f
&= ~MP_NEG
;
135 gf_div(&q
, &u
, u
, v
);
137 t
= gf_mul(MP_NEW
, X
, q
);
139 MP_DROP(x
); x
= X
; X
= t
;
140 t
= gf_mul(MP_NEW
, Y
, q
);
142 MP_DROP(y
); y
= Y
; Y
= t
;
151 if (*gcd
) MP_DROP(*gcd
);
156 /* --- Perform a little normalization --- */
160 /* --- If @a@ and @b@ got swapped, swap the coefficients back --- */
163 mp
*t
= x
; x
= y
; y
= t
;
167 /* --- Store the results --- */
172 if (*xx
) MP_DROP(*xx
);
179 if (*yy
) MP_DROP(*yy
);
185 MP_DROP(X
); MP_DROP(Y
);
186 MP_DROP(a
); MP_DROP(b
);
189 /*----- Test rig ----------------------------------------------------------*/
193 static int gcd(dstr
*v
)
196 mp
*a
= *(mp
**)v
[0].buf
;
197 mp
*b
= *(mp
**)v
[1].buf
;
198 mp
*g
= *(mp
**)v
[2].buf
;
199 mp
*x
= *(mp
**)v
[3].buf
;
200 mp
*y
= *(mp
**)v
[4].buf
;
202 mp
*gg
= MP_NEW
, *xx
= MP_NEW
, *yy
= MP_NEW
;
203 gf_gcd(&gg
, &xx
, &yy
, a
, b
);
205 fputs("\n*** mp_gcd(x) failed", stderr
);
206 fputs("\na = ", stderr
); mp_writefile(a
, stderr
, 16);
207 fputs("\nb = ", stderr
); mp_writefile(b
, stderr
, 16);
208 fputs("\nexpect = ", stderr
); mp_writefile(x
, stderr
, 16);
209 fputs("\nresult = ", stderr
); mp_writefile(xx
, stderr
, 16);
214 fputs("\n*** mp_gcd(y) failed", stderr
);
215 fputs("\na = ", stderr
); mp_writefile(a
, stderr
, 16);
216 fputs("\nb = ", stderr
); mp_writefile(b
, stderr
, 16);
217 fputs("\nexpect = ", stderr
); mp_writefile(y
, stderr
, 16);
218 fputs("\nresult = ", stderr
); mp_writefile(yy
, stderr
, 16);
224 mp
*ax
= gf_mul(MP_NEW
, a
, xx
);
225 mp
*by
= gf_mul(MP_NEW
, b
, yy
);
226 ax
= gf_add(ax
, ax
, by
);
228 fputs("\n*** (Alternative result found.)\n", stderr
);
234 fputs("\n*** mp_gcd(gcd) failed", stderr
);
235 fputs("\na = ", stderr
); mp_writefile(a
, stderr
, 16);
236 fputs("\nb = ", stderr
); mp_writefile(b
, stderr
, 16);
237 fputs("\nexpect = ", stderr
); mp_writefile(g
, stderr
, 16);
238 fputs("\nresult = ", stderr
); mp_writefile(gg
, stderr
, 16);
242 MP_DROP(a
); MP_DROP(b
); MP_DROP(g
); MP_DROP(x
); MP_DROP(y
);
243 MP_DROP(gg
); MP_DROP(xx
); MP_DROP(yy
);
244 assert(mparena_count(MPARENA_GLOBAL
) == 0);
248 static test_chunk tests
[] = {
249 { "gcd", gcd
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
253 int main(int argc
, char *argv
[])
256 test_run(argc
, argv
, tests
, SRCDIR
"/tests/gf");
262 /*----- That's all, folks -------------------------------------------------*/