Beispiel #1
0
int main(){
    
    bignum valor, aux, total;  
    int n, f, i, bill = 1;  
    char str[MAXDIGITS];  
    
    scanf("%d %d", &n, &f);
    while( n ) {  
           
        scanf("%s", &str );  
        stringToBignum( str, &valor );  

        for( i = 1; i < n; i++ ){
             scanf("%s", &str );  
             stringToBignum( str, &aux );
             
             add_bignum( &valor, &aux, &total );
             
             valor = total; 
        }
        
        printf("Bill #%d costs ", bill++ );
        print_bignum( &valor );  
        sprintf( str, "%d", f );
        stringToBignum( str, &aux );
        divide_bignum( &valor, &aux, &total );
        printf(": each friend should pay ");
        print_bignum(&total);  
        printf("\n\n");
        
        scanf("%d %d", &n, &f);
    }
    
    return 0;  
}  
Beispiel #2
0
void subtract_bignum(bignum *a, bignum *b, bignum *c)
{
    int borrow, v, i;
    if ((a->signbit == MINUS) || (b->signbit == MINUS)) {
        b->signbit = -1 * b->signbit;
        add_bignum(a, b, c);
        b->signbit = -1 * b->signbit;
        return;
    }
    if (compare_bignum(a, b) == PLUS) {
        subtract_bignum(b, a, c);
        c->signbit = MINUS;
        return;
    }
    c->lastdigit = max(a->lastdigit, b->lastdigit);
    borrow = 0;
    for (i = 0; i <= c->lastdigit; i++) {
        v = (a->digits[i] - borrow - b->digits[i]);
        if (a->digits[i] > 0)
            borrow = 0;
        if (v < 0) {
            v = v + 10;
            borrow = 1;
        }
        c->digits[i] = (char) v % 10;
    }
    zero_justify(c);
}
/** Test Function */
int main( )
{
	bignum n1, n2, n3, zero;

	// Instantiate string buffers for big numbers a and b
	char* a = calloc( MAXDIGITS, sizeof( char ) );
	char* b = calloc( MAXDIGITS, sizeof( char ) );

	// Get big numbers a and b as strings
	printf( "a = " );
	fgets( a, MAXDIGITS, stdin );

	printf( "b = " );
	fgets( b, MAXDIGITS, stdin );

	// Remove the newline character (if present) from string buffers
	char* nl;
	if( ( nl = strchr( a, '\n' ) ) != NULL ) *nl = '\0';
	if( ( nl = strchr( b, '\n' ) ) != NULL ) *nl = '\0';

	// Create a and b big number instances via the string buffers
	string_to_bignum( a, &n1 );
	string_to_bignum( b, &n2 );

	// Test addition
	add_bignum( &n1, &n2, &n3 );
	printf( "a + b = " );
	print_bignum( &n3 );

	// Test comparison
	int comparison = compare_bignum( &n1, &n2 );
	printf( "a %s b\n", comparison == 0 ? "==" : ( comparison < 0 ? ">" : "<" ) );

	// Test subtraction
	subtract_bignum( &n1, &n2, &n3 );
	printf( "a - b = " );    
	print_bignum( &n3 );

	// Test multiplication
	multiply_bignum( &n1, &n2, &n3 );
	printf( "a * b = " );
	print_bignum( &n3 );

	// Test division
	int_to_bignum( 0, &zero );
	if( compare_bignum( &zero, &n2 ) == 0 )
	{
		printf( "a / b = NaN\n" );
	}
	else 
	{
		divide_bignum( &n1, &n2, &n3 );
		printf( "a / b = " );
		print_bignum( &n3 );
	}

	return 0;
}
Beispiel #4
0
void multiply_bignum(bignum *a, bignum *b, bignum *c)
{
    bignum row, tmp;
    int i, j;
    initialize_bignum(c);
    row = *a;
    for (i = 0; i <= b->lastdigit; i++) {
        for (j = 1; j <= b->digits[i]; j++) {
            add_bignum(c, &row, &tmp);
            *c = tmp;
        }
        digit_shift(&row, 1);
    }
    c->signbit = a->signbit * b->signbit;
    zero_justify(c);
}
/** Subtracts big numbers; i.e. c = a - b */
void subtract_bignum( bignum* a, bignum* b, bignum* c )
{
	int borrow;           /* has anything been borrowed? */
	int v;                /* placeholder digit */
	int i;                /* counter */

	initialize_bignum( c );

	if( ( a->signbit == MINUS ) || ( b->signbit == MINUS ) ) 
	{
		b->signbit = -1 * b->signbit;
		add_bignum( a, b, c );
		b->signbit = -1 * b->signbit;
		return;
	}

	if( compare_bignum( a, b ) == PLUS ) 
	{
		subtract_bignum( b, a, c );
		c->signbit = MINUS;
		return;
	}

	c->lastdigit = max( a->lastdigit, b->lastdigit );
	borrow = 0;

	for( i = 0; i <= ( c->lastdigit ); i++ ) 
	{
		v = ( a->digits[ i ] - borrow - b->digits[ i ] );

		if( a->digits[ i ] > 0)
		{
			borrow = 0;
		}

		if( v < 0 ) 
		{
			v = v + 10;
			borrow = 1;
		}

		c->digits[ i ] = ( char ) v % 10;
	}

	zero_justify( c );
}
Beispiel #6
0
int main(int argc, char ** argv)
{
	int j, n; // n == num_digits
	char * dummy_str;
	bignum_t f1, f2, f3, pow_ten, ten;

	if(argc < 2)
	{
		printf("Usage: %s num_digits\n"
			"warning: using num_digits = 1000\n", argv[0]);
		n = 1000;
	}
	else
		n = atoi(argv[1]);
	
	init_bignum(&f1);
	init_bignum(&f2);
	init_bignum(&f3);
	init_bignum(&pow_ten);
	init_bignum(&ten);
	
	ulong_to_bignum(&ten, 10);
	pow_bignum(&pow_ten, &ten, n - 1);

	// n digits + a small buffer
	dummy_str = malloc(n + 2);
	
	ulong_to_bignum(&f1, 1);
	ulong_to_bignum(&f2, 1);
	for(j = 3; ; j++)
	{
		copy_bignum(&f3, &f2);
		add_bignum(&f3, &f1);
		// if the current_fibonacci_number >= 10**n, then we're done
		if(ge_bignum(&f3, &pow_ten))
			break;
		swap_bignum(&f1, &f2);
		swap_bignum(&f2, &f3);
	}
	
	printf("First term in Fibonacci sequence to contain %d digits: F_%d\n", n, j);

	free(dummy_str);
	return 0;
}
Beispiel #7
0
void multiply_bignum(bignum *a, bignum *b, bignum *c) {
	bignum row;			/* represent shifted row */
	bignum tmp;			/* placeholder bignum */
	int i,j;			/* counters */

	initialize_bignum(c);

	row = *a;

	for(i = 0; i <= b->lastdigit; i++) {
		for(j = 1; j <= b->digits[i]; j++) {
			add_bignum(c,&row,&tmp);
			*c = tmp;
		}
		digit_shift(&row,1);
	}

	c->signbit = a->signbit * b->signbit;

	zero_justify(c);
}
Beispiel #8
0
int main(void) {
	int a,b;
	bignum n1,n2,n3,zero;

	while (scanf("%d %d",&a,&b) != EOF) {
		printf("a = %d    b = %d\n",a,b);
		int_to_bignum(a,&n1);
		int_to_bignum(b,&n2);

		add_bignum(&n1,&n2,&n3);
		printf("addition -- ");
		print_bignum(&n3);

		printf("compare_bignum a ? b = %d\n",compare_bignum(&n1, &n2));

		subtract_bignum(&n1,&n2,&n3);
		printf("subtraction -- ");
		print_bignum(&n3);

    multiply_bignum(&n1,&n2,&n3);
		printf("multiplication -- ");
    print_bignum(&n3);

		int_to_bignum(0,&zero);
		if(compare_bignum(&zero, &n2) == 0)
			printf("division -- NaN \n");
    else {
			divide_bignum(&n1,&n2,&n3);
			printf("division -- ");
                	print_bignum(&n3);
		}
		printf("--------------------------\n");
	}
	
	return 0;
}
Beispiel #9
0
/** Generate a new key.
 *  \param id The name to embed in the key.
 *  \return The newly generated key, containing public and private data.
 *  \sa gale_crypto_public() */
