Exemple #1
0
int bn2mpz(mpz_t out, bn_t in)
{
	int size = bn_size_str(in, 10);
	char * temp = malloc(sizeof(char)*size);

	bn_write_str(temp, size, in, 10);
	mpz_set_str(out,temp, 10);

	free(temp);
	return 0;
}
Exemple #2
0
int fp_size_str(const fp_t a, int radix) {
	bn_t t;
	int digits = 0;

	bn_null(t);

	TRY {
		bn_new(t);

		fp_prime_back(t, a);

		digits = bn_size_str(t, radix);
	} CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		bn_free(t);
	}

	return digits;
}
Exemple #3
0
void bn_write_str(char *str, int len, const bn_t a, int radix) {
	bn_t t;
	dig_t d;
	int digits, l, i, j;
	char c;

	bn_null(t);

	l = bn_size_str(a, radix);
	if (len < l) {
		THROW(ERR_NO_BUFFER);
	}

	if (radix < 2 || radix > 64) {
		THROW(ERR_NO_VALID)
	}

	if (bn_is_zero(a) == 1) {
		*str++ = '0';
		*str = '\0';
		return;
	}

	TRY {
		bn_new(t);
		bn_copy(t, a);

		j = 0;
		if (t->sign == BN_NEG) {
			str[j] = '-';
			j++;
			t->sign = BN_POS;
		}

		digits = 0;
		while (!bn_is_zero(t)) {
			bn_div_rem_dig(t, &d, t, (dig_t)radix);
			str[j] = util_conv_char(d);
			digits++;
			j++;
		}

		/* Reverse the digits of the string. */
		i = 0;
		if (str[0] == '-') {
			i = 1;
		}

		j = l - 2;
		while (i < j) {
			c = str[i];
			str[i] = str[j];
			str[j] = c;
			++i;
			--j;
		}

		str[l - 1] = '\0';
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		bn_free(t);
	}
}