Esempio n. 1
0
int RSAKey::Generate( int bits )
{
	Bignum pm1, qm1, phi_n;

	/*
	 * We don't generate e; we just use a standard one always.
	 */
	this->exponent = bignum_from_long(RSA_EXPONENT);

	/*
	 * Generate p and q: primes with combined length `bits', not
	 * congruent to 1 modulo e. (Strictly speaking, we wanted (p-1)
	 * and e to be coprime, and (q-1) and e to be coprime, but in
	 * general that's slightly more fiddly to arrange. By choosing
	 * a prime e, we can simplify the criterion.)
	 */
	this->p = primegen(bits / 2, RSA_EXPONENT, 1, NULL, 1);
	this->q = primegen(bits - bits / 2, RSA_EXPONENT, 1, NULL, 2);

	/*
	 * Ensure p > q, by swapping them if not.
	 */
	if (bignum_cmp(this->p, this->q) < 0)
		swap( p, q );

	/*
	 * Now we have p, q and e. All we need to do now is work out
	 * the other helpful quantities: n=pq, d=e^-1 mod (p-1)(q-1),
	 * and (q^-1 mod p).
	 */
	this->modulus = bigmul(this->p, this->q);
	pm1 = copybn(this->p);
	decbn(pm1);
	qm1 = copybn(this->q);
	decbn(qm1);
	phi_n = bigmul(pm1, qm1);
	freebn(pm1);
	freebn(qm1);
	this->private_exponent = modinv(this->exponent, phi_n);
	this->iqmp = modinv(this->q, this->p);

	/*
	 * Clean up temporary numbers.
	 */
	freebn(phi_n);

	return 1;
}
Esempio n. 2
0
int main(int argc, char* argv[])
{
	int x[] = {0,0,0,0};

	bigmul(87654321, 12345678, x);

	printf("%d  %d  %d   %d\n", x[0],x[1],x[2],x[3]);

	return 0;
}
Esempio n. 3
0
File: main.c Progetto: process/euler
void fac(bignum num, int factor)
{
	int i;
	bignum temp;

	for(i = 2; i <= factor; i++)
	{
		bigset(temp, i, 10);
		bigmul(num, num, temp);
	}
}
Esempio n. 4
0
// Returns in r the solution of x == r[k] (mod m[k]), k = 0, ..., n-1
void crt (rawtype *r, rawtype *m, size_t n)
{
    size_t t, k;
    rawtype *mm = new rawtype[n], *x = new rawtype[n], *s = new rawtype[n];
    modint y;
    rawtype o;

    o = modint::modulus;

    mm[0] = m[0];
    for (t = 1; t < n; t++)
        mm[t] = bigmul (mm, mm, m[t], t);

    for (t = 0; t < n; t++)
        s[t] = 0;

    for (k = 0; k < n; k++)
    {
        setmodulus (m[k]);

        y = 1;
        for (t = 0; t < n; t++)
            if (t != k) y *= m[t];

        y = r[k] / y;

        bigdiv (x, mm, m[k], n);
        x[n - 1] = bigmul (x, x, y, n - 1);

        if (bigadd (s, x, n) || bigcmp (s, mm, n) > 0)
            bigsub (s, mm, n);
    }

    moveraw (r, s, n);

    setmodulus (o);

    delete[] mm;
    delete[] x;
    delete[] s;
}
Esempio n. 5
0
int main() {
  while (fgets(cbit0, MAXN, stdin) != NULL) {
    fgets(cbit1, MAXN, stdin);
    memset (bit0, 0, sizeof(int)*MAXN);
    memset (bit1, 0, sizeof(int)*MAXN);
    assign (bit0, cbit0); 
    assign (bit1, cbit1);
    bigmul(bit0, bit1);
    bigprint (sum);
  }


  return 0;
}
Esempio n. 6
0
// Divides n words in s by f, stores result in d, returns remainder
rawtype basediv (rawtype *dest, rawtype *src1, rawtype src2, size_t len, rawtype carry)
{
    size_t t;
    rawtype tmp[2];

    for (t = 0; t < len; t++)
    {
        tmp[1] = bigmul (tmp, &carry, Base, 1);
        if (src1) bigadd (tmp, src1 + t, 1, 1);
        carry = bigdiv (tmp, tmp, src2, 2);
        dest[t] = tmp[0];
    }

    return carry;
}
Esempio n. 7
0
/*
 * Verify that the public data in an RSA key matches the private
 * data. We also check the private data itself: we ensure that p >
 * q and that iqmp really is the inverse of q mod p.
 */
