示例#1
0
void
open_hypercube(int dimension, ppl_Polyhedron_t ph) {
  int i;
  mpz_t z_one;
  mpz_t z_minus_one;
  ppl_Coefficient_t coeff_one;
  ppl_Coefficient_t coeff_minus_one;
  ppl_Linear_Expression_t le;
  ppl_Constraint_t c;
  ppl_Constraint_System_t cs;

  mpz_init_set_si(z_one, 1);
  mpz_init_set_si(z_minus_one, -1);
  ppl_new_Coefficient(&coeff_one);
  ppl_assign_Coefficient_from_mpz_t(coeff_one, z_one);
  ppl_new_Coefficient(&coeff_minus_one);
  ppl_assign_Coefficient_from_mpz_t(coeff_minus_one, z_minus_one);
  ppl_new_Linear_Expression_with_dimension(&le, dimension);
  ppl_new_Constraint_System(&cs);
  for (i = 0; i < dimension; ++i) {
    ppl_Linear_Expression_add_to_coefficient(le, i, coeff_one);
    /* Variable(i) > 0 */
    ppl_new_Constraint(&c, le, PPL_CONSTRAINT_TYPE_GREATER_THAN);
    ppl_Constraint_System_insert_Constraint(cs, c);
    ppl_delete_Constraint(c);
    /* Variable(i) < 1 */
    ppl_Linear_Expression_add_to_inhomogeneous(le, coeff_minus_one);
    ppl_new_Constraint(&c, le, PPL_CONSTRAINT_TYPE_LESS_THAN);
    ppl_Constraint_System_insert_Constraint(cs, c);
    ppl_delete_Constraint(c);
    /* Zero `le' */
    ppl_Linear_Expression_add_to_coefficient(le, i, coeff_minus_one);
    ppl_Linear_Expression_add_to_inhomogeneous(le, coeff_one);
  }
  ppl_Polyhedron_add_constraints(ph, cs);
  ppl_delete_Constraint_System(cs);
  ppl_delete_Linear_Expression(le);
  ppl_delete_Coefficient(coeff_minus_one);
  ppl_delete_Coefficient(coeff_one);
  mpz_clear(z_minus_one);
  mpz_clear(z_one);
}
示例#2
0
void flint_mpz_init_set_readonly(mpz_t z, const fmpz_t f)
{
    if (COEFF_IS_MPZ(*f))
    {
        *z = *COEFF_TO_PTR(*f);
    }
    else
    {
        mpz_init_set_si(z, *f);
    }
}
示例#3
0
int main() {
	int i;
	mpz_t r;
	mpz_init_set_si(r,2);
	for(i=0;i>-1;i++) {
		gmp_printf("%d %Zd\n",i,r);
		mpz_mul_si(r,r,2);
		mpz_sub_ui(r,r,1);
	}
	mpz_clear(r);
	return 0;
}
示例#4
0
int UnipolyAddCoeffsOfXd(unipoly p1, unipoly p2, int d)
{
  int i;
  mpz_t z1, z2;

  if ( UPDeg(p1)>=d )
    if ( UPMax(p1)!=0 )  mpz_init_set_si(z1, p1[d].i);
    else mpz_init_set(z1, p1[d].z);
  else mpz_init(z1);
  
  if ( UPDeg(p2)>=d )
  {
    if ( UPMax(p2)!=0 )  mpz_init_set_si(z2, p2[d].i);
    else mpz_init_set(z2, p2[d].z);
    mpz_add(z1, z1, z2);
    mpz_clear(z2);
  }
  i = mpz_get_si(z1);
  mpz_clear(z1);
  return i;
}
示例#5
0
VAL MKBIGSI(VM* vm, signed long val) {
    mpz_t* bigint;
    VAL cl = allocate(vm, sizeof(Closure) + sizeof(mpz_t), 0);
    bigint = (mpz_t*)(((char*)cl) + sizeof(Closure));

    mpz_init_set_si(*bigint, val);

    SETTY(cl, BIGINT);
    cl -> info.ptr = (void*)bigint;

    return cl;
}
示例#6
0
void
check_sequence (int argc, char *argv[])
{
  unsigned long  n;
  unsigned long  limit = 100 * BITS_PER_MP_LIMB;
  mpz_t          want_ln, want_ln1, got_ln, got_ln1;

  if (argc > 1 && argv[1][0] == 'x')
    limit = ULONG_MAX;
  else if (argc > 1)
    limit = atoi (argv[1]);

  /* start at n==0 */
  mpz_init_set_si (want_ln1, -1); /* L[-1] */
  mpz_init_set_ui (want_ln,  2);  /* L[0]   */
  mpz_init (got_ln);
  mpz_init (got_ln1);

  for (n = 0; n < limit; n++)
    {
      mpz_lucnum2_ui (got_ln, got_ln1, n);
      MPZ_CHECK_FORMAT (got_ln);
      MPZ_CHECK_FORMAT (got_ln1);
      if (mpz_cmp (got_ln, want_ln) != 0 || mpz_cmp (got_ln1, want_ln1) != 0)
        {
          printf ("mpz_lucnum2_ui(%lu) wrong\n", n);
          mpz_trace ("want ln ", want_ln);
          mpz_trace ("got  ln ",  got_ln);
          mpz_trace ("want ln1", want_ln1);
          mpz_trace ("got  ln1",  got_ln1);
          abort ();
        }

      mpz_lucnum_ui (got_ln, n);
      MPZ_CHECK_FORMAT (got_ln);
      if (mpz_cmp (got_ln, want_ln) != 0)
        {
          printf ("mpz_lucnum_ui(%lu) wrong\n", n);
          mpz_trace ("want ln", want_ln);
          mpz_trace ("got  ln", got_ln);
          abort ();
        }

      mpz_add (want_ln1, want_ln1, want_ln);  /* L[n+1] = L[n] + L[n-1] */
      mpz_swap (want_ln1, want_ln);
    }

  mpz_clear (want_ln);
  mpz_clear (want_ln1);
  mpz_clear (got_ln);
  mpz_clear (got_ln1);
}
/*
 * Function:  compute_bbp_first_sum_gmp 
 * --------------------
 * Computes the first summand in the BBP formula using GNU GMP.
 *
 *  d: digit to be calculated
 *  base: the base
 *  c: a fixed positive integer
 *  p: a simple polynomial like x or x^2
 *  start_at_0: start the summation at k=0, if true, at k=1, otherwise.  Most 
 *              instances of the BBP formula, such as pi, have you start at 0.  
 *              But some, such as log(2), have you start at 1.
 *
 *  returns: the value of the first sum
 */
