コード例 #1
0
// generate a random mpz_mat_t with the given number of rows and columns and number of bits per entry
void mpz_randmat(mpz_mat_t mat, ulong r, ulong c, ulong maxbits)
{
   ulong bits;
   mpz_t temp;
   mpz_init(temp);
   
   long i;
   for (i = 0; i < r; i++)
   {
		long j;
		for (j = 0; j < c; j++)
		{
#if VARY_BITS
         bits = z_randint(maxbits+1);
#else
         bits = maxbits;
#endif
         if (bits == 0) mpz_set_ui(temp, 0);
         else 
         {
#if SPARSE
            if (z_randint(10) == 1) mpz_rrandomb(temp, randstate, bits);
            else mpz_set_ui(temp, 0);
#else
            mpz_rrandomb(temp, randstate, bits);
#endif
#if SIGNS
            if (z_randint(2)) mpz_neg(temp, temp);
#endif
         }
         mpz_set(mat->entries[i*c+j], temp);
		}
   }
   mpz_clear(temp);
} 
コード例 #2
0
ファイル: t-md_2exp.c プロジェクト: AlexeiSheplyakov/gmp.pkg
void
check_random ()
{
  gmp_randstate_ptr rands;
  mpz_t bs;
  unsigned long arg_size, size_range;
  mpq_t q, r;
  int i;
  mp_bitcnt_t shift;
  int reps = 10000;

  rands = RANDS;

  mpz_init (bs);
  mpq_init (q);
  mpq_init (r);

  for (i = 0; i < reps; i++)
    {
      mpz_urandomb (bs, rands, 32);
      size_range = mpz_get_ui (bs) % 11 + 2; /* 0..4096 bit operands */

      mpz_urandomb (bs, rands, size_range);
      arg_size = mpz_get_ui (bs);
      mpz_rrandomb (mpq_numref (q), rands, arg_size);
      do
	{
	  mpz_urandomb (bs, rands, size_range);
	  arg_size = mpz_get_ui (bs);
	  mpz_rrandomb (mpq_denref (q), rands, arg_size);
	}
      while (mpz_sgn (mpq_denref (q)) == 0);

      /* We now have a random rational in q, albeit an unnormalised one.  The
	 lack of normalisation should not matter here, so let's save the time a
	 gcd would require.  */

      mpz_urandomb (bs, rands, 32);
      shift = mpz_get_ui (bs) % 4096;

      mpq_mul_2exp (r, q, shift);

      if (mpq_cmp (r, q) < 0)
	{
	  printf ("mpq_mul_2exp wrong on random\n");
	  abort ();
	}

      mpq_div_2exp (r, r, shift);

      if (mpq_cmp (r, q) != 0)
	{
	  printf ("mpq_mul_2exp or mpq_div_2exp wrong on random\n");
	  abort ();
	}
    }
  mpq_clear (q);
  mpq_clear (r);
  mpz_clear (bs);
}
コード例 #3
0
ファイル: hex-random.c プロジェクト: AllardJ/Tomato
void hex_random_op2 (enum hex_random_op op,  unsigned long maxbits,
		     char **ap, char **rp)
{
  mpz_t a, r;
  unsigned long abits;
  unsigned signs;

  mpz_init (a);
  mpz_init (r);

  abits = gmp_urandomb_ui (state, 32) % maxbits;

  mpz_rrandomb (a, state, abits);

  signs = gmp_urandomb_ui (state, 1);
  if (signs & 1)
    mpz_neg (a, a);

  switch (op)
    {
    default:
      abort ();
    case OP_SQR:
      mpz_mul (r, a, a);
      break;
    }

  gmp_asprintf (ap, "%Zx", a);
  gmp_asprintf (rp, "%Zx", r);

  mpz_clear (a);
  mpz_clear (r);
}
コード例 #4
0
void mpz_mat_randintrel(mpz_mat_t mat, ulong r, ulong c, ulong maxbits)
{
   ulong bits;
   mpz_t temp;
   mpz_init(temp);

   long i;
   for (i = 0; i < r; i++)
   {
#if VARY_BITS
      bits = z_randint(maxbits+1);
#else
      bits = maxbits;
#endif
      if (bits == 0) mpz_set_ui(temp, 0);
      else 
      {
         mpz_rrandomb(temp, randstate, bits);
#if SIGNS
         if (z_randint(2)) mpz_neg(temp, temp);
#endif
      }
      mpz_set(mat->entries[i*c + c - 1], temp);
      long j;
      for (j = 0; j < i; j++)
         mpz_set_ui(mat->entries[i*c + j], 0);
      mpz_set_ui(mat->entries[i*c + i], 1);
      for (j = i + 1; j < c - 1; j++)
         mpz_set_ui(mat->entries[i*c + j], 0);
   }

   mpz_clear(temp);

}
コード例 #5
0
void randpoly_unsigned(mpz_poly_t pol, unsigned long length, unsigned long maxbits)
{
   unsigned long bits;
   mpz_t temp;
   mpz_init(temp);

   mpz_poly_zero(pol);
   
   unsigned long i;
   for (i = 0; i < length; i++)
   {
#if VARY_BITS
       bits = randint(maxbits);
#else
       bits = maxbits;
#endif
       if (bits == 0) mpz_set_ui(temp,0);
       else 
       {
          mpz_rrandomb(temp, randstate, bits);
       }
       mpz_poly_set_coeff(pol, i, temp);
       
   }
   
   mpz_clear(temp);
} 
コード例 #6
0
ファイル: gmpy2_random.c プロジェクト: godbomb/gmpy
static PyObject *
GMPy_MPZ_rrandomb_Function(PyObject *self, PyObject *args)
{
    MPZ_Object *result;
    mp_bitcnt_t len;

    if (PyTuple_GET_SIZE(args) != 2) {
        TYPE_ERROR("mpz_rrandomb() requires 2 arguments");
        return NULL;
    }

    if (!RandomState_Check(PyTuple_GET_ITEM(args, 0))) {
        TYPE_ERROR("mpz_rrandomb() requires 'random_state' and 'bit_count' arguments");
        return NULL;
    }

    len = mp_bitcnt_t_From_Integer(PyTuple_GET_ITEM(args, 1));
    if (len == (mp_bitcnt_t)(-1) && PyErr_Occurred()) {
        TYPE_ERROR("mpz_rrandomb() requires 'random_state' and 'bit_count' arguments");
        return NULL;
    }

    if ((result = GMPy_MPZ_New(NULL))) {
        mpz_rrandomb(result->z, RANDOM_STATE(PyTuple_GET_ITEM(args, 0)), len);
    }

    return (PyObject*)result;
}
コード例 #7
0
ファイル: t-gcd.c プロジェクト: AllardJ/Tomato
/* Test operands from a table of seed data.  This variant creates the operands
   using plain ol' mpz_rrandomb.  This is a hack for better coverage of the gcd
   code, which depends on that the random number generators give the exact
   numbers we expect.  */