int rsa_verify(struct RSAKey *key)
{
    Bignum n, ed, pm1, qm1;
    int cmp;

    /* n must equal pq. */
    n = bigmul(key->p, key->q);
    cmp = bignum_cmp(n, key->modulus);
    freebn(n);
    if (cmp != 0)
	return 0;

    /* e * d must be congruent to 1, modulo (p-1) and modulo (q-1). */
    pm1 = copybn(key->p);
    decbn(pm1);
    ed = modmul(key->exponent, key->private_exponent, pm1);
    cmp = bignum_cmp(ed, One);
    sfree(ed);
    if (cmp != 0)
	return 0;

    qm1 = copybn(key->q);
    decbn(qm1);
    ed = modmul(key->exponent, key->private_exponent, qm1);
    cmp = bignum_cmp(ed, One);
    sfree(ed);
    if (cmp != 0)
	return 0;

    /*
     * Ensure p > q.
     */
    if (bignum_cmp(key->p, key->q) <= 0)
	return 0;

    /*
     * Ensure iqmp * q is congruent to 1, modulo p.
     */
    n = modmul(key->iqmp, key->q, key->p);
    cmp = bignum_cmp(n, One);
    sfree(n);
    if (cmp != 0)
	return 0;

    return 1;
}
Esempio n. 8
0
/*
 * Verify that the public data in an RSA key matches the private
 * data. We also check the private data itself: we ensure that p >
 * q and that iqmp really is the inverse of q mod p.
 */
