static HOST_WIDE_INT
element_number (gfc_array_ref *ar)
{
  mpz_t multiplier, offset, extent, n;
  gfc_array_spec *as;
  HOST_WIDE_INT i, rank;

  as = ar->as;
  rank = as->rank;
  mpz_init_set_ui (multiplier, 1);
  mpz_init_set_ui (offset, 0);
  mpz_init (extent);
  mpz_init (n);

  for (i = 0; i < rank; i++)
    { 
      if (ar->dimen_type[i] != DIMEN_ELEMENT)
        gfc_internal_error ("element_number(): Bad dimension type");

      mpz_sub (n, *get_mpz (ar->start[i]), *get_mpz (as->lower[i]));
 
      mpz_mul (n, n, multiplier);
      mpz_add (offset, offset, n);
 
      mpz_sub (extent, *get_mpz (as->upper[i]), *get_mpz (as->lower[i]));
      mpz_add_ui (extent, extent, 1);
 
      if (mpz_sgn (extent) < 0)
        mpz_set_ui (extent, 0);
 
      mpz_mul (multiplier, multiplier, extent);
    } 
 
  i = mpz_get_ui (offset);
 
  mpz_clear (multiplier);
  mpz_clear (offset);
  mpz_clear (extent);
  mpz_clear (n);
 
  return i;
}
예제 #2
0
파일: ecpp.c 프로젝트: xcvii/gkecpp
static int ec_order(mpz_t order, mpz_t a, mpz_t b, unsigned long n)
{
  /* use the inefficient naive approach to count the points of an elliptic
   * curve E(a, b) over finite field GF(n):
   *   |(E(a, b))/GF(n)| =
   *           n + 1 + sum (x in GF(n)) (jacobi_symbol((x^3 + a*x + b), n))
   */

  unsigned long i;
  mpz_t tmp;
  int order_exists;

  if (!(n & 1))
  {
    order_exists = 0;
  }
  else
  {
    mpz_init(tmp);

    mpz_set_ui(order, n);
    mpz_add_ui(order, order, 1);

    for (i = 0; i < n; ++i)
    {
      mpz_set_ui(tmp, i);
      mpz_mul_ui(tmp, tmp, i);
      mpz_mul_ui(tmp, tmp, i);
      mpz_addmul_ui(tmp, a, i);
      mpz_add(tmp, tmp, b);

      mpz_set_si(tmp, mpz_kronecker_ui(tmp, n));
      mpz_add(order, order, tmp);
    }

    order_exists = 1;

    mpz_clear(tmp);
  }

  return order_exists;
}
예제 #3
0
파일: fib.c 프로젝트: AlbertVeli/misc-tools
int main(int argc, char *argv[])
{
   mpz_t n, sum;

   if (!parse_args(argc, argv)) {
      print_usage();
      return 1;
   }

   mpz_init(n);
   load_gmp_string(n, opt.number);

   /* Sum of first n fibonacci numbers is fib(n + 2) - 1.
    *
    * For instance, sum of first 5 fib numbers is:
    *
    * fib(5+2) - 1 = 13 - 1 = 12
    * or 1 + 1 + 2 + 3 + 5 = 12
    *
    *      n : 0  1  2  3  4  5  6  7   8
    * fib(n) : 0  1  1  2  3  5  8  13  21
    */
   if (opt.sum) {
      /* n + 2 */
      mpz_add_ui(n, n, 2);
   }

   mpz_init(sum);
   fib(sum, n);

   if (opt.sum) {
      /* - 1 */
      mpz_sub_ui(sum, sum, 1);
   }

   gmp_printf("%Zd\n", sum);

   mpz_clear(n);
   mpz_clear(sum);

   return 0;
}
예제 #4
0
int isPrime(const char *s)
{
    mpz_t maxValue;
    mpz_t iterator;
    mpz_t maxIterator;
    mpz_t remainder;
    
    mpz_init_set_str(maxValue, s, 10);
    mpz_init(remainder);
    mpz_init(iterator);
    mpz_init(maxIterator);
    mpz_cdiv_q_ui(maxIterator, maxValue, 2);
    for (mpz_set_ui(iterator, 2); mpz_cmp(iterator, maxIterator) <= 0; mpz_add_ui(iterator, iterator, 1))
    {
        mpz_cdiv_r(remainder, maxValue, iterator);
        if (mpz_cmp_ui(remainder, 0) == 0)
            return 0;
    }
    return 1;
}
예제 #5
0
파일: unigcd.cpp 프로젝트: AnZhg/mU
/**
\brief 基b下整数n的长度
*/
void IntegerLength_BigBase(mpz_ptr r,mpz_ptr n,mpz_ptr b)
{
	static mpz_t nn;
	mpz_init(nn);
	mpz_set(nn,n);
	mpz_set_ui(r,0);
	while(1)
	{
		if(mpz_size(nn)!=0)
		{
			mpz_add_ui(r,r,1);
			mpz_div(nn,nn,b);
		}
		else
		{
			mpz_clear(nn);
			return ;
		}
	}
}
예제 #6
0
파일: djcs.c 프로젝트: tiehuis/libhcs
int djcs_import_public_key(djcs_public_key *pk, const char *json)
{
    JSON_Value *root = json_parse_string(json);
    JSON_Object *obj = json_value_get_object(root);
    pk->s = json_object_get_number(obj, "s");
    pk->n = malloc(sizeof(mpz_t) * (pk->s + 1));

    mpz_init(pk->n[0]);
    mpz_set_str(pk->n[0], json_object_get_string(obj, "n"), HCS_INTERNAL_BASE);
    json_value_free(root);

    /* Calculate remaining values */
    mpz_add_ui(pk->g, pk->n[0], 1);
    for (unsigned long i = 1; i <= pk->s; ++i) {
        mpz_init_set(pk->n[i], pk->n[i-1]);
        mpz_mul(pk->n[i], pk->n[i], pk->n[0]);
    }

    return 0;
}
예제 #7
0
파일: pnum.c 프로젝트: cgutman/pnum
void *SumThread(void *context)
{
	PSUM_THREAD_CONTEXT tcontext = (PSUM_THREAD_CONTEXT)context;

	while (mpz_cmp(tcontext->factor, tcontext->end) < 0)
	{
		//Check if the number is divisible by the factor
		if (mpz_divisible_p(tcontext->num, tcontext->factor) != 0)
		{
			pthread_mutex_lock(tcontext->termMutex);

			//Add the factor
			mpz_add(*tcontext->sum, *tcontext->sum, tcontext->factor);

			//Add the other factor in the pair
			mpz_divexact(tcontext->otherfactor, tcontext->num, tcontext->factor);
			mpz_add(*tcontext->sum, *tcontext->sum, tcontext->otherfactor);

			//Bail early if we've exceeded our number
			if (mpz_cmp(*tcontext->sum, tcontext->num) > 0)
			{
				pthread_mutex_unlock(tcontext->termMutex);
				break;
			}

			pthread_mutex_unlock(tcontext->termMutex);
		}

		//This is a valid cancellation point
		pthread_testcancel();

		mpz_add_ui(tcontext->factor, tcontext->factor, 1);
	}

	pthread_mutex_lock(tcontext->termMutex);
	(*tcontext->termCount)++;
	pthread_cond_signal(tcontext->termVar);
	pthread_mutex_unlock(tcontext->termMutex);

	pthread_exit(NULL);
}
예제 #8
0
int main(int argc, char **argv)
{
    int counter = 0;
    mpz_t n, sum;
    test();
    mpz_init(n);
    mpz_set_ui(n, 8);
    mpz_init(sum);
    while (counter < 11)
    {
        if (isTruncatableLeft(n) && isTruncatableRight(n))
        {
            printNumber("", n, "\n");
            counter++;
            mpz_add(sum, sum, n);
        }
        mpz_add_ui(n, n, 1);
    }
    printNumber("total=", sum, "\n");
    return 0;
}
예제 #9
0
파일: crypt.c 프로젝트: MFreeze/m2moca
void dhGenPubKey(dhpvk_t *pvk, dhpbk_t *pbk) {
    // Integer used for random numbers
    if (!seed_initialized)
        rnInit();

    do {
        mpz_urandomb(pbk->p, seed, DH_KEY_SIZE);        
    } while (!mpz_probab_prime_p(pbk->p, NB_TURN));
    mpz_set(pvk->p, pbk->p);

    mpz_set_ui(pbk->g, 2);
    // Search alpha so that alpha is a generator
    while(!isGenerator(pbk->p, pbk->g))
        mpz_add_ui(pbk->g, pbk->g, 1);

    mpz_set(pvk->g, pbk->g);

    mpz_urandomm(pvk->a, seed, pvk->p);

    mpz_powm(pbk->pow, pbk->g, pvk->a, pbk->p);
}
예제 #10
0
파일: rabin.c 프로젝트: abbrev/rabbitsign
/*
 * Compute the T_f transform modulo n.
 *
 * Because only one quarter of the possible hashes can be signed with
 * a given key, we need to transform the hash.  First, we want to
 * ensure that the result is nonzero, so we shift the hash by 8 bits
 * and add a 1 to the end.  The resulting number is called m'.
 *
 * Second, we want to multiply it by a number k whose Legendre symbols
 * (k|p) and (k|q) are known, so that (km'|p) = (k|p)(m'|p) = 1 and
 * (km'|q) = (k|q)(km'|q) = 1.  Since we need both to be true
 * simultaneously, regardless of the values of (m'|p) and (m'|q), we
 * clearly need four possible values of k.
 *
 * As it happens, TI's keys all follow a precise format: they all have
 * p === 3 and q === 7 (mod 8).  As a result, we know that
 *
 *  (-1|p) = (-1|q) = -1
 *
 *  (2|p) = -1, (2|q) = 1
 *
 * So TI has defined the following transformation functions:
 *
 *  T_0(x) = -2x'
 *  T_1(x) = -x'
 *  T_2(x) = x'
 *  T_3(x) = 2x'
 *
 * where x' = 256x + 1.
 *
 * In the usual case of p === 3 and q === 7 (mod 8), then, two of the
 * possible (T_f(m)|p) will equal 1:
 *
 *  If (m'|p) = 1, then (T_0(m)|p) = (T_2(m)|p) = 1.
 *  If (m'|p) = -1, then (T_1(m)|p) = (T_3(m)|p) = 1.
 *
 * Two of the possible (T_f(m)|q) will equal 1:
 *
 *  If (m'|q) = 1, then (T_2(m)|q) = (T_3(m)|q) = 1.
 *  If (m'|q) = -1, then (T_0(m)|q) = (T_1(m)|q) = 1.
 *
 * Thus we can choose exactly one f value with
 * (T_f(m)|p) = (T_f(m)|q) = 1.
 *
 * If r === 5 (mod 8) is a prime, (-1|r) = 1, while (2|r) = -1.  Thus
 * a similar logic holds:
 *
 *  If (m'|r) = 1, then (T_1(m)|r) = (T_2(m)|r) = 1.
 *  If (m'|r) = -1, then (T_0(m)|r) = (T_3(m)|r) = 1.
 *
 * So if {p,q} === {3,5}, {5,7}, or {3,7} (mod 8), given any m, we can
 * pick an f with (T_f(m)|p) = (T_f(m)|q) = 1.
 *
 */