void compute_bbp_first_sum_gmp(mpf_t sum, unsigned long int d, int base, long int c, void (*p)(mpz_t, mpz_t), bool start_at_0) {
    mpf_set_d(sum, 0.0);
    signed long int k_start = start_at_0 ? 0 : 1;

    mpz_t k;
    mpz_init_set_si(k, k_start);
    double upper = floor((double) d / (double) c);
    while (mpz_cmp_d(k, upper) <= 0)
    {        
        mpz_t poly_result;
        mpz_init(poly_result);
        (*p)(poly_result, k);
        
        mpz_t num;
        mpz_init(num);
        
        mpz_t exponent;
        mpz_init_set(exponent, k);
        mpz_mul_si(exponent, exponent, c);
        mpz_mul_si(exponent, exponent, -1);
        mpz_add_ui(exponent, exponent, d);
        
        modular_pow_gmp(num, base, exponent, poly_result);
        mpf_t num_float;
        mpf_init(num_float);
        mpf_set_z(num_float, num);
        mpz_clear(num);
        mpz_clear(exponent);
        
        mpf_t denom;
        mpf_init_set_d(denom, mpz_get_d(poly_result));
        
        mpz_clear(poly_result);
        
        mpf_t quotient;
        mpf_init(quotient);
        mpf_div(quotient, num_float, denom);        
        mpf_clear(num_float);
        mpf_clear(denom);
        
        mpf_add(sum, sum, quotient);
        mpf_clear(quotient);
        
        mod_one_gmp(sum, sum);
        
        mpz_add_ui(k, k, 1);
    }
    mpz_clear(k);
        
    mod_one_gmp(sum, sum);
}
示例#8
0
文件: pl-gmp.c 项目: SWI-Prolog/swipl
void
mpz_init_set_si64(mpz_t mpz, int64_t i)
{
#if SIZEOF_LONG == 8
  mpz_init_set_si(mpz, (long)i);
#else
  DEBUG(2, Sdprintf("Converting %" PRId64 " to MPZ\n", i));

  if ( i >= LONG_MIN && i <= LONG_MAX )
  { mpz_init_set_si(mpz, (long)i);
  } else
  { mpz_init(mpz);
    if ( i >= 0 )
    { mpz_import(mpz, sizeof(i), ORDER, 1, 0, 0, &i);
    } else
    { i = -i;
      mpz_import(mpz, sizeof(i), ORDER, 1, 0, 0, &i);
      mpz_neg(mpz, mpz);
    }
  }
  DEBUG(2, gmp_printf("\t--> %Zd\n", mpz));
#endif
}
示例#9
0
unipoly UnipolyResetMax(unipoly p)
{  /* assumed int coeffs */  
  int d = UPDeg(p), M = 0, m = 0;
  
  for ( ; d>=0 ; --d ) { if (p[d].i>M) M=p[d].i; if (p[d].i<m) m=p[d].i; }
  UPSetMax(p, MAX(M, -m));
  if ( UPMax(p)>INT_MAX/2 )
  {
    for ( d=UPSize(p) ; d>UPDeg(p) ; --d ) mpz_init(p[d].z);
    for ( ; d>=0 ; --d )  mpz_init_set_si(p[d].z, (long)p[d].i);
    UPSetMax(p, 0);
  }

  return p;
}
示例#10
0
gmp_ZZpairOrNull rawWeightRange(M2_arrayint wts,
                                       const RingElement *a)
  /* The first component of the degree is used, unless the degree monoid is trivial,
     in which case the degree of each variable is taken to be 1.
     Returns lo,hi degree.  If the ring is not a graded ring or a polynomial ring
     then (0,0) is returned.
  */
{
     try {
          int lo,hi;
          a->degree_weights(wts,lo,hi);
          if (error()) return 0;
          gmp_ZZpair p = new gmp_ZZpair_struct;
          p->a = newitem(__mpz_struct);
          p->b = newitem(__mpz_struct);
          mpz_init_set_si(p->a, static_cast<long>(lo));
          mpz_init_set_si(p->b, static_cast<long>(hi));
          return p;
     }
     catch (exc::engine_error e) {
          ERROR(e.what());
          return NULL;
     }
}
示例#11
0
bool RingZZ::initialize_ZZ(const PolynomialRing *deg_ring)
{
  initialize_ring(0);
  _elem_size = sizeof(mpz_t);
  _zero_elem = new_elem();
  mpz_init_set_si(_zero_elem, 0);

  zeroV = from_int(0);
  oneV = from_int(1);
  minus_oneV = from_int(-1);

  degree_ring = deg_ring;
  coeffR = new CoefficientRingZZ_NTL(this);
  return true;
}
示例#12
0
VAL MKBIGSI(VM* vm, signed long val) {
    idris_requireAlloc(IDRIS_MAXGMP);

    mpz_t* bigint;
    VAL cl = allocate(sizeof(Closure) + sizeof(mpz_t), 0);
    idris_doneAlloc();
    bigint = (mpz_t*)(((char*)cl) + sizeof(Closure));

    mpz_init_set_si(*bigint, val);

    SETTY(cl, CT_BIGINT);
    cl -> info.ptr = (void*)bigint;

    return cl;
}
示例#13
0
文件: main.c 项目: Ceylo/CryptoA5
int main(int argc, const char * argv[])
{
    mpz_t a[7], mod, n;
    mpz_inits(a[0], a[1], a[2], a[3], a[4], a[5], a[6], n, NULL);
    mpz_init_set_si(mod, 5);
    mpz_set_si(a[4], 1);
    mpz_set_si(n, 5);
    
    PointRef g = PointCreateFromInt(0,1);
    PointRef p = PointCreateFromInt(2,4);
    PointRef q = PointCreateFromInt(3,1);
    PointRef pTeta = PointCreateTeta();
    PointRef qTeta = PointCreateTeta();
    PointRef p2 = PointCreateFromInt(3,2);
    PointRef p2Inv = PointCreateFromInt(3,3);

    
    CurveRef curve = CurveCreate(mod, n, a, g);
    
    assert(curve != NULL);
	assert(curve->a[4] != NULL);
	assert(curve->a[6] != NULL);
	assert(curve->g != NULL);
	assert(curve->mod != NULL);
    
    addPoints(p, q, curve);
    
    addDoublePoint(p2, p2, curve);
    
    addPteta(pTeta, q, curve);
    
    addQteta(p, qTeta, curve);
    
    addResultTeta(p2, p2Inv, curve);
    
    mpz_clears(mod, a[0], a[1], a[2], a[3], a[4], a[5], a[6], NULL);
    PointDestroy(g);
    PointDestroy(p);
    PointDestroy(q);
    PointDestroy(pTeta);
    PointDestroy(qTeta);
    PointDestroy(p2);
    PointDestroy(p2Inv);
    
    CurveDestroy(curve);

    return 0;
}
/*
 * Initialize z and copy a into z
 * - a is interpreted as a signed n-bit integer
 * - a must be normalized modulo (2^n)
 */