bool RSAKey::Check() const
{
	Bignum n, ed, pm1, qm1;
	int cmp;

	/* n must equal pq. */
	n = bigmul(this->p, this->q);
	cmp = bignum_cmp(n, this->modulus);
	freebn(n);
	if (cmp != 0)
		return 0;

	/* e * d must be congruent to 1, modulo (p-1) and modulo (q-1). */
	pm1 = copybn(this->p);
	decbn(pm1);
	ed = modmul(this->exponent, this->private_exponent, pm1);
	cmp = bignum_cmp(ed, One);
	delete [] ed;
	if (cmp != 0)
		return 0;

	qm1 = copybn(this->q);
	decbn(qm1);
	ed = modmul(this->exponent, this->private_exponent, qm1);
	cmp = bignum_cmp(ed, One);
	delete [] ed;
	if (cmp != 0)
		return 0;

	/*
	 * Ensure p > q.
	 */
	if (bignum_cmp(this->p, this->q) <= 0)
		return 0;

	/*
	 * Ensure iqmp * q is congruent to 1, modulo p.
	 */
	n = modmul(this->iqmp, this->q, this->p);
	cmp = bignum_cmp(n, One);
	delete [] n;
	if (cmp != 0)
		return 0;

	return 1;
}
Esempio n. 9
0
// Multiplicates n words in s by f, adds s2, stores result to d, returns overflow word
rawtype basemuladd (rawtype *dest, rawtype *src1, rawtype *src2, rawtype src3, size_t len, rawtype carry)
{
    size_t t;
    rawtype tmp[2], tmpcarry[2];

    tmpcarry[0] = carry;
    tmpcarry[1] = 0;

    for (t = len; t--;)
    {
        tmp[1] = bigmul (tmp, src1 + t, src3, 1);
        bigadd (tmp, tmpcarry, 1, 1);
        if (src2) bigadd (tmp, src2 + t, 1, 1);
        dest[t] = bigdiv (tmpcarry, tmp, Base, 2);
    }

    return tmpcarry[0];
}
Esempio n. 10
0
int main()
{
	printf("PE 53\n");

	memset(factorials, 0, sizeof(bignum)*101);
	bigset(factorials[0], 1);
	bigset(factorials[1], 1);
	bigset(factorials[2], 2);

	char numstring[200];

	int count = 0;
	int percentCount = 0;

	int n;
	int r;

	bignum numerator;
	bignum denominator;
	bignum temp;

	for(n = 1; n <= 100; n++)
	{
		for(r = 2; r <= n; r++)
		{
			bigfactorial(n, numerator);
			bigfactorial(r, denominator);
			bigfactorial(n-r, temp);
			bigmul(denominator, denominator, temp);
			bigdiv(temp, numerator, denominator);
			if(biglength(temp) > 6)
				count++;
		}

		UpdateProgress(++percentCount);
	}

	printf("\nAnswer: %d\n", count);

	return 0;
}
Esempio n. 11
0
int rsa_generate(struct RSAKey *key, struct RSAAux *aux, int bits,
                 progfn_t pfn, void *pfnparam) {
    Bignum pm1, qm1, phi_n;

    /*
     * Set up the phase limits for the progress report. We do this
     * by passing minus the phase number.
     *
     * For prime generation: our initial filter finds things
     * coprime to everything below 2^16. Computing the product of
     * (p-1)/p for all prime p below 2^16 gives about 20.33; so
     * among B-bit integers, one in every 20.33 will get through
     * the initial filter to be a candidate prime.
     *
     * Meanwhile, we are searching for primes in the region of 2^B;
     * since pi(x) ~ x/log(x), when x is in the region of 2^B, the
     * prime density will be d/dx pi(x) ~ 1/log(B), i.e. about
     * 1/0.6931B. So the chance of any given candidate being prime
     * is 20.33/0.6931B, which is roughly 29.34 divided by B.
     *
     * So now we have this probability P, we're looking at an
     * exponential distribution with parameter P: we will manage in
     * one attempt with probability P, in two with probability
     * P(1-P), in three with probability P(1-P)^2, etc. The
     * probability that we have still not managed to find a prime
     * after N attempts is (1-P)^N.
     * 
     * We therefore inform the progress indicator of the number B
     * (29.34/B), so that it knows how much to increment by each
     * time. We do this in 16-bit fixed point, so 29.34 becomes
     * 0x1D.57C4.
     */
    pfn(pfnparam, -1, -0x1D57C4/(bits/2));
    pfn(pfnparam, -2, -0x1D57C4/(bits-bits/2));
    pfn(pfnparam, -3, 5);

    /*
     * We don't generate e; we just use a standard one always.
     */
    key->exponent = bignum_from_short(RSA_EXPONENT);

    /*
     * Generate p and q: primes with combined length `bits', not
     * congruent to 1 modulo e. (Strictly speaking, we wanted (p-1)
     * and e to be coprime, and (q-1) and e to be coprime, but in
     * general that's slightly more fiddly to arrange. By choosing
     * a prime e, we can simplify the criterion.)
     */
    aux->p = primegen(bits/2, RSA_EXPONENT, 1, 1, pfn, pfnparam);
    aux->q = primegen(bits - bits/2, RSA_EXPONENT, 1, 2, pfn, pfnparam);

    /*
     * Ensure p > q, by swapping them if not.
     */
    if (bignum_cmp(aux->p, aux->q) < 0) {
        Bignum t = aux->p;
        aux->p = aux->q;
        aux->q = t;
    }

    /*
     * Now we have p, q and e. All we need to do now is work out
     * the other helpful quantities: n=pq, d=e^-1 mod (p-1)(q-1),
     * and (q^-1 mod p).
     */
    pfn(pfnparam, 3, 1);
    key->modulus = bigmul(aux->p, aux->q);
    pfn(pfnparam, 3, 2);
    pm1 = copybn(aux->p);
    decbn(pm1);
    qm1 = copybn(aux->q);
    decbn(qm1);
    phi_n = bigmul(pm1, qm1);
    pfn(pfnparam, 3, 3);
    freebn(pm1);
    freebn(qm1);
    key->private_exponent = modinv(key->exponent, phi_n);
    pfn(pfnparam, 3, 4);
    aux->iqmp = modinv(aux->q, aux->p);
    pfn(pfnparam, 3, 5);

    /*
     * Clean up temporary numbers.
     */
    freebn(phi_n);

    return 1;
}
Esempio n. 12
0
int main()
{
	SieveOfEratosthenes(Primes, 1000);

	srand(time(NULL));

	char croaks[] = "PPPPNNPPPNPPNPN";

	RunSimulations();

	bignum numerator;
	bigset(numerator, 119);

	bignum denominator;
	bigset(denominator, 300);

	bignum temp1;
	bigset(temp1, 190);

	bignum temp2;
	bigset(temp2, 405);

	bignum temp3;
	biginit(temp3);

	bignum temp4;
	biginit(temp4);

	bignum temp5;
	biginit(temp5);

	int i;

	for(i = 1; i < 15; i++)
	{
		bigmulint(temp3,bigadd(temp3, temp1, temp2), 7695);

		bigsetbig(temp4, temp1);
		bigsetbig(temp5, temp2);

		bigadd(temp1, bigmulint(temp1, temp1, 81), bigmulint(temp5, temp5, 1805));
		bigadd(temp2, bigmulint(temp2, temp2, 5890), bigmulint(temp4, temp4, 7614));

		if(croaks[i] == 'P')
			bigmulint(temp1, temp1, 2);

		else
			bigmulint(temp2, temp2, 2);
			
		bigmul(numerator, numerator, bigadd(temp4, temp1, temp2));
		bigmul(denominator, denominator, bigmulint(temp3,temp3,3));

		char string1[1000];
		char string2[1000];
		biggetstr(string1, numerator);
		biggetstr(string2, denominator);
		printf("%s/%s\n", string1, string2);
	}

	char string1[1000];
	char string2[1000];
	biggetstr(string1, numerator);
	biggetstr(string2, denominator);
	printf("%s/%s\n", string1, string2);

	return 0;
}