struct gale_group gale_crypto_generate(struct gale_text id) {
	RSA *rsa = NULL;
	int bits = gale_text_to_number(gale_var(G_("GALE_AUTH_BITS")));
	struct gale_group output = gale_group_empty();
	struct gale_fragment frag;

	if (0 == bits) bits = 768; /* default value */
	if (bits < 512) {
		gale_alert(GALE_WARNING,G_("expanding key size to 512"),0);
		bits = 512;
	}

	crypto_i_seed();
	gale_alert(GALE_NOTICE,G_("generating key, please wait..."),0);
	rsa = RSA_generate_key(bits,RSA_F4,NULL,NULL);
	assert(NULL != rsa);

	frag.type = frag_text;
	frag.name = G_("key.id");
	frag.value.text = id;
	gale_group_add(&output,frag);

	frag.type = frag_number;
	frag.name = G_("rsa.bits");
	frag.value.number = bits;
	gale_group_add(&output,frag);

	add_bignum(&output,G_("rsa.modulus"),GALE_RSA_MODULUS_LEN,1,rsa->n);
	add_bignum(&output,G_("rsa.exponent"),GALE_RSA_MODULUS_LEN,1,rsa->e);
	add_bignum(&output,G_("rsa.private.exponent"),
		GALE_RSA_MODULUS_LEN,1,rsa->d);
	add_bignum(&output,G_("rsa.private.prime"),
		GALE_RSA_PRIME_LEN,2,rsa->p,rsa->q);
	add_bignum(&output,G_("rsa.private.prime.exponent"),
		GALE_RSA_PRIME_LEN,2,rsa->dmp1,rsa->dmq1);
	add_bignum(&output,G_("rsa.private.coefficient"),
		GALE_RSA_PRIME_LEN,1,rsa->iqmp);

	if (NULL != rsa) RSA_free(rsa);
	return output;
}