[Gmp-commit] /var/hg/gmp: Make mpz_invert behave correctly for Z_1.

mercurial at gmplib.org mercurial at gmplib.org
Thu Jan 2 23:31:37 UTC 2014


details:   /var/hg/gmp/rev/aee45ff1d972
changeset: 16152:aee45ff1d972
user:      Torbjorn Granlund <tege at gmplib.org>
date:      Fri Jan 03 00:31:29 2014 +0100
description:
Make mpz_invert behave correctly for Z_1.

diffstat:

 ChangeLog            |  6 ++++++
 mpz/invert.c         |  7 +------
 tests/mpz/t-invert.c |  7 ++++---
 3 files changed, 11 insertions(+), 9 deletions(-)

diffs (71 lines):

diff -r a9bffcf3a610 -r aee45ff1d972 ChangeLog
--- a/ChangeLog	Thu Jan 02 21:23:21 2014 +0100
+++ b/ChangeLog	Fri Jan 03 00:31:29 2014 +0100
@@ -1,3 +1,9 @@
+2014-01-03  Torbjorn Granlund  <tege at gmplib.org>
+
+	* mpz/invert.c: Rely on gcdext for all operands, removing faulty
+	special case.
+	* tests/mpz/t-invert.c: Enforce correct behaviour for |mod| = 1.
+
 2014-01-02  Niels Möller  <nisse at lysator.liu.se>
 
 	* doc/gmp.texi (Low-level Functions): Document mpn_sizeinbase.
diff -r a9bffcf3a610 -r aee45ff1d972 mpz/invert.c
--- a/mpz/invert.c	Thu Jan 02 21:23:21 2014 +0100
+++ b/mpz/invert.c	Fri Jan 03 00:31:29 2014 +0100
@@ -2,7 +2,7 @@
    If X has an inverse, return non-zero and store inverse in INVERSE,
    otherwise, return 0 and put garbage in INVERSE.
 
-Copyright 1996-2001, 2005, 2012 Free Software Foundation, Inc.
+Copyright 1996-2001, 2005, 2012, 2014 Free Software Foundation, Inc.
 
 This file is part of the GNU MP Library.
 
@@ -32,11 +32,6 @@
   xsize = ABSIZ (x);
   nsize = ABSIZ (n);
 
-  /* No inverse exists if the leftside operand is 0.  Likewise, no
-     inverse exists if the mod operand is 1.  */
-  if (xsize == 0 || (nsize == 1 && (PTR (n))[0] == 1))
-    return 0;
-
   size = MAX (xsize, nsize) + 1;
   TMP_MARK;
 
diff -r a9bffcf3a610 -r aee45ff1d972 tests/mpz/t-invert.c
--- a/tests/mpz/t-invert.c	Thu Jan 02 21:23:21 2014 +0100
+++ b/tests/mpz/t-invert.c	Fri Jan 03 00:31:29 2014 +0100
@@ -1,6 +1,6 @@
 /* Test mpz_invert.
 
-Copyright 1991, 1993, 1994, 1996, 1997, 2000-2005, 2008, 2009, 2012 Free
+Copyright 1991, 1993, 1994, 1996, 1997, 2000-2005, 2008, 2009, 2012, 2014 Free
 Software Foundation, Inc.
 
 This file is part of the GNU MP Library test suite.
@@ -71,11 +71,12 @@
 	{
 	  MPZ_CHECK_FORMAT (ainv);
 
-	  if (mpz_cmp_ui (ainv, 0) <= 0 || mpz_cmpabs (ainv, m) >= 0)
+	  if (mpz_cmp_ui (ainv, 0) < 0 || mpz_cmpabs (ainv, m) >= 0)
 	    {
 	      fprintf (stderr, "ERROR in test %d\n", test);
 	      gmp_fprintf (stderr, "Inverse out of range.\n");
 	      gmp_fprintf (stderr, "a = %Zx\n", a);
+	      gmp_fprintf (stderr, "1/a = %Zx\n", ainv);
 	      gmp_fprintf (stderr, "m = %Zx\n", m);
 	      abort ();
 	    }
@@ -83,7 +84,7 @@
 	  mpz_mul (t, ainv, a);
 	  mpz_mod (t, t, m);
 
-	  if (mpz_cmp_ui (t, 1) != 0)
+	  if (mpz_cmp_ui (t, mpz_cmpabs_ui (m, 1) != 0) != 0)
 	    {
 	      fprintf (stderr, "ERROR in test %d\n", test);
 	      gmp_fprintf (stderr, "a^(-1)*a != 1 (mod m)\n");


More information about the gmp-commit mailing list