raw
mpi-genesis             1 /* mpi-gcd.c  -  MPI functions
mpi-genesis 2 * Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
mpi-genesis 3 *
mpi-genesis 4 * This file is part of GnuPG.
mpi-genesis 5 *
mpi-genesis 6 * GnuPG is free software; you can redistribute it and/or modify
mpi-genesis 7 * it under the terms of the GNU General Public License as published by
mpi-genesis 8 * the Free Software Foundation; either version 3 of the License, or
mpi-genesis 9 * (at your option) any later version.
mpi-genesis 10 *
mpi-genesis 11 * GnuPG is distributed in the hope that it will be useful,
mpi-genesis 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
mpi-genesis 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
mpi-genesis 14 * GNU General Public License for more details.
mpi-genesis 15 *
mpi-genesis 16 * You should have received a copy of the GNU General Public License
mpi-genesis 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
mpi-genesis 18 */
mpi-genesis 19
mpi-genesis 20 #include <config.h>
mpi-genesis 21 #include <stdio.h>
mpi-genesis 22 #include <stdlib.h>
mpi-genesis 23 #include "mpi-internal.h"
mpi-genesis 24
mpi-genesis 25 /****************
mpi-genesis 26 * Find the greatest common divisor G of A and B.
mpi-genesis 27 * Return: true if this 1, false in all other cases
mpi-genesis 28 */
mpi-genesis 29 int
mpi-genesis 30 mpi_gcd( MPI g, MPI xa, MPI xb )
mpi-genesis 31 {
mpi-genesis 32 MPI a, b;
mpi-genesis 33
mpi-genesis 34 a = mpi_copy(xa);
mpi-genesis 35 b = mpi_copy(xb);
mpi-genesis 36
mpi-genesis 37 /* TAOCP Vol II, 4.5.2, Algorithm A */
mpi-genesis 38 a->sign = 0;
mpi-genesis 39 b->sign = 0;
mpi-genesis 40 while( mpi_cmp_ui( b, 0 ) ) {
mpi-genesis 41 mpi_fdiv_r( g, a, b ); /* g used as temorary variable */
mpi-genesis 42 mpi_set(a,b);
mpi-genesis 43 mpi_set(b,g);
mpi-genesis 44 }
mpi-genesis 45 mpi_set(g, a);
mpi-genesis 46
mpi-genesis 47 mpi_free(a);
mpi-genesis 48 mpi_free(b);
mpi-genesis 49 return !mpi_cmp_ui( g, 1);
mpi-genesis 50 }
mpi-genesis 51
mpi-genesis 52
mpi-genesis 53