Esempio n. 1
0
/*
 * call-seq:
 *   rand_state.urandomm(integer)
 *
 * From the GMP Manual:
 *
 * Generate a uniformly distributed random integer in the range 0 to
 * _integer-1_, inclusive. _integer_ can be an instance of GMP::Z,
 *  Fixnum, or Bignum
 */
VALUE r_gmprandstate_urandomm(VALUE self, VALUE arg)
{
  MP_RANDSTATE *self_val;
  MP_INT *res_val, *arg_val;
  int free_arg_val = 0;
  VALUE res;

  mprandstate_get_struct(self,self_val);

  if (GMPZ_P(arg)) {
    mpz_get_struct(arg, arg_val);
  } else if (FIXNUM_P(arg)) {
    mpz_temp_alloc(arg_val);
    mpz_init_set_ui(arg_val, FIX2INT(arg));
    free_arg_val = 1;
  } else if (BIGNUM_P(arg)) {
    mpz_temp_from_bignum(arg_val, arg);
    free_arg_val = 1;
  } else {
    typeerror_as(ZXB, "arg");
  }

  mpz_make_struct_init(res, res_val);
  mpz_urandomm(res_val, self_val, arg_val);
  if (free_arg_val) { mpz_temp_free(arg_val); }

  return res;
}
Esempio n. 2
0
/* Implements Legendre symbol only, requiring that p is an odd prime */
static int
mpz_ui_kronecker (mp_limb_t ul, const mpz_t p)
{
  mpz_t t, u;
  int r;

  mpz_init_set_ui (u, ul);
  mpz_init_set (t, p);
  mpz_sub_ui (t, t, 1);
  mpz_tdiv_q_2exp (t, t, 1);
  mpz_powm (t, u, t, p);

  r = mpz_cmp_ui (t, 1);
  if (r < 0)
    r = 0;
  else if (r == 0)
    r = 1;
  else
    {
      mpz_sub (t, p, t);
      ASSERT (mpz_cmp_ui (t, 1) == 0);
      r = -1;
    }
  mpz_clear (t);
  mpz_clear (u);

  return r;
}
Esempio n. 3
0
void SecretShare::getShares(mpz_t** shares, mpz_t* secrets, int size){
	mpz_t coefficient;
	mpz_init(coefficient);
	mpz_t temp;
	mpz_init_set_ui(temp, 0);
	int peer; 
	for(int i = 0; i < size; i++){
		for(int degree = 0; degree < threshold+1; degree++){
			if(degree == 0)
				mpz_set(coefficient,secrets[i]);
			else{
				mpz_urandomm(coefficient, rstate, fieldSize); // 
				if(degree == threshold && mpz_sgn(coefficient) == 0)
					mpz_add_ui(coefficient, coefficient, 1); 
			}

			for(int peer = 0; peer < peers; peer++){
				modMul(temp, sharingMatrix[peer][degree], coefficient);
				modAdd(shares[peer][i],shares[peer][i], temp);
			}
		}
	}
	mpz_clear(coefficient); 
	mpz_clear(temp); 
}
Esempio n. 4
0
void init_point(Point *pc){
	mpz_init_set_ui (pc->Z, 1);
	mpz_init (pc->PQ[0]);
	mpz_init (pc->PQ[1]);
	mpz_init (pc->PQ[2]);
	mpz_init (pc->PQ[3]);
}
Esempio n. 5
0
int Problem012(void) {
	mpz_t test_triangle_no, n;
	int divisor_count;

	mpz_init(test_triangle_no);
	mpz_init_set_ui(n,1);

	while(1) {
		/*Generate*/
		mpz_triangular(test_triangle_no,n);
		/*Count*/
		divisor_count = count_divisors_of_triangular_num(n);

		/*Test*/
//		if(divisor_count>200)
//			gmp_printf("\t%Zd-%d\n",test_triangle_no,divisor_count);

		if(divisor_count>=500) {
			/*Succeed*/
			gmp_printf("%30Zd", test_triangle_no);
			break;
		}

		/*Fail and try again*/
		mpz_add_ui(n,n,1);
	}

	mpz_clear(test_triangle_no);
	mpz_clear(n);
	return 0;
}
Esempio n. 6
0
/* Calculate r satisfying r*d == 1 mod 2^n. */
void
mpz_invert_2exp (mpz_t r, mpz_t a, unsigned long n)
{
  unsigned long  i;
  mpz_t  inv, prod;

  //ASSERT (mpz_odd_p (a));

  mpz_init_set_ui (inv, 1L);
  mpz_init (prod);

  for (i = 1; i < n; i++)
    {
      mpz_mul (prod, inv, a);
      if (mpz_tstbit (prod, i) != 0)
        mpz_setbit (inv, i);
    }

  mpz_mul (prod, inv, a);
  mpz_tdiv_r_2exp (prod, prod, n);
  //ASSERT (mpz_cmp_ui (prod, 1L) == 0);

  mpz_set (r, inv);

  mpz_clear (inv);
  mpz_clear (prod);
}
Esempio n. 7
0
void srp_get_A(srp_t *srp, gchar *out)
{
    mpz_t g;
    mpz_t A;
    size_t o;
    
    if (!srp)
        return;

    if (srp->A) {
        g_memmove(out, srp->A, 32);
        return;
    }
    
    mpz_init_set_ui(g, SRP_VAR_g);
    mpz_init2(A, 256);
    
    mpz_powm(A, g, srp->a, srp->n);
    mpz_export(out, &o, -1, 1, 0, 0, A);
    
    mpz_clear(A);
    mpz_clear(g);

    srp->A = (gchar *) g_malloc(32);
    if (srp->A)
        memcpy(srp->A, out, 32);
}
void testPolys()
{
	struct Fq_poly **lagrangePolys;
	struct Fq_poly *output;
	mpz_t *codewords, q, *temp;
	int degree = 6, length = degree + 1, i;
	int delta_i[4] = {1, 2, 3, 4};
	int finalDecision;


	codewords = (mpz_t*) calloc(length, sizeof(mpz_t));


	for(i = 0; i < length; i ++)
	{
		mpz_init(codewords[i]);
	}
	mpz_init_set_ui(q, 101);

	mpz_set_ui(codewords[0], 44);
	mpz_set_ui(codewords[1], 2);
	mpz_set_ui(codewords[2], 96);
	mpz_set_ui(codewords[3], 86);


	temp = completePartialSecretSharing(delta_i, codewords, codewords[3], q, 4);

	output = getPolyFromCodewords(temp, delta_i, 4, q);
}
Esempio n. 9
0
void p(long n, mpz_t* pcache, mpz_t result) {
    if(mpz_cmp_ui(pcache[n], 0) != 0) {
        mpz_set(result, pcache[n]);
        return;
    }
    mpz_t sum;
    mpz_init_set_ui(sum, 0);
    mpz_t pk;
    mpz_init(pk);
    long k = 1, i = 1, j = 1;
    while(n-k >= 0) {
        p(n-k, pcache, pk);
        if(j % 4 == 3 || j % 4 == 0) {
            mpz_sub(sum, sum, pk);
        } else {
            mpz_add(sum, sum, pk);
        }
        i = i < 0 ? i*-1 + 1 : i*-1;
        k = i*(3*i-1)/2;
        ++j;
    }
    mpz_set(pcache[n], sum);
    mpz_set(result, sum);
    mpz_clear(sum);
    mpz_clear(pk);
    return;
}
Esempio n. 10
0
ecc_point* existPoint(mpz_t  p){
	mpz_t l;
	mpz_init(l);
	mpz_pow_ui(l,p,3);
	mpz_addmul(l,a,p);
	mpz_add(l,l,b);
	mpz_mod(l,l,prime);
	mpz_t i;
	mpz_init_set_ui(i,0);
	mpz_t y;
	mpz_init(y);
	if (quadratic_residue(y,l,prime)==1){
		gmp_printf("entrei");

		ecc_point* r= malloc(sizeof(ecc_point));
		mpz_init_set((*r).x,p);
		mpz_init_set((*r).y,y);
		return r;
	} else
		return NULL;
/*	while(mpz_cmp(i,prime)!=0){
		mpz_set(y,i);
		mpz_pow_ui(y,y,2);
		mpz_mod(y,y,prime);
		gmp_printf(" x %Zd Y %Zd \n",l,y);
		if (mpz_cmp(y,l)==0){
			ecc_point* r= malloc(sizeof(ecc_point));
			mpz_init_set((*r).x,p);
			mpz_init_set((*r).y,i);
			return r;
		}else
			mpz_add_ui(i,i,1);
	}*/
	return NULL;
}
int testSecretScheme(struct Fq_poly *polyToTest, mpz_t secret, mpz_t q, unsigned int threshold, unsigned int numShares)
{
	mpz_t secret_index, *polyTestSecret;
	int degreeOfPoly = getHighestDegree(polyToTest);

	mpz_t *temp;
	int i;


	mpz_init_set_ui(secret_index, numShares + 1);

	polyTestSecret = evalutePoly(polyToTest, secret_index, q);


	for(i = 0; i < numShares; i ++)
	{
		mpz_set_ui(secret_index, i + 1);
		temp = evalutePoly(polyToTest, secret_index, q);
	}

	if(threshold == degreeOfPoly + 1 &&	0 == mpz_cmp(*polyTestSecret, secret))
	{
		return 0;
	}

	return 1;
}
Esempio n. 12
0
/*-------------------------------------------------------------------*/
static void convert_to_integer(mpz_poly_t *alg_sqrt, mpz_t n,
				mpz_t c, mpz_t m1, 
				mpz_t m0, mpz_t res) {

	/* given the completed square root, apply the homomorphism
	   to convert the polynomial to an integer. We do this
	   by evaluating alg_sqrt at -c*m0/m1, with all calculations
	   performed mod n */

	uint32 i;
	mpz_t m1_pow;
	mpz_t m0c;

	mpz_init_set(m1_pow, m1);
	mpz_poly_mod_q(alg_sqrt, n, alg_sqrt);

	mpz_init_set_ui(m0c, 0);
	mpz_submul(m0c, m0, c);
	mpz_mod(m0c, m0c, n);

	i = alg_sqrt->degree;
	mpz_set(res, alg_sqrt->coeff[i]);
	while (--i) {
		mpz_mul(res, res, m0c);
		mpz_addmul(res, m1_pow, alg_sqrt->coeff[i]);
		mpz_mul(m1_pow, m1_pow, m1);
	}
	mpz_mul(res, res, m0c);
	mpz_addmul(res, m1_pow, alg_sqrt->coeff[i]);
	mpz_mod(res, res, n);

	mpz_clear(m1_pow);
	mpz_clear(m0c);
}
Esempio n. 13
0
/* Equality of integers with up to 53 bits */
void
check_onebits (void)
{
  mpz_t   x, x2;
  double  y;
  int     i;

  mpz_init_set_ui (x, 0L);
  mpz_init (x2);

  for (i = 0; i < 512; i++)
    {
      mpz_mul_2exp (x, x, 1);
      mpz_add_ui (x, x, 1L);

      y = mpz_get_d (x);
      mpz_set_d (x2, y);

      /* stop if any truncation is occurring */
      if (mpz_cmp (x, x2) != 0)
        break;

      check_one ("check_onebits", x, y, 0, 0);
      check_one ("check_onebits", x, -y, 1, 0);
      mpz_neg (x, x);
      check_one ("check_onebits", x, y, -1, 0);
      check_one ("check_onebits", x, -y, 0, 0);
      mpz_neg (x, x);
    }

  mpz_clear (x);
  mpz_clear (x2);
}
Esempio n. 14
0
/*
 * call-seq:
 *   GMP::RandState.new()
 *   GMP::RandState.new(:mt) #=> uses gmp_randinit_mt
 *   GMP::RandState.new(:lc_2exp, a, c, m2exp) #=> uses gmp_randinit_lc_2exp
 *   GMP::RandState.new(:lc_2exp_size, size) #=> uses gmp_randinit_lc_2exp_size
 *
 * Initializes a new Random State object. Multiple GMP::RandState objects can
 * be instantiated. They may use different generators and the states
 * are kept separate.
 */