static void signed_bv2mpz(mpz_t z, uint32_t n, uint32_t *a) {
    mpz_t aux;
    uint32_t k;

    assert(n > 0);

    k = (n + 31) >> 5;
    mpz_init(z);
    bvconst_get_mpz(a, k, z);
    if (bvconst_tst_bit(a, n-1)) { // sign bit = 1
        mpz_init_set_si(aux, -1);
        mpz_mul_2exp(aux, aux, n);
        mpz_add(z, z, aux);
        mpz_clear(aux);
    }
}
示例#15
0
文件: gmp.c 项目: AllardJ/Tomato
/* {{{ convert_to_gmp
 * Convert zval to be gmp number */
static int convert_to_gmp(mpz_t * *gmpnumber, zval **val, int base TSRMLS_DC) 
{
	int ret = 0;
	int skip_lead = 0;

	*gmpnumber = emalloc(sizeof(mpz_t));

	switch (Z_TYPE_PP(val)) {
	case IS_LONG:
	case IS_BOOL:
	case IS_CONSTANT:
		{
			convert_to_long_ex(val);
			mpz_init_set_si(**gmpnumber, Z_LVAL_PP(val));
		}
		break;
	case IS_STRING:
		{
			char *numstr = Z_STRVAL_PP(val);

			if (Z_STRLEN_PP(val) > 2) {
				if (numstr[0] == '0') {
					if (numstr[1] == 'x' || numstr[1] == 'X') {
						base = 16;
						skip_lead = 1;
					} else if (base != 16 && (numstr[1] == 'b' || numstr[1] == 'B')) {
						base = 2;
						skip_lead = 1;
					}
				}
			}
			ret = mpz_init_set_str(**gmpnumber, (skip_lead ? &numstr[2] : numstr), base);
		}
		break;
	default:
		php_error_docref(NULL TSRMLS_CC, E_WARNING,"Unable to convert variable to GMP - wrong type");
		efree(*gmpnumber);
		return FAILURE;
	}

	if (ret) {
		FREE_GMP_NUM(*gmpnumber);
		return FAILURE;
	}
	
	return SUCCESS;
}
示例#16
0
static CBIGINT *_powf(CBIGINT *a, double f, bool invert)
{
	if (invert)
	{
		mpz_t b;

		if (!mpz_fits_slong_p(a->n))
			return NULL;

		mpz_init_set_si(b, (long)f);
		mpz_pow_ui(b, b, mpz_get_si(a->n));

		return BIGINT_create(b);
	}
	else
		return BIGINT_make_int(a, f, mpz_pow_ui);
}
示例#17
0
int
check_si (mpz_t sz, mpz_t oz, long si, long oi, int c)
{
  mpz_t t;
  int fail;

  if (mpz_cmp_si (sz, oi) != c)
    {
      printf ("mpz_cmp_si (sz, %ld) != %i.\n", oi, c);
      printf (" sz="); mpz_out_str (stdout, 10, sz); printf ("\n");
      abort ();
    }

  if ((si < oi ? -1 : si > oi) != c)
    return 1;

  mpz_init_set_si (t, si);

  if ((fail = mpz_cmp_si (sz, si)) != 0)
    printf ("mpz_cmp_si (sz, %ld) != 0.\n", si);
  if (mpz_cmp_si (oz, si) != -c)
    printf ("mpz_cmp_si (oz, %ld) != %i.\n", si, -c), fail = 1;
  if (! mpz_fits_slong_p (sz))
    printf ("mpz_fits_slong_p (sz) != 1.\n"), fail = 1;
  if (mpz_get_si (sz) != si)
    printf ("mpz_get_si (sz) != %ld.\n", si), fail = 1;
  if (mpz_cmp (t, sz) != 0)
    {
      printf ("mpz_init_set_si (%ld) failed.\n", si);
      printf (" got="); mpz_out_str (stdout, 10, t); printf ("\n");
      fail = 1;
    }

  mpz_clear (t);

  if (fail)
    {
      printf (" sz="); mpz_out_str (stdout, 10, sz); printf ("\n");
      printf (" oz="); mpz_out_str (stdout, 10, oz); printf ("\n");
      printf (" si=%ld\n", si);
      abort ();
    }

  return 0;
}
示例#18
0
void
gfc_get_section_index (gfc_array_ref *ar, mpz_t *section_index, mpz_t *offset)
{
    int i;
    mpz_t delta;
    mpz_t tmp;

    mpz_set_si (*offset, 0);
    mpz_init (tmp);
    mpz_init_set_si (delta, 1);
    for (i = 0; i < ar->dimen; i++)
    {
        mpz_init (section_index[i]);
        switch (ar->dimen_type[i])
        {
        case DIMEN_ELEMENT:
        case DIMEN_RANGE:
            if (ar->start[i])
            {
                mpz_sub (tmp, ar->start[i]->value.integer,
                         ar->as->lower[i]->value.integer);
                mpz_mul (tmp, tmp, delta);
                mpz_add (*offset, tmp, *offset);
                mpz_set (section_index[i], ar->start[i]->value.integer);
            }
            else
                mpz_set (section_index[i], ar->as->lower[i]->value.integer);
            break;

        case DIMEN_VECTOR:
            gfc_internal_error ("TODO: Vector sections in data statements");

        default:
            gcc_unreachable ();
        }

        mpz_sub (tmp, ar->as->upper[i]->value.integer,
                 ar->as->lower[i]->value.integer);
        mpz_add_ui (tmp, tmp, 1);
        mpz_mul (delta, tmp, delta);
    }

    mpz_clear (tmp);
    mpz_clear (delta);
}
示例#19
0
文件: bg.c 项目: rde1024/ironpipe
void bg_decrypt_block(bg_prikey key, FILE *in, FILE *out, mp_size_t len) {
	mpz_t y, xqmp, xpmq, pp, qq, negone;
	unsigned long int l;
	bbs_state *bbs;

	mpz_init(y);
	mpz_init(xqmp);
	mpz_init(xpmq);
	mpz_init(pp);
	mpz_init(qq);
	mpz_init_set_si(negone, -1);
	bbs = init_bbs(negone, key.n);
	bbs_gen(bbs, NULL, 8*len, 0);
	bbs_iter(bbs);
	bbs_close(bbs);
	l = bbs->len;
	mpz_inp_raw(y, in);

	mpz_add_ui(pp, key.p, 1);
	mpz_tdiv_q_ui(pp, pp, 4);
	mpz_pow_ui(pp, pp, l);
	mpz_powm_sec(pp, y, pp, key.p);

	mpz_add_ui(qq, key.q, 1);
	mpz_tdiv_q_ui(qq, qq, 4);
	mpz_pow_ui(qq, qq, l);
	mpz_powm_sec(qq, y, qq, key.q);

	mpz_mul(xqmp, key.q, pp);
	mpz_powm(pp, key.q, negone, key.p);
	mpz_mul(xqmp, xqmp, pp);

	mpz_mul(xpmq, key.p, qq);
	mpz_powm(qq, key.p, negone, key.q);
	mpz_mul(xpmq, xpmq, qq);

	mpz_add(y, xqmp, xpmq);
	mpz_mod(y, y, key.n);

	bbs = init_bbs(y, key.n);
	while(len && !feof(in)) len -= bg_xor(bbs, in, out, len);
	bbs_close(bbs);
}
示例#20
0
void test_inv_mod()
{
	const char *list[][2] = {
		{"2","3"},
		{"2","5"},
		{"20","41"},
		{"-20","31"},
		{"-20","-29"}
	};
	int n = sizeof(list)/sizeof(list[0]);
	int correct = 0;
	test_info(stderr,"Start testing inv_mod.");
	for(int i = 0; i < n; i++)
	{
		mpz_t a,b,c,d;
		mpz_init_set_str(a,list[i][0],10);
		mpz_init_set_str(b,list[i][1],10);
		mpz_init(c);
		mpz_init(d);
		cpt_inv_mod(d,a,b);
		mpz_mul(c,a,d);
		mpz_mod(c,c,b);
		if(mpz_cmp_si(c,1)!=0)
		{
			mpz_t tmp;
			mpz_init_set_si(tmp,1);
			test_unmatch(stderr,"test_inv_mod",c,tmp);
			mpz_clear(tmp);
		}
		else
		{
			correct++;
		}
		mpz_clear(a);
		mpz_clear(b);
		mpz_clear(c);
		mpz_clear(d);
	}
	if(correct == n)
		test_info(stderr,"test_inv_mod success.");
	else
		test_info(stderr,"test_inv_mod failed.");
}
示例#21
0
gmp_ZZorNull IM2_RingElement_to_Integer(const RingElement *a)
  /* If the ring of a is ZZ, or ZZ/p, this returns the underlying representation.
     Otherwise, NULL is returned, and an error is given */
{
  const Ring *R = a->get_ring();
  if (R->is_ZZ())
    {
      void *f = a->get_value().poly_val;
      return static_cast<gmp_ZZ>(f);
    }
  if (R->cast_to_Z_mod() != 0)
    {
      gmp_ZZ result = newitem(__mpz_struct);
      mpz_init_set_si(result, R->coerce_to_int(a->get_value()));
      return result;
    }
  ERROR("Expected ZZ or ZZ/p as base ring");
  return 0;
}
示例#22
0
//------------------------------------------------------------------------------
// Name: pow
//------------------------------------------------------------------------------
knumber_base *knumber_integer::pow(knumber_base *rhs) {

	if(knumber_integer *const p = dynamic_cast<knumber_integer *>(rhs)) {

		if(is_zero() && p->is_even() && p->sign() < 0) {
			delete this;
			return new knumber_error(knumber_error::ERROR_POS_INFINITY);
		}

		mpz_pow_ui(mpz_, mpz_, mpz_get_ui(p->mpz_));

		if(p->sign() < 0) {
			return reciprocal();
		} else {
			return this;
		}
	} else if(knumber_float *const p = dynamic_cast<knumber_float *>(rhs)) {
		knumber_float *f = new knumber_float(this);
		delete this;
		return f->pow(p);
	} else if(knumber_fraction *const p = dynamic_cast<knumber_fraction *>(rhs)) {
		knumber_fraction *f = new knumber_fraction(this);
		delete this;
		return f->pow(p);
	} else if(knumber_error *const p = dynamic_cast<knumber_error *>(rhs)) {
		if(p->sign() > 0) {
			knumber_error *e = new knumber_error(knumber_error::ERROR_POS_INFINITY);
			delete this;
			return e;
		} else if(p->sign() < 0) {
			mpz_init_set_si(mpz_, 0);
			return this;
		} else {
			knumber_error *e = new knumber_error(knumber_error::ERROR_UNDEFINED);
			delete this;
			return e;
		}
	}

	Q_ASSERT(0);
	return 0;
}
示例#23
0
static obj int64_to_bignum(INT_64 a)
{
    INT_32 p_hi, p_lo;
    mpz_t z1, z2, z3;

#ifndef HAVE_INT_64
    p_hi = a.digits[0] << 16 | a.digits[1];
    p_lo = a.digits[2] << 16 | a.digits[3];
#else
    p_hi = a >> 32;
    p_lo = a & 0xffffffff;
#endif

    mpz_init_set_si(z1, p_hi);
    mpz_init(z2);
    mpz_init(z3);
    mpz_mul_2exp(z2, z1, 32);
    mpz_set_ui(z1, p_lo);
    mpz_ior(z3, z1, z2);
    return mpz_to_bignum(z3);
}
示例#24
0
/**
 *  Compute exponential of x to e mod n
 */
