Exemple #1
0
int eb_upk(eb_t r, const eb_t p) {
	fb_t t0, t1;
	int res = 0;

	fb_null(t0);
	fb_null(t1);

	TRY {
		fb_new(t0);
		fb_new(t1);

		eb_rhs(t1, p);

		if (eb_curve_is_super()) {
			/* t0 = c^2. */
			fb_sqr(t0, eb_curve_get_c());
			/* t0 = 1/c^2. */
			fb_inv(t0, t0);
			/* t0 = t1/c^2. */
			fb_mul(t0, t0, t1);
			res = (fb_trc(t0) == 0);
			/* Solve t1^2 + t1 = t0. */
			fb_slv(t1, t0);
			/* If this is not the correct solution, try the other. */
			if (fb_get_bit(t1, 0) != fb_get_bit(p->y, 0)) {
				fb_add_dig(t1, t1, 1);
			}
			/* x3 = x1, y3 = t1 * c, z3 = 1. */
			fb_mul(r->y, t1, eb_curve_get_c());
		} else {
			fb_sqr(t0, p->x);
			/* t0 = 1/x1^2. */
			fb_inv(t0, t0);
			/* t0 = t1/x1^2. */
			fb_mul(t0, t0, t1);
			res = (fb_trc(t0) == 0);
			/* Solve t1^2 + t1 = t0. */
			fb_slv(t1, t0);
			/* If this is not the correct solution, try the other. */
			if (fb_get_bit(t1, 0) != fb_get_bit(p->y, 0)) {
				fb_add_dig(t1, t1, 1);
			}
			/* x3 = x1, y3 = t1 * x1, z3 = 1. */
			fb_mul(r->y, t1, p->x);
		}
		fb_copy(r->x, p->x);
		fb_set_dig(r->z, 1);

		r->norm = 1;
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		fb_free(t0);
		fb_free(t1);
	}
	return res;
}
Exemple #2
0
void eb_hlv(eb_t r, const eb_t p) {
	fb_t l, t;

	fb_null(l);
	fb_null(t);

	TRY {
		fb_new(l);
		fb_new(t);

		/* Solve l^2 + l = u + a. */
		switch (eb_curve_opt_a()) {
			case RLC_ZERO:
				fb_copy(t, p->x);
				break;
			case RLC_ONE:
				fb_add_dig(t, p->x, (dig_t)1);
				break;
			case RLC_TINY:
				fb_add_dig(t, p->x, eb_curve_get_a()[0]);
				break;
			default:
				fb_add(t, p->x, eb_curve_get_a());
				break;
		}

		fb_slv(l, t);

		if (p->norm == 1) {
			/* Compute t = v + u * lambda. */
			fb_mul(t, l, p->x);
			fb_add(t, t, p->y);
		} else {
			/* Compute t = u * (u + lambda_P + lambda). */
			fb_add(t, l, p->y);
			fb_add(t, t, p->x);
			fb_mul(t, t, p->x);
		}

		/* If Tr(t) = 0 then lambda_P = lambda, u = sqrt(t + u). */
		if (fb_trc(t) == 0) {
			fb_copy(r->y, l);
			fb_add(t, t, p->x);
			fb_srt(r->x, t);
		} else {
			/* Else lambda_P = lambda + 1, u = sqrt(t). */
			fb_add_dig(r->y, l, 1);
			fb_srt(r->x, t);
		}
		fb_set_dig(r->z, 1);
		r->norm = 2;
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		fb_free(l);
		fb_free(t);
	}
}
void fb_slv_basic(fb_t c, const fb_t a) {
	int i;
	fb_t t0;

	fb_null(t0);

	TRY {
		fb_new(t0);

		fb_copy(t0, a);
		fb_copy(c, a);

		for (i = 0; i < (FB_BITS - 1) / 2; i++) {
			fb_sqr(c, c);
			fb_sqr(c, c);
			fb_add(c, c, t0);
		}

		fb_add_dig(c, c, fb_trc(c));
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		fb_free(t0);
	}
}
void fb_slv_quick(fb_t c, const fb_t a) {
	fb_slvn_low(c, a);
	fb_add_dig(c, c, fb_trc(c));
}