VALUE r_gmprandstatesg_new(int argc, VALUE *argv, VALUE klass)
{
  MP_RANDSTATE *rs_val;
  VALUE rs;
  VALUE algorithm, arg2, arg3, arg4;
  ID algorithm_id = rb_intern("default");
  MP_INT *a_val;
  unsigned long c_val, m2exp_val;
  unsigned long size_val;
  int free_a_val = 0;

  ID default_algorithm      = rb_intern("default");
  ID mt_algorithm           = rb_intern("mt");
  ID lc_2exp_algorithm      = rb_intern("lc_2exp");
  ID lc_2exp_size_algorithm = rb_intern("lc_2exp_size");

  (void)klass;

  mprandstate_make_struct(rs, rs_val);
  rb_scan_args(argc, argv, "04", &algorithm, &arg2, &arg3, &arg4);
  if (NIL_P(algorithm))    { algorithm_id = rb_intern("default"); }  /* default value */
  if (SYMBOL_P(algorithm)) { algorithm_id = rb_to_id(algorithm); }
  if (algorithm_id == default_algorithm ||
      algorithm_id == mt_algorithm) {
    if (argc > 1)
      rb_raise(rb_eArgError, "wrong # of arguments (%d for 0 or 1)", argc);
    gmp_randinit_default(rs_val);
  } else if (algorithm_id == lc_2exp_algorithm) {
    if (argc != 4)
      rb_raise(rb_eArgError, "wrong # of arguments (%d for 4)", argc);
    if (GMPZ_P(arg2)) {
      mpz_get_struct(arg2, a_val);
    } else if (FIXNUM_P(arg2)) {
      mpz_temp_alloc(a_val);
      mpz_init_set_ui(a_val, FIX2INT(arg2));
      free_a_val = 1;
    } else if (BIGNUM_P(arg2)) {
      mpz_temp_from_bignum(a_val, arg2);
      free_a_val = 1;
    } else {
      typeerror_as(ZXB, "b");
    }
    c_val     = NUM2LONG(arg3);
    m2exp_val = NUM2LONG(arg4);
    gmp_randinit_lc_2exp(rs_val, a_val, c_val, m2exp_val);
  } else if (algorithm_id == lc_2exp_size_algorithm) {
    if (argc != 2)
      rb_raise(rb_eArgError, "wrong # of arguments (%d for 2)", argc);
    size_val = NUM2LONG(arg2);
    if (size_val > 128)
      rb_raise(rb_eArgError, "size must be within [0..128]");
    if (gmp_randinit_lc_2exp_size (rs_val, size_val) == 0)
      rb_raise(rb_eArgError, "could not gmp_randinit_lc_2exp_size with %lu", size_val);
  }

  if (free_a_val) { mpz_temp_free(a_val); }
  rb_obj_call_init(rs, argc, argv);

  return rs;
}
Esempio n. 15
0
File: yn.c Progetto: Kirija/XPIR
/* compute in s an approximation of S1 = sum((n-k)!/k!*y^k,k=0..n)
   return e >= 0 the exponent difference between the maximal value of |s|
   during the for loop and the final value of |s|.
*/
static mpfr_exp_t
mpfr_yn_s1 (mpfr_ptr s, mpfr_srcptr y, unsigned long n)
{
  unsigned long k;
  mpz_t f;
  mpfr_exp_t e, emax;

  mpz_init_set_ui (f, 1);
  /* we compute n!*S1 = sum(a[k]*y^k,k=0..n) where a[k] = n!*(n-k)!/k!,
     a[0] = (n!)^2, a[1] = n!*(n-1)!, ..., a[n-1] = n, a[n] = 1 */
  mpfr_set_ui (s, 1, MPFR_RNDN); /* a[n] */
  emax = MPFR_EXP(s);
  for (k = n; k-- > 0;)
    {
      /* a[k]/a[k+1] = (n-k)!/k!/(n-(k+1))!*(k+1)! = (k+1)*(n-k) */
      mpfr_mul (s, s, y, MPFR_RNDN);
      mpz_mul_ui (f, f, n - k);
      mpz_mul_ui (f, f, k + 1);
      /* invariant: f = a[k] */
      mpfr_add_z (s, s, f, MPFR_RNDN);
      e = MPFR_EXP(s);
      if (e > emax)
        emax = e;
    }
  /* now we have f = (n!)^2 */
  mpz_sqrt (f, f);
  mpfr_div_z (s, s, f, MPFR_RNDN);
  mpz_clear (f);
  return emax - MPFR_EXP(s);
}
Esempio n. 16
0
/* Verify that the factor reduction hasn't broken anything */
static void verify_factor_array(mpz_t n, mpz_t* farray, int nfacs)
{
  int i, j;
  mpz_t t;
  mpz_init_set_ui(t, 1);
  /* Assert we don't have duplicates */
  for (i = 0; i < nfacs; i++) {
    for (j = i+1; j < nfacs; j++) {
      if (mpz_cmp(farray[i],farray[j]) == 0) { gmp_printf("duplicate: F[%d] = F[%d] = %Zd\n", i, j, farray[i]); croak("assert"); }
    }
  }
  /* Assert that all factors multiply to n */
  for (i = 0; i < nfacs; i++)
    mpz_mul(t, t, farray[i]);
  if (mpz_cmp(t, n) != 0) { gmp_printf("farray doesn't multiply: n=%Zd t=%Zd\n", n, t); croak("assert"); }
  /* Assert that gcd of each non-identical factor is 1 */
  for (i = 0; i < nfacs; i++) {
    for (j = i+1; j < nfacs; j++) {
      if (mpz_cmp(farray[i],farray[j]) != 0) {
        mpz_gcd(t, farray[i], farray[j]);
        if (mpz_cmp_ui(t, 1) != 0) { gmp_printf("gcd: farray[%d] = %Zd  farray[%d] = %Zd\n", i, farray[i], j, farray[j]); croak("assert"); }
      }
    }
  }
  mpz_clear(t);
}
Esempio n. 17
0
void make_headers(void)
{
	mpz_t newk, oldk, thing, div;
	mpz_init(newk);
	mpz_init(thing);
	mpz_ui_pow_ui(thing, 2, M);
	mpz_init_set(div, thing);	
	mpz_init_set_ui(oldk, 3);
	int size = SIZE;
	for(int i = 0; i <= LEVELS; i++)
	{
		mpz_set_ui(newk, 0);
		int r = 1;
		int n;
		while(mpz_get_ui(newk) < 3)
		{
			r++;
			double bottom = mpz_get_d(oldk);
			n = floor(((M+1)*log(2.0) + log((double) r))/log(bottom));
			mpz_pow_ui(thing, oldk, n);
			mpz_cdiv_q(newk, thing, div);
		}
		headers[i].chunksize = n;
		headers[i].K = mpz_get_ui(oldk);
		mpz_set(oldk,newk);
		headers[i].size = size;
		size = ceil((double)size/n);
	}
	fwrite(headers, sizeof(header), LEVELS + 1, output);
	mpz_clear(oldk);
	mpz_clear(div);
	mpz_clear(thing);
	mpz_clear(newk);
	
}	
Esempio n. 18
0
//------------------------------------------------------------------------------
void IDDContent::TO_MPZ(mpz_t &rop, const IDDC &n)
{
       if(n == zero) mpz_set_ui(rop, 0);
  else if(n == one) mpz_set_ui(rop, 1);
  else
  {
    if(n->ul_fit())
      mpz_set_ui(rop,n->val());
    else    
    {
      mpz_t _1;
      mpz_t g,p,d;
      mpz_init_set_ui(_1, 1);
      mpz_init(g);
      mpz_init(p);
      mpz_init(d);
      TO_MPZ(g,n->g);
      unsigned int pui = TO(n->p, 32, 0);
      TO_MPZ(d,n->d);
      pui = 1<<pui;
      mpz_mul_2exp(p, _1, pui);
      mpz_mul(rop,p,d);
      mpz_add(rop,g,rop);
    }
  }
}
Esempio n. 19
0
long long testPerformance(long long tests, int bit_count, int repetitions)
{
	gmp_randstate_t RAND;
	gmp_randinit_default(RAND);
	gmp_randseed_ui(RAND, getTime());

	mpz_t base;
	mpz_init_set_ui(base, 1);
	mpz_mul_2exp(base, base, bit_count-1); // base = 2^(bit_count-1)

	mpz_t n;
	mpz_init(n);
	long long time = 0;
	int test;
	for (test = 0; test < tests; test++) {
		mpz_urandomb(n, RAND, bit_count-1);
		mpz_add(n, n, base); // n has bit_count bits

		time += measureTime(n, repetitions);
	}
	mpz_clear(n);
	mpz_clear(base);
	gmp_randclear(RAND);

	return time / tests; // Average
}
Esempio n. 20
0
/**
 * Return the product of the primes between i and j inclusive
 * as well as phi of the product of primes.
 * TODO: Build a vector of primes and use a tree based multiplication.
 */