void cpt_exp_mod(mpz_t res, mpz_srcptr x, mpz_srcptr e, mpz_srcptr, n)
{
	if(mpz_cmp_si(e,0) < 0)
	{
		mpz_t me;
		mpz_init(me);
		mpz_neg(me,e);
		cpt_exp_mod(res,x,me,n);
		cpt_inv_mod(res,res,n);
		mpz_clear(me);
		return;
	}
	if(mpz_cmp_si(e,0) == 0)
	{
		mpz_set_si(res,1);
		return;
	}

	/* Get the binary expansion of e */
	const char * bin = mpz_get_str(NULL,2,e);
	unsigned int l = strlen(bin);
	mpz_t y;
	mpz_init_set_si(y,1);
	for(int i = 0; i < l; i++)
	{
		mpz_mul(y,y,2);
		mpz_mod(y,y,n);
		if(bin[i])
		{
			mpz_mul(y,x,y);
			mpz_mod(y,y,n);
		}
	}
	mpz_set(res,y);

	mpz_clear(y);
	free(bin);
}
示例#25
0
文件: x-relem.cpp 项目: b-stone/M2
gmp_ZZorNull IM2_RingElement_to_Integer(const RingElement *a)
/* If the ring of a is ZZ, or ZZ/p, this returns the underlying representation.
   Otherwise, NULL is returned, and an error is given */
{
  const Ring *R = a->get_ring();
  if (R->is_ZZ())
    {
      void *f = a->get_value().poly_val;
      return static_cast<gmp_ZZ>(f);
    }
  if (R->isFinitePrimeField())
    {
      gmp_ZZ result = newitem(__mpz_struct);

      std::pair<bool, long> res = R->coerceToLongInteger(a->get_value());
      assert(res.first);

      mpz_init_set_si(result, static_cast<int>(res.second));
      return result;
    }
  ERROR("Expected ZZ or ZZ/p as base ring");
  return 0;
}
void
check_n (void)
{
  int    ret;

  /* %n suppressed */
  {
    int n = 123;
    gmp_sscanf ("   ", " %*n", &n);
    ASSERT_ALWAYS (n == 123);
  }
  {
    int n = 123;
    fromstring_gmp_fscanf ("   ", " %*n", &n);
    ASSERT_ALWAYS (n == 123);
  }


#define CHECK_N(type, string)                           \
  do {                                                  \
    type  x[2];                                         \
    char  fmt[128];                                     \
    int   ret;                                          \
                                                        \
    x[0] = ~ (type) 0;                                  \
    x[1] = ~ (type) 0;                                  \
    sprintf (fmt, "abc%%%sn", string);                  \
    ret = gmp_sscanf ("abc", fmt, &x[0]);               \
                                                        \
    ASSERT_ALWAYS (ret == 0);                           \
                                                        \
    /* should write whole of x[0] and none of x[1] */   \
    ASSERT_ALWAYS (x[0] == 3);                          \
    ASSERT_ALWAYS (x[1] == (type) ~ (type) 0);		\
                                                        \
  } while (0)

  CHECK_N (char,      "hh");
  CHECK_N (long,      "l");
#if HAVE_LONG_LONG
  CHECK_N (long long, "L");
#endif
#if HAVE_INTMAX_T
  CHECK_N (intmax_t,  "j");
#endif
#if HAVE_PTRDIFF_T
  CHECK_N (ptrdiff_t, "t");
#endif
  CHECK_N (short,     "h");
  CHECK_N (size_t,    "z");

  /* %Zn */
  {
    mpz_t  x[2];
    mpz_init_set_si (x[0], -987L);
    mpz_init_set_si (x[1],  654L);
    ret = gmp_sscanf ("xyz   ", "xyz%Zn", x[0]);
    MPZ_CHECK_FORMAT (x[0]);
    MPZ_CHECK_FORMAT (x[1]);
    ASSERT_ALWAYS (ret == 0);
    ASSERT_ALWAYS (mpz_cmp_ui (x[0], 3L) == 0);
    ASSERT_ALWAYS (mpz_cmp_ui (x[1], 654L) == 0);
    mpz_clear (x[0]);
    mpz_clear (x[1]);
  }
  {
    mpz_t  x;
    mpz_init (x);
    ret = fromstring_gmp_fscanf ("xyz   ", "xyz%Zn", x);
    ASSERT_ALWAYS (ret == 0);
    ASSERT_ALWAYS (mpz_cmp_ui (x, 3L) == 0);
    mpz_clear (x);
  }

  /* %Qn */
  {
    mpq_t  x[2];
    mpq_init (x[0]);
    mpq_init (x[1]);
    mpq_set_ui (x[0], -987L, 654L);
    mpq_set_ui (x[1], 4115L, 226L);
    ret = gmp_sscanf ("xyz   ", "xyz%Qn", x[0]);
    MPQ_CHECK_FORMAT (x[0]);
    MPQ_CHECK_FORMAT (x[1]);
    ASSERT_ALWAYS (ret == 0);
    ASSERT_ALWAYS (mpq_cmp_ui (x[0], 3L, 1L) == 0);
    ASSERT_ALWAYS (mpq_cmp_ui (x[1], 4115L, 226L) == 0);
    mpq_clear (x[0]);
    mpq_clear (x[1]);
  }
  {
    mpq_t  x;
    mpq_init (x);
    ret = fromstring_gmp_fscanf ("xyz   ", "xyz%Qn", x);
    ASSERT_ALWAYS (ret == 0);
    ASSERT_ALWAYS (mpq_cmp_ui (x, 3L, 1L) == 0);
    mpq_clear (x);
  }

  /* %Fn */
  {
    mpf_t  x[2];
    mpf_init (x[0]);
    mpf_init (x[1]);
    mpf_set_ui (x[0], -987L);
    mpf_set_ui (x[1],  654L);
    ret = gmp_sscanf ("xyz   ", "xyz%Fn", x[0]);
    MPF_CHECK_FORMAT (x[0]);
    MPF_CHECK_FORMAT (x[1]);
    ASSERT_ALWAYS (ret == 0);
    ASSERT_ALWAYS (mpf_cmp_ui (x[0], 3L) == 0);
    ASSERT_ALWAYS (mpf_cmp_ui (x[1], 654L) == 0);
    mpf_clear (x[0]);
    mpf_clear (x[1]);
  }
  {
    mpf_t  x;
    mpf_init (x);
    ret = fromstring_gmp_fscanf ("xyz   ", "xyz%Fn", x);
    ASSERT_ALWAYS (ret == 0);
    ASSERT_ALWAYS (mpf_cmp_ui (x, 3L) == 0);
    mpf_clear (x);
  }
}
示例#27
0
/* *******************************************************************************************
 * mpz_extrastronglucas_prp:
 * Let U_n = LucasU(p,1), V_n = LucasV(p,1), and D=p^2-4.
 * An "extra strong Lucas pseudoprime" to the base p is a composite n = (2^r)*s+(D/n), where
 * s is odd and (n,2D)=1, such that either U_s == 0 mod n and V_s == +/-2 mod n, or
 * V_((2^t)*s) == 0 mod n for some t with 0 <= t < r-1 [(D/n) is the Jacobi symbol]
 * *******************************************************************************************/
