예제 #1
0
파일: misc.c 프로젝트: Cl3Kener/gmp
void
mpz_erandomb_nonzero (mpz_ptr rop, gmp_randstate_t rstate, unsigned long nbits)
{
  mpz_erandomb (rop, rstate, nbits);
  if (mpz_sgn (rop) == 0)
    mpz_set_ui (rop, 1L);
}
예제 #2
0
파일: t-div_2exp.c 프로젝트: AllardJ/Tomato
void
check_random (int argc, char *argv[])
{
  gmp_randstate_ptr  rands = RANDS;
  int            reps = 100;
  mpz_t          a;
  unsigned long  d;
  int            i;

  if (argc == 2)
    reps = atoi (argv[1]);

  mpz_init (a);

  for (i = 0; i < reps; i++)
    {
      /* exponentially within 2 to 257 bits */
      mpz_erandomb (a, rands, urandom () % 8 + 2);

      d = urandom () % 256;

      check_all (a, d);
    }

  mpz_clear (a);
}
예제 #3
0
파일: t-mtox.c 프로젝트: mahdiz/mpclib
void
check_random (void)
{
  mpz_t  z;
  int    i;
  char   *got, *want;
  gmp_randstate_ptr  rands = RANDS;

  mpz_init (z);

  for (i = 0; i < 1000; i++)
    {
      mpz_erandomb (z, rands, 6 * BITS_PER_MP_LIMB);
      got = mtox (z);
      want = mpz_get_str (NULL, 16, z);
      if (strcmp (got, want) != 0)
        {
          printf ("mtox wrong result\n");
          printf ("  got  \"%s\"\n", got);
          printf ("  want \"%s\"\n", want);
          abort ();
        }
      (*__gmp_free_func) (got, strlen (got) + 1);
      (*__gmp_free_func) (want, strlen (want) + 1);
    }

  mpz_clear (z);
}
예제 #4
0
void
check_rand (void)
{
  gmp_randstate_t  rands;
  unsigned long  got, want;
  int    i;
  mpz_t  x, y;

  mpz_init (x);
  mpz_init (y);
  gmp_randinit_default(rands);

  for (i = 0; i < 2000; i++)
    {
      mpz_erandomb (x, rands, 6 * GMP_NUMB_BITS);
      mpz_negrandom (x, rands);
      mpz_mul_2exp (x, x, urandom(rands) % (4 * GMP_NUMB_BITS));

      mpz_erandomb (y, rands, 6 * GMP_NUMB_BITS);
      mpz_negrandom (y, rands);
      mpz_mul_2exp (y, y, urandom(rands) % (4 * GMP_NUMB_BITS));

      want = refmpz_hamdist (x, y);
      got = mpz_hamdist (x, y);
      if (got != want)
        {
          printf    ("mpz_hamdist wrong on random\n");
          printf    ("  got  %lu\n", got);
          printf    ("  want %lu\n", want);
          mpz_trace ("  x   ", x);
          mpz_trace ("  y   ", y);
          abort();
        }
    }
  mpz_clear (x);
  mpz_clear (y);
  gmp_randclear(rands);
}
void
check_random (int argc, char *argv[])
{
  gmp_randstate_ptr rands = RANDS;
  int    reps = 5000;
  mpz_t  a, q, got;
  int    i, qneg;
  unsigned long  d;

  if (argc == 2)
    reps = atoi (argv[1]);

  mpz_init (a);
  mpz_init (q);
  mpz_init (got);

  for (i = 0; i < reps; i++)
    {
      d = (unsigned long) urandom();
      mpz_erandomb (q, rands, 512);
      mpz_mul_ui (a, q, d);

      for (qneg = 0; qneg <= 1; qneg++)
        {
          mpz_divexact_ui (got, a, d);
          MPZ_CHECK_FORMAT (got);
          if (mpz_cmp (got, q) != 0)
            {
              printf    ("mpz_divexact_ui wrong\n");
              mpz_trace ("    a", a);
              printf    ("    d=%lu\n", d);
              mpz_trace ("    q", q);
              mpz_trace ("  got", got);
              abort ();
            }

          mpz_neg (q, q);
          mpz_neg (a, a);
        }

    }

  mpz_clear (a);
  mpz_clear (q);
  mpz_clear (got);
}
예제 #6
0
파일: t-divis.c 프로젝트: BrianGladman/mpir
void
check_random (int reps)
{
  gmp_randstate_t rands;
  mpz_t   a, d, r;
  int     i;
  int     want;
 
  gmp_randinit_default(rands);
  mpz_init (a);
  mpz_init (d);
  mpz_init (r);

  for (i = 0; i < reps; i++)
    {
      mpz_erandomb (a, rands, 512);
      mpz_erandomb_nonzero (d, rands, 512);

      mpz_fdiv_r (r, a, d);

      want = (mpz_sgn (r) == 0);
      check_one (a, d, want);

      mpz_sub (a, a, r);
      check_one (a, d, 1);

      if (mpz_cmpabs_ui (d, 1L) == 0)
        continue;

      mpz_add_ui (a, a, 1L);
      check_one (a, d, 0);
    }

  mpz_clear (a);
  mpz_clear (d);
  mpz_clear (r);
  gmp_randclear(rands);
}
예제 #7
0
void
check_random (int reps)
{
  gmp_randstate_ptr rands = RANDS;
  mpz_t   a, d, r;
  int     i;
  int     want;

  mpz_init (a);
  mpz_init (d);
  mpz_init (r);

  for (i = 0; i < reps; i++)
    {
      mpz_erandomb (a, rands, 1 << 19);
      mpz_erandomb_nonzero (d, rands, 1 << 18);

      mpz_fdiv_r (r, a, d);

      want = (mpz_sgn (r) == 0);
      check_one (a, d, want);

      mpz_sub (a, a, r);
      check_one (a, d, 1);

      if (mpz_cmpabs_ui (d, 1L) == 0)
        continue;

      mpz_add_ui (a, a, 1L);
      check_one (a, d, 0);
    }

  mpz_clear (a);
  mpz_clear (d);
  mpz_clear (r);
}