Exemple #1
0
static int
testsimpel(void)
{
	const char str42[] = "2a";
	MINT *t2;
	char *s;

	mp_madd(c42, c1, t0);
	testmcmp(c43, t0, "madd0");
	mp_madd(t0, c1, t0);
	testmcmp(c44, t0, "madd1");
	mp_msub(t0, c1, t0);
	testmcmp(c43, t0, "msub0");
	mp_msub(t0, c1, t0);
	testmcmp(c42, t0, "msub1");
	mp_move(c42, t0);
	testmcmp(c42, t0, "move0");

	t2 = mp_xtom(str42);
	testmcmp(c42, t2, "xtom");
	s = mp_mtox(t2);
	if (strcmp(str42, s) == 0)
		printf("ok %d - %s\n", ++tnr, "mtox0");
	else
		printf("not ok %d - %s\n", ++tnr, "mtox0");
	mp_mfree(t2);
}
Exemple #2
0
void
mp_invert(MINT *x1, MINT *x0, MINT *c)
{
	MINT u2, u3;
	MINT v2, v3;
	MINT zero;
	MINT q, r;
	MINT t;
	MINT x0_prime;
	static MINT *one = NULL;

	/*
	 * Minimize calls to allocators.  Don't use pointers for local
	 * variables, for the one "initialized" multiple precision
	 * variable, do it just once.
	 */
	if (one == NULL)
		one = mp_itom(1);

	zero.len = q.len = r.len = t.len = 0;

	x0_prime.len = u2.len = u3.len = 0;
	_mp_move(x0, &u3);
	_mp_move(x0, &x0_prime);

	v2.len = v3.len = 0;
	_mp_move(one, &v2);
	_mp_move(x1, &v3);

	while (mp_mcmp(&v3, &zero) != 0) {
		/* invariant: x0*u1 + x1*u2 = u3 */
		/* invariant: x0*v1 + x2*v2 = v3 */
		/* invariant: x(n+1) = x(n-1) % x(n) */
		mp_mdiv(&u3, &v3, &q, &r);
		_mp_move(&v3, &u3);
		_mp_move(&r, &v3);

		mp_mult(&q, &v2, &t);
		mp_msub(&u2, &t, &t);
		_mp_move(&v2, &u2);
		_mp_move(&t, &v2);
	}
	/* now x0*u1 + x1*u2 == 1, therefore,  (u2*x1) % x0  == 1 */
	_mp_move(&u2, c);
	if (mp_mcmp(c, &zero) < 0) {
		mp_madd(&x0_prime, c, c);
	}
	_mp_xfree(&zero);
	_mp_xfree(&v2);
	_mp_xfree(&v3);
	_mp_xfree(&u2);
	_mp_xfree(&u3);
	_mp_xfree(&q);
	_mp_xfree(&r);
	_mp_xfree(&t);
}
Exemple #3
0
static void
m_sub(MINT *a, MINT *b, MINT *c)
{
	int x, i;
	int borrow;
	short one;
	MINT mone;

	one = 1;
	mone.len = 1;
	mone.val = &one;
	c->val = _mp_xalloc(a->len, "m_sub");
	borrow = 0;
	for (i = 0; i < b->len; i++) {
		x = borrow + a->val[i] - b->val[i];
		if (x & 0100000) {
			borrow = -1;
			c->val[i] = (short)(x & 077777);
		} else {
			borrow = 0;
			c->val[i] = (short)x;
		}
	}
	for (; i < a->len; i++) {
		x = borrow + a->val[i];
		if (x & 0100000) {
			c->val[i] = (short)(x & 077777);
		} else {
			borrow = 0;
			c->val[i] = (short)x;
		}
	}
	if (borrow < 0) {
		for (i = 0; i < a->len; i++) {
			c->val[i] ^= 077777;
		}
		c->len = a->len;
		mp_madd(c, &mone, c);
	}
	for (i = a->len-1; i >= 0; --i) {
		if (c->val[i] > 0) {
			if (borrow == 0) {
				c->len = i + 1;
			} else {
				c->len = -i - 1;
			}
			return;
		}
	}
	free(c->val);
}
Exemple #4
0
void
mp_msub(MINT *a, MINT *b, MINT *c)
{
	MINT x, y;
	int sign;

	x.len = y.len = 0;
	_mp_move(a, &x);
	_mp_move(b, &y);
	_mp_xfree(c);
	sign = 1;
	if (x.len >= 0) {
		if (y.len >= 0) {
			if (x.len >= y.len) {
				m_sub(&x, &y, c);
			} else {
				sign = -1;
				mp_msub(&y, &x, c);
			}
		} else {
			y.len = -y.len;
			mp_madd(&x, &y, c);
		}
	} else {
		if (y.len <= 0) {
			x.len = -x.len;
			y.len = -y.len;
			mp_msub(&y, &x, c);
		} else {
			x.len = -x.len;
			mp_madd(&x, &y, c);
			sign = -1;
		}
	}
	c->len = sign * c->len;
	_mp_xfree(&x);
	_mp_xfree(&y);
}
Exemple #5
0
static int
m_in(MINT *a, short b, FILE *f)
{
	MINT x, y, ten;
	int sign, c;
	short qten, qy;

	_mp_xfree(a);
	sign = 1;
	ten.len = 1;
	ten.val = &qten;
	qten = b;
	x.len = 0;
	y.len = 1;
	y.val = &qy;
	while ((c = getc(f)) != EOF)
	switch (c) {

	case '\\':
		(void) getc(f);
		continue;
	case '\t':
	case '\n':
		a->len *= sign;
		_mp_xfree(&x);
		return (0);
	case ' ':
		continue;
	case '-':
		sign = -sign;
		continue;
	default:
		if (c >= '0' && c <= '9') {
			qy = c - '0';
			mp_mult(&x, &ten, a);
			mp_madd(a, &y, a);
			_mp_move(a, &x);
			continue;
		} else {
			(void) ungetc(c, stdin);
			a->len *= sign;
			return (0);
		}
	}
	return (EOF);
}
Exemple #6
0
/*
 * Choose middle 64 bits of the common key to use as our des key, possibly
 * overwriting the lower order bits by setting parity. 
 */
static void
extractdeskey(MINT *ck, DesData *deskey)
{
        MINT *a;
        MINT *z;
        short r;
        int i;
        short base = (1 << 8);
        char *k;

        z = mp_itom(0);
        a = mp_itom(0);
        mp_madd(ck, z, a);
        for (i = 0; i < ((KEYSIZE - 64) / 2) / 8; i++) {
                mp_sdiv(a, base, a, &r);
        }
        k = (char *)deskey;
        for (i = 0; i < 8; i++) {
                mp_sdiv(a, base, a, &r);
                *k++ = r;
        }
	mp_mfree(z);
        mp_mfree(a);
}
Exemple #7
0
/*
 * Choose top 128 bits of the common key to use as our idea key.
 */
static void
extractideakey(MINT *ck, IdeaData *ideakey)
{
        MINT *a;
        MINT *z;
        short r;
        int i;
        short base = (1 << 8);
        char *k;

        z = mp_itom(0);
        a = mp_itom(0);
        mp_madd(ck, z, a);
        for (i = 0; i < ((KEYSIZE - 128) / 8); i++) {
                mp_sdiv(a, base, a, &r);
        }
        k = (char *)ideakey;
        for (i = 0; i < 16; i++) {
                mp_sdiv(a, base, a, &r);
                *k++ = r;
        }
	mp_mfree(z);
        mp_mfree(a);
}
Exemple #8
0
void madd(MINT *a, MINT *b, MINT *c) { mp_madd(a, b, c); }