static void applyf(mpz_t res,	  /* mpz to store result */
		   const mpz_t m, /* MD5 hash */
		   const mpz_t n, /* public key */
		   int f)	  /* f (0, 1, 2, 3) */
{
  mpz_mul_ui(res, m, 256);
  mpz_add_ui(res, res, 1);

  switch (f) {
  case 0:
    mpz_add(res, res, res);
  case 1:
    mpz_sub(res, n, res);
    break;
  case 2:
    break;
  case 3:
    mpz_add(res, res, res);
    break;
  }
}
예제 #11
0
/**
 * Return true if there exists an a such that 1 < gcd(a, n) < n for some a <= r. 
 */
int check_a_exists(mpz_t n, mpz_t r) {
  mpz_t a, gcd;
  mpz_init(a);
  mpz_init(gcd);

  int exists = FALSE;

  // Simply iterate for values of a from 1 to r and see if equations hold for the gcd of a and n
  for (mpz_set_ui(a, 1); mpz_cmp(a, r) <= 0; mpz_add_ui(a, a, 1)) {
    mpz_gcd(gcd, a, n);
    if (mpz_cmp_ui(gcd, 1) > 0 && mpz_cmp(gcd, n) < 0) {
      exists = TRUE;
      break;
    }
  }

  mpz_clear(a);
  mpz_clear(gcd);

  return exists;
}
예제 #12
0
int main(int argc, char* argv[])
{
  int i = 0;
  mpz_t test;
  mpz_t base;

  if (argc != 2)
  {
    printf("Usage: %s <num>\n", argv[0]);
    printf("  Where <num> is the start of a range of 25 numbers to test\n");
    return 0;
  }

  mpz_init_set_str(test, argv[1], 10);
  mpz_init_set_ui(base, 2);

  for (i = 0; i < 25; i++)
  {
    gmp_printf("%6Zd -", test);
    printf("%3d", mpz_prp(test, base));
    printf("%3d", mpz_euler_prp(test, base));
    printf("%3d", mpz_sprp(test, base));
    printf("%3d", mpz_fibonacci_prp(test, 1, -1));
    printf("%3d", mpz_lucas_prp(test, 1, -1));
    printf("%3d", mpz_stronglucas_prp(test, 1, -1));
    printf("%3d", mpz_extrastronglucas_prp(test, 3));
    printf("%3d", mpz_selfridge_prp(test));
    printf("%3d", mpz_strongselfridge_prp(test));
    printf("%3d", mpz_bpsw_prp(test));
    printf("%3d", mpz_strongbpsw_prp(test));
    printf("\n"); fflush(stdout);

    mpz_add_ui(test, test, 1);
  }

  mpz_clear(test);
  mpz_clear(base);

  return 0;
}
예제 #13
0
static void
dsa_find_generator(mpz_t g,
		   void *random_ctx, nettle_random_func random,
		   void *progress_ctx, nettle_progress_func progress,
		   const mpz_t p, const mpz_t q)
{
  mpz_t e;
  mpz_t n;
  
  /* e = (p-1)/q */
  mpz_init_set(e, p);
  mpz_sub_ui(e, e, 1);
  mpz_divexact(e, e, q);

  /* n = p-2 = |2, 3, ... p-1| */
  mpz_init_set(n, p);
  mpz_sub_ui(n, n, 2);

  for (;;)
    {
      nettle_mpz_random(g, random_ctx, random, n);
      mpz_add_ui(g, g, 2);

      if (progress)
	progress(progress_ctx, 'g');
      mpz_powm(g, g, e, p);
      
      if (mpz_cmp_ui(g, 1))
	{
	  /* g != 1. Finished. */
	  if (progress)
	    progress(progress_ctx, '\n');

	  mpz_clear(e);
	  mpz_clear(n);

	  return;
	}
    }
}
예제 #14
0
void damgard_jurik::key_gen(int modulusbits) {
    mpz_t p, q;

    gmp_randstate_t rand;
    mpz_init(pubkey->n);
    mpz_init(pubkey->g);
    mpz_init(prvkey->lambda);
    mpz_init(p);
    mpz_init(q);

    init_rand(rand, modulusbits / 8 + 1);
    do
    {
        do
            mpz_urandomb(p, rand, modulusbits / 2);
        while( !mpz_probab_prime_p(p, 10) );

        do
            mpz_urandomb(q, rand, modulusbits / 2);
        while( !mpz_probab_prime_p(q, 10) );

        /* compute the public modulus n = p q */

        mpz_mul(pubkey->n, p, q);
    } while( !mpz_tstbit(pubkey->n, modulusbits - 1));
    pubkey->bits = modulusbits;

    mpz_add_ui(pubkey->g, pubkey->n, 1);

    compute_cache();

    /* Compute Private Key */
    mpz_sub_ui(p, p, 1);
    mpz_sub_ui(q, q, 1);
    mpz_lcm(prvkey->lambda, p, q);

    mpz_clear(p);
    mpz_clear(q);
    gmp_randclear(rand);
}
예제 #15
0
파일: prho.c 프로젝트: FairSky/ggnfs
int prho(mpz_t factor1, mpz_t factor2, mpz_t n, long c, long maxIts)
/* It is assumed that the input is composite. */
{ static mpz_t a, b, oldA, oldB, tmp, tmp2;
  static int initialized=0;
  long   i, its;

  if (!(initialized)) {
    mpz_init(a); mpz_init(b); mpz_init(tmp); mpz_init(tmp2);
    mpz_init(oldA); mpz_init(oldB);
    initialized=1;
  }
  if (c<1) c=1;
  if (c>0x000000FF) c &= 0x000000FF;
  mpz_set_ui(a, 1);
  mpz_set_ui(b, 1);
  its=0;
  do {
    mpz_set(oldA, a); mpz_set(oldB, b);
    mpz_set_ui(tmp2, 1);
    for (i=25; i>0; i--) {
      mpz_mul(tmp, a, a); mpz_add_ui(tmp, tmp, c); mpz_mod(a, tmp, n);
      mpz_mul(tmp, b, b); mpz_add_ui(tmp, tmp, c); mpz_mod(b, tmp, n);
      mpz_mul(tmp, b, b); mpz_add_ui(tmp, tmp, c); mpz_mod(b, tmp, n);

      mpz_sub(tmp, a, b); mpz_mul(tmp2, tmp2, tmp); mpz_mod(tmp2, tmp2, n);
    }
    its += 25;
    mpz_gcd(tmp, tmp2, n);
  } while ((mpz_cmp_ui(tmp, 1)==0) && (its<maxIts));
  if (its >= maxIts) return -1;
  if (mpz_cmp(tmp, n) == 0) {
    mpz_set(a, oldA); mpz_set(b, oldB);
    do {
      mpz_mul(tmp, a, a); mpz_add_ui(tmp, tmp, c); mpz_mod(a, tmp, n);
      mpz_mul(tmp, b, b); mpz_add_ui(tmp, tmp, c); mpz_mod(b, tmp, n);
      mpz_mul(tmp, b, b); mpz_add_ui(tmp, tmp, c); mpz_mod(b, tmp, n);
      mpz_sub(tmp, a, b);
      mpz_gcd(tmp, tmp, n);
    } while (mpz_cmp_ui(tmp, 1)==0);
  }
  if (mpz_cmp(tmp, n) < 0) {
    mpz_set(factor1, tmp);
    mpz_div(factor2, n, factor1);
    return 0;
  }
  return -1; /* Unknown failure. */
}
예제 #16
0
파일: crypt.c 프로젝트: MFreeze/m2moca
void egKeyGen(egpvk_t *pvk, egpbk_t *pbk) {
    // Useful for random generation
    if (!seed_initialized)
		rnInit();

    // Temporary variables
    mpz_t tmp;
    mpz_init(tmp);
    
    // p definition
    do {
        mpz_urandomb(pvk->p, seed, EG_KEY_SIZE);        
    } while (!mpz_probab_prime_p(pvk->p, NB_TURN));

    mpz_set(pbk->p, pvk->p);

    mpz_set_ui(pbk->alpha, 2);
    // Search alpha so that alpha is a generator
    while(!isGenerator(pbk->p, pbk->alpha))
        mpz_add_ui(pbk->alpha, pbk->alpha, 1);

    // Compute private key
    mpz_urandomb(pvk->a, seed, EG_KEY_SIZE);

    // Compute last part of public key
    mpz_powm(pbk->beta, pbk->alpha, pvk->a, pvk->p);

    // Compute k
    mpz_set_ui(tmp, NB_CHAR);
    pvk->k = 0;
    while (mpz_cmp(tmp, pbk->p) < 0) {
        mpz_mul_ui(tmp, tmp, NB_CHAR);
        pvk->k ++;
    }

    pbk->k = pvk->k;

    mpz_clear(tmp);
}
예제 #17
0
/*
 * \brief                Encrypts/decrypts a message using the RSA algorithm.
 *
 * \param result         a field to populate with the result of your RSA calculation.
 * \param message        the message to perform RSA on. (probably a cert in this case)
 * \param e              the encryption key from the key_file passed in through the
 *                       command-line arguments
 * \param n              the modulus for RSA from the modulus_file passed in through
 *                       the command-line arguments
 *
 * Fill in this function with your proj0 solution or see staff solutions.
 */
