예제 #1
0
PointRef PointCreateInvert(PointRef p, CurveRef curve)
{
	if (PointIsTeta(p))
		return PointCreateTeta();
	
    PointRef q = PointCopy(p);
	
	//	P = (xp, yp)
	// -P = (xp, - yp - a1.xp - a3)
	
	mpz_t mon;
	
	// mon = a1.xp
	mpz_init_set(mon, curve->a[1]);
	mpz_mul(mon, mon, q->x);
	mpz_mod(mon, mon, curve->mod);
	
	// yp = -yp
	mpz_neg(q->y, q->y);
	mpz_mod(q->y, q->y, curve->mod);
	
	// yp = -yp - a1.xp
	mpz_sub(q->y, q->y, mon);
	mpz_mod(q->y, q->y, curve->mod);
	
	// yp = -yp - a1.xp - a3
	mpz_sub(q->y, q->y, curve->a[3]);
	mpz_mod(q->y, q->y, curve->mod);
	
	mpz_clear(mon);
	
    return q;
}
예제 #2
0
// generate a dense random mpz_mat_t with up to the given length and number of bits per entry
void mpz_randmat_dense(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 
         {
            mpz_rrandomb(temp, randstate, bits);
#if SIGNS
            if (z_randint(2)) mpz_neg(temp, temp);
#endif
         }
         mpz_set(mat->entries[i*c+j], temp);
		}
   }
   mpz_clear(temp);
} 
예제 #3
0
void randpoly(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);
#if SIGNS
          if (randint(2)) mpz_neg(temp,temp);