int mpz_extrastronglucas_prp(mpz_t n, long int p)
{
  mpz_t zD;
  mpz_t s;
  mpz_t nmj; /* n minus jacobi(D/n) */
  mpz_t res;
  mpz_t uh, vl, vh, ql, qh, tmp; /* these are needed for the LucasU and LucasV part of this function */
  long int d = p*p - 4;
  long int q = 1;
  unsigned long int r = 0;
  int ret = 0;
  int j = 0;

  if (d == 0) /* Does not produce a proper Lucas sequence */
    return PRP_ERROR;

  if (mpz_cmp_ui(n, 2) < 0)
    return PRP_COMPOSITE;

  if (mpz_divisible_ui_p(n, 2))
  {
    if (mpz_cmp_ui(n, 2) == 0)
      return PRP_PRIME;
    else
      return PRP_COMPOSITE;
  }

  mpz_init_set_si(zD, d);
  mpz_init(res);

  mpz_mul_ui(res, zD, 2);
  mpz_gcd(res, res, n);
  if ((mpz_cmp(res, n) != 0) && (mpz_cmp_ui(res, 1) > 0))
  {
    mpz_clear(zD);
    mpz_clear(res);
    return PRP_COMPOSITE;
  }

  mpz_init(s);
  mpz_init(nmj);

  /* nmj = n - (D/n), where (D/n) is the Jacobi symbol */
  mpz_set(nmj, n);
  ret = mpz_jacobi(zD, n);
  if (ret == -1)
    mpz_add_ui(nmj, nmj, 1);
  else if (ret == 1)
    mpz_sub_ui(nmj, nmj, 1);

  r = mpz_scan1(nmj, 0);
  mpz_fdiv_q_2exp(s, nmj, r);

  /* make sure that either (U_s == 0 mod n and V_s == +/-2 mod n), or */
  /* V_((2^t)*s) == 0 mod n for some t with 0 <= t < r-1              */
  mpz_init_set_si(uh, 1);
  mpz_init_set_si(vl, 2);
  mpz_init_set_si(vh, p);
  mpz_init_set_si(ql, 1);
  mpz_init_set_si(qh, 1);
  mpz_init_set_si(tmp,0);

  for (j = mpz_sizeinbase(s,2)-1; j >= 1; j--)
  {
    /* ql = ql*qh (mod n) */
    mpz_mul(ql, ql, qh);
    mpz_mod(ql, ql, n);
    if (mpz_tstbit(s,j) == 1)
    {
      /* qh = ql*q */
      mpz_mul_si(qh, ql, q);

      /* uh = uh*vh (mod n) */
      mpz_mul(uh, uh, vh);
      mpz_mod(uh, uh, n);

      /* vl = vh*vl - p*ql (mod n) */
      mpz_mul(vl, vh, vl);
      mpz_mul_si(tmp, ql, p);
      mpz_sub(vl, vl, tmp);
      mpz_mod(vl, vl, n);

      /* vh = vh*vh - 2*qh (mod n) */
      mpz_mul(vh, vh, vh);
      mpz_mul_si(tmp, qh, 2);
      mpz_sub(vh, vh, tmp);
      mpz_mod(vh, vh, n);
    }
    else
    {
      /* qh = ql */
      mpz_set(qh, ql);

      /* uh = uh*vl - ql (mod n) */
      mpz_mul(uh, uh, vl);
      mpz_sub(uh, uh, ql);
      mpz_mod(uh, uh, n);

      /* vh = vh*vl - p*ql (mod n) */
      mpz_mul(vh, vh, vl);
      mpz_mul_si(tmp, ql, p);
      mpz_sub(vh, vh, tmp);
      mpz_mod(vh, vh, n);

      /* vl = vl*vl - 2*ql (mod n) */
      mpz_mul(vl, vl, vl);
      mpz_mul_si(tmp, ql, 2);
      mpz_sub(vl, vl, tmp);
      mpz_mod(vl, vl, n);
    }
  }
  /* ql = ql*qh */
  mpz_mul(ql, ql, qh);

  /* qh = ql*q */
  mpz_mul_si(qh, ql, q);

  /* uh = uh*vl - ql */
  mpz_mul(uh, uh, vl);
  mpz_sub(uh, uh, ql);

  /* vl = vh*vl - p*ql */
  mpz_mul(vl, vh, vl);
  mpz_mul_si(tmp, ql, p);
  mpz_sub(vl, vl, tmp);

  /* ql = ql*qh */
  mpz_mul(ql, ql, qh);

  mpz_mod(uh, uh, n);
  mpz_mod(vl, vl, n);

  /* tmp = n-2, for the following comparison */
  mpz_sub_ui(tmp, n, 2);

  /* uh contains LucasU_s and vl contains LucasV_s */
  if (((mpz_cmp_ui(uh, 0) == 0) && ((mpz_cmp(vl, tmp) == 0) || (mpz_cmp_si(vl, 2) == 0)))
    || (mpz_cmp_ui(vl, 0) == 0))
  {
    mpz_clear(zD);
    mpz_clear(s);
    mpz_clear(nmj);
    mpz_clear(res);
    mpz_clear(uh);
    mpz_clear(vl);
    mpz_clear(vh);
    mpz_clear(ql);
    mpz_clear(qh);
    mpz_clear(tmp);
    return PRP_PRP;
  }

  for (j = 1; j < r-1; j++)
  {
    /* vl = vl*vl - 2*ql (mod n) */
    mpz_mul(vl, vl, vl);
    mpz_mul_si(tmp, ql, 2);
    mpz_sub(vl, vl, tmp);
    mpz_mod(vl, vl, n);

    /* ql = ql*ql (mod n) */
    mpz_mul(ql, ql, ql);
    mpz_mod(ql, ql, n);

    if (mpz_cmp_ui(vl, 0) == 0)
    {
      mpz_clear(zD);
      mpz_clear(s);
      mpz_clear(nmj);
      mpz_clear(res);
      mpz_clear(uh);
      mpz_clear(vl);
      mpz_clear(vh);
      mpz_clear(ql);
      mpz_clear(qh);
      mpz_clear(tmp);
      return PRP_PRP;
    }
  }

  mpz_clear(zD);
  mpz_clear(s);
  mpz_clear(nmj);
  mpz_clear(res);
  mpz_clear(uh);
  mpz_clear(vl);
  mpz_clear(vh);
  mpz_clear(ql);
  mpz_clear(qh);
  mpz_clear(tmp);
  return PRP_COMPOSITE;

}/* method mpz_extrastronglucas_prp */
示例#28
0
/* *******************************************************************************
 * mpz_lucas_prp:
 * A "Lucas pseudoprime" with parameters (P,Q) is a composite n with D=P^2-4Q,
 * (n,2QD)=1 such that U_(n-(D/n)) == 0 mod n [(D/n) is the Jacobi symbol]
 * *******************************************************************************/