static void
perform_rsa(mpz_t result, mpz_t message, mpz_t e, mpz_t n)
{
  mpz_t odd;
  mpz_init(odd);
  mpz_add_ui(result, result, 1);

  while (mpz_cmp_ui(e, 0)){
      mpz_mod_ui(odd, e, 2);
      if (mpz_cmp_ui(odd, 0)) {
          mpz_mul(result, result, message);
          mpz_mod(result, result, n);
          mpz_sub_ui(e, e, 1);
      } else {
          mpz_mul(message, message, message);
          mpz_mod(message, message, n);
          mpz_div_ui(e, e, 2);
      }
    } 

    mpz_clear(odd);
}
예제 #18
0
/**
 * Run step 5 of the AKS algorithm.
 */
int check_polys(mpz_t r, mpz_t n) {
  mpz_t a, lim;
  mpz_init(a);
  mpz_init(lim);

  int status = PRIME;
  if (aks_debug) gmp_printf("computing upper limit\n");
  compute_upper_limit(lim, r, n);
  if (aks_debug) gmp_printf("lim=%Zd\n", lim);
  // For values of a from 1 to sqrt(totient(r)) * log(n)
  for (mpz_set_ui(a, 1); mpz_cmp(a, lim) <= 0; mpz_add_ui(a, a, 1)) {
    if (!check_poly(n, a, r)) {
      status = COMPOSITE;
      break;
    }
  }

  mpz_clear(a);
  mpz_clear(lim);

  return status;
}
예제 #19
0
파일: l5.c 프로젝트: krasm/coursera
int main(int argc, char * argv[]) {
	mpz_t p, g, h, b;
	mpz_t tmp, val;
	avl_tree_t * dict = avl_alloc_tree(l5_cmpr, NULL);

	mpz_inits(tmp, val, NULL);
	mpz_init_set_str(p, P, 10);
	mpz_init_set_str(g, G, 10);
	mpz_init_set_str(h, H, 10);
	mpz_init_set_ui(b, B20);


	for(unsigned long int i = 0; i <= B20; i++) {
		l5_lhs_t * r = l5_lhs_new(g, h, p, i);
		//gmp_printf("%Zd\n", r->value);
		avl_node_t * n = avl_insert(dict, r);
		assert(n != NULL);
	}
	for(unsigned long int i = 0; i <= B20; i++) {
		l5_lhs_t * r = l5_rhs_new(g, h, p, i);
		//gmp_printf("%Zd\n", r->value);
		avl_node_t * n = avl_search(dict, r);
		if(n != NULL) {
			l5_lhs_t * f = (l5_lhs_t*)n->item;

			mpz_t result;
			mpz_init(result);
			mpz_set_ui(result, B20);
			mpz_mul_ui(result, result, r->x);
			mpz_add_ui(result, result, f->x);

			gmp_printf("%Zd\n", result);

			exit(0);
		}
	}

	return 0;
}
예제 #20
0
void get_fst_primes(const int primec, mpz_t *primev) {

  int i; /* number of primes found */
  mpz_t n;
  
  /* Add first prime number, 2 */
  mpz_init_set_ui(primev[0], 2);
  i = 1;

  /* Initial prime candidate is set to 3 */
  mpz_init_set_ui(n, 3);
  
  while (i < primec) {
    if(mpz_probab_prime_p(n, 25)) {
      mpz_init_set(primev[i], n);
      ++i;
    }
    mpz_add_ui(n, n, 2);
  }

  mpz_clear(n);
}
예제 #21
0
int main() {
	unsigned long long yup, nope, n;
	mpz_t vt, va;
	mpz_init_set_ui(vt, 7);
	mpz_init_set_ui(va, 6);
	yup = 0;
	nope = 0;
	n = 2;
	while (n <= 50000000) {
		//printf("%llu %s\n", n, mpz_get_str(NULL, 10, vt));
    if (mpz_probab_prime_p(vt, 4) > 0) {
			yup++;
    } else {
			nope++;
    }
		mpz_add_ui(va, va, 4);
		mpz_add(vt, vt, va);
		n++;
	}
	printf("%llu %llu\n", yup, nope);
	
}
예제 #22
0
/* Select the 2, 4, or 6 numbers we will try to factor. */
static void choose_m(mpz_t* mlist, long D, mpz_t u, mpz_t v, mpz_t N,
                     mpz_t t, mpz_t Nplus1)
{
  int i, j;
  mpz_add_ui(Nplus1, N, 1);

  mpz_sub(mlist[0], Nplus1, u);     /* N+1-u */
  mpz_add(mlist[1], Nplus1, u);     /* N+1+u */
  for (i = 2; i < 6; i++)
    mpz_set_ui(mlist[i], 0);

  if (D == -3) {
    /* If reading Cohen, be sure to see the errata for page 474. */
    mpz_mul_si(t, v, 3);
    mpz_add(t, t, u);
    mpz_tdiv_q_2exp(t, t, 1);
    mpz_sub(mlist[2], Nplus1, t);   /* N+1-(u+3v)/2 */
    mpz_add(mlist[3], Nplus1, t);   /* N+1+(u+3v)/2 */
    mpz_mul_si(t, v, -3);
    mpz_add(t, t, u);
    mpz_tdiv_q_2exp(t, t, 1);
    mpz_sub(mlist[4], Nplus1, t);   /* N+1-(u-3v)/2 */
    mpz_add(mlist[5], Nplus1, t);   /* N+1+(u-3v)/2 */
  } else if (D == -4) {
    mpz_mul_ui(t, v, 2);
    mpz_sub(mlist[2], Nplus1, t);   /* N+1-2v */
    mpz_add(mlist[3], Nplus1, t);   /* N+1+2v */
  }
  /* m must not be prime */
  for (i = 0; i < 6; i++)
    if (mpz_sgn(mlist[i]) && _GMP_is_prob_prime(mlist[i]))
      mpz_set_ui(mlist[i], 0);
  /* Sort the m values so we test the smallest first */
  for (i = 0; i < 5; i++)
    if (mpz_sgn(mlist[i]))
      for (j = i+1; j < 6; j++)
        if (mpz_sgn(mlist[j]) && mpz_cmp(mlist[i],mlist[j]) > 0)
          mpz_swap( mlist[i], mlist[j] );
}
예제 #23
0
damgard_jurik::damgard_jurik(unsigned long s, int bitsmodule, damgard_jurik_get_rand_t rand_func,
                                    void *buf, size_t len, void* pvk_buf, size_t pvk_len) {
    mpz_t tmp;
    pubkey = new damgard_jurik_pubkey_t();
    prvkey = new damgard_jurik_prvkey_t();
    this->s_max = s;
    this->rand_func = rand_func;

    mpz_init(pubkey->n);
    mpz_init(pubkey->g);
    mpz_init(tmp);
    mpz_import(pubkey->n, len, 1, 1, 0, 0, buf);

    pubkey->bits = bitsmodule;

    mpz_add_ui(pubkey->g, pubkey->n, 1);
    compute_cache();

    mpz_init(prvkey->lambda);
    mpz_import(prvkey->lambda, pvk_len, 1, 1, 0, 0, pvk_buf);

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

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

      mpz_fdiv_r (r, a, d);

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

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

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

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

  mpz_clear (a);
  mpz_clear (d);
  mpz_clear (r);
  gmp_randclear(rands);
}
예제 #25
0
파일: vnrw_gmp.c 프로젝트: vanilala/vncrypt
static int VNRW_GMP_E1( const VNAsymCryptCtx_t * ctx, mpz_t zm )
{
	int ret = 0, jacobi = 0;

	VNRW_GMP_Ctx_t * gmpCtx = VN_CONTAINER_OF( ctx, VNRW_GMP_Ctx_t, mCtx );
	assert( VN_TYPE_VNRWSign_GMP == ctx->mType || VN_TYPE_VNRWEnc_GMP == ctx->mType );

	mpz_mul_ui( zm, zm, 2 );
	mpz_add_ui( zm, zm, 1 );

	jacobi = mpz_jacobi( zm, gmpCtx->mN );

	if( 1 == jacobi )
	{
		mpz_mul_ui( zm, zm, 4 );
	} else if( -1 == jacobi ) {
		mpz_mul_ui( zm, zm, 2 );
	} else {
		ret = 0;
	}

	return ret;
}
예제 #26
0
void paillier_pubkey::init_key(unsigned int _bits, char* rawKey) {
  int init_s_;
  bits = _bits;

  init_key();
  
  mpz_import(nj[1], _bits / 8, 1, sizeof(char), 0, 0, rawKey);
  mpz_add_ui(g, nj[1], 1);
  memcpy(&init_s_, rawKey+_bits/8, sizeof(int));
  
  // The client should not be using s above MAX_S
  if (init_s_ >= MAX_S)
  {
    std::cout << "PaillierKeys: WARNING. The client tries to use s>=MAX_S. Setting s=MAX_S-1."<<std::endl;
    init_s = MAX_S-1;
  } 
  else init_s = init_s_;

  for (int i = 2; i <= init_s+1; i++)
  {
    mpz_pow_ui(nj[i], nj[1], i);
  }
}
예제 #27
0
int inc_nonce(unsigned char * n){
	int i;
	size_t pointer = crypto_secretbox_NONCEBYTES;
	unsigned char * tempnonce;	

	mpz_t nonce,inc;

	mpz_init_set_str(inc,"1",10);
	mpz_init(nonce);		
	mpz_import(nonce,crypto_secretbox_NONCEBYTES,1,1,0,0,n);

	if(VISIBLE){
		printf("\nNonce now:\n");
		mpz_out_str(NULL,10,nonce);
	}

	mpz_add_ui(nonce,nonce,1);

	tempnonce = (unsigned char *)mpz_export(NULL,&pointer,1,sizeof(char),0,0,nonce);
	for(i=0;i<crypto_secretbox_NONCEBYTES;i++){
		n[i] = tempnonce[i];
	}
}
예제 #28
0
int
dsa_generate_keypair(struct dsa_public_key *pub,
		     struct dsa_private_key *key,
		     void *random_ctx, nettle_random_func random,
		     void *progress_ctx, nettle_progress_func progress,
		     /* Size of key, in bits.
		      * Use size = 512 + 64 * l for the official
		      * NIS key sizes. */
		     unsigned bits)
{
  mpz_t t;
  
  if (bits < DSA_MIN_P_BITS)
    return 0;
  
  dsa_nist_gen(pub->p, pub->q,
	       random_ctx, random,
	       progress_ctx, progress,
	       bits);
  
  dsa_find_generator(pub->g,
		     random_ctx, random,
		     progress_ctx, progress,
		     pub->p, pub->q);

  mpz_init_set(t, pub->q);
  mpz_sub_ui(t, t, 2);
  nettle_mpz_random(key->x, random_ctx, random, t);

  mpz_add_ui(key->x, key->x, 1);

  mpz_powm(pub->y, pub->g, key->x, pub->p);

  mpz_clear(t);

  return 1;
}
예제 #29
0
static PyObject *
GMPy_MPZ_ISub_Slot(PyObject *self, PyObject *other)
{
    MPZ_Object *rz;

    if (!(rz =  GMPy_MPZ_New(NULL)))
        return NULL;

    if (CHECK_MPZANY(other)) {
        mpz_sub(rz->z, MPZ(self), MPZ(other));
        return (PyObject*)rz;
    }

    if (PyIntOrLong_Check(other)) {
        int error;
        long temp = GMPy_Integer_AsLongAndError(other, &error);

        if (!error) {
            if (temp >= 0) {
                mpz_sub_ui(rz->z, MPZ(self), temp);
            }
            else {
                mpz_add_ui(rz->z, MPZ(self), -temp);
            }
        }
        else {
            mpz_t tempz;
            mpz_inoc(tempz);
            mpz_set_PyIntOrLong(tempz, other);
            mpz_sub(rz->z, MPZ(self), tempz);
            mpz_cloc(tempz);
        }
        return (PyObject*)rz;
    }

    Py_RETURN_NOTIMPLEMENTED;
}
예제 #30
0
int main(int argc, char *argv[]) {
    FILE *file = fopen("names.txt", "r");
    char line[12];
    int size = 5163;
    char **names = malloc(size * sizeof(char*));
    int count = 0;
    int i;
    char c;
    mpz_t number;

    if (file == NULL) {
        exit(EXIT_FAILURE);
    }

    mpz_init(number);    
    
    for (i = 0 ; i < size ; i++) {
        fscanf(file, "%s", line);
        names[i] = strdup(line);
    }

    /* sorts the list of names alphabetically */
    qsort(names, size, sizeof(char *), cmp);

    for (i = 0 ; i < size ; i++) {
        c = *names[i]++;   
        int count = 0;
        do {
            count += c - 'A' + 1;
            c = *names[i]++;   
        } while (c != '\0');
        mpz_add_ui(number, number, (i + 1) * count);
    }
    mpz_out_str(stdout, 10, number);
    printf("\n");
    return 0;
}