Example #1
0
int
main()
{
	mpl_int a;
	int i;

	if (mpl_init(&a) != MPL_OK) {
		printf("can't init\n");
		goto end;
	}

	a.top = 1;
	a.dig[0] = 0x09090909;
	a.dig[1] = 0x09090909;

	for (i = 0; i < 8; i++)
		printf("%i: %i\n", i, mpl_check_bit(&a, i));

	for (i = 48; i < 64; i++)
		printf("%i: %i\n", i, mpl_check_bit(&a, i));

	mpl_clear(&a);
end:
	return 0;
}
Example #2
0
void
var_init(struct variable *var)
{
	assert(var != NULL);

	var->type = 0;

	//FIXME: need to check ret status
	mpl_init(&var->bnum);
	octstr_init(&var->octstr);
	str_init(&var->str);
}
Example #3
0
int
main()
{
  struct stack sp;
	
	stk_init(&sp);
	mpl_int a, b, c, d, e, f;
	char as[] = "12";
	char bs[] = "128";
	char cs[] = "5";
	
	char str[10];
	int i;
	
	mpl_init(&a);
	mpl_init(&b);
	mpl_init(&c);
	
	mpl_set_str(&a, as, 10);	
	mpl_set_str(&b, bs, 10);
	mpl_set_str(&c, cs, 10);
	
	push_item(&sp, &a);
	push_item(&sp, &b);
	push_item(&sp, &c);
	
	print_stk(&sp);
	
	pop_item(&sp, &d);
	pop_item(&sp, &e);
	pop_item(&sp, &f);
	
	mpl_add(&d, &f, &e);
	mpl_to_str(&d, str, 10, 10);
	for (i = 0; str[i] != '\0'; i++)
		printf("%c", str[i]);
	printf("\n");
	return 0;
}
Example #4
0
File: syntax.c Project: loiso/scalc
static void
pr_expr(void)
{
	number val;
	int rv;
	
	rv = mpl_init(&val.value);
	if (rv != MPL_OK){
		err = MEM_ERR;
		goto out;
	}
	
	switch (token.type) {
	
	case TOKEN_LPARENTH:
		get_next_token();
		add_expr();
		
		if (err == SYN_ERR || err == MEM_ERR) {
			goto out;
		}
		if (token.type != TOKEN_RPARENTH) {
			err = SYN_ERR;
			goto out;
		} else {
			get_next_token();
		}
		goto out;
		
	case TOKEN_INTEGER:
		rv = mpl_copy(&val.value, &token.num.value);
		val.frac = token.num.frac;
		if (rv != MPL_OK)
			goto err;
			
		rv = push_item(&sp, &val);
		if (rv != OK)
			goto err;

		get_next_token();
		goto out;
		
	default:
		err = SYN_ERR;
		goto out;
	}
err:
	err = MEM_ERR;
out:
	return ;
}
Example #5
0
File: syntax.c Project: loiso/scalc
void
parse(void)
{
	int rv;
	
	stk_init(&sp);
	err = 0;
	
	rv = mpl_init(&token.num.value);
	if (rv != MPL_OK) {
		printf("memory error\n");
		return ;
	}
	get_next_token();
	
	list_expr();
}
Example #6
0
int
mpl_mod_exp(mpl_int *c, const mpl_int *a, const mpl_int *y, const mpl_int *b)
{

	mpl_int w[MPL_MODEXP_STACK];
	mpl_int e, s, mu, z;
	_mpl_int_t *dp, buffer;
	int i, k, nbits, rc;
	int do_single;
	unsigned int mask, x, n, tmp, cnt;

	if ((rc = mpl_initv(&e, &s, &mu, &z, NULL)) != MPL_OK)
		return rc;

	if (mpl_isneg(y)) {
		if ((rc = mpl_mod_inv(&z, a, b)) != MPL_OK)
			goto end;
	} else {
		if ((rc = mpl_copy(&z, a)) != MPL_OK)
			goto end;
	}

	n = mpl_nr_bits(y);

	if (n <= 7)
		k = 2;
	else if (n <= 36)
		k = 3;
	else if (n <= 140)
		k = 4;
	else if (n <= 450)
		k = 5;
	else if (n <= 1303)
		k = 6;
	else if (n <= 3529)
		k = 7;
	else
		k = 8;

	cnt = 0;

	for (i = 0; i < 1 << (k-1); i++) {
		rc = mpl_init(&w[i]);
		if (rc != MPL_OK) {
			for (i = 0; i < cnt; i++)
				mpl_clear(&w[i]);
			goto end;
		}
		++cnt;
	}

	/* reduce Z */
	rc = mpl_div(NULL, &z, &z, b);
	if (rc != MPL_OK)
		goto err;

	/* e = a */
	rc = mpl_copy(&e, &z);
	if (rc != MPL_OK)
		goto err;

	/* c = 1 */
	mpl_set_one(&s);

	/* prepare reduction constant */
	rc = mpl_reduce_barrett_setup(&mu, b);
	if (rc != MPL_OK)
		goto err;

	/* e = a^{2^(k-1)} */
	for (i = 0; i < k-1; i++) {
		rc = mpl_sqr(&e, &e);
		if (rc != MPL_OK)
			goto err;
		rc = mpl_reduce_barrett(&e, &e, b, &mu);
		if (rc != MPL_OK)
			goto err;
	}

	/* Now fill precomputed table. */
	rc = mpl_copy(&w[0], &e);
	if (rc != MPL_OK)
		goto err;

	for (i = 1; i < 1 << (k-1); i++) {
		/* w[i] = (w[i-1] * a) mod b */
		rc = mpl_mul(&w[i], &w[i-1], &z);
		if (rc != MPL_OK)
			goto err;
		rc = mpl_reduce_barrett(&w[i], &w[i], b, &mu);
		if (rc != MPL_OK)
			goto err;
	}
	
	buffer = nbits = do_single = cnt = 0;

	/* Count bits of the topmost MP integer digit. */
	dp = y->dig + y->top;
	tmp = *dp;
	for (cnt = 0; tmp > 0; cnt++)
		tmp >>= 1;
	nbits = cnt;
	buffer = *dp--;

	/* Precalculated window mask. */
	mask = (1 << k) - 1;

	while (n > 0) {
		unsigned int left, xmask;

		if (nbits == 0) {
			buffer = *dp--;
			nbits = MPL_INT_BITS;
		}

		/* Check most significant bit of the bit buffer. */
		if ((buffer & (1 << (nbits-1))) == 0) {
			/* c = c^2 mod b */
			rc = mpl_sqr(&s, &s);
			if (rc != MPL_OK)
				goto err;

			rc = mpl_reduce_barrett(&s, &s, b, &mu);
			if (rc != MPL_OK)
				goto err;

			--nbits;
			--n;
			continue;
		}

		if (nbits >= k) {
			/* We have enough bits in the buffer to fill window. */
			x = (buffer & (mask << (nbits-k))) >> (nbits-k);
			nbits -= k;
			n -= k;
		} else {