int mpz_lucas_prp(mpz_t n, long int p, long int q)
{
  mpz_t zD;
  mpz_t res;
  mpz_t index;
  mpz_t uh, vl, vh, ql, qh, tmp; /* used for calculating the Lucas U sequence */
  int s = 0, j = 0;
  int ret = 0;
  long int d = p*p - 4*q;

  if (d == 0) /* Does not produce a proper Lucas sequence */
    return PRP_ERROR;

  if (mpz_cmp_ui(n, 2) < 0)
    return PRP_COMPOSITE;

  if (mpz_divisible_ui_p(n, 2))
  {
    if (mpz_cmp_ui(n, 2) == 0)
      return PRP_PRIME;
    else
      return PRP_COMPOSITE;
  }

  mpz_init(index);
  mpz_init_set_si(zD, d);
  mpz_init(res);

  mpz_mul_si(res, zD, q);
  mpz_mul_ui(res, res, 2);
  mpz_gcd(res, res, n);
  if ((mpz_cmp(res, n) != 0) && (mpz_cmp_ui(res, 1) > 0))
  {
    mpz_clear(zD);
    mpz_clear(res);
    mpz_clear(index);
    return PRP_COMPOSITE;
  }

  /* index = n-(D/n), where (D/n) is the Jacobi symbol */
  mpz_set(index, n);
  ret = mpz_jacobi(zD, n);
  if (ret == -1)
    mpz_add_ui(index, index, 1);
  else if (ret == 1)
    mpz_sub_ui(index, index, 1);

  /* mpz_lucasumod(res, p, q, index, n); */
  mpz_init_set_si(uh, 1);
  mpz_init_set_si(vl, 2);
  mpz_init_set_si(vh, p);
  mpz_init_set_si(ql, 1);
  mpz_init_set_si(qh, 1);
  mpz_init_set_si(tmp,0);

  s = mpz_scan1(index, 0);
  for (j = mpz_sizeinbase(index,2)-1; j >= s+1; j--)
  {
    /* ql = ql*qh (mod n) */
    mpz_mul(ql, ql, qh);
    mpz_mod(ql, ql, n);
    if (mpz_tstbit(index,j) == 1)
    {
      /* qh = ql*q */
      mpz_mul_si(qh, ql, q);

      /* uh = uh*vh (mod n) */
      mpz_mul(uh, uh, vh);
      mpz_mod(uh, uh, n);

      /* vl = vh*vl - p*ql (mod n) */
      mpz_mul(vl, vh, vl);
      mpz_mul_si(tmp, ql, p);
      mpz_sub(vl, vl, tmp);
      mpz_mod(vl, vl, n);

      /* vh = vh*vh - 2*qh (mod n) */
      mpz_mul(vh, vh, vh);
      mpz_mul_si(tmp, qh, 2);
      mpz_sub(vh, vh, tmp);
      mpz_mod(vh, vh, n);
    }
    else
    {
      /* qh = ql */
      mpz_set(qh, ql);

      /* uh = uh*vl - ql (mod n) */
      mpz_mul(uh, uh, vl);
      mpz_sub(uh, uh, ql);
      mpz_mod(uh, uh, n);

      /* vh = vh*vl - p*ql (mod n) */
      mpz_mul(vh, vh, vl);
      mpz_mul_si(tmp, ql, p);
      mpz_sub(vh, vh, tmp);
      mpz_mod(vh, vh, n);

      /* vl = vl*vl - 2*ql (mod n) */
      mpz_mul(vl, vl, vl);
      mpz_mul_si(tmp, ql, 2);
      mpz_sub(vl, vl, tmp);
      mpz_mod(vl, vl, n);
    }
  }
  /* ql = ql*qh */
  mpz_mul(ql, ql, qh);

  /* qh = ql*q */
  mpz_mul_si(qh, ql, q);

  /* uh = uh*vl - ql */
  mpz_mul(uh, uh, vl);
  mpz_sub(uh, uh, ql);

  /* vl = vh*vl - p*ql */
  mpz_mul(vl, vh, vl);
  mpz_mul_si(tmp, ql, p);
  mpz_sub(vl, vl, tmp);

  /* ql = ql*qh */
  mpz_mul(ql, ql, qh);

  for (j = 1; j <= s; j++)
  {
    /* uh = uh*vl (mod n) */
    mpz_mul(uh, uh, vl);
    mpz_mod(uh, uh, n);

    /* vl = vl*vl - 2*ql (mod n) */
    mpz_mul(vl, vl, vl);
    mpz_mul_si(tmp, ql, 2);
    mpz_sub(vl, vl, tmp);
    mpz_mod(vl, vl, n);

    /* ql = ql*ql (mod n) */
    mpz_mul(ql, ql, ql);
    mpz_mod(ql, ql, n);
  }

  mpz_mod(res, uh, n); /* uh contains our return value */

  mpz_clear(zD);
  mpz_clear(index);
  mpz_clear(uh);
  mpz_clear(vl);
  mpz_clear(vh);
  mpz_clear(ql);
  mpz_clear(qh);
  mpz_clear(tmp);

  if (mpz_cmp_ui(res, 0) == 0)
  {
    mpz_clear(res);
    return PRP_PRP;
  }
  else
  {
    mpz_clear(res);
    return PRP_COMPOSITE;
  }

}/* method mpz_lucas_prp */
示例#29
0
/* *************************************************************************
 * mpz_fibonacci_prp:
 * A "Fibonacci pseudoprime" with parameters (P,Q), P > 0, Q=+/-1, is a
 * composite n for which V_n == P mod n
 * [V is the Lucas V sequence with parameters P,Q]
 * *************************************************************************/
