--- /dev/null
+/* -*-c-*-
+ *
+ * $Id: mp-gcd.c,v 1.1 1999/11/17 18:02:16 mdw Exp $
+ *
+ * Extended GCD calculation
+ *
+ * (c) 1999 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------*
+ *
+ * This file is part of Catacomb.
+ *
+ * Catacomb is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Library General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * Catacomb is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with Catacomb; if not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
+ */
+
+/*----- Revision history --------------------------------------------------*
+ *
+ * $Log: mp-gcd.c,v $
+ * Revision 1.1 1999/11/17 18:02:16 mdw
+ * New multiprecision integer arithmetic suite.
+ *
+ */
+
+/*----- Header files ------------------------------------------------------*/
+
+#include "mp.h"
+
+/*----- Main code ---------------------------------------------------------*/
+
+/* --- @mp_gcd@ --- *
+ *
+ * Arguments: @mp **gcd, **xx, **yy@ = where to write the results
+ * @mp *a, *b@ = sources (must be nonzero)
+ *
+ * Returns: ---
+ *
+ * Use: Calculates @gcd(a, b)@, and two numbers @x@ and @y@ such that
+ * @ax + by = gcd(a, b)@. This is useful for computing modular
+ * inverses. Neither @a@ nor @b@ may be zero. Note that,
+ * unlike @mp_div@ for example, it is not possible to specify
+ * explicit destinations -- new MPs are always allocated.
+ */
+
+void mp_gcd(mp **gcd, mp **xx, mp **yy, mp *a, mp *b)
+{
+ mp *X = MP_ONE, *Y = MP_ZERO;
+ mp *x = MP_ZERO, *y = MP_ONE;
+ mp *u, *v;
+ size_t shift = 0;
+ int ext = xx || yy;
+ int swap = 0;
+
+ /* --- Ensure that @a@ is larger than @b@ --- */
+
+ if (MP_CMP(a, <, b)) {
+ { mp *t = a; a = b; b = t; }
+ swap = 1;
+ }
+
+ /* --- Take a reference to the arguments --- */
+
+ a = MP_COPY(a);
+ b = MP_COPY(b);
+
+ /* --- Make sure @a@ and @b@ are not both even --- */
+
+ if (((a->v[0] | b->v[0]) & 1) == 0) {
+ mpscan asc, bsc;
+
+ /* --- Break off my copies --- */
+
+ MP_SPLIT(a);
+ MP_SPLIT(b);
+ MP_SCAN(&asc, a);
+ MP_SCAN(&bsc, b);
+
+ /* --- Start scanning --- */
+
+ for (;;) {
+ if (!MP_STEP(&asc) || !MP_STEP(&bsc))
+ assert(((void)"zero argument passed to mp_gcd", 0));
+ if (MP_BIT(&asc) || MP_BIT(&bsc))
+ break;
+ shift++;
+ }
+
+ /* --- Shift @a@ and @b@ down --- */
+
+ a = mp_lsr(a, a, shift);
+ b = mp_lsr(b, b, shift);
+ }
+
+ /* --- Set up @u@ and @v@ --- */
+
+ u = MP_COPY(a);
+ v = MP_COPY(b);
+
+ /* --- Start the main loop --- */
+
+ for (;;) {
+
+ /* --- While @u@ is even --- */
+
+ {
+ mpscan sc, xsc, ysc;
+ size_t n = 0, nn = 0;
+
+ MP_SCAN(&sc, u);
+ MP_SCAN(&xsc, X); MP_SCAN(&ysc, Y);
+ for (;;) {
+ MP_STEP(&sc);
+ MP_STEP(&xsc); MP_STEP(&ysc);
+ if (MP_BIT(&sc))
+ break;
+ if (ext && (MP_BIT(&xsc) | MP_BIT(&ysc))) {
+ if (n) {
+ X = mp_lsr(X, X, n);
+ Y = mp_lsr(Y, Y, n);
+ n = 0;
+ }
+ X = mp_add(X, X, b);
+ Y = mp_sub(Y, Y, a);
+ MP_SCAN(&xsc, X);
+ MP_SCAN(&ysc, Y);
+ MP_STEP(&xsc); MP_STEP(&ysc);
+ }
+ n++; nn++;
+ }
+
+ if (nn) {
+ u = mp_lsr(u, u, nn);
+ if (ext && n) {
+ X = mp_lsr(X, X, n);
+ Y = mp_lsr(Y, Y, n);
+ }
+ }
+ }
+
+ /* --- While @v@ is even --- */
+
+ {
+ mpscan sc, xsc, ysc;
+ size_t n = 0, nn = 0;
+
+ MP_SCAN(&sc, v);
+ MP_SCAN(&xsc, x); MP_SCAN(&ysc, y);
+ for (;;) {
+ MP_STEP(&sc);
+ MP_STEP(&xsc); MP_STEP(&ysc);
+ if (MP_BIT(&sc))
+ break;
+ if (ext && (MP_BIT(&xsc) | MP_BIT(&ysc))) {
+ if (n) {
+ x = mp_lsr(x, x, n);
+ y = mp_lsr(y, y, n);
+ n = 0;
+ }
+ x = mp_add(x, x, b);
+ y = mp_sub(y, y, a);
+ MP_SCAN(&xsc, x);
+ MP_SCAN(&ysc, y);
+ MP_STEP(&xsc); MP_STEP(&ysc);
+ }
+ n++; nn++;
+ }
+
+ if (nn) {
+ v = mp_lsr(v, v, nn);
+ if (ext && n) {
+ x = mp_lsr(x, x, n);
+ y = mp_lsr(y, y, n);
+ }
+ }
+ }
+
+ /* --- End-of-loop fiddling --- */
+
+ if (MP_CMP(u, >=, v)) {
+ u = mp_sub(u, u, v);
+ if (ext) {
+ X = mp_sub(X, X, x);
+ Y = mp_sub(Y, Y, y);
+ }
+ } else {
+ v = mp_sub(v, v, u);
+ if (ext) {
+ x = mp_sub(x, x, X);
+ y = mp_sub(y, y, Y);
+ }
+ }
+
+ if (MP_CMP(u, ==, MP_ZERO))
+ break;
+ }
+
+ /* --- Write the results out --- */
+
+ if (gcd)
+ *gcd = mp_lsl(v, v, shift);
+ else
+ MP_DROP(v);
+
+ /* --- Perform a little normalization --- *
+ *
+ * Ensure that the coefficient returned is positive, if there is only one.
+ * If there are two, favour @y@.
+ */
+
+ if (ext) {
+ if (swap) {
+ mp *t = x; x = y; y = t;
+ }
+ if (yy) {
+ if (y->f & MP_NEG) {
+ y = mp_add(y, y, a);
+ x = mp_sub(x, x, b);
+ }
+ } else if (x->f & MP_NEG)
+ x = mp_add(x, x, b);
+
+ if (xx) *xx = x; else MP_DROP(x);
+ if (yy) *yy = y; else MP_DROP(y);
+ }
+
+ MP_DROP(u);
+ MP_DROP(X); MP_DROP(Y);
+ MP_DROP(a); MP_DROP(b);
+}
+
+/*----- Test rig ----------------------------------------------------------*/
+
+#ifdef TEST_RIG
+
+static int gcd(dstr *v)
+{
+ int ok = 1;
+ mp *a = *(mp **)v[0].buf;
+ mp *b = *(mp **)v[1].buf;
+ mp *g = *(mp **)v[2].buf;
+ mp *x = *(mp **)v[3].buf;
+ mp *y = *(mp **)v[4].buf;
+
+ mp *gg, *xx, *yy;
+ mp_gcd(&gg, &xx, &yy, a, b);
+ if (MP_CMP(x, !=, xx)) {
+ fputs("\n*** mp_gcd(x) failed", stderr);
+ fputs("\na = ", stderr); mp_writefile(a, stderr, 10);
+ fputs("\nb = ", stderr); mp_writefile(b, stderr, 10);
+ fputs("\nexpect = ", stderr); mp_writefile(x, stderr, 10);
+ fputs("\nresult = ", stderr); mp_writefile(xx, stderr, 10);
+ fputc('\n', stderr);
+ ok = 0;
+ }
+ if (MP_CMP(y, !=, yy)) {
+ fputs("\n*** mp_gcd(y) failed", stderr);
+ fputs("\na = ", stderr); mp_writefile(a, stderr, 10);
+ fputs("\nb = ", stderr); mp_writefile(b, stderr, 10);
+ fputs("\nexpect = ", stderr); mp_writefile(y, stderr, 10);
+ fputs("\nresult = ", stderr); mp_writefile(yy, stderr, 10);
+ fputc('\n', stderr);
+ ok = 0;
+ }
+
+ if (!ok) {
+ mp *ax = mp_mul(MP_NEW, a, xx);
+ mp *by = mp_mul(MP_NEW, b, yy);
+ ax = mp_add(ax, ax, by);
+ if (MP_CMP(ax, ==, gg))
+ fputs("\n*** (Alternative result found.)\n", stderr);
+ MP_DROP(ax);
+ MP_DROP(by);
+ }
+
+ if (MP_CMP(g, !=, gg)) {
+ fputs("\n*** mp_gcd(gcd) failed", stderr);
+ fputs("\na = ", stderr); mp_writefile(a, stderr, 10);
+ fputs("\nb = ", stderr); mp_writefile(b, stderr, 10);
+ fputs("\nexpect = ", stderr); mp_writefile(g, stderr, 10);
+ fputs("\nresult = ", stderr); mp_writefile(gg, stderr, 10);
+ fputc('\n', stderr);
+ ok = 0;
+ }
+ MP_DROP(a); MP_DROP(b); MP_DROP(g); MP_DROP(x); MP_DROP(y);
+ MP_DROP(gg); MP_DROP(xx); MP_DROP(yy);
+ return (ok);
+}
+
+static test_chunk tests[] = {
+ { "gcd", gcd, { &type_mp, &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
+ { 0, 0, { 0 } }
+};
+
+int main(int argc, char *argv[])
+{
+ sub_init();
+ test_run(argc, argv, tests, SRCDIR "/tests/mp");
+ return (0);
+}
+
+#endif
+
+/*----- That's all, folks -------------------------------------------------*/