#endif
       }
       mpz_poly_set_coeff(pol, i, temp);
       
   }
   
   mpz_clear(temp);
} 
예제 #4
0
void
check_all (mpz_ptr a, unsigned long d)
{
    check_one (a, d);
    mpz_neg (a, a);
    check_one (a, d);
}
예제 #5
0
파일: ecc_curve.c 프로젝트: rfsbarreto/ECC
ecc_point* sum(ecc_point p1,ecc_point p2){
	ecc_point* result;
	result = malloc(sizeof(ecc_point));
	mpz_init((*result).x);
	mpz_init((*result).y);
	if (mpz_cmp(p1.x,p2.x)==0 && mpz_cmp(p1.y,p2.y)==0)
		result=double_p(p1);
	else
		if( mpz_cmp(p1.x,p2.x)==0 && mpz_cmpabs(p2.y,p1.y)==0)
			result=INFINITY_POINT;
		else{
			mpz_t delta_x,x,y,delta_y,s,s_2;
			mpz_init(delta_x);
			mpz_init(x); mpz_init(y);
			mpz_init(s); mpz_init(s_2);
			mpz_init(delta_y);
			mpz_sub(delta_x,p1.x,p2.x);
			mpz_sub(delta_y,p1.y,p2.y);
			mpz_mod(delta_x,delta_x,prime);
			mpz_invert(delta_x,delta_x,prime);
			mpz_mul(s,delta_x,delta_y);
			mpz_mod(s,s,prime);
			mpz_pow_ui(s_2,s,2);
			mpz_sub(x,s_2,p1.x);
			mpz_sub(x,x,p2.x);
			mpz_mod(x,x,prime);
			mpz_set((*result).x,x);
			mpz_sub(delta_x,p2.x,x);
			mpz_neg(y,p2.y);
			mpz_addmul(y,s,delta_x);
			mpz_mod(y,y,prime);
			mpz_set((*result).y,y);
		};
	return result;	
}
예제 #6
0
파일: ecc_curve.c 프로젝트: rfsbarreto/ECC
ecc_point* double_p(ecc_point p){
	ecc_point* result;
	result= malloc(sizeof(ecc_point));
	mpz_init((*result).x);
	mpz_init((*result).y);
	printf("DP ");
	if (mpz_cmp_ui(p.y,0)!=0){
		mpz_t s,d_y,d_x,y;
		mpz_init(d_y);
		mpz_init(s);
		mpz_init(y);
		mpz_init(d_x);
		mpz_pow_ui(s,p.x,2);
		mpz_mul_si(s,s,3);
		mpz_add(s,s,a);
		mpz_mul_si(d_y,p.y,2);
		mpz_mod(d_y,d_y,prime);
		mpz_invert(d_y,d_y,prime);
		mpz_mul(s,s,d_y);
		mpz_mod(s,s,prime);	
		mpz_mul_ui(d_x,p.x,2);
		mpz_pow_ui((*result).x,s,2);
		mpz_sub((*result).x,(*result).x,d_x);
		mpz_mod((*result).x,(*result).x,prime);
		mpz_neg((*result).y,p.y);
		mpz_sub(d_x,p.x,(*result).x);
		mpz_mul(s,s,d_x);
		mpz_add((*result).y,(*result).y,s);
		mpz_mod((*result).y,(*result).y,prime);
	}else
		result=INFINITY_POINT;
	return result;
}
예제 #7
0
static int neg(void *a, void *b)
{
   LTC_ARGCHK(a != NULL);
   LTC_ARGCHK(b != NULL);
   mpz_neg(b, a);
   return CRYPT_OK;
}
예제 #8
0
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);
}
예제 #9
0
void mpfr_squaring_int_exp(mpfr_t R, mpfr_t x, mpz_t a)
{
	if(mpz_cmp_ui(a, 0) < 0)
	{
		mpz_t b;
		mpz_init_set(b, a);
		mpz_neg(b, b);
		mpfr_squaring_int_exp(R, x, b);
		mpfr_ui_div(R, 1, R, MPFR_RNDN);
	}
	else
	{
		mpfr_t y;
		mpz_t n;
		mpfr_init_set(y, x, MPFR_RNDN);
		mpfr_set_ui(R, 1, MPFR_RNDN);
		mpz_init_set(n, a);
		while(mpz_cmp_ui(n, 0) > 0)
		{
			if(mpz_odd_p(n))
			{
				mpfr_mul(R, R, y, MPFR_RNDN);
				mpz_sub_ui(n, n, 1);
			}
			mpfr_mul(y, y, y, MPFR_RNDN);
			mpz_div_ui(n, n, 2);
		}
	}
}
예제 #10
0
void
check_twobits (void)
{
  unsigned long  i, j, got, want;
  mpz_t  x, y;

  mpz_init (x);
  mpz_init (y);
  for (i = 0; i < 5 * GMP_NUMB_BITS; i++)
    {
      for (j = 0; j < 5 * GMP_NUMB_BITS; j++)
        {
          mpz_set_ui (x, 0L);
          mpz_setbit (x, i);
          mpz_set_ui (y, 0L);
          mpz_setbit (y, j);

          want = 2 * (i != j);
          got = mpz_hamdist (x, y);
          if (got != want)
            {
              printf    ("mpz_hamdist wrong on 2 bits pos/pos\n");
            wrong:
              printf    ("  i    %lu\n", i);
              printf    ("  j    %lu\n", j);
              printf    ("  got  %lu\n", got);
              printf    ("  want %lu\n", want);
              mpz_trace ("  x   ", x);
              mpz_trace ("  y   ", y);
              abort();
            }

          mpz_neg (x, x);
          mpz_neg (y, y);
          want = ABS ((long) (i-j));
          got = mpz_hamdist (x, y);
          if (got != want)
            {
              printf    ("mpz_hamdist wrong on 2 bits neg/neg\n");
              goto wrong;
            }
        }

    }
  mpz_clear (x);
  mpz_clear (y);
}
예제 #11
0
파일: misc.c 프로젝트: Cl3Kener/gmp
void
mpz_negrandom (mpz_ptr rop, gmp_randstate_t rstate)
{
  mp_limb_t  n;
  _gmp_rand (&n, rstate, 1);
  if (n != 0)
    mpz_neg (rop, rop);
}
예제 #12
0
파일: big.o.c 프로젝트: hoobaa/mecl
cl_object
_ecl_big_negate(cl_object a)
{
        cl_index size_a = ECL_BIGNUM_ABS_SIZE(a);
        cl_object z = _ecl_alloc_compact_bignum(size_a);
        mpz_neg(z->big.big_num, a->big.big_num);
        return big_normalize(z);
}
예제 #13
0
void mp2gmp(z *src, mpz_t dest) {

	mpz_import(dest, (size_t)(abs(src->size)), -1, sizeof(fp_digit), 
			0, (size_t)0, src->val);

	if (src->size < 0)
		mpz_neg(dest, dest);
}
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);
}
예제 #15
0
static void
oppose_constraint (CloogMatrix *m, int row)
{
  int k;

  /* Do not oppose the first column: it is the eq/ineq one.  */
  for (k = 1; k < m->NbColumns; k++)
    mpz_neg (m->p[row][k], m->p[row][k]);
}
예제 #16
0
파일: long.cpp 프로젝트: lameiro/pyston
extern "C" PyObject* PyLong_FromString(const char* str, char** pend, int base) noexcept {
    RELEASE_ASSERT(pend == NULL, "unsupported");

    int sign = 1;
    if ((base != 0 && base < 2) || base > 36) {
        PyErr_SetString(PyExc_ValueError, "long() arg 2 must be >= 2 and <= 36");
        return NULL;
    }
    while (*str != '\0' && isspace(Py_CHARMASK(*str)))
        str++;
    if (*str == '+')
        ++str;
    else if (*str == '-') {
        ++str;
        sign = -1;
    }
    while (*str != '\0' && isspace(Py_CHARMASK(*str)))
        str++;
    if (base == 0) {
        /* No base given.  Deduce the base from the contents
           of the string */
        if (str[0] != '0')
            base = 10;
        else if (str[1] == 'x' || str[1] == 'X')
            base = 16;
        else if (str[1] == 'o' || str[1] == 'O')
            base = 8;
        else if (str[1] == 'b' || str[1] == 'B')
            base = 2;
        else
            /* "old" (C-style) octal literal, still valid in
               2.x, although illegal in 3.x */
            base = 8;
    }
    /* Whether or not we were deducing the base, skip leading chars
       as needed */
    if (str[0] == '0'
        && ((base == 16 && (str[1] == 'x' || str[1] == 'X')) || (base == 8 && (str[1] == 'o' || str[1] == 'O'))
            || (base == 2 && (str[1] == 'b' || str[1] == 'B'))))
        str += 2;


    BoxedLong* rtn = new BoxedLong();
    if (str[strlen(str) - 1] == 'L') {
        std::string without_l(str, strlen(str) - 1);
        int r = mpz_init_set_str(rtn->n, without_l.c_str(), base);
        RELEASE_ASSERT(r == 0, "");
    } else {
        int r = mpz_init_set_str(rtn->n, str, base);
        RELEASE_ASSERT(r == 0, "");
    }

    if (sign == -1)
        mpz_neg(rtn->n, rtn->n);

    return rtn;
}
예제 #17
0
Value Bignum::negate() const
{
  mpz_t result;
  mpz_init(result);
  mpz_neg(result, _z);
  Value value = normalize(result);
  MPZ_CLEAR(result);
  return value;
}
예제 #18
0
파일: tdiv_qr.c 프로젝트: clear731/lattice
void
fmpz_tdiv_qr(fmpz_t f, fmpz_t s, const fmpz_t g, const fmpz_t h)
{
    fmpz c1 = *g;
    fmpz c2 = *h;

    if (fmpz_is_zero(h))
    {
        flint_printf("Exception: division by zero in fmpz_tdiv_qr\n");
        abort();
    }

    if (!COEFF_IS_MPZ(c1))      /* g is small */
    {
        if (!COEFF_IS_MPZ(c2))  /* h is also small */
        {
            fmpz q = c1 / c2;   /* compute C quotient */
            fmpz r = c1 - c2 * q;   /* compute remainder */

            fmpz_set_si(f, q);
            fmpz_set_si(s, r);
        }
        else                    /* h is large and g is small */
        {
            fmpz_set_ui(f, WORD(0)); /* g is zero */
            fmpz_set_si(s, c1);
        }
    }
    else                        /* g is large */
    {
        __mpz_struct *mpz_ptr, *mpz_ptr2;

        _fmpz_promote(f); /* must not hang on to ptr whilst promoting s */
        mpz_ptr2 = _fmpz_promote(s);
		mpz_ptr  = COEFF_TO_PTR(*f);

		if (!COEFF_IS_MPZ(c2))  /* h is small */
        {
            if (c2 > 0)         /* h > 0 */
            {
                flint_mpz_tdiv_qr_ui(mpz_ptr, mpz_ptr2, COEFF_TO_PTR(c1), c2);
            }
            else
            {
                flint_mpz_tdiv_qr_ui(mpz_ptr, mpz_ptr2, COEFF_TO_PTR(c1), -c2);
                mpz_neg(mpz_ptr, mpz_ptr);
            }
        }
        else                    /* both are large */
        {
            mpz_tdiv_qr(mpz_ptr, mpz_ptr2, COEFF_TO_PTR(c1), COEFF_TO_PTR(c2));
        }
        _fmpz_demote_val(f);    /* division by h may result in small value */
        _fmpz_demote_val(s);    /* division by h may result in small value */
    }
}
예제 #19
0
파일: fdiv_q.c 프로젝트: goens/flint2
void
fmpz_fdiv_q(fmpz_t f, const fmpz_t g, const fmpz_t h)
{
    fmpz c1 = *g;
    fmpz c2 = *h;

    if (fmpz_is_zero(h))
    {
        printf("Exception: division by zero in fmpz_fdiv_q\n");
        abort();
    }

    if (!COEFF_IS_MPZ(c1))      /* g is small */
    {
        if (!COEFF_IS_MPZ(c2))  /* h is also small */
        {
            fmpz q = c1 / c2;       /* compute C quotient */
            fmpz r = c1 - c2 * q;   /* compute remainder */

            if (r && (c2 ^ r) < 0L)
                --q;

            fmpz_set_si(f, q);
        }
        else                    /* h is large and g is small */
        {
            if ((c1 > 0L && fmpz_sgn(h) < 0) || (c1 < 0L && fmpz_sgn(h) > 0))  /* signs are the same */
                fmpz_set_si(f, -1L);   /* quotient is negative, round down to minus one */
            else 
                fmpz_zero(f);
        }
    }
    else                        /* g is large */
    {
        __mpz_struct *mpz_ptr = _fmpz_promote(f);

        if (!COEFF_IS_MPZ(c2))  /* h is small */
        {
            if (c2 > 0)         /* h > 0 */
            {
                mpz_fdiv_q_ui(mpz_ptr, COEFF_TO_PTR(c1), c2);
            }
            else
            {
                mpz_cdiv_q_ui(mpz_ptr, COEFF_TO_PTR(c1), -c2);
                mpz_neg(mpz_ptr, mpz_ptr);
            }
        }
        else                    /* both are large */
        {
            mpz_fdiv_q(mpz_ptr, COEFF_TO_PTR(c1), COEFF_TO_PTR(c2));
        }
        _fmpz_demote_val(f);    /* division by h may result in small value */
    }
}
예제 #20
0
/* Check that hardware rounding doesn't make mpz_get_d_2exp return a value
   outside its defined range. */
