Exemplo n.º 1
0
Values new_public(const uint32_t & L, const uint32_t & N){
//    L = 1024, N = 160
//    L = 2048, N = 224
//    L = 2048, N = 256
//    L = 3072, N = 256
    RNG::BBS(static_cast <MPI> (static_cast <unsigned int> (now()))); // seed just in case not seeded

    // random prime q
    MPI q = bintompi("1" + RNG::BBS().rand(N - 1));
    q = nextprime(q);
    while (bitsize(q) > N){
        q = bintompi("1" + RNG::BBS().rand(N - 1));
        q = nextprime(q);
    }

    // random prime p = kq + 1
    MPI p = bintompi("1" + RNG::BBS().rand(L - 1));                   // pick random starting point
    p = ((p - 1) / q) * q + 1;                                        // set starting point to value such that p = kq + 1 for some k, while maintaining bitsize
    while (!knuth_prime_test(p, 25)){
        p += q;
    }

    // generator g with order q
    MPI g = 1, h = 1;
    MPI exp = (p - 1) / q;
    while (g == 1){
        h++;
        g = powm(h, exp, p);
    }

    return {p, q, g};
}
Exemplo n.º 2
0
void BBS::init(const PGPMPI & seed, const unsigned int & bits, PGPMPI p, PGPMPI q){
    if (!seeded){
        /*
        p and q should be:
            prime
            congruent to 3 mod 4
            gcd(p - 1, q - 1) is small
        */
        gmp_randclass rng(gmp_randinit_default);                 // set up rng for initializing BBS
        rng.seed(rng.get_z_bits(bits));                          // seed itself with random garbage
        if (p == 0){
            p = rng.get_z_bits(bits);
            p = nextprime(p);                                    // find closest prime
            while ((p & 3) != 3){                                // search for primes that are 3 = p mod 4
                p += 1;
                p = nextprime(p);                                // find next prime
            }
        }
        if (q == 0){
            q = rng.get_z_bits(bits);
            q = nextprime(q);                                    // find closest prime
            PGPMPI pq_gcd = 1025;
            while (((q & 3) != 3) && (pq_gcd < 1024)){           // search for primes that are 3 = q mod 4 and gcd(p - 1, q - 1) is small
                q += 1;
                q = nextprime(q);                                // find next prime
                pq_gcd = mpigcd(p-1, q-1);
            }
        }
        m = p * q;
        state = seed;
        seeded = true;
    }
}
Exemplo n.º 3
0
/* This function is called repetitively from the main program */
void labwork( void )
{
  
  prime = nextprime ( prime );
  display_string ( 0, itoaconv( prime ));
  display_update();
}
Exemplo n.º 4
0
int main()
{ /* program to find a trap-door prime */
    BOOL found;
    int i,spins;
    long seed;
    Big pp[NPRIMES],q,p,t;
    ofstream prime_data("prime.dat");
    cout << "Enter 9 digit seed= ";
    cin >> seed;
    irand(seed);
    cout << "Enter 4 digit seed= ";
    cin >> spins;
    for (i=0;i<spins;i++) brand();
    pp[0]=2;
    do
    {  /* find prime p = 2.pp[1].pp[2]....+1 */
        p=2;
        for (i=1;i<NPRIMES-1;i++)
        { /* generate all but last prime */
            q=rand(i+6,10);
            pp[i]=nextprime(q);
            p*=pp[i];
        }
        do
        { /* find last prime component such that p is prime */
            q=nextprime(q);
            pp[NPRIMES-1]=q;
            t=p*pp[NPRIMES-1];
            t+=1;
        } while(!prime(t));
        p=t;
        found=TRUE;
        for (i=0;i<NPRIMES;i++)
        { /* check that PROOT is a primitive root */
            if (pow(PROOT,(p-1)/pp[i],p)==1) 
            {
                found=FALSE;
                break;
            }
        }
    } while (!found);
    prime_data << NPRIMES << "\n";
    for (i=0;i<NPRIMES;i++) prime_data << pp[i] << endl;
    cout << "prime= \n" << p;
    return 0;
}
Exemplo n.º 5
0
Values keygen(unsigned int bits){
    RNG::BBS(static_cast <MPI> (static_cast <unsigned int> (now()))); // seed just in case not seeded

    bits /= 5;
    // random prime q - only used for key generation
    MPI q = bintompi(RNG::BBS().rand(bits));
    q = nextprime(q);
    while (bitsize(q) > bits){
        q = bintompi(RNG::BBS().rand(bits));
        q = nextprime(q);
    }
    bits *= 5;

    // random prime p = kq + 1
    MPI p = bintompi("1" + RNG::BBS().rand(bits - 1));                // pick random starting point
    p = ((p - 1) / q) * q + 1;                                        // set starting point to value such that p = kq + 1 for some k, while maintaining bitsize
    while (!knuth_prime_test(p, 25)){
        p += q;
    }

    // generator g with order p
    MPI g = 1;
    MPI h = 1;
    MPI exp = (p - 1) / q;
    while (g == 1){
        g = powm(++h, exp, p);
    }

    // 0 < x < p
    MPI x = 0;
    while ((x == 0) || (p <= x)){
        x = bintompi(RNG::BBS().rand(bits));
    }

    // y = g^x mod p
    MPI y;
    y = powm(g, x, p);

    return {p, g, y, x};
}
Exemplo n.º 6
0
/**
 * Initialize the hash table.
 *
 * Params
 * 	size - The size you want the table to be (will go up by next prime)
 *
 * Return
 * 	A new dhash
 */