int mpz_fibonacci_prp(mpz_t n, long int p, long int q)
{
  mpz_t pmodn, zP;
  mpz_t vl, vh, ql, qh, tmp; /* used for calculating the Lucas V sequence */
  int s = 0, j = 0;

  if (p*p-4*q == 0)
    return PRP_ERROR;

  if (((q != 1) && (q != -1)) || (p <= 0))
    return PRP_ERROR;

  if (mpz_cmp_ui(n, 2) < 0)
    return PRP_COMPOSITE;

  if (mpz_divisible_ui_p(n, 2))
  {
    if (mpz_cmp_ui(n, 2) == 0)
      return PRP_PRIME;
    else
      return PRP_COMPOSITE;
  }

  mpz_init_set_ui(zP, p);
  mpz_init(pmodn);
  mpz_mod(pmodn, zP, n);

  /* mpz_lucasvmod(res, p, q, n, n); */
  mpz_init_set_si(vl, 2);
  mpz_init_set_si(vh, p);
  mpz_init_set_si(ql, 1);
  mpz_init_set_si(qh, 1);
  mpz_init_set_si(tmp,0);

  s = mpz_scan1(n, 0);
  for (j = mpz_sizeinbase(n,2)-1; j >= s+1; j--)
  {
    /* ql = ql*qh (mod n) */
    mpz_mul(ql, ql, qh);
    mpz_mod(ql, ql, n);
    if (mpz_tstbit(n,j) == 1)
    {
      /* qh = ql*q */
      mpz_mul_si(qh, ql, q);

      /* vl = vh*vl - p*ql (mod n) */
      mpz_mul(vl, vh, vl);
      mpz_mul_si(tmp, ql, p);
      mpz_sub(vl, vl, tmp);
      mpz_mod(vl, vl, n);

      /* vh = vh*vh - 2*qh (mod n) */
      mpz_mul(vh, vh, vh);
      mpz_mul_si(tmp, qh, 2);
      mpz_sub(vh, vh, tmp);
      mpz_mod(vh, vh, n);
    }
    else
    {
      /* qh = ql */
      mpz_set(qh, ql);

      /* vh = vh*vl - p*ql (mod n) */
      mpz_mul(vh, vh, vl);
      mpz_mul_si(tmp, ql, p);
      mpz_sub(vh, vh, tmp);
      mpz_mod(vh, vh, n);

      /* vl = vl*vl - 2*ql (mod n) */
      mpz_mul(vl, vl, vl);
      mpz_mul_si(tmp, ql, 2);
      mpz_sub(vl, vl, tmp);
      mpz_mod(vl, vl, n);
    }
  }
  /* ql = ql*qh */
  mpz_mul(ql, ql, qh);

  /* qh = ql*q */
  mpz_mul_si(qh, ql, q);

  /* vl = vh*vl - p*ql */
  mpz_mul(vl, vh, vl);
  mpz_mul_si(tmp, ql, p);
  mpz_sub(vl, vl, tmp);

  /* ql = ql*qh */
  mpz_mul(ql, ql, qh);

  for (j = 1; j <= s; j++)
  {
    /* vl = vl*vl - 2*ql (mod n) */
    mpz_mul(vl, vl, vl);
    mpz_mul_si(tmp, ql, 2);
    mpz_sub(vl, vl, tmp);
    mpz_mod(vl, vl, n);

    /* ql = ql*ql (mod n) */
    mpz_mul(ql, ql, ql);
    mpz_mod(ql, ql, n);
  }

  mpz_mod(vl, vl, n); /* vl contains our return value */

  if (mpz_cmp(vl, pmodn) == 0)
  {
    mpz_clear(zP);
    mpz_clear(pmodn);
    mpz_clear(vl);
    mpz_clear(vh);
    mpz_clear(ql);
    mpz_clear(qh);
    mpz_clear(tmp);
    return PRP_PRP;
  }
  mpz_clear(zP);
  mpz_clear(pmodn);
  mpz_clear(vl);
  mpz_clear(vh);
  mpz_clear(ql);
  mpz_clear(qh);
  mpz_clear(tmp);
  return PRP_COMPOSITE;

}/* method mpz_fibonacci_prp */
示例#30
0
void
gfc_advance_section (mpz_t *section_index, gfc_array_ref *ar,
                     mpz_t *offset_ret)
{
    int i;
    mpz_t delta;
    mpz_t tmp;
    bool forwards;
    int cmp;

    for (i = 0; i < ar->dimen; i++)
    {
        if (ar->dimen_type[i] != DIMEN_RANGE)
            continue;

        if (ar->stride[i])
        {
            mpz_add (section_index[i], section_index[i],
                     ar->stride[i]->value.integer);
            if (mpz_cmp_si (ar->stride[i]->value.integer, 0) >= 0)
                forwards = true;
            else
                forwards = false;
        }
        else
        {
            mpz_add_ui (section_index[i], section_index[i], 1);
            forwards = true;
        }

        if (ar->end[i])
            cmp = mpz_cmp (section_index[i], ar->end[i]->value.integer);
        else
            cmp = mpz_cmp (section_index[i], ar->as->upper[i]->value.integer);

        if ((cmp > 0 && forwards)
                || (cmp < 0 && ! forwards))
        {
            /* Reset index to start, then loop to advance the next index.  */
            if (ar->start[i])
                mpz_set (section_index[i], ar->start[i]->value.integer);
            else
                mpz_set (section_index[i], ar->as->lower[i]->value.integer);
        }
        else
            break;
    }

    mpz_set_si (*offset_ret, 0);
    mpz_init_set_si (delta, 1);
    mpz_init (tmp);
    for (i = 0; i < ar->dimen; i++)
    {
        mpz_sub (tmp, section_index[i], ar->as->lower[i]->value.integer);
        mpz_mul (tmp, tmp, delta);
        mpz_add (*offset_ret, tmp, *offset_ret);

        mpz_sub (tmp, ar->as->upper[i]->value.integer,
                 ar->as->lower[i]->value.integer);
        mpz_add_ui (tmp, tmp, 1);
        mpz_mul (delta, tmp, delta);
    }
    mpz_clear (tmp);
    mpz_clear (delta);
}