static void
check_round (void)
{
  static const unsigned long data[] = { 1, 32, 53, 54, 64, 128, 256, 512 };
  mpz_t   z;
  double  got;
  long    got_exp;
  int     i, rnd_mode, old_rnd_mode;

  mpz_init (z);
  old_rnd_mode = tests_hardware_getround ();

  for (rnd_mode = 0; rnd_mode < 4; rnd_mode++)
    {
      tests_hardware_setround (rnd_mode);

      for (i = 0; i < numberof (data); i++)
        {
          mpz_set_ui (z, 1L);
          mpz_mul_2exp (z, z, data[i]);
          mpz_sub_ui (z, z, 1L);

          got = mpz_get_d_2exp (&got_exp, z);
          if (got < 0.5 || got >= 1.0)
            {
              printf    ("mpz_get_d_2exp wrong on 2**%lu-1\n", data[i]);
              printf    ("result out of range, expect 0.5 <= got < 1.0\n");
              printf    ("   rnd_mode = %d\n", rnd_mode);
              printf    ("   data[i]  = %lu\n", data[i]);
              mpz_trace ("   z    ", z);
              d_trace   ("   got  ", got);
              printf    ("   got exp  %ld\n", got_exp);
              abort();
            }

          mpz_neg (z, z);
          got = mpz_get_d_2exp (&got_exp, z);
          if (got <= -1.0 || got > -0.5)
            {
              printf    ("mpz_get_d_2exp wrong on -2**%lu-1\n", data[i]);
              printf    ("result out of range, expect -1.0 < got <= -0.5\n");
              printf    ("   rnd_mode = %d\n", rnd_mode);
              printf    ("   data[i]  = %lu\n", data[i]);
              mpz_trace ("   z    ", z);
              d_trace   ("   got  ", got);
              printf    ("   got exp  %ld\n", got_exp);
              abort();
            }
        }
    }

  mpz_clear (z);
  tests_hardware_setround (old_rnd_mode);
}
예제 #21
0
파일: bignum.c 프로젝트: libbitc/libbitc
void bn_setvch(mpz_t vo, const void *data_, size_t data_len)
{
	const unsigned char *data = data_;

	mpz_import(vo, data_len, -1, 1, 1, 0, data);

	if ((data_len > 0) && (data[data_len - 1] & 0x80)) {
		mpz_clrbit(vo, mpz_sizeinbase(vo, 2) - 1);
		mpz_neg(vo, vo);
	}
}
예제 #22
0
파일: arith.c 프로젝트: gsmadhusudan/Balsa
/* ReadBasedMP_INTFromString : Shortens the actions definitions for literal reading */
PtrMP_INT ReadBasedMP_INTFromString (char *str, int base, bool negative)
{
    PtrMP_INT ret = NewMP_INT (0);

    RemoveCharFromString (str, '_');
    mpz_init_set_str (ret, str, base);
    if (negative)
        mpz_neg (ret, ret);

    return ret;
}
예제 #23
0
파일: Pretty.cpp 프로젝트: maTHmU/nV
var Pretty(Kernel& k, const Rational& x) {
    mpq_canonicalize(const_cast<mpq_ptr>(x.mpq));
    Integer *a = new Integer, *b = new Integer;
    mpq_get_num(a->mpz, x.mpq);
    mpq_get_den(b->mpz, x.mpq);
    if(mpz_cmp_ui(b->mpz, 1) == 0)
        return Pretty(k, Z(a));
    if(mpz_sgn(a->mpz) < 0)
    {
        mpz_neg(a->mpz, a->mpz);
        return nV::tuple($.Minus, nV::tuple(SYS(Divide), a, b));
    }
예제 #24
0
static PyObject *
_GMPy_MPZ_Minus(PyObject *x, CTXT_Object *context)
{
    MPZ_Object *result;

    if (!(result = GMPy_MPZ_New(context))) {
        return NULL;
    }

    mpz_neg(result->z, MPZ(x));
    return (PyObject*)result;
}
예제 #25
0
static void
check_one (mpz_ptr z)
{
  int    inex;
  int    sh, neg;
  mpfr_t f;
  mpz_t  got;

  mpfr_init2 (f, MAX( mpz_sizeinbase (z, 2), MPFR_PREC_MIN) );
  mpz_init (got);

  for (sh = -2*GMP_NUMB_BITS ; sh < 2*GMP_NUMB_BITS ; sh++)
    {
      for (neg = 0; neg <= 1; neg++)
        {
          mpz_neg (z, z);
          mpfr_set_z (f, z, MPFR_RNDN);

          if (sh < 0)
            {
              mpz_tdiv_q_2exp (z, z, -sh);
              mpfr_div_2exp (f, f, -sh, MPFR_RNDN);
            }
          else
            {
              mpz_mul_2exp (z, z, sh);
              mpfr_mul_2exp (f, f, sh, MPFR_RNDN);
            }

          inex = mpfr_get_z (got, f, MPFR_RNDZ);

          if (mpz_cmp (got, z) != 0)
            {
              printf ("Wrong result for shift=%d\n", sh);
              printf ("     f "); mpfr_dump (f);
              printf ("   got "); mpz_dump (got);
              printf ("  want "); mpz_dump (z);
              exit (1);
            }
          if (! SAME_SIGN (inex, - mpfr_cmp_z (f, z)))
            {
              printf ("Wrong inexact value for shift=%d\n", sh);
              printf ("    f "); mpfr_dump (f);
              printf ("  got %+d\n", inex);
              printf (" want %+d\n", -mpfr_cmp_z (f, z));
              exit (1);
            }
        }
    }

  mpfr_clear (f);
  mpz_clear (got);
}
예제 #26
0
void
mpc_neg (mpc_t *rop, mpc_t op)
{
  mpc_t temp;

  temp.precision = op.precision;
  mpz_init (temp.object);
  mpz_set (temp.object, op.object);
  
  rop->precision = op.precision;
  mpz_neg (rop->object, temp.object);
}
예제 #27
0
GmpInt& GmpInt::operator/=(long value)
{
    copyIfShared();
    if(value >= 0)
        mpz_tdiv_q_ui(mData->mInteger, mData->mInteger, value);
    else
    {
        mpz_neg(mData->mInteger, mData->mInteger);
        mpz_tdiv_q_ui(mData->mInteger, mData->mInteger, -value);
    }
    return *this;
}
예제 #28
0
void mpz_sqrtmn
	(mpz_ptr root, mpz_srcptr a, 
	mpz_srcptr p, mpz_srcptr q, mpz_srcptr n)
{
	mpz_t g, u, v;
	mpz_init(g), mpz_init(u), mpz_init(v);
	mpz_gcdext(g, u, v, p, q);
	if (mpz_cmp_ui(g, 1L) == 0)
	{
		mpz_t root_p, root_q, root1, root2, root3, root4;
		/* single square roots */
		mpz_init(root_p), mpz_init(root_q);
		mpz_sqrtmp(root_p, a, p);
		mpz_sqrtmp(root_q, a, q);
		/* construct common square root */
		mpz_init_set(root1, root_q);
		mpz_init_set(root2, root_p);
		mpz_init_set(root3, root_q);
		mpz_init_set(root4, root_p);
		mpz_mul(root1, root1, u);
		mpz_mul(root1, root1, p);
		mpz_mul(root2, root2, v);
		mpz_mul(root2, root2, q);
		mpz_add(root1, root1, root2);
		mpz_mod(root1, root1, n);
		mpz_sqrtmn_2(root2, root1, n);
		mpz_neg(root3, root3);
		mpz_mul(root3, root3, u);
		mpz_mul(root3, root3, p);
		mpz_mul(root4, root4, v);
		mpz_mul(root4, root4, q);
		mpz_add(root3, root3, root4);
		mpz_mod(root3, root3, n);
		mpz_sqrtmn_2 (root4, root3, n);
		/* choose smallest root */
		mpz_set(root, root1);
		if (mpz_cmpabs(root2, root) < 0)
			mpz_set(root, root2);
		if (mpz_cmpabs(root3, root) < 0)
			mpz_set(root, root3);
		if (mpz_cmpabs(root4, root) < 0)
			mpz_set(root, root4);
		mpz_clear(root_p), mpz_clear(root_q);
		mpz_clear(root1), mpz_clear(root2);
		mpz_clear(root3), mpz_clear(root4);
		mpz_clear(g), mpz_clear(u), mpz_clear(v);
		return;
	}
	mpz_clear(g), mpz_clear(u), mpz_clear(v);
	/* error, return zero root */
	mpz_set_ui(root, 0L);
}
예제 #29
0
/* Copies the ZZ into the mpz_t
   Assumes output has been mpz_init'd.
   AUTHOR: David Harvey
           Joel B. Mohler moved the ZZX_getitem_as_mpz code out to this function (2007-03-13) */
static void ZZ_to_mpz(mpz_t output, const struct ZZ* x)
{
    unsigned char stack_bytes[4096];
    unsigned long size = NumBytes(*x);
    int use_heap = (size > sizeof(stack_bytes));
    unsigned char* bytes = use_heap ? (unsigned char*) malloc(size) : stack_bytes;
    BytesFromZZ(bytes, *x, size);
    mpz_import(output, size, -1, 1, 0, 0, bytes);
    if (sign(*x) < 0)
        mpz_neg(output, output);
    if (use_heap)
        free(bytes);
}
예제 #30
0
파일: window_unsh.c 프로젝트: krisk0/razin
void
fmpz_neg_1arg( fmpz_t v )
// inspired by FLINT fmpz_neg
 {
  if (!COEFF_IS_MPZ(*v))
   *v = -*v;
  else
   {
    __mpz_struct* mpz_ptr = _fmpz_promote(v);
    // TODO: write effective subroutine to invert GMP mpz in-place
    mpz_neg(mpz_ptr, mpz_ptr);
   }
 }