dhash
dhInit(size_t size, dhashDestroyFunc destroy)
{
	uint i;
	dhash table = xmalloc(sizeof(struct _dhash));
	table->tableSize = nextprime(size);
	table->destroy = destroy;
	table->hashed = xmalloc(table->tableSize * sizeof(struct _dlist));
	for (i=0; i < table->tableSize; i++) {
		table->hashed[i] = dlInit(NULL);
	}
	return table;
}
Exemplo n.º 7
0
int main()
{
    int n, i, j;
    printf("Enter a number : ");
    scanf("%d",&n);
    for(i=n+1;;i++)
    {
        if(nextprime(i))
        {
            break;
        }
    }
    return 0;
}
Exemplo n.º 8
0
int main(int argc, char **argv)
{
	int p, m, f, t;

	if (argc != 2) {
		fprintf(stderr, "usage: e51 familysize\n");
		return EXIT_FAILURE;
	}
	t = atoi(argv[1]);

	for (p = 13;; p = nextprime(p))
		for (m = 1; m < countto(p); m++) {
			f = primefamily(m, p);
			if (f >= t) {
				printf("%d\t0x%x\t%d\t%d\n", p, m, replace(m, p, 0), replace(m, p, 1));
				return EXIT_SUCCESS;
			}
		}
	return EXIT_SUCCESS;
}
Exemplo n.º 9
0
// create a new hashtable; parameter is a size hint
hashtable_t *hashtable_new(int sizehint) {
	
	//use sizehint to find next prime number for hashtable size 
	int num_buckets = nextprime(sizehint);

	//allocate memory for hashtable
	hashtable_t *mytable = NULL;
	//mytable = malloc(sizeof(hashtable_t));
	mytable = malloc(sizeof(int)*4);
	
	//allocates a new linked list in each index of mytable
	mytable->hashtable = malloc(sizeof(list_t*)*num_buckets);
	int i=0;
	for(;i<num_buckets;i++){
		mytable->hashtable[i] = NULL;
	}
	
	mytable->size = num_buckets;
	return mytable;
}
Exemplo n.º 10
0
/* evaluate the Knuth-Schroeppel function, cf Robert D. Silverman,
   "The Multiple Polynomial Quadratic Sieve", Math. of Comp. volume 48,
    number 177, 1987, page 335 */
