Ciao,

10 ott 2023, 09:14 da t...@gmplib.org:

> Stefan Liebler <s...@linux.ibm.com> writes:
>
>  In my case, before calling mpn_divexact_by3c v6 was {0x1, 0x0} and thus 
>


> OK.  We should clearly have tests/*call.asm and tests/*check.c for
> s390_64 and s390_32.  (But the check file might want to re-randomise the
> magic register values after checking them.)
>

Another approach, simpler in my opinion, is to write a test checking the return 
value of the function mpn_divexact_by3c. The manual documents that the "return 
value c satisfy c*b^n + a-i = 3*q, where...", exactly, so we should test the 
return value.

In the library we have some ASSERTs, checking if the return value is zero or 
not, but I believe we do not have any place where the correct return value is 
really checked.

Does the attached test detect the suspicious code?

Ĝis,
m
/* Test for mulmod_bknp1 function.

   Contributed to the GNU project by Marco Bodrato.

Copyright 2023 Free Software Foundation, Inc.

This file is part of the GNU MP Library test suite.

The GNU MP Library test suite is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.

The GNU MP Library test suite 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 General
Public License for more details.

You should have received a copy of the GNU General Public License along with
the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */


#include <stdlib.h>
#include <stdio.h>

#include "gmp-impl.h"
#include "tests.h"

/* Sizes are up to 2^SIZE_LOG limbs */
#ifndef SIZE_LOG
#define SIZE_LOG 7
#endif

#ifndef COUNT
#define COUNT 5000
#endif

#define MAX_N (1 << SIZE_LOG)
#define MIN_N 1

int
main (int argc, char **argv)
{
  mp_ptr np, n3p, rp;
  int count = COUNT;
  int test;
  gmp_randstate_ptr rands;
  TMP_DECL;
  TMP_MARK;

  TESTS_REPS (count, argv, argc);

  tests_start ();
  rands = RANDS;

  np = TMP_ALLOC_LIMBS (MAX_N);
  n3p = TMP_ALLOC_LIMBS (MAX_N + 1);
  rp = 1 + TMP_ALLOC_LIMBS (MAX_N + 1);

  for (test = 0; test < count; test++)
    {
      unsigned size_min;
      unsigned size_range;
      mp_size_t n;
      mp_limb_t r_before, r_after;
      mp_limb_t res, expected;

      for (size_min = 1; (1L << size_min) < MIN_N; size_min++)
	;

      /* We generate n in the MIN_N <= n <= (1 << size_range). */
      size_range = size_min
	+ gmp_urandomm_ui (rands, SIZE_LOG + 1 - size_min);

      n = MIN_N
	+ gmp_urandomm_ui (rands, (1L << size_range) + 1 - MIN_N);

      mpn_random (np, n);

      mpn_random2 (rp - 1, n + 2);
      r_before = rp[-1];
      r_after = rp[n + 1];

      expected = mpn_mul_1 (n3p, np, n, 3);
      if ((n & 1) == 0)
	{
	  res = mpn_divexact_by3 (rp, n3p, n / 2);
	  res = mpn_divexact_by3c (rp + n / 2, n3p + n / 2, n / 2, res);
	}
      else
	res = mpn_divexact_by3c (rp, n3p, n, 0);

      if (rp[-1] != r_before || rp[n + 1] != r_after
	  || mpn_cmp (rp, np, n) != 0 || res != expected)
	{
	  printf ("ERROR in test %d, n = %d\n",
		  test, (int) n);
	  if (rp[-1] != r_before)
	    {
	      printf ("before rp:"); mpn_dump (rp - 1, 1);
	      printf ("keep:   "); mpn_dump (&r_before, 1);
	    }
	  if (rp[n + 1] != r_after)
	    {
	      printf ("after rp:"); mpn_dump (rp + n + 1, 1);
	      printf ("keep:   "); mpn_dump (&r_after, 1);
	    }
	  if (res != expected)
	    {
	      printf ("retval  :"); mpn_dump (&res, 1);
	      printf ("expected:"); mpn_dump (&expected, 1);
	    }
	  mpn_dump (np, n);
	  mpn_dump (n3p, n);
	  mpn_dump (rp, n);

	  abort();
	}
      ASSERT_ALWAYS (res < 3);
    }
  TMP_FREE;
  tests_end ();
  return 0;
}
_______________________________________________
gmp-devel mailing list
gmp-devel@gmplib.org
https://gmplib.org/mailman/listinfo/gmp-devel

Reply via email to