void
check_kolmo1 (void)
{
  static const struct {
    unsigned int seed;
    int nb;
    const char *want;
  } data[] = {
    { 59618, 38208, "5"},
    { 76521, 49024, "3"},
    { 85869, 54976, "1"},
    { 99449, 63680, "1"},
    {112453, 72000, "1"}
  };

  gmp_randstate_t rs;
  mpz_t  bs, a, b, want;
  int    i, unb, vnb, nb;

  gmp_randinit_default (rs);

  mpz_inits (bs, a, b, want, NULL);

  for (i = 0; i < numberof (data); i++)
    {
      nb = data[i].nb;

      gmp_randseed_ui (rs, data[i].seed);

      mpz_urandomb (bs, rs, 32);
      unb = mpz_get_ui (bs) % nb;
      mpz_urandomb (bs, rs, 32);
      vnb = mpz_get_ui (bs) % nb;

      mpz_rrandomb (a, rs, unb);
      mpz_rrandomb (b, rs, vnb);

      mpz_set_str_or_abort (want, data[i].want, 0);

      one_test (a, b, want, -1);
    }

  mpz_clears (bs, a, b, want, NULL);
  gmp_randclear (rs);
}
コード例 #8
0
ファイル: t-nextprime.c プロジェクト: 119/aircam-openwrt
int
main (int argc, char **argv)
{
  int i;
  int reps = 20;
  gmp_randstate_ptr rands;
  mpz_t bs, x, nxtp, ref_nxtp;
  unsigned long size_range;

  tests_start();
  rands = RANDS;

  run ("2", 1000, "0x1ef7", diff1);

  run ("3", 1000 - 1, "0x1ef7", NULL);

  run ("0x8a43866f5776ccd5b02186e90d28946aeb0ed914", 50,
       "0x8a43866f5776ccd5b02186e90d28946aeb0eeec5", diff3);

  run ("0x10000000000000000000000000000000000000", 50,
       "0x100000000000000000000000000000000010ab", diff4);

  run ("0x1c2c26be55317530311facb648ea06b359b969715db83292ab8cf898d8b1b", 50,
       "0x1c2c26be55317530311facb648ea06b359b969715db83292ab8cf898da957", diff5);

  mpz_init (bs);
  mpz_init (x);
  mpz_init (nxtp);
  mpz_init (ref_nxtp);

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

  for (i = 0; i < reps; i++)
    {
      mpz_urandomb (bs, rands, 32);
      size_range = mpz_get_ui (bs) % 8 + 2; /* 0..1024 bit operands */

      mpz_urandomb (bs, rands, size_range);
      mpz_rrandomb (x, rands, mpz_get_ui (bs));

/*      gmp_printf ("%ld: %Zd\n", mpz_sizeinbase (x, 2), x); */

      mpz_nextprime (nxtp, x);
      refmpz_nextprime (ref_nxtp, x);
      if (mpz_cmp (nxtp, ref_nxtp) != 0)
	abort ();
    }

  mpz_clear (bs);
  mpz_clear (x);
  mpz_clear (nxtp);
  mpz_clear (ref_nxtp);

  tests_end ();
  return 0;
}
コード例 #9
0
ファイル: t-root.c プロジェクト: RodneyBates/M3Devel
int
main (int argc, char **argv)
{
  mpz_t x2;
  mpz_t root1;
  mp_size_t x2_size;
  int i;
  int reps = 5000;
  unsigned long nth;
  gmp_randstate_ptr rands;
  mpz_t bs;
  unsigned long bsi, size_range;

  tests_start ();
  rands = RANDS;

  mpz_init (bs);

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

  mpz_init (x2);
  mpz_init (root1);

  /* This triggers a gcc 4.3.2 bug */
  mpz_set_str (x2, "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80000000000000000000000000000000000000000000000000000000000000002", 16);
  mpz_root (root1, x2, 2);
  check_one (root1, x2, 2, -1);

  for (i = 0; i < reps; i++)
    {
      mpz_urandomb (bs, rands, 32);
      size_range = mpz_get_ui (bs) % 12 + 2;

      mpz_urandomb (bs, rands, size_range);
      x2_size = mpz_get_ui (bs) + 10;
      mpz_rrandomb (x2, rands, x2_size);

      mpz_urandomb (bs, rands, 15);
      nth = mpz_getlimbn (bs, 0) % mpz_sizeinbase (x2, 2) + 2;

      mpz_root (root1, x2, nth);

      mpz_urandomb (bs, rands, 4);
      bsi = mpz_get_ui (bs);
      if ((bsi & 1) != 0)
	{
	  /* With 50% probability, set x2 near a perfect power.  */
	  mpz_pow_ui (x2, root1, nth);
	  if ((bsi & 2) != 0)
	    {
	      mpz_sub_ui (x2, x2, bsi >> 2);
	      mpz_abs (x2, x2);
	    }
	  else
コード例 #10
0
ファイル: t-set_q.c プロジェクト: BrianGladman/mpir
void
check_rand (void)
{
  unsigned long  min_prec = __GMPF_BITS_TO_PREC (1);
  gmp_randstate_t  rands;
  unsigned long  prec;
  mpf_t  got;
  mpq_t  q;
  int    i;

  mpf_init (got);
  mpq_init (q);
  gmp_randinit_default(rands);

  for (i = 0; i < 400; i++)
    {
      /* result precision */
      prec = min_prec + gmp_urandomm_ui (rands, 20L);
      refmpf_set_prec_limbs (got, prec);

      /* num */
      prec = gmp_urandomm_ui (rands, 20L * GMP_NUMB_BITS);
      mpz_rrandomb (mpq_numref(q), rands, prec);

      /* possibly negative num */
      if (gmp_urandomb_ui (rands, 1L))
        mpz_neg (mpq_numref(q), mpq_numref(q));

      /* den, non-zero */
      do {
        prec = gmp_urandomm_ui (rands, 20L * GMP_NUMB_BITS);
        mpz_rrandomb (mpq_denref(q), rands, prec);
      } while (mpz_sgn (mpq_denref(q)) <= 0);

      check_one (got, q);
    }

  mpf_clear (got);
  mpq_clear (q);
  gmp_randclear(rands);
}
コード例 #11
0
ファイル: tget_z.c プロジェクト: BreakawayConsulting/mpfr
static void
check (void)
{
  mpz_t  z;

  mpz_init (z);

  mpz_set_ui (z, 0L);
  check_one (z);

  mpz_set_si (z, 123L);
  check_one (z);

  mpz_rrandomb (z, RANDS, 2*GMP_NUMB_BITS);
  check_one (z);

  mpz_rrandomb (z, RANDS, 5*GMP_NUMB_BITS);
  check_one (z);

  mpz_clear (z);
}
コード例 #12
0
ファイル: tget_z.c プロジェクト: Scorpiion/Renux_cross_gcc
static void
check (void)
{
  mpz_t  z;

  mpz_init (z);

  mpz_set_ui (z, 0L);
  check_one (z);

  mpz_set_si (z, 123L);
  check_one (z);

  mpz_rrandomb (z, RANDS, 2*BITS_PER_MP_LIMB);
  check_one (z);

  mpz_rrandomb (z, RANDS, 5*BITS_PER_MP_LIMB);
  check_one (z);

  mpz_clear (z);
}
コード例 #13
0
ファイル: t-root.c プロジェクト: mahdiz/mpclib
int
main (int argc, char **argv)
{
  mpz_t x2;
  mpz_t x;
  mpz_t temp, temp2;
  mp_size_t x2_size;
  int i;
  int reps = 1000;
  unsigned long nth;
  gmp_randstate_ptr rands;
  mpz_t bs;
  unsigned long bsi, size_range;

  tests_start ();
  rands = RANDS;

  mpz_init (bs);

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

  mpz_init (x2);
  mpz_init (x);
  mpz_init (temp);
  mpz_init (temp2);

  for (i = 0; i < reps; i++)
    {
      mpz_urandomb (bs, rands, 32);
      size_range = mpz_get_ui (bs) % 12 + 2;

      mpz_urandomb (bs, rands, size_range);
      x2_size = mpz_get_ui (bs) + 10;
      mpz_rrandomb (x2, rands, x2_size);

      mpz_urandomb (bs, rands, 15);
      nth = mpz_getlimbn (bs, 0) % mpz_sizeinbase (x2, 2) + 2;

      mpz_root (x, x2, nth);

      mpz_urandomb (bs, rands, 4);
      bsi = mpz_get_ui (bs);
      if ((bsi & 1) != 0)
	{
	  /* With 50% probability, set x2 near a perfect power.  */
	  mpz_pow_ui (x2, x, nth);
	  if ((bsi & 2) != 0)
	    {
	      mpz_sub_ui (x2, x2, bsi >> 2);
	      mpz_abs (x2, x2);
	    }
	  else
コード例 #14
0
ファイル: hex-random.c プロジェクト: AllardJ/Tomato
char *
hex_rrandomb (unsigned long bits)
{
  char *res;
  mpz_t x;

  mpz_init (x);
  mpz_rrandomb (x, state, bits);
  gmp_asprintf (&res, "%Zx", x);
  mpz_clear (x);
  return res;
}
コード例 #15
0
ファイル: t-mod_1.c プロジェクト: AlexeiSheplyakov/gmp.pkg
int
main (int argc, char **argv)
{
  gmp_randstate_ptr rands;
  int i;
  unsigned a_bits;
  unsigned b_bits;
  mpz_t a;
  mpz_t b;

  tests_start ();
  rands = RANDS;
  mpz_init (a);
  mpz_init (b);

  for (i = 0; i < 300; i++)
    {
      mp_size_t asize;
      a_bits = 1 + gmp_urandomm_ui (rands, 1000);
      b_bits = 1 + gmp_urandomm_ui (rands, GMP_NUMB_BITS);

      mpz_rrandomb (a, rands, a_bits);
      mpz_rrandomb (b, rands, b_bits);

      asize = SIZ(a);
      if (!asize)
	asize = 1;
      if (mpz_sgn (b) == 0)
	mpz_set_ui (b, 1);

      check_one (PTR(a), asize, PTR(b)[0]);
    }

  mpz_clear (a);
  mpz_clear (b);

  tests_end ();
  return 0;
}
コード例 #16
0
ファイル: hex-random.c プロジェクト: AllardJ/Tomato
char *
hex_rrandomb_export (void *dst, size_t *countp,
		     int order, size_t size, int endian, unsigned long bits)
{
  char *res;
  mpz_t x;
  mpz_init (x);
  mpz_rrandomb (x, state, bits);
  gmp_asprintf (&res, "%Zx", x);
  mpz_export (dst, countp, order, size, endian, 0, x);
  mpz_clear (x);
  return res;
}
コード例 #17
0
ファイル: t-get_d.c プロジェクト: AllardJ/Tomato
void
check_random (int argc, char **argv)
{
  gmp_randstate_ptr rands = RANDS;

  double d;
  mpq_t q;
  mpz_t a, t;
  int exp;

  int test, reps = 100000;

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

  mpq_init (q);
  mpz_init (a);
  mpz_init (t);

  for (test = 0; test < reps; test++)
    {
      mpz_rrandomb (a, rands, 53);
      mpz_urandomb (t, rands, 32);
      exp = mpz_get_ui (t) % (2*MAXEXP) - MAXEXP;

      d = my_ldexp (mpz_get_d (a), exp);
      mpq_set_d (q, d);
      /* Check that n/d = a * 2^exp, or
	 d*a 2^{exp} = n */
      mpz_mul (t, a, mpq_denref (q));
      if (exp > 0)
	mpz_mul_2exp (t, t, exp);
      else
	{
	  if (!mpz_divisible_2exp_p (t, -exp))
	    goto fail;
	  mpz_div_2exp (t, t, -exp);
	}
      if (mpz_cmp (t, mpq_numref (q)) != 0)
	{
	fail:
	  printf ("ERROR (check_random test %d): bad mpq_set_d results\n", test);
	  printf ("%.16g\n", d);
	  gmp_printf ("%Qd\n", q);
	  abort ();
	}
    }
  mpq_clear (q);
  mpz_clear (t);
  mpz_clear (a);
}
コード例 #18
0
ファイル: t-set_f.c プロジェクト: BrianGladman/mpir
void
check_various (void)
{gmp_randstate_t rands;
  mpz_t  z;

  mpz_init (z);
  gmp_randinit_default(rands);

  mpz_set_ui (z, 0L);
  check_one (z);

  mpz_set_si (z, 123L);
  check_one (z);

  mpz_rrandomb (z, rands, 2*BITS_PER_MP_LIMB);
  check_one (z);

  mpz_rrandomb (z, rands, 5*BITS_PER_MP_LIMB);
  check_one (z);

  mpz_clear (z);
  gmp_randclear(rands);
}
コード例 #19
0
ファイル: t-gcd.c プロジェクト: AllardJ/Tomato
void
make_chain_operands (mpz_t ref, mpz_t a, mpz_t b, gmp_randstate_t rs, int nb1, int nb2, int chain_len)
{
  mpz_t bs, temp1, temp2;
  int j;

  mpz_inits (bs, temp1, temp2, NULL);

  /* Generate a division chain backwards, allowing otherwise unlikely huge
     quotients.  */

  mpz_set_ui (a, 0);
  mpz_urandomb (bs, rs, 32);
  mpz_urandomb (bs, rs, mpz_get_ui (bs) % nb1 + 1);
  mpz_rrandomb (b, rs, mpz_get_ui (bs));
  mpz_add_ui (b, b, 1);
  mpz_set (ref, b);

  for (j = 0; j < chain_len; j++)
    {
      mpz_urandomb (bs, rs, 32);
      mpz_urandomb (bs, rs, mpz_get_ui (bs) % nb2 + 1);
      mpz_rrandomb (temp2, rs, mpz_get_ui (bs) + 1);
      mpz_add_ui (temp2, temp2, 1);
      mpz_mul (temp1, b, temp2);
      mpz_add (a, a, temp1);

      mpz_urandomb (bs, rs, 32);
      mpz_urandomb (bs, rs, mpz_get_ui (bs) % nb2 + 1);
      mpz_rrandomb (temp2, rs, mpz_get_ui (bs) + 1);
      mpz_add_ui (temp2, temp2, 1);
      mpz_mul (temp1, a, temp2);
      mpz_add (b, b, temp1);
    }

  mpz_clears (bs, temp1, temp2, NULL);
}
コード例 #20
0
void mpz_mat_randajtai(mpz_mat_t mat, ulong r, ulong c, double alpha)
{
   ulong i, j, d, bits;
   mpz_t tmp;

   r = mat->r;
   c = mat->c;
   d = r;

   if (c != r)
   {
      printf("Exception: mpz_mat_ajtai called on an ill-formed matrix\n");
      abort();
   }

   mpz_init(tmp);

   for (i = 0; i < d; i++)
   {
      bits = (ulong) pow((double) (2*d - i), alpha);

      mpz_rrandomb(mat->entries[i*c + i], randstate, bits);
      mpz_add_ui(mat->entries[i*c + i], mat->entries[i*c + i], 2);
      mpz_fdiv_q_2exp(mat->entries[i*c + i], mat->entries[i*c + i], 1);

	   for (j = i + 1; j < d; j++)
      {
         mpz_rrandomb(mat->entries[j*c + i], randstate, bits);
         if (z_randint(2))
            mpz_neg(mat->entries[j*c + i], mat->entries[j*c + i]);
         mpz_set_ui(mat->entries[i*c + j], 0L);
      }
   }

   mpz_clear(tmp);
}
コード例 #21
0
ファイル: t-perfsqr.c プロジェクト: STAR111/GCC_parser
/* Exercise mpz_perfect_square_p compared to what mpz_sqrt says. */
void
check_sqrt (int reps)
{
  mpz_t x2, x2t, x;
  mp_size_t x2n;
  int res;
  int i;
  /* int cnt = 0; */
  gmp_randstate_ptr rands = RANDS;
  mpz_t bs;

  mpz_init (bs);

  mpz_init (x2);
  mpz_init (x);
  mpz_init (x2t);

  for (i = 0; i < reps; i++)
    {
      mpz_urandomb (bs, rands, 9);
      x2n = mpz_get_ui (bs);
      mpz_rrandomb (x2, rands, x2n);
      /* mpz_out_str (stdout, -16, x2); puts (""); */

      res = mpz_perfect_square_p (x2);
      mpz_sqrt (x, x2);
      mpz_mul (x2t, x, x);

      if (res != (mpz_cmp (x2, x2t) == 0))
        {
          printf    ("mpz_perfect_square_p and mpz_sqrt differ\n");
          mpz_trace ("   x  ", x);
          mpz_trace ("   x2 ", x2);
          mpz_trace ("   x2t", x2t);
          printf    ("   mpz_perfect_square_p %d\n", res);
          printf    ("   mpz_sqrt             %d\n", mpz_cmp (x2, x2t) == 0);
          abort ();
        }

      /* cnt += res != 0; */
    }
  /* printf ("%d/%d perfect squares\n", cnt, reps); */

  mpz_clear (bs);
  mpz_clear (x2);
  mpz_clear (x);
  mpz_clear (x2t);
}
コード例 #22
0
ファイル: gmprandstate.c プロジェクト: sagmor/gmp
/*
 * call-seq:
 *   rand_state.rrandomb(fixnum)
 *
 * From the GMP Manual:
 *
 * Generate a random integer with long strings of zeros and ones in the binary
 * representation. Useful for testing functions and algorithms, since this kind
 * of random numbers have proven to be more likely to trigger corner-case bugs.
 * The random number will be in the range 0 to 2^n-1, inclusive. 
 */
VALUE r_gmprandstate_rrandomb(VALUE self, VALUE arg)
{
  MP_RANDSTATE *self_val;
  MP_INT *res_val;
  VALUE res;

  mprandstate_get_struct(self,self_val);

  if (FIXNUM_P(arg)) {
    mpz_make_struct_init(res, res_val);
    mpz_rrandomb(res_val, self_val, FIX2INT(arg));
  } else {
    typeerror(X);
  }

  return res;
}
コード例 #23
0
void F_mpz_test_random(F_mpz_t f, ulong bits)
{
	if (bits == 0)
	{
		F_mpz_zero(f);
      return;
	}
	
	mpz_t temp;
	mpz_init(temp);
	
	mpz_rrandomb(temp, randstate, bits);
#if SIGNS
	if (z_randint(2)) mpz_neg(temp, temp);
#endif
   
	F_mpz_set_mpz(f, temp);

   mpz_clear(temp);
}
コード例 #24
0
static void
test_modulo (gmp_randstate_t rands, const char *name,
	     const struct ecc_modulo *m, unsigned count)
{
  mpz_t r;
  unsigned j;

  mpz_init (r);

  for (j = 0; j < count; j++)
    {
      if (j & 1)
	mpz_rrandomb (r, rands, 2*m->size * GMP_NUMB_BITS);
      else
	mpz_urandomb (r, rands, 2*m->size * GMP_NUMB_BITS);

      test_one (name, m, r);
    }
  mpz_clear (r);
}
コード例 #25
0
ファイル: t-popcount.c プロジェクト: BrianGladman/mpir
void
check_random (void)
{
  gmp_randstate_t rands;
  mpz_t bs;
  mpz_t arg;
  unsigned long arg_size, size_range;
  unsigned long got, ref;
  int i;

  gmp_randinit_default(rands);

  mpz_init (bs);
  mpz_init (arg);

  for (i = 0; i < 10000; i++)
    {
      mpz_urandomb (bs, rands, 32);
      size_range = mpz_get_ui (bs) % 11 + 2; /* 0..4096 bit operands */

      mpz_urandomb (bs, rands, size_range);
      arg_size = mpz_get_ui (bs);
      mpz_rrandomb (arg, rands, arg_size);

      got = mpz_popcount (arg);
      ref = refmpz_popcount (arg);
      if (got != ref)
	{
          printf ("mpz_popcount wrong on random\n"); 
          printf ("         ");   mpz_out_str (stdout, 10, arg); printf ("\n");
          printf ("         0x"); mpz_out_str (stdout, 16, arg); printf ("\n");
          printf ("   got   %lu\n", got);
          printf ("   want  %lu\n", ref);
          abort();                                    
	  abort ();
	}
    }
  mpz_clear (arg);
  mpz_clear (bs);
  gmp_randclear(rands);
}
コード例 #26
0
ファイル: hex-random.c プロジェクト: AllardJ/Tomato
void
hex_random_str_op (unsigned long maxbits,
		   int base, char **ap, char **rp)
{
  mpz_t a;
  unsigned long abits;
  unsigned signs;

  mpz_init (a);

  abits = gmp_urandomb_ui (state, 32) % maxbits;

  mpz_rrandomb (a, state, abits);

  signs = gmp_urandomb_ui (state, 2);
  if (signs & 1)
    mpz_neg (a, a);

  *ap = mpz_get_str (NULL, 16, a);
  *rp = mpz_get_str (NULL, base, a);

  mpz_clear (a);
}
コード例 #27
0
ファイル: t-pow.c プロジェクト: 119/aircam-openwrt
void
check_random (int reps)
{
  mpz_t              base, want;
  mp_size_t          base_size;
  int                i;
  unsigned long      size_range, exp;
  gmp_randstate_ptr  rands = RANDS;

  mpz_init (base);
  mpz_init (want);

  for (i = 0; i < reps; i++)
    {
      /* exponentially random 0 to 2^13 bits for base */
      mpz_urandomb (want, rands, 32);
      size_range = mpz_get_ui (want) % 12 + 2;
      mpz_urandomb (want, rands, size_range);
      base_size = mpz_get_ui (want);
      mpz_rrandomb (base, rands, base_size);

      /* randomly signed base */
      mpz_urandomb (want, rands, 2);
      if ((mpz_get_ui (want) & 1) != 0)
	mpz_neg (base, base);

      /* random 5 bits for exponent */
      mpz_urandomb (want, rands, 5L);
      exp = mpz_get_ui (want);

      refmpz_pow_ui (want, base, exp);
      check_one (want, base, exp);
    }

  mpz_clear (base);
  mpz_clear (want);
}
コード例 #28
0
ファイル: hex-random.c プロジェクト: AllardJ/Tomato
void
hex_random_scan_op (enum hex_random_op op, unsigned long maxbits,
		    char **ap, unsigned long *b, unsigned long *r)
{
  mpz_t a;
  unsigned long abits, bbits;
  unsigned signs;

  mpz_init (a);

  abits = gmp_urandomb_ui (state, 32) % maxbits;
  bbits = gmp_urandomb_ui (state, 32) % (maxbits + 100);

  mpz_rrandomb (a, state, abits);

  signs = gmp_urandomb_ui (state, 1);
  if (signs & 1)
    mpz_neg (a, a);

  switch (op)
    {
    default:
      abort ();

    case OP_SCAN0:
      *r = mpz_scan0 (a, bbits);
      break;
    case OP_SCAN1:
      *r = mpz_scan1 (a, bbits);
      break;
    }
  gmp_asprintf (ap, "%Zx", a);
  *b = bbits;

  mpz_clear (a);
}
コード例 #29
0
int
main (int argc, char **argv)
{
    mpz_t op1, op2;
    mp_size_t size;
    int i;
    int reps = 10000;
    char *str, *buf;
    int base;
    gmp_randstate_ptr rands;
    mpz_t bs;
    unsigned long bsi, size_range;

    tests_start ();
    rands = RANDS;

    mpz_init (bs);

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

    mpz_init (op1);
    mpz_init (op2);

    for (i = 0; i < reps; i++)
    {
        /* 1. Generate random mpz_t and convert to a string and back to mpz_t
        again.  */
        mpz_urandomb (bs, rands, 32);
        size_range = mpz_get_ui (bs) % 12 + 2;	/* 2..13 */
        mpz_urandomb (bs, rands, size_range);	/* 3..8191 bits */
        size = mpz_get_ui (bs);
        mpz_rrandomb (op1, rands, size);

        mpz_urandomb (bs, rands, 1);
        bsi = mpz_get_ui (bs);
        if ((bsi & 1) != 0)
            mpz_neg (op1, op1);

        mpz_urandomb (bs, rands, 32);
        bsi = mpz_get_ui (bs);
        base = bsi % 62 + 1;
        if (base == 1)
            base = 0;

        str = mpz_get_str ((char *) 0, base, op1);
        mpz_set_str_or_abort (op2, str, base);

        if (mpz_cmp (op1, op2))
        {
            fprintf (stderr, "ERROR, op1 and op2 different in test %d\n", i);
            fprintf (stderr, "str  = %s\n", str);
            fprintf (stderr, "base = %d\n", base);
            fprintf (stderr, "op1  = ");
            debug_mp (op1, -16);
            fprintf (stderr, "op2  = ");
            debug_mp (op2, -16);
            abort ();
        }

        (*__gmp_free_func) (str, strlen (str) + 1);

#if 0
        /* 2. Generate random string and convert to mpz_t and back to a string
        again.  */
        mpz_urandomb (bs, rands, 32);
        size_range = mpz_get_ui (bs) % 10 + 2;	/* 2..11 */
        mpz_urandomb (bs, rands, size_range);	/* 3..2047 bits */
        len = mpz_get_ui (bs);
        buf = (*__gmp_allocate_func) (len + 1);
        string_urandomb (buf, len, base);
        mpz_set_str_or_abort (op1, buf, base);
        str = mpz_get_str ((char *) 0, base, op1);

        if (strcmp (str, buf) != 0)
        {
            fprintf (stderr, "ERROR, str and buf different\n");
            fprintf (stderr, "str  = %s\n", str);
            fprintf (stderr, "buf  = %s\n", buf);
            fprintf (stderr, "base = %d\n", base);
            fprintf (stderr, "op1  = ");
            debug_mp (op1, -16);
            abort ();
        }

        (*__gmp_free_func) (buf, len + 1);
        (*__gmp_free_func) (str, strlen (str) + 1);
#endif
    }

    mpz_clear (bs);
    mpz_clear (op1);
    mpz_clear (op2);

    tests_end ();
    exit (0);
}
コード例 #30
0
int
main (int argc, char **argv)
{
  mpz_t op1, op2, ref;
  int i, j, chain_len;
  gmp_randstate_ptr rands;
  mpz_t bs;
  unsigned long bsi, size_range;
  int reps = 200;

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

  tests_start ();
  rands = RANDS;

  check_data ();

  mpz_init (bs);
  mpz_init (op1);
  mpz_init (op2);
  mpz_init (ref);
  mpz_init (gcd1);
  mpz_init (gcd2);
  mpz_init (temp1);
  mpz_init (temp2);
  mpz_init (s);
  mpz_init (t);

  for (i = 0; i < reps; i++)
    {
      /* Generate plain operands with unknown gcd.  These types of operands
	 have proven to trigger certain bugs in development versions of the
	 gcd code.  The "hgcd->row[3].rsize > M" ASSERT is not triggered by
	 the division chain code below, but that is most likely just a result
	 of that other ASSERTs are triggered before it.  */

      mpz_urandomb (bs, rands, 32);
      size_range = mpz_get_ui (bs) % 13 + 2;

      mpz_urandomb (bs, rands, size_range);
      mpz_urandomb (op1, rands, mpz_get_ui (bs) + MIN_OPERAND_BITSIZE);
      mpz_urandomb (bs, rands, size_range);
      mpz_urandomb (op2, rands, mpz_get_ui (bs) + MIN_OPERAND_BITSIZE);

      mpz_urandomb (bs, rands, 2);
      bsi = mpz_get_ui (bs);
      if ((bsi & 1) != 0)
	mpz_neg (op1, op1);
      if ((bsi & 2) != 0)
	mpz_neg (op2, op2);

      one_test (op1, op2, NULL, i);

      /* Generate a division chain backwards, allowing otherwise unlikely huge
	 quotients.  */

      mpz_set_ui (op1, 0);
      mpz_urandomb (bs, rands, 32);
      mpz_urandomb (bs, rands, mpz_get_ui (bs) % 16 + 1);
      mpz_rrandomb (op2, rands, mpz_get_ui (bs));
      mpz_add_ui (op2, op2, 1);
      mpz_set (ref, op2);

      mpz_urandomb (bs, rands, 32);
      chain_len = mpz_get_ui (bs) % 50;

      for (j = 0; j < chain_len; j++)
	{
	  mpz_urandomb (bs, rands, 32);
	  mpz_urandomb (bs, rands, mpz_get_ui (bs) % 12 + 1);
	  mpz_rrandomb (temp2, rands, mpz_get_ui (bs) + 1);
	  mpz_add_ui (temp2, temp2, 1);
	  mpz_mul (temp1, op2, temp2);
	  mpz_add (op1, op1, temp1);

	  /* Don't generate overly huge operands.  */
	  if (SIZ (op1) > 400)
	    break;

	  mpz_urandomb (bs, rands, 32);
	  mpz_urandomb (bs, rands, mpz_get_ui (bs) % 12 + 1);
	  mpz_rrandomb (temp2, rands, mpz_get_ui (bs) + 1);
	  mpz_add_ui (temp2, temp2, 1);
	  mpz_mul (temp1, op1, temp2);
	  mpz_add (op2, op2, temp1);

	  /* Don't generate overly huge operands.  */
	  if (SIZ (op2) > 400)
	    break;
	}
      one_test (op1, op2, ref, i);
    }

  mpz_clear (bs);
  mpz_clear (op1);
  mpz_clear (op2);
  mpz_clear (ref);
  mpz_clear (gcd1);
  mpz_clear (gcd2);
  mpz_clear (temp1);
  mpz_clear (temp2);
  mpz_clear (s);
  mpz_clear (t);

  tests_end ();
  exit (0);
}