void mpz_primorial_range(
			 mpz_t primorial,
			 mpz_t phi,
			 const uint32_t i,
			 const uint32_t j)
{
  mpz_t p;
  mpz_t t;
  mpz_init_set_ui(p, i-1);
  mpz_init(t);
  mpz_nextprime(p, p);
    
  mpz_set_ui(primorial, 1);
  mpz_set_ui(phi, 1);
  while (mpz_cmp_ui(p, j) <= 0) {
    gmp_printf("%Zd\n", p);
    mpz_sub_ui(t, p, 1);
    mpz_mul(primorial, primorial, p);
    mpz_mul(phi, phi, t);
    mpz_nextprime(p, p);
  }
    
  mpz_clear(t);
  mpz_clear(p);
}
MP_INT Encode (int p, int length, int *list)
{
  MP_INT powers, code;
  int i;

  mpz_init_set_ui (&code, 0);

  for (i = 1; i <= length; ++i) {
     mpz_init_set_si (&powers, 0);
     if (list[i] != 0) 
        mpz_ui_pow_ui (&powers, p, i-1);
     mpz_add (&code, &code, &powers);
  }
/*
     if (list[i] != 0) {
        MP_INT factor;
        mpz_init_set_si (&factor, list[i]);
        mpz_ui_pow_ui (&powers, p, i);
        mpz_mul (&powers, &powers, &factor);
        mpz_add (&code, &code, &powers);
     }
  }
*/
  return code;
}
Esempio n. 22
0
/**
 * Computes the product of the first w primes such that the product <= B
 */