unsigned long
find_multiplier (mpz_t N, double B)
{
  unsigned long k, bestk = 1;
  double p, f, g, maxf = 0.0;
  mpz_t kN;
  
  mpz_init (kN);
  for (k = 1; k < 100; k = nextprime (k))
    {
      mpz_mul_ui (kN, N, k);
      /* FIXME: Silverman writes "if N = 1 mod 8" but isn't it kN instead? */
      if (mpz_kronecker_ui (kN, 2) == 1 && mpz_fdiv_ui (kN, 8) == 1)
        f = 2.0 * log (2.0);
      else
        f = 0.0;
      for (p = getprime (2.0); p <= B; p = getprime (p))
        {
          if (mpz_kronecker_ui (kN, (unsigned long) p) == 1)
            {
              g = ((k % (unsigned long) p) == 0) ? (1.0 / p) : (2.0 / p);
              f += g * log (p);
            }
        }
      f -= 0.5 * log ((double) k);
      if (f > maxf)
        {
          maxf = f;
          bestk = k;
        }
      getprime (0.0); /* free prime buffer */
    }
  mpz_clear (kN);
  
  return bestk;
}
Exemplo n.º 11
0
/* This function is called repetitively from the main program */
void labwork( void ){
	prime = nextprime(prime);
	display_string(0, itoaconv(prime));
	display_update();
	int btns = getbtns();
	if(btns){
		int sw = getsw();
		//BTN2
		if(btns  & 0x4){
			mytime &= 0x0fff; //remove old digit
			mytime |= (sw<<12); //set new digit
		}
		//BTN3
		if(btns & 0x2){
			mytime &= 0xf0ff;
			mytime |= (sw<<8);
		}
		//BTN2
		if(btns & 0x1){
			mytime &= 0xff0f;
			mytime |= (sw<<4);
		}
	}
}
Exemplo n.º 12
0
void labwork( void )
{
  prime = nextprime( prime );
  display_string( 0, itoaconv( prime ) );
  display_update();

/*
    // If BTN2 & BTN3 is pressed at the same time a different binary number is returned.
    // If any button is pressed after getbtns() is called the status of button will not be used.
    unsigned int btns_status = getbtns();
    unsigned int sw_status = getsw();

    
    // BTN2 is pressed
    if (btns_status == 1) {
        mytime &= 0xff0f; //Remove third digit from my time.
        sw_status <<= 4;
        mytime += sw_status; // Add switched sw_status to third digit of mytime
        
    // BTN3 is pressed
    } else if (btns_status == 2) {
        mytime &= 0xf0ff;
        sw_status <<= 8;
        mytime += sw_status;
        
    // BTN4 is pressed
    } else if (btns_status == 4) {
        mytime &= 0x0fff;
        sw_status <<= 12;
        mytime += sw_status;
    }

    unsigned int overflow = IFS(0);
    overflow &= 0x000100;
    overflow >>= 8;

    
    if (overflow == 1) {
        if (timeoutcount == 10) { // Helps getting the 'delay' correct
            timeoutcount = 0;
            time2string( textstring, mytime );
            display_string( 3, textstring );
            display_update();
            tick( &mytime );
            if (first_tick == 1) {
                *porte = 0;
                first_tick = 0;
            } else {
                *porte += 1;
            }
            display_image(96, icon);
        } else {
            timeoutcount++;
        }
        IFS(0) = 0; // Set flag to 0 again. NOTE: MAY NEED CHANGING IN ASSIGNMENT 3
    }



	mytime &= 0x0000;
    unsigned int overflow = IFS(0);
    overflow &= 0x000100;
    overflow >>= 8;
    mytime += overflow;
time2string( textstring, mytime );
            display_string( 3, textstring );
            display_update();



*/
}