示例#1
0
void testbigsum(void)
{
bignum *n1,*result,*n2;
result=(bignum *)malloc(sizeof(bignum));
n1    =(bignum *)malloc(sizeof(bignum));
n2    =(bignum *)malloc(sizeof(bignum));

    while(1)
    {

    *result=*n1=*n2=BIGZERO;
    printf("Enter The First Big Number:\n");
    getbignum(n1);
    printf("\n\nEnter The Second Big Number:\n");
    getbignum(n2);
//    bigadd(n1,n2,result);
//    printf("\nNumber1+Number2 =");
//    putbignum(result);
//    bigsub(n1,n2,result);
//    printf("\nNumber1-Number2 =");
//    putbignum(result);
//    *result=BIGZERO;
//    bigmul(n1,n2,result);
//    printf("\nNumber1*Number2 =");
//    putbignum(result);
//    *result=BIGZERO;
    printf("\nNumber1/Number2 =");
    bigdiv(n1,n2,result);
    putbignum(result);
    *result=BIGZERO;
//    printf("\nNumber1%%Number2 =");
//    bigmod(n1,n2,result);
//    putbignum(result);
//    *result=BIGZERO;

//    switch(bigCmp(n1,n2)+2){
//            case (3):printf("\n\nNumber1>Number2\n");break;
//            case (2):printf("\n\nNumber1=Number2\n");break;
//            case (1):printf("\n\nNumber1<Number2\n");break;
//        }

    printf("\n\n***************************\n\n");

    }
free(result);
free(n1);
free(n2);

}
// 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;
}
// 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];
}
示例#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;
}
示例#5
0
文件: main.cpp 项目: process/euler
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;
}
示例#6
0
int dsa_generate(struct dss_key *key, int bits, progfn_t pfn,
		 void *pfnparam)
{
    Bignum qm1, power, g, h, tmp;
    unsigned pfirst, qfirst;
    int progress;

    /*
     * 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, PROGFN_PHASE_EXTENT, 1, 0x2800);
    pfn(pfnparam, PROGFN_EXP_PHASE, 1, -0x1D57C4 / 160);
    pfn(pfnparam, PROGFN_PHASE_EXTENT, 2, 0x40 * bits);
    pfn(pfnparam, PROGFN_EXP_PHASE, 2, -0x1D57C4 / bits);

    /*
     * In phase three we are finding an order-q element of the
     * multiplicative group of p, by finding an element whose order
     * is _divisible_ by q and raising it to the power of (p-1)/q.
     * _Most_ elements will have order divisible by q, since for a
     * start phi(p) of them will be primitive roots. So
     * realistically we don't need to set this much below 1 (64K).
     * Still, we'll set it to 1/2 (32K) to be on the safe side.
     */
    pfn(pfnparam, PROGFN_PHASE_EXTENT, 3, 0x2000);
    pfn(pfnparam, PROGFN_EXP_PHASE, 3, -32768);

    /*
     * In phase four we are finding an element x between 1 and q-1
     * (exclusive), by inventing 160 random bits and hoping they
     * come out to a plausible number; so assuming q is uniformly
     * distributed between 2^159 and 2^160, the chance of any given
     * attempt succeeding is somewhere between 0.5 and 1. Lacking
     * the energy to arrange to be able to specify this probability
     * _after_ generating q, we'll just set it to 0.75.
     */
    pfn(pfnparam, PROGFN_PHASE_EXTENT, 4, 0x2000);
    pfn(pfnparam, PROGFN_EXP_PHASE, 4, -49152);

    pfn(pfnparam, PROGFN_READY, 0, 0);

    invent_firstbits(&pfirst, &qfirst);
    /*
     * Generate q: a prime of length 160.
     */
    key->q = primegen(160, 2, 2, NULL, 1, pfn, pfnparam, qfirst);
    /*
     * Now generate p: a prime of length `bits', such that p-1 is
     * divisible by q.
     */
    key->p = primegen(bits-160, 2, 2, key->q, 2, pfn, pfnparam, pfirst);

    /*
     * Next we need g. Raise 2 to the power (p-1)/q modulo p, and
     * if that comes out to one then try 3, then 4 and so on. As
     * soon as we hit a non-unit (and non-zero!) one, that'll do
     * for g.
     */
    power = bigdiv(key->p, key->q);    /* this is floor(p/q) == (p-1)/q */
    h = bignum_from_long(1);
    progress = 0;
    while (1) {
	pfn(pfnparam, PROGFN_PROGRESS, 3, ++progress);
	g = modpow(h, power, key->p);
	if (bignum_cmp(g, One) > 0)
	    break;		       /* got one */
	tmp = h;
	h = bignum_add_long(h, 1);
	freebn(tmp);
    }
    key->g = g;
    freebn(h);

    /*
     * Now we're nearly done. All we need now is our private key x,
     * which should be a number between 1 and q-1 exclusive, and
     * our public key y = g^x mod p.
     */
    qm1 = copybn(key->q);
    decbn(qm1);
    progress = 0;
    while (1) {
	int i, v, byte, bitsleft;
	Bignum x;

	pfn(pfnparam, PROGFN_PROGRESS, 4, ++progress);
	x = bn_power_2(159);
	byte = 0;
	bitsleft = 0;

	for (i = 0; i < 160; i++) {
	    if (bitsleft <= 0)
		bitsleft = 8, byte = random_byte();
	    v = byte & 1;
	    byte >>= 1;
	    bitsleft--;
	    bignum_set_bit(x, i, v);
	}

	if (bignum_cmp(x, One) <= 0 || bignum_cmp(x, qm1) >= 0) {
	    freebn(x);
	    continue;
	} else {
	    key->x = x;
	    break;
	}
    }
    freebn(qm1);

    key->y = modpow(key->g, key->x, key->p);

    return 1;
}