void mpz_bounded_primorial(int* w, mpz_t primorial, mpz_t phi, const mpz_t B) {
  mpz_t p;
  mpz_t t;

  if (mpz_cmp_ui(B, 1) <= 0) {
    mpz_set_ui(primorial, 0);
    mpz_set_ui(phi, 0);
    *w = 0;
    return;
  }

  mpz_init_set_ui(p, 1);
  mpz_init(t);

  mpz_set_ui(primorial, 1);
  mpz_set_ui(phi, 1);
  *w = 0;

  do {
    mpz_nextprime(p, p);
    (*w) ++;
    mpz_mul(primorial, primorial, p);
    mpz_sub_ui(t, p, 1);
    mpz_mul(phi, phi, t);
  } while (mpz_cmp(primorial, B) <= 0);
  mpz_divexact(primorial, primorial, p);
  mpz_divexact(phi, phi, t);
  (*w) --;

  mpz_clear(p);
  mpz_clear(t);
}
Esempio n. 23
0
void mpfr_taylor_nat_log(mpfr_t R, mpfr_t x, mpz_t n)
{
	assert(mpz_cmp_ui(n, 0) > 0);
	assert(mpfr_cmp_ui(x, 0) > 0);

	mpfr_t a, t, tt;
	mpfr_exp_t b;
	mpz_t k;
	unsigned int f = 1000, F = 1000;

	mpfr_init(a);
	mpfr_frexp(&b, a, x, MPFR_RNDN);
	mpfr_ui_sub(a, 1, a, MPFR_RNDN);
	
	mpfr_init_set(t, a, MPFR_RNDN);
	mpfr_init(tt);
	mpfr_set(R, a, MPFR_RNDN);

	for(mpz_init_set_ui(k, 2); mpz_cmp(k, n) < 0; mpz_add_ui(k, k, 1))
	{
		mpfr_mul(t, t, a, MPFR_RNDN);
		mpfr_div_z(tt, t, k, MPFR_RNDN);
		mpfr_add(R, R, tt, MPFR_RNDN);
	}

	mpfr_mul_si(a, MPFR_NAT_LOG_2, b, MPFR_RNDN);
	mpfr_sub(R, a, R, MPFR_RNDN);
}
Esempio n. 24
0
void
check_limb (void)
{
  int        i;
  mp_limb_t  limb;
  mpz_t      z;
  char       *s;

  check_one ("0", "%Md", CNST_LIMB(0));
  check_one ("1", "%Md", CNST_LIMB(1));

  /* "i" many 1 bits, tested against mpz_get_str in decimal and hex */
  limb = 1;
  mpz_init_set_ui (z, 1L);
  for (i = 1; i <= GMP_LIMB_BITS; i++)
    {
      s = mpz_get_str (NULL, 10, z);
      check_one (s, "%Mu", limb);
      (*__gmp_free_func) (s, strlen (s) + 1);

      s = mpz_get_str (NULL, 16, z);
      check_one (s, "%Mx", limb);
      (*__gmp_free_func) (s, strlen (s) + 1);

      s = mpz_get_str (NULL, -16, z);
      check_one (s, "%MX", limb);
      (*__gmp_free_func) (s, strlen (s) + 1);

      limb = 2*limb + 1;
      mpz_mul_2exp (z, z, 1L);
      mpz_add_ui (z, z, 1L);
    }

  mpz_clear (z);
}
int
gfc_interpret_derived (unsigned char *buffer, size_t buffer_size, gfc_expr *result)
{
  gfc_component *cmp;
  gfc_constructor *head = NULL, *tail = NULL;
  int ptr;
  tree type;

  /* The attributes of the derived type need to be bolted to the floor.  */
  result->expr_type = EXPR_STRUCTURE;

  type = gfc_typenode_for_spec (&result->ts);
  cmp = result->ts.derived->components;

  /* Run through the derived type components.  */
  for (;cmp; cmp = cmp->next)
    {
      if (head == NULL)
	head = tail = gfc_get_constructor ();
      else
	{
	  tail->next = gfc_get_constructor ();
	  tail = tail->next;
	}

      /* The constructor points to the component.  */
      tail->n.component = cmp;

      tail->expr = gfc_constant_result (cmp->ts.type, cmp->ts.kind,
					&result->where);
      tail->expr->ts = cmp->ts;

      /* Copy shape, if needed.  */
      if (cmp->as && cmp->as->rank)
	{
	  int n;

	  tail->expr->expr_type = EXPR_ARRAY;
	  tail->expr->rank = cmp->as->rank;

	  tail->expr->shape = gfc_get_shape (tail->expr->rank);
	  for (n = 0; n < tail->expr->rank; n++)
	     {
	       mpz_init_set_ui (tail->expr->shape[n], 1);
	       mpz_add (tail->expr->shape[n], tail->expr->shape[n],
			cmp->as->upper[n]->value.integer);
	       mpz_sub (tail->expr->shape[n], tail->expr->shape[n],
			cmp->as->lower[n]->value.integer);
	     }
	}

      ptr = TREE_INT_CST_LOW (DECL_FIELD_OFFSET (cmp->backend_decl));
      gfc_target_interpret_expr (&buffer[ptr], buffer_size - ptr,
				 tail->expr);

      result->value.constructor = head;
    }
    
  return int_size_in_bytes (type);
}
Esempio n. 26
0
int
promoteToMPQNumber(number *n)
{ switch(n->type)
  { case V_INTEGER:
      promoteToMPZNumber(n);
      /*FALLTHOURGH*/
    case V_MPZ:
    { n->value.mpq->_mp_num = n->value.mpz[0];
      mpz_init_set_ui(mpq_denref(n->value.mpq), 1L);
      n->type = V_MPQ;
      break;
    }
    case V_MPQ:
      break;
    case V_FLOAT:
    { double v = n->value.f;

      n->type = V_MPQ;
      mpq_init(n->value.mpq);
      mpq_set_d(n->value.mpq, v);
      break;
    }
  }

  return TRUE;
}
Esempio n. 27
0
	int BnetSRP3::init( const char* username_, const char* password_, const byte * salt_ )
	{
		if(!srp3_inited)
		{
			gmp_randinit_default(rand);
			gmp_randseed_ui(rand, (unsigned long)time(NULL));
			mpz_init2(N, 256);
			mpz_import(N, 32, -1, 1, 0, 0, SRP3_N);
			mpz_init_set_ui(g, SRP3_g);
			srp3_inited = true;
		}
		uint i;
		const char* source;

		if (!username_) {
			return -1;
		}

		uint len = strlen(username_);
		username.resize(len);
		source = username_;
		for(i = 0; i < len; ++ i)
		{
			username[i] = toupper(*(source++));
		}

		if (!((password_ == NULL) ^ (salt_ == NULL))) {
			return -1;
		}

		if (password_!=NULL) {
			len = strlen(password_);
			password.resize(len);
			source =  password_;
			for(i = 0; i < len; ++ i)
			{
				password[i] = toupper(*(source++));
			}
			mpz_init2(a, 256);
			mpz_init(b);
			mpz_urandomm(a, rand, N); /* generates the private key */

			mpz_t s;
			mpz_init2(s, 256);
			mpz_urandomb(s, rand, 256);
			mpz_export(raw_salt, NULL, 1, 4, -1, 0, s);
		} else {
			password = "";
			mpz_init(a);
			mpz_init2(b, 256);
			mpz_urandomm(b, rand, N);

			setSalt(salt_);
		}

		B_inited = false;

		return 0;
	}
Esempio n. 28
0
uint64_t isomorphism(uint64_t additive_elt, const cyclic_group_t* mult_group)
{
	assert(additive_elt < mult_group->prime);
	mpz_t base, power, prime, primroot;
	mpz_init_set_ui(base, mult_group->known_primroot);
	mpz_init_set_ui(power, additive_elt);
	mpz_init_set_ui(prime, mult_group->prime);
	mpz_init(primroot);
	mpz_powm(primroot, base, power, prime);
	uint64_t retv = (uint64_t) mpz_get_ui(primroot);
	log_trace("zmap", "Isomorphism: %llu", retv);
	mpz_clear(base);
	mpz_clear(power);
	mpz_clear(prime);
	mpz_clear(primroot);
	return retv;
}
Esempio n. 29
0
MPI
mpi_alloc_set_ui( unsigned long u)
{
    MPI n = alloc_bytes(sizeof *n, "mpi_copy");

    mpz_init_set_ui(n, u);
    return n;
}
Esempio n. 30
0
/* Calculate inv satisfying r*a == 1 mod 2^n. */
void
mpz_invert_ui_2exp (mpz_t r, unsigned long a, unsigned long n)
{
  mpz_t  az;
  mpz_init_set_ui (az, a);
  mpz_invert_2exp (r, az, n);
  mpz_clear (az);
}