Beispiel #1
0
void ep_rhs(fp_t rhs, const ep_t p) {
	fp_t t0;
	fp_t t1;

	fp_null(t0);
	fp_null(t1);

	TRY {
		fp_new(t0);
		fp_new(t1);

		/* t0 = x1^2. */
		fp_sqr(t0, p->x);
		/* t1 = x1^3. */
		fp_mul(t1, t0, p->x);

		/* t1 = x1^3 + a * x1 + b. */
		switch (ep_curve_opt_a()) {
			case OPT_ZERO:
				break;
			case OPT_ONE:
				fp_add(t1, t1, p->x);
				break;
#if FP_RDC != MONTY
			case OPT_DIGIT:
				fp_mul_dig(t0, p->x, ep_curve_get_a()[0]);
				fp_add(t1, t1, t0);
				break;
#endif
			default:
				fp_mul(t0, p->x, ep_curve_get_a());
				fp_add(t1, t1, t0);
				break;
		}

		switch (ep_curve_opt_b()) {
			case OPT_ZERO:
				break;
			case OPT_ONE:
				fp_add_dig(t1, t1, 1);
				break;
#if FP_RDC != MONTY
			case OPT_DIGIT:
				fp_add_dig(t1, t1, ep_curve_get_b()[0]);
				break;
#endif
			default:
				fp_add(t1, t1, ep_curve_get_b());
				break;
		}

		fp_copy(rhs, t1);

	} CATCH_ANY {
		THROW(ERR_CAUGHT);
	} FINALLY {
		fp_free(t0);
		fp_free(t1);
	}
}
Beispiel #2
0
int fp2_srt(fp2_t c, fp2_t a) {
	int r = 0;
	fp_t t1;
	fp_t t2;
	fp_t t3;

	fp_null(t1);
	fp_null(t2);
	fp_null(t3);

	TRY {
		fp_new(t1);
		fp_new(t2);
		fp_new(t3);

		/* t1 = a[0]^2 - u^2 * a[1]^2 */
		fp_sqr(t1, a[0]);
		fp_sqr(t2, a[1]);
		for (int i = -1; i > fp_prime_get_qnr(); i--) {
			fp_add(t1, t1, t2);
		}
		for (int i = 0; i <= fp_prime_get_qnr(); i++) {
			fp_sub(t1, t1, t2);
		}		
		fp_add(t1, t1, t2);

		if (fp_srt(t2, t1)) {
			/* t1 = (a_0 + sqrt(t1)) / 2 */
			fp_add(t1, a[0], t2);
			fp_set_dig(t3, 2);
			fp_inv(t3, t3);
			fp_mul(t1, t1, t3);

			if (!fp_srt(t3, t1)) {
				/* t1 = (a_0 - sqrt(t1)) / 2 */
				fp_sub(t1, a[0], t2);
				fp_set_dig(t3, 2);
				fp_inv(t3, t3);
				fp_mul(t1, t1, t3);
				fp_srt(t3, t1);
			}
			/* c_0 = sqrt(t1) */
			fp_copy(c[0], t3);
			/* c_1 = a_1 / (2 * sqrt(t1)) */
			fp_dbl(t3, t3);
			fp_inv(t3, t3);
			fp_mul(c[1], a[1], t3);
			r = 1;
		}
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		fp_free(t1);
		fp_free(t2);
		fp_free(t3);
	}
	return r;
}
Beispiel #3
0
/**
 * Normalizes a point represented in projective coordinates.
 *
 * @param r			- the result.
 * @param p			- the point to normalize.
 */
static void ep_norm_imp(ep_t r, const ep_t p, int inverted) {
	if (!p->norm) {
		fp_t t0, t1;

		fp_null(t0);
		fp_null(t1);

		TRY {

			fp_new(t0);
			fp_new(t1);

			if (inverted) {
				fp_copy(t1, p->z);
			} else {
				fp_inv(t1, p->z);
			}
			fp_sqr(t0, t1);
			fp_mul(r->x, p->x, t0);
			fp_mul(t0, t0, t1);
			fp_mul(r->y, p->y, t0);
			fp_set_dig(r->z, 1);
		}
		CATCH_ANY {
			THROW(ERR_CAUGHT);
		}
		FINALLY {
			fp_free(t0);
			fp_free(t1);
		}
	}
Beispiel #4
0
void fp2_sqr_basic(fp2_t c, fp2_t a) {
	fp_t t0, t1, t2;

	fp_null(t0);
	fp_null(t1);
	fp_null(t2);

	TRY {
		fp_new(t0);
		fp_new(t1);
		fp_new(t2);

		/* t0 = (a_0 + a_1). */
		fp_add(t0, a[0], a[1]);

		/* t1 = (a_0 - a_1). */
		fp_sub(t1, a[0], a[1]);

		/* t1 = a_0 + u^2 * a_1. */
		for (int i = -1; i > fp_prime_get_qnr(); i--) {
			fp_sub(t1, t1, a[1]);
		}
		for (int i = 0; i <= fp_prime_get_qnr(); i++) {
			fp_add(t1, t1, a[1]);
		}

		if (fp_prime_get_qnr() == -1) {
			/* t2 = 2 * a_0. */
			fp_dbl(t2, a[0]);
			/* c_1 = 2 * a_0 * a_1. */
			fp_mul(c[1], t2, a[1]);
			/* c_0 = a_0^2 + a_1^2 * u^2. */
			fp_mul(c[0], t0, t1);
		} else {
			/* c_1 = a_0 * a_1. */
			fp_mul(c[1], a[0], a[1]);
			/* c_0 = a_0^2 + a_1^2 * u^2. */
			fp_mul(c[0], t0, t1);
			for (int i = -1; i > fp_prime_get_qnr(); i--) {
				fp_add(c[0], c[0], c[1]);
			}
			for (int i = 0; i <= fp_prime_get_qnr(); i++) {
				fp_sub(c[0], c[0], c[1]);
			}			
			/* c_1 = 2 * a_0 * a_1. */
			fp_dbl(c[1], c[1]);
		}
		/* c = c_0 + c_1 * u. */
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		fp_free(t0);
		fp_free(t1);
		fp_free(t2);
	}
}
Beispiel #5
0
void fp2_inv(fp2_t c, fp2_t a) {
	fp_t t0, t1;

	fp_null(t0);
	fp_null(t1);

	TRY {
		fp_new(t0);
		fp_new(t1);

		/* t0 = a_0^2, t1 = a_1^2. */
		fp_sqr(t0, a[0]);
		fp_sqr(t1, a[1]);

		/* t1 = 1/(a_0^2 + a_1^2). */
#ifndef FP_QNRES
		if (fp_prime_get_qnr() != -1) {
			if (fp_prime_get_qnr() == -2) {
				fp_dbl(t1, t1);
				fp_add(t0, t0, t1);
			} else {
				if (fp_prime_get_qnr() < 0) {
					fp_mul_dig(t1, t1, -fp_prime_get_qnr());
					fp_add(t0, t0, t1);
				} else {
					fp_mul_dig(t1, t1, fp_prime_get_qnr());
					fp_sub(t0, t0, t1);
				}
			}
		} else {
			fp_add(t0, t0, t1);
		}
#else
		fp_add(t0, t0, t1);
#endif

		fp_inv(t1, t0);

		/* c_0 = a_0/(a_0^2 + a_1^2). */
		fp_mul(c[0], a[0], t1);
		/* c_1 = - a_1/(a_0^2 + a_1^2). */
		fp_mul(c[1], a[1], t1);
		fp_neg(c[1], c[1]);
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		fp_free(t0);
		fp_free(t1);
	}
}
Beispiel #6
0
void ed_norm_sim(ed_t *r, const ed_t *t, int n) {
	int i;
	fp_t a[n];

	for (i = 0; i < n; i++) {
		fp_null(a[i]);
	}

	TRY {
		for (i = 0; i < n; i++) {
			fp_new(a[i]);
			fp_copy(a[i], t[i]->z);
		}

		fp_inv_sim(a, (const fp_t *)a, n);

		for (i = 0; i < n; i++) {
			fp_mul(r[i]->x, t[i]->x, a[i]);
			fp_mul(r[i]->y, t[i]->y, a[i]);
#if ED_ADD == EXTND
			fp_mul(r[i]->t, t[i]->t, a[i]);
#endif
			fp_set_dig(r[i]->z, 1);
		}
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		for (i = 0; i < n; i++) {
			fp_free(a[i]);
		}
	}
}
Beispiel #7
0
int fp_cmp_dig(const fp_t a, dig_t b) {
#if FP_RDC == MONTY
	fp_t t;
	int r = CMP_EQ;

	fp_null(t);

	TRY {
		fp_new(t);

		fp_prime_conv_dig(t, b);
		r = fp_cmp(a, t);
	} CATCH_ANY {
		THROW(ERR_CAUGHT);
	} FINALLY {
		fp_free(t);
	}

	return r;
#else
	for (int i = 1; i < FP_DIGS; i++) {
		if (a[i] > 0) {
			return CMP_GT;
		}
	}

	return fp_cmp1_low(a[0], b);
#endif
}
Beispiel #8
0
int ep_upk(ep_t r, const ep_t p) {
	fp_t t;
	int result = 0;

	fp_null(t);

	TRY {
		fp_new(t);

		ep_rhs(t, p);

		/* t0 = sqrt(x1^3 + a * x1 + b). */
		result = fp_srt(t, t);

		if (result) {
			/* Verify if least significant bit of the result matches the
			 * compressed y-coordinate. */
			if (fp_get_bit(t, 0) != fp_get_bit(p->y, 0)) {
				fp_neg(t, t);
			}
			fp_copy(r->x, p->x);
			fp_copy(r->y, t);
			fp_set_dig(r->z, 1);
			r->norm = 1;
		}
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		fp_free(t);
	}
	return result;
}
Beispiel #9
0
void fp2_mul_art(fp2_t c, fp2_t a) {
	fp_t t;

	fp_null(t);

	TRY {
		fp_new(t);

#ifdef FP_QNRES
		/* (a_0 + a_1 * i) * i = -a_1 + a_0 * i. */
		fp_copy(t, a[0]);
		fp_neg(c[0], a[1]);
		fp_copy(c[1], t);
#else
		/* (a_0 + a_1 * u) * u = (a_1 * u^2) + a_0 * u. */
		fp_copy(t, a[0]);
		fp_neg(c[0], a[1]);
		for (int i = -1; i > fp_prime_get_qnr(); i--) {
			fp_sub(c[0], c[0], a[1]);
		}
		fp_copy(c[1], t);
#endif
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		fp_free(t);
	}
}
Beispiel #10
0
/**
* Normalizes a point represented in projective coordinates.
*
* @param r			- the result.
* @param p			- the point to normalize.
*/
void ed_norm(ed_t r, const ed_t p) {
	if (ed_is_infty(p)) {
		ed_set_infty(r);
		return;
	}

	if (fp_cmp_dig(p->z, 1) == CMP_EQ) {
		/* If the point is represented in affine coordinates, we just copy it. */
		ed_copy(r, p);
	} else {
		fp_t z_inv;

		fp_null(z_inv);
		fp_new(z_inv);

		fp_inv(z_inv, p->z);

		fp_mul(r->x, p->x, z_inv);
		fp_mul(r->y, p->y, z_inv);
	#if ED_ADD == EXTND
		fp_mul(r->t, p->t, z_inv);
	#endif

		fp_set_dig(r->z, 1);

		fp_free(z_inv);
	}
}
Beispiel #11
0
int fp2_upk(fp2_t c, fp2_t a) {
	int result, b = fp_get_bit(a[1], 0);
	fp_t t;

	fp_null(t);

	TRY {
		fp_new(t);

		/* a_0^2 + a_1^2 = 1, thus a_1^2 = 1 - a_0^2. */
		fp_sqr(t, a[0]);
		fp_sub_dig(t, t, 1);
		fp_neg(t, t);

		/* a1 = sqrt(a_0^2). */
		result = fp_srt(t, t);

		if (result) {
			/* Verify if least significant bit of the result matches the
			 * compressed second coordinate. */
			if (fp_get_bit(t, 0) != b) {
				fp_neg(t, t);
			}
			fp_copy(c[0], a[0]);
			fp_copy(c[1], t);
		}
	} CATCH_ANY {
		THROW(ERR_CAUGHT);
	} FINALLY {
		fp_free(t);
	}

	return result;
}
Beispiel #12
0
void fp_exp_monty(fp_t c, const fp_t a, const bn_t b) {
	fp_t t[2];

	fp_null(t[0]);
	fp_null(t[1]);

	if (bn_is_zero(b)) {
		fp_set_dig(c, 1);
		return;
	}

	TRY {
		fp_new(t[0]);
		fp_new(t[1]);

		fp_set_dig(t[0], 1);
		fp_copy(t[1], a);

		for (int i = bn_bits(b) - 1; i >= 0; i--) {
			int j = bn_get_bit(b, i);
			dv_swap_cond(t[0], t[1], FP_DIGS, j ^ 1);
			fp_mul(t[0], t[0], t[1]);
			fp_sqr(t[1], t[1]);
			dv_swap_cond(t[0], t[1], FP_DIGS, j ^ 1);
		}

		if (bn_sign(b) == BN_NEG) {
			fp_inv(c, t[0]);
		} else {
			fp_copy(c, t[0]);
		}
	} CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		fp_free(t[1]);
		fp_free(t[0]);
	}
}
Beispiel #13
0
int ed_affine_is_valid(const fp_t x, const fp_t y) {
	fp_t tmpFP0;
	fp_t tmpFP1;
	fp_t tmpFP2;

	fp_null(tmpFP0);
	fp_null(tmpFP1);
	fp_null(tmpFP2);

	int r = 0;

	TRY {
		fp_new(tmpFP0);
		fp_new(tmpFP1);
		fp_new(tmpFP2);

		// a * X^2 + Y^2 - 1 - d * X^2 * Y^2 =?= 0
		fp_sqr(tmpFP0, x);
		fp_mul(tmpFP0, core_get()->ed_a, tmpFP0);
		fp_sqr(tmpFP1, y);
		fp_add(tmpFP1, tmpFP0, tmpFP1);
		fp_sub_dig(tmpFP1, tmpFP1, 1);
		fp_sqr(tmpFP0, x);
		fp_mul(tmpFP0, core_get()->ed_d, tmpFP0);
		fp_sqr(tmpFP2, y);
		fp_mul(tmpFP2, tmpFP0, tmpFP2);
		fp_sub(tmpFP0, tmpFP1, tmpFP2);

		r = fp_is_zero(tmpFP0);
	} CATCH_ANY {
		THROW(ERR_CAUGHT);
	} FINALLY {
		fp_free(tmpFP0);
		fp_free(tmpFP1);
		fp_free(tmpFP2);
	}
	return r;
}
Beispiel #14
0
int ed_is_valid(const ed_t p) {
	ed_t t;
#if ED_ADD == EXTND
	fp_t x_times_y;
#endif
	int r = 0;

	ed_null(t);
#if ED_ADD == EXTND
	fp_null(x_times_y);
#endif

	if (fp_is_zero(p->z)) {
		r = 0;
	} else {
		TRY {
#if ED_ADD == EXTND
			fp_new(x_times_y);
#endif
			ed_new(t);
			ed_norm(t, p);

			// check t coordinate
#if ED_ADD == PROJC
			r = ed_affine_is_valid(t->x, t->y);
#elif ED_ADD == EXTND
			fp_mul(x_times_y, t->x, t->y);
			if (fp_cmp(x_times_y, t->t) != CMP_EQ) {
				r = 0;
			} else {
				r = ed_affine_is_valid(t->x, t->y);
			}
#endif
			// if (r == 0) {
			// 	util_printf("\n\n(X, Y, T, Z) = \n");
			// 	ed_print(p);
			// }
		} CATCH_ANY {
			THROW(ERR_CAUGHT);
		} FINALLY {
#if ED_ADD == EXTND
			fp_free(x_times_y);
#endif
			ed_free(t);
		}
	}
	return r;
}
Beispiel #15
0
int fp_cmp_dig(const fp_t a, dig_t b) {
	fp_t t;
	int r = RLC_EQ;

	fp_null(t);

	TRY {
		fp_new(t);
		fp_prime_conv_dig(t, b);
		r = fp_cmp(a, t);
	} CATCH_ANY {
		THROW(ERR_CAUGHT);
	} FINALLY {
		fp_free(t);
	}

	return r;
}
Beispiel #16
0
int ed_is_infty(const ed_t p) {
	assert(!fp_is_zero(p->z));
	int ret = 0;
	fp_t norm_y;

	fp_null(norm_y);
	fp_new(norm_y);

	fp_inv(norm_y, p->z);
	fp_mul(norm_y, p->y, norm_y);

	if (fp_cmp_dig(norm_y, 1) == CMP_EQ && fp_is_zero(p->x)) {
		ret = 1;
	}

	fp_free(norm_y);
	return ret;
}
Beispiel #17
0
/**
 * Detects an optimization based on the curve coefficients.
 *
 * @param[out] opt		- the resulting optimization.
 * @param[in] a			- the curve coefficient.
 */
static void detect_opt(int *opt, fp_t a) {
	fp_t t;

	fp_null(t);

	TRY {
		fp_new(t);
		fp_prime_conv_dig(t, 3);
		fp_neg(t, t);

		if (fp_cmp(a, t) == CMP_EQ) {
			*opt = OPT_MINUS3;
		} else {
			if (fp_is_zero(a)) {
				*opt = OPT_ZERO;
			} else {
				fp_set_dig(t, 1);
				if (fp_cmp_dig(a, 1) == CMP_EQ) {
					*opt = OPT_ONE;
				} else {
					if (fp_cmp_dig(a, 2) == CMP_EQ) {
						*opt = OPT_TWO;
					} else {
						if (fp_bits(a) <= FP_DIGIT) {
							*opt = OPT_DIGIT;
						} else {
							*opt = RELIC_OPT_NONE;
						}
					}
				}
			}
		}
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		fp_free(t);
	}
}
Beispiel #18
0
void fp3_mul_art(fp3_t c, fp3_t a) {
	fp_t t;

	fp_null(t);

	TRY {
		fp_new(t);

		/* (a_0 + a_1 * u + a_1 * u^2) * u = a_0 * u + a_1 * u^2 + a_1 * u^3. */
		fp_copy(t, a[0]);
		fp_dbl(c[0], a[2]);
		fp_neg(c[0], c[0]);
		fp_copy(c[2], a[1]);
		fp_copy(c[1], t);
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		fp_free(t);
	}
}
Beispiel #19
0
void fp_exp_basic(fp_t c, const fp_t a, const bn_t b) {
	int i, l;
	fp_t r;

	fp_null(r);

	if (bn_is_zero(b)) {
		fp_set_dig(c, 1);
		return;
	}

	TRY {
		fp_new(r);

		l = bn_bits(b);

		fp_copy(r, a);

		for (i = l - 2; i >= 0; i--) {
			fp_sqr(r, r);
			if (bn_get_bit(b, i)) {
				fp_mul(r, r, a);
			}
		}

		if (bn_sign(b) == BN_NEG) {
			fp_inv(c, r);
		} else {
			fp_copy(c, r);
		}
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		fp_free(r);
	}
}
Beispiel #20
0
void pp_dbl_k2_basic(fp2_t l, ep_t r, ep_t p, ep_t q) {
	fp_t s;
	ep_t t;

	fp_null(s);
	ep_null(t);

	TRY {
		fp_new(s);
		ep_new(t);

		ep_copy(t, p);
		ep_dbl_slp_basic(r, s, p);
		fp_add(l[0], t->x, q->x);
		fp_mul(l[0], l[0], s);
		fp_sub(l[0], t->y, l[0]);
		fp_copy(l[1], q->y);
	} CATCH_ANY {
		THROW(ERR_CAUGHT);
	} FINALLY {
		fp_free(s);
		ep_free(t);
	}
}
Beispiel #21
0
void fp_exp_slide(fp_t c, const fp_t a, const bn_t b) {
	fp_t t[1 << (FP_WIDTH - 1)], r;
	int i, j, l;
	uint8_t win[FP_BITS + 1];

	fp_null(r);

	if (bn_is_zero(b)) {
		fp_set_dig(c, 1);
		return;
	}


	/* Initialize table. */
	for (i = 0; i < (1 << (FP_WIDTH - 1)); i++) {
		fp_null(t[i]);
	}

	TRY {
		for (i = 0; i < (1 << (FP_WIDTH - 1)); i ++) {
			fp_new(t[i]);
		}
		fp_new(r);

		fp_copy(t[0], a);
		fp_sqr(r, a);

		/* Create table. */
		for (i = 1; i < 1 << (FP_WIDTH - 1); i++) {
			fp_mul(t[i], t[i - 1], r);
		}

		fp_set_dig(r, 1);
		l = FP_BITS + 1;
		bn_rec_slw(win, &l, b, FP_WIDTH);
		for (i = 0; i < l; i++) {
			if (win[i] == 0) {
				fp_sqr(r, r);
			} else {
				for (j = 0; j < util_bits_dig(win[i]); j++) {
					fp_sqr(r, r);
				}
				fp_mul(r, r, t[win[i] >> 1]);
			}
		}

		if (bn_sign(b) == BN_NEG) {
			fp_inv(c, r);
		} else {
			fp_copy(c, r);
		}
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		for (i = 0; i < (1 << (FP_WIDTH - 1)); i++) {
			fp_free(t[i]);
		}
		fp_free(r);
	}
}
Beispiel #22
0
/**
 * Adds two points represented in affine coordinates on an ordinary prime
 * elliptic curve.
 *
 * @param[out] r			- the result.
 * @param[out] s			- the slope.
 * @param[in] p				- the first point to add.
 * @param[in] q				- the second point to add.
 */
static void ep_add_basic_imp(ep_t r, fp_t s, const ep_t p, const ep_t q) {
	fp_t t0, t1, t2;

	fp_null(t0);
	fp_null(t1);
	fp_null(t2);

	TRY {
		fp_new(t0);
		fp_new(t1);
		fp_new(t2);

		/* t0 = x2 - x1. */
		fp_sub(t0, q->x, p->x);
		/* t1 = y2 - y1. */
		fp_sub(t1, q->y, p->y);

		/* If t0 is zero. */
		if (fp_is_zero(t0)) {
			if (fp_is_zero(t1)) {
				/* If t1 is zero, q = p, should have doubled. */
				ep_dbl_basic(r, p);
			} else {
				/* If t1 is not zero and t0 is zero, q = -p and r = infinity. */
				ep_set_infty(r);
			}
		} else {
			/* t2 = 1/(x2 - x1). */
			fp_inv(t2, t0);
			/* t2 = lambda = (y2 - y1)/(x2 - x1). */
			fp_mul(t2, t1, t2);

			/* x3 = lambda^2 - x2 - x1. */
			fp_sqr(t1, t2);
			fp_sub(t0, t1, p->x);
			fp_sub(t0, t0, q->x);

			/* y3 = lambda * (x1 - x3) - y1. */
			fp_sub(t1, p->x, t0);
			fp_mul(t1, t2, t1);
			fp_sub(r->y, t1, p->y);

			fp_copy(r->x, t0);
			fp_copy(r->z, p->z);

			if (s != NULL) {
				fp_copy(s, t2);
			}

			r->norm = 1;
		}
		fp_free(t0);
		fp_free(t1);
		fp_free(t2);
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		fp_free(t0);
		fp_free(t1);
		fp_free(t2);
	}
}
Beispiel #23
0
void pp_dbl_k2_projc_lazyr(fp2_t l, ep_t r, ep_t p, ep_t q) {
	fp_t t0, t1, t2, t3, t4, t5;
	dv_t u0, u1;

	fp_null(t0);
	fp_null(t1);
	fp_null(t2);
	fp_null(t3);
	fp_null(t4);
	fp_null(t5);
	dv_null(u0);
	dv_null(u1);

	TRY {
		fp_new(t0);
		fp_new(t1);
		fp_new(t2);
		fp_new(t3);
		fp_new(t4);
		fp_new(t5);
		dv_new(u0);
		dv_new(u1);

		/* For these curves, we always can choose a = -3. */
		/* dbl-2001-b formulas: 3M + 5S + 8add + 1*4 + 2*8 + 1*3 */
		/* http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#doubling-dbl-2001-b */

		/* t0 = delta = z1^2. */
		fp_sqr(t0, p->z);

		/* t1 = gamma = y1^2. */
		fp_sqr(t1, p->y);

		/* t2 = beta = x1 * y1^2. */
		fp_mul(t2, p->x, t1);

		/* t3 = alpha = 3 * (x1 - z1^2) * (x1 + z1^2). */
		fp_sub(t3, p->x, t0);
		fp_add(t4, p->x, t0);
		fp_mul(t4, t3, t4);
		fp_dbl(t3, t4);
		fp_add(t3, t3, t4);

		/* t2 = 4 * beta. */
		fp_dbl(t2, t2);
		fp_dbl(t2, t2);

		/* z3 = (y1 + z1)^2 - gamma - delta. */
		fp_add(r->z, p->y, p->z);
		fp_sqr(r->z, r->z);
		fp_sub(r->z, r->z, t1);
		fp_sub(r->z, r->z, t0);

		/* l0 = 2 * gamma - alpha * (delta * xq + x1). */
		fp_dbl(t1, t1);
		fp_mul(t5, t0, q->x);
		fp_add(t5, t5, p->x);
		fp_mul(t5, t5, t3);
		fp_sub(l[0], t1, t5);

		/* x3 = alpha^2 - 8 * beta. */
		fp_dbl(t5, t2);
		fp_sqr(r->x, t3);
		fp_sub(r->x, r->x, t5);

		/* y3 = alpha * (4 * beta - x3) - 8 * gamma^2. */
		fp_sqrn_low(u0, t1);
		fp_addc_low(u0, u0, u0);
		fp_subm_low(r->y, t2, r->x);
		fp_muln_low(u1, r->y, t3);
		fp_subc_low(u1, u1, u0);
		fp_rdcn_low(r->y, u1);

		/* l1 = - z3 * delta * yq. */
		fp_mul(l[1], r->z, t0);
		fp_mul(l[1], l[1], q->y);

		r->norm = 0;
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		fp_free(t0);
		fp_free(t1);
		fp_free(t2);
		fp_free(t3);
		fp_free(t4);
		fp_free(t5);
		dv_free(u0);
		dv_free(u1);
	}
}
Beispiel #24
0
int fp_srt(fp_t c, const fp_t a) {
	bn_t e;
	fp_t t0;
	fp_t t1;
	int r = 0;

	bn_null(e);
	fp_null(t0);
	fp_null(t1);

	TRY {
		bn_new(e);
		fp_new(t0);
		fp_new(t1);

		/* Make e = p. */
		e->used = FP_DIGS;
		dv_copy(e->dp, fp_prime_get(), FP_DIGS);

		if (fp_prime_get_mod8() == 3 || fp_prime_get_mod8() == 7) {
			/* Easy case, compute a^((p + 1)/4). */
			bn_add_dig(e, e, 1);
			bn_rsh(e, e, 2);

			fp_exp(t0, a, e);
			fp_sqr(t1, t0);
			r = (fp_cmp(t1, a) == CMP_EQ);
			fp_copy(c, t0);
		} else {
			int f = 0, m = 0;

			/* First, check if there is a root. Compute t1 = a^((p - 1)/2). */
			bn_rsh(e, e, 1);
			fp_exp(t0, a, e);

			if (fp_cmp_dig(t0, 1) != CMP_EQ) {
				/* Nope, there is no square root. */
				r = 0;
			} else {
				r = 1;
				/* Find a quadratic non-residue modulo p, that is a number t2
				 * such that (t2 | p) = t2^((p - 1)/2)!= 1. */
				do {
					fp_rand(t1);
					fp_exp(t0, t1, e);
				} while (fp_cmp_dig(t0, 1) == CMP_EQ);

				/* Write p - 1 as (e * 2^f), odd e. */
				bn_lsh(e, e, 1);
				while (bn_is_even(e)) {
					bn_rsh(e, e, 1);
					f++;
				}

				/* Compute t2 = t2^e. */
				fp_exp(t1, t1, e);

				/* Compute t1 = a^e, c = a^((e + 1)/2) = a^(e/2 + 1), odd e. */
				bn_rsh(e, e, 1);
				fp_exp(t0, a, e);
				fp_mul(e->dp, t0, a);
				fp_sqr(t0, t0);
				fp_mul(t0, t0, a);
				fp_copy(c, e->dp);

				while (1) {
					if (fp_cmp_dig(t0, 1) == CMP_EQ) {
						break;
					}
					fp_copy(e->dp, t0);
					for (m = 0; (m < f) && (fp_cmp_dig(t0, 1) != CMP_EQ); m++) {
						fp_sqr(t0, t0);
					}
					fp_copy(t0, e->dp);
					for (int i = 0; i < f - m - 1; i++) {
						fp_sqr(t1, t1);
					}
					fp_mul(c, c, t1);
					fp_sqr(t1, t1);
					fp_mul(t0, t0, t1);
					f = m;
				}
			}
		}
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		bn_free(e);
		fp_free(t0);
		fp_free(t1);
	}
	return r;
}
Beispiel #25
0
/**
 * Adds two points represented in projective coordinates on an ordinary prime
 * elliptic curve.
 *
 * @param[out] r			- the result.
 * @param[in] p				- the first point to add.
 * @param[in] q				- the second point to add.
 */
static void ep_add_projc_imp(ep_t r, const ep_t p, const ep_t q) {
#if defined(EP_MIXED) && defined(STRIP)
	/* If code size is a problem, leave only the mixed version. */
	ep_add_projc_mix(r, p, q);
#else /* General addition. */

#if defined(EP_MIXED) || !defined(STRIP)
	/* Test if z2 = 1 only if mixed coordinates are turned on. */
	if (q->norm) {
		ep_add_projc_mix(r, p, q);
		return;
	}
#endif

	fp_t t0, t1, t2, t3, t4, t5, t6;

	fp_null(t0);
	fp_null(t1);
	fp_null(t2);
	fp_null(t3);
	fp_null(t4);
	fp_null(t5);
	fp_null(t6);

	TRY {
		fp_new(t0);
		fp_new(t1);
		fp_new(t2);
		fp_new(t3);
		fp_new(t4);
		fp_new(t5);
		fp_new(t6);

		/* add-2007-bl formulas: 11M + 5S + 9add + 4*2 */
		/* http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#addition-add-2007-bl */

		/* t0 = z1^2. */
		fp_sqr(t0, p->z);

		/* t1 = z2^2. */
		fp_sqr(t1, q->z);

		/* t2 = U1 = x1 * z2^2. */
		fp_mul(t2, p->x, t1);

		/* t3 = U2 = x2 * z1^2. */
		fp_mul(t3, q->x, t0);

		/* t6 = z1^2 + z2^2. */
		fp_add(t6, t0, t1);

		/* t0 = S2 = y2 * z1^3. */
		fp_mul(t0, t0, p->z);
		fp_mul(t0, t0, q->y);

		/* t1 = S1 = y1 * z2^3. */
		fp_mul(t1, t1, q->z);
		fp_mul(t1, t1, p->y);

		/* t3 = H = U2 - U1. */
		fp_sub(t3, t3, t2);

		/* t0 = R = 2 * (S2 - S1). */
		fp_sub(t0, t0, t1);
		fp_dbl(t0, t0);

		/* If E is zero. */
		if (fp_is_zero(t3)) {
			if (fp_is_zero(t0)) {
				/* If I is zero, p = q, should have doubled. */
				ep_dbl_projc(r, p);
			} else {
				/* If I is not zero, q = -p, r = infinity. */
				ep_set_infty(r);
			}
		} else {
			/* t4 = I = (2*H)^2. */
			fp_dbl(t4, t3);
			fp_sqr(t4, t4);

			/* t5 = J = H * I. */
			fp_mul(t5, t3, t4);

			/* t4 = V = U1 * I. */
			fp_mul(t4, t2, t4);

			/* x3 = R^2 - J - 2 * V. */
			fp_sqr(r->x, t0);
			fp_sub(r->x, r->x, t5);
			fp_dbl(t2, t4);
			fp_sub(r->x, r->x, t2);

			/* y3 = R * (V - x3) - 2 * S1 * J. */
			fp_sub(t4, t4, r->x);
			fp_mul(t4, t4, t0);
			fp_mul(t1, t1, t5);
			fp_dbl(t1, t1);
			fp_sub(r->y, t4, t1);

			/* z3 = ((z1 + z2)^2 - z1^2 - z2^2) * H. */
			fp_add(r->z, p->z, q->z);
			fp_sqr(r->z, r->z);
			fp_sub(r->z, r->z, t6);
			fp_mul(r->z, r->z, t3);
		}
		r->norm = 0;
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		fp_free(t0);
		fp_free(t1);
		fp_free(t2);
		fp_free(t3);
		fp_free(t4);
		fp_free(t5);
		fp_free(t6);
	}
#endif
}
Beispiel #26
0
void pp_dbl_lit_k12(fp12_t l, ep_t r, ep_t p, ep2_t q) {
	fp_t t0, t1, t2, t3, t4, t5, t6;
	int one = 1, zero = 0;

	fp_null(t0);
	fp_null(t1);
	fp_null(t2);
	fp_null(t3);
	fp_null(t4);
	fp_null(t5);
	fp_null(t6);

	TRY {
		fp_new(t0);
		fp_new(t1);
		fp_new(t2);
		fp_new(t3);
		fp_new(t4);
		fp_new(t5);
		fp_new(t6);

		fp_sqr(t0, p->x);
		fp_sqr(t1, p->y);
		fp_sqr(t2, p->z);

		fp_mul(t4, ep_curve_get_b(), t2);

		fp_dbl(t3, t4);
		fp_add(t3, t3, t4);

		fp_add(t4, p->x, p->y);
		fp_sqr(t4, t4);
		fp_sub(t4, t4, t0);
		fp_sub(t4, t4, t1);
		fp_add(t5, p->y, p->z);
		fp_sqr(t5, t5);
		fp_sub(t5, t5, t1);
		fp_sub(t5, t5, t2);
		fp_dbl(t6, t3);
		fp_add(t6, t6, t3);
		fp_sub(r->x, t1, t6);
		fp_mul(r->x, r->x, t4);
		fp_add(r->y, t1, t6);
		fp_sqr(r->y, r->y);
		fp_sqr(t4, t3);
		fp_dbl(t6, t4);
		fp_add(t6, t6, t4);
		fp_dbl(t6, t6);
		fp_dbl(t6, t6);
		fp_sub(r->y, r->y, t6);
		fp_mul(r->z, t1, t5);
		fp_dbl(r->z, r->z);
		fp_dbl(r->z, r->z);
		r->norm = 0;

		if (ep2_curve_is_twist() == EP_MTYPE) {
			one ^= 1;
			zero ^= 1;
		}

		fp2_dbl(l[zero][one], q->x);
		fp2_add(l[zero][one], l[zero][one], q->x);
		fp_mul(l[zero][one][0], l[zero][one][0], t0);
		fp_mul(l[zero][one][1], l[zero][one][1], t0);

		fp_sub(l[zero][zero][0], t3, t1);
		fp_zero(l[zero][zero][1]);

		fp_mul(l[one][one][0], q->y[0], t5);
		fp_mul(l[one][one][1], q->y[1], t5);
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		fp_free(t0);
		fp_free(t1);
		fp_free(t2);
		fp_free(t3);
		fp_free(t4);
		fp_free(t5);
		fp_free(t6);
	}
}
Beispiel #27
0
/**
 * Doubles a point represented in projective coordinates on an ordinary prime
 * elliptic curve.
 *
 * @param r					- the result.
 * @param p					- the point to double.
 */
static void ep_dbl_projc_imp(ep_t r, const ep_t p) {
	fp_t t0, t1, t2, t3, t4, t5;

	fp_null(t1);
	fp_null(t2);
	fp_null(t3);
	fp_null(t4);
	fp_null(t5);

	TRY {

		fp_new(t0);
		fp_new(t1);
		fp_new(t2);
		fp_new(t3);
		fp_new(t4);
		fp_new(t5);

		if (!p->norm && ep_curve_opt_a() == OPT_MINUS3) {
			/* dbl-2001-b formulas: 3M + 5S + 8add + 1*4 + 2*8 + 1*3 */
			/* http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#doubling-dbl-2001-b */

			/* t0 = delta = z1^2. */
			fp_sqr(t0, p->z);

			/* t1 = gamma = y1^2. */
			fp_sqr(t1, p->y);

			/* t2 = beta = x1 * y1^2. */
			fp_mul(t2, p->x, t1);

			/* t3 = alpha = 3 * (x1 - z1^2) * (x1 + z1^2). */
			fp_sub(t3, p->x, t0);
			fp_add(t4, p->x, t0);
			fp_mul(t4, t3, t4);
			fp_dbl(t3, t4);
			fp_add(t3, t3, t4);

			/* x3 = alpha^2 - 8 * beta. */
			fp_dbl(t2, t2);
			fp_dbl(t2, t2);
			fp_dbl(t5, t2);
			fp_sqr(r->x, t3);
			fp_sub(r->x, r->x, t5);

			/* z3 = (y1 + z1)^2 - gamma - delta. */
			fp_add(r->z, p->y, p->z);
			fp_sqr(r->z, r->z);
			fp_sub(r->z, r->z, t1);
			fp_sub(r->z, r->z, t0);

			/* y3 = alpha * (4 * beta - x3) - 8 * gamma^2. */
			fp_dbl(t1, t1);
			fp_sqr(t1, t1);
			fp_dbl(t1, t1);
			fp_sub(r->y, t2, r->x);
			fp_mul(r->y, r->y, t3);
			fp_sub(r->y, r->y, t1);
		} else if (ep_curve_opt_a() == OPT_ZERO) {
			/* dbl-2009-l formulas: 2M + 5S + 6add + 1*8 + 3*2 + 1*3. */
			/* http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#doubling-dbl-2009-l */

			/* A = X1^2 */
			fp_sqr(t0, p->x);

			/* B = Y1^2 */
			fp_sqr(t1, p->y);

			/* C = B^2 */
			fp_sqr(t2, t1);

			/* D = 2*((X1+B)^2-A-C) */
			fp_add(t1, t1, p->x);
			fp_sqr(t1, t1);
			fp_sub(t1, t1, t0);
			fp_sub(t1, t1, t2);
			fp_dbl(t1, t1);

			/* E = 3*A */
			fp_dbl(t3, t0);
			fp_add(t0, t3, t0);

			/* F = E^2 */
			fp_sqr(t3, t0);

			/* Z3 = 2*Y1*Z1 */
			fp_mul(r->z, p->y, p->z);
			fp_dbl(r->z, r->z);

			/* X3 = F-2*D */
			fp_sub(r->x, t3, t1);
			fp_sub(r->x, r->x, t1);

			/* Y3 = E*(D-X3)-8*C */
			fp_sub(r->y, t1, r->x);
			fp_mul(r->y, r->y, t0);
			fp_dbl(t2, t2);
			fp_dbl(t2, t2);
			fp_dbl(t2, t2);
			fp_sub(r->y, r->y, t2);
		} else {
			/* dbl-2007-bl formulas: 1M + 8S + 1*a + 10add + 1*8 + 2*2 + 1*3 */
			/* http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html#doubling-dbl-2007-bl */

			/* t0 = x1^2, t1 = y1^2, t2 = y1^4. */
			fp_sqr(t0, p->x);
			fp_sqr(t1, p->y);
			fp_sqr(t2, t1);

			if (!p->norm) {
				/* t3 = z1^2. */
				fp_sqr(t3, p->z);

				if (ep_curve_get_a() == OPT_ZERO) {
					/* z3 = 2 * y1 * z1. */
					fp_mul(r->z, p->y, p->z);
					fp_dbl(r->z, r->z);
				} else {
					/* z3 = (y1 + z1)^2 - y1^2 - z1^2. */
					fp_add(r->z, p->y, p->z);
					fp_sqr(r->z, r->z);
					fp_sub(r->z, r->z, t1);
					fp_sub(r->z, r->z, t3);
				}
			} else {
				/* z3 = 2 * y1. */
				fp_dbl(r->z, p->y);
			}

			/* t4 = S = 2*((x1 + y1^2)^2 - x1^2 - y1^4). */
			fp_add(t4, p->x, t1);
			fp_sqr(t4, t4);
			fp_sub(t4, t4, t0);
			fp_sub(t4, t4, t2);
			fp_dbl(t4, t4);

			/* t5 = M = 3 * x1^2 + a * z1^4. */
			fp_dbl(t5, t0);
			fp_add(t5, t5, t0);
			if (!p->norm) {
				fp_sqr(t3, t3);
				switch (ep_curve_opt_a()) {
					case OPT_ZERO:
						break;
					case OPT_ONE:
						fp_add(t5, t5, ep_curve_get_a());
						break;
					case OPT_DIGIT:
						fp_mul_dig(t1, t3, ep_curve_get_a()[0]);
						fp_add(t5, t5, t1);
						break;
					default:
						fp_mul(t1, ep_curve_get_a(), t3);
						fp_add(t5, t5, t1);
						break;
				}
			} else {
				switch (ep_curve_opt_a()) {
					case OPT_ZERO:
						break;
					case OPT_ONE:
						fp_add_dig(t5, t5, (dig_t)1);
						break;
					case OPT_DIGIT:
						fp_add_dig(t5, t5, ep_curve_get_a()[0]);
						break;
					default:
						fp_add(t5, t5, ep_curve_get_a());
						break;
				}
			}

			/* x3 = T = M^2 - 2 * S. */
			fp_sqr(r->x, t5);
			fp_dbl(t1, t4);
			fp_sub(r->x, r->x, t1);

			/* y3 = M * (S - T) - 8 * y1^4. */
			fp_dbl(t2, t2);
			fp_dbl(t2, t2);
			fp_dbl(t2, t2);
			fp_sub(t4, t4, r->x);
			fp_mul(t5, t5, t4);
			fp_sub(r->y, t5, t2);
		}

		r->norm = 0;
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		fp_free(t0);
		fp_free(t1);
		fp_free(t2);
		fp_free(t3);
		fp_free(t4);
		fp_free(t5);
	}
}
Beispiel #28
0
/**
 * Adds a point represented in affine coordinates to a point represented in
 * projective coordinates.
 *
 * @param[out] r			- the result.
 * @param[in] p				- the projective point.
 * @param[in] q				- the affine point.
 */
static void ep_add_projc_mix(ep_t r, const ep_t p, const ep_t q) {
	fp_t t0, t1, t2, t3, t4, t5, t6;

	fp_null(t0);
	fp_null(t1);
	fp_null(t2);
	fp_null(t3);
	fp_null(t4);
	fp_null(t5);
	fp_null(t6);

	TRY {
		fp_new(t0);
		fp_new(t1);
		fp_new(t2);
		fp_new(t3);
		fp_new(t4);
		fp_new(t5);
		fp_new(t6);

		/* madd-2007-bl formulas: 7M + 4S + 9add + 1*4 + 3*2. */
		/* http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#addition-madd-2007-bl */

		if (!p->norm) {
			/* t0 = z1^2. */
			fp_sqr(t0, p->z);

			/* t3 = U2 = x2 * z1^2. */
			fp_mul(t3, q->x, t0);

			/* t1 = S2 = y2 * z1^3. */
			fp_mul(t1, t0, p->z);
			fp_mul(t1, t1, q->y);

			/* t3 = H = U2 - x1. */
			fp_sub(t3, t3, p->x);

			/* t1 = R = 2 * (S2 - y1). */
			fp_sub(t1, t1, p->y);
			fp_dbl(t1, t1);
		} else {
			/* H = x2 - x1. */
			fp_sub(t3, q->x, p->x);

			/* t1 = R = 2 * (y2 - y1). */
			fp_sub(t1, q->y, p->y);
			fp_dbl(t1, t1);
		}

		/* t2 = HH = H^2. */
		fp_sqr(t2, t3);

		/* If E is zero. */
		if (fp_is_zero(t3)) {
			if (fp_is_zero(t1)) {
				/* If I is zero, p = q, should have doubled. */
				ep_dbl_projc(r, p);
			} else {
				/* If I is not zero, q = -p, r = infinity. */
				ep_set_infty(r);
			}
		} else {
			/* t4 = I = 4*HH. */
			fp_dbl(t4, t2);
			fp_dbl(t4, t4);

			/* t5 = J = H * I. */
			fp_mul(t5, t3, t4);

			/* t4 = V = x1 * I. */
			fp_mul(t4, p->x, t4);

			/* x3 = R^2 - J - 2 * V. */
			fp_sqr(r->x, t1);
			fp_sub(r->x, r->x, t5);
			fp_dbl(t6, t4);
			fp_sub(r->x, r->x, t6);

			/* y3 = R * (V - x3) - 2 * Y1 * J. */
			fp_sub(t4, t4, r->x);
			fp_mul(t4, t4, t1);
			fp_mul(t1, p->y, t5);
			fp_dbl(t1, t1);
			fp_sub(r->y, t4, t1);

			if (!p->norm) {
				/* z3 = (z1 + H)^2 - z1^2 - HH. */
				fp_add(r->z, p->z, t3);
				fp_sqr(r->z, r->z);
				fp_sub(r->z, r->z, t0);
				fp_sub(r->z, r->z, t2);
			} else {
				/* z3 = 2 * H. */
				fp_dbl(r->z, t3);
			}
		}
		r->norm = 0;
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		fp_free(t0);
		fp_free(t1);
		fp_free(t2);
		fp_free(t3);
		fp_free(t4);
		fp_free(t5);
		fp_free(t6);
	}
}
Beispiel #29
0
void ep_param_set(int param) {
	int plain = 0, endom = 0, super = 0;
	char str[2 * FP_BYTES + 2];
	fp_t a, b, beta;
	ep_t g;
	bn_t r, h, lamb;

	fp_null(a);
	fp_null(b);
	fp_null(beta);
	bn_null(lamb);
	ep_null(g);
	bn_null(r);
	bn_null(h);

	TRY {
		fp_new(a);
		fp_new(b);
		fp_new(beta);
		bn_new(lamb);
		ep_new(g);
		bn_new(r);
		bn_new(h);

		core_get()->ep_id = 0;

		switch (param) {
#if defined(EP_ENDOM) && FP_PRIME == 158
			case BN_P158:
				ASSIGNK(BN_P158, BN_158);
				endom = 1;
				break;
#endif
#if defined(EP_PLAIN) && FP_PRIME == 160
			case SECG_P160:
				ASSIGN(SECG_P160, SECG_160);
				plain = 1;
				break;
#endif
#if defined(EP_ENDOM) && FP_PRIME == 160
			case SECG_K160:
				ASSIGNK(SECG_K160, SECG_160D);
				endom = 1;
				break;
#endif
#if defined(EP_PLAIN) && FP_PRIME == 192
			case NIST_P192:
				ASSIGN(NIST_P192, NIST_192);
				plain = 1;
				break;
#endif
#if defined(EP_ENDOM) && FP_PRIME == 192
			case SECG_K192:
				ASSIGNK(SECG_K192, SECG_192);
				endom = 1;
				break;
#endif
#if defined(EP_PLAIN) && FP_PRIME == 221
			case CURVE_22103:
				ASSIGN(CURVE_22103, PRIME_22103);
				plain = 1;
				break;
#endif
#if defined(EP_PLAIN) && FP_PRIME == 224
			case NIST_P224:
				ASSIGN(NIST_P224, NIST_224);
				plain = 1;
				break;
#endif
#if defined(EP_ENDOM) && FP_PRIME == 224
			case SECG_K224:
				ASSIGNK(SECG_K224, SECG_224);
				endom = 1;
				break;
#endif
#if defined(EP_PLAIN) && FP_PRIME == 226
			case CURVE_4417:
				ASSIGN(CURVE_4417, PRIME_22605);
				plain = 1;
				break;
#endif
#if defined(EP_ENDOM) && FP_PRIME == 254
			case BN_P254:
				ASSIGNK(BN_P254, BN_254);
				endom = 1;
				break;
#endif
#if defined(EP_PLAIN) && FP_PRIME == 251
			case CURVE_1174:
				ASSIGN(CURVE_1174, PRIME_25109);
				plain = 1;
				break;
#endif
#if defined(EP_PLAIN) && FP_PRIME == 255
			case CURVE_25519:
				ASSIGN(CURVE_25519, PRIME_25519);
				plain = 1;
				break;
#endif
#if defined(EP_PLAIN) && FP_PRIME == 256
			case NIST_P256:
				ASSIGN(NIST_P256, NIST_256);
				plain = 1;
				break;
#endif
#if defined(EP_ENDOM) && FP_PRIME == 256
			case SECG_K256:
				ASSIGNK(SECG_K256, SECG_256);
				endom = 1;
				break;
			case BN_P256:
				ASSIGNK(BN_P256, BN_256);
				endom = 1;
				break;
#endif
#if defined(EP_PLAIN) & FP_PRIME == 382
			case CURVE_67254:
				ASSIGN(CURVE_67254, PRIME_382105);
				plain = 1;
				break;
#endif
#if defined(EP_PLAIN) && FP_PRIME == 383
			case CURVE_383187:
				ASSIGN(CURVE_383187, PRIME_383187);
				plain = 1;
				break;
#endif
#if defined(EP_PLAIN) && FP_PRIME == 384
			case NIST_P384:
				ASSIGN(NIST_P384, NIST_384);
				plain = 1;
				break;
#endif
#if defined(EP_PLAIN) && FP_PRIME == 477
			case B24_P477:
				ASSIGN(B24_P477, B24_477);
				plain = 1;
				break;
#endif
#if defined(EP_ENDOM) && FP_PRIME == 508
			case KSS_P508:
				ASSIGNK(KSS_P508, KSS_508);
				endom = 1;
				break;
#endif
#if defined(EP_PLAIN) && FP_PRIME == 511
			case CURVE_511187:
				ASSIGN(CURVE_511187, PRIME_511187);
				plain = 1;
				break;
#endif
#if defined(EP_PLAIN) && FP_PRIME == 521
			case NIST_P521:
				ASSIGN(NIST_P521, NIST_521);
				plain = 1;
				break;
#endif
#if defined(EP_ENDOM) && FP_PRIME == 638
			case BN_P638:
				ASSIGNK(BN_P638, BN_638);
				endom = 1;
				break;
			case B12_P638:
				ASSIGNK(B12_P638, B12_638);
				endom = 1;
				break;
#endif
#if defined(EP_SUPER) && FP_PRIME == 1536
			case SS_P1536:
				ASSIGN(SS_P1536, SS_1536);
				super = 1;
				break;
#endif
			default:
				(void)str;
				THROW(ERR_NO_VALID);
				break;
		}

		/* Do not generate warnings. */
		(void)endom;
		(void)plain;
		(void)beta;

		fp_zero(g->z);
		fp_set_dig(g->z, 1);
		g->norm = 1;

#if defined(EP_PLAIN)
		if (plain) {
			ep_curve_set_plain(a, b, g, r, h);
			core_get()->ep_id = param;
		}
#endif

#if defined(EP_ENDOM)
		if (endom) {
			ep_curve_set_endom(b, g, r, h, beta, lamb);
			core_get()->ep_id = param;
		}
#endif

#if defined(EP_SUPER)
		if (super) {
			ep_curve_set_super(a, b, g, r, h);
			core_get()->ep_id = param;
		}
#endif
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		fp_free(a);
		fp_free(b);
		fp_free(beta);
		bn_free(lamb);
		ep_free(g);
		bn_free(r);
		bn_free(h);
	}
}
Beispiel #30
0
/**
 * Doubles a point represented in affine coordinates on an ordinary prime
 * elliptic curve.
 *
 * @param[out] r			- the result.
 * @param[out] s			- the slope.
 * @param[in] p				- the point to double.
 */
static void ep_dbl_basic_imp(ep_t r, fp_t s, const ep_t p) {
	fp_t t0, t1, t2;

	fp_null(t0);
	fp_null(t1);
	fp_null(t2);

	TRY {
		fp_new(t0);
		fp_new(t1);
		fp_new(t2);

		/* t0 = 1/2 * y1. */
		fp_dbl(t0, p->y);
		fp_inv(t0, t0);

		/* t1 = 3 * x1^2 + a. */
		fp_sqr(t1, p->x);
		fp_copy(t2, t1);
		fp_dbl(t1, t1);
		fp_add(t1, t1, t2);

		switch (ep_curve_opt_a()) {
			case OPT_ZERO:
				break;
			case OPT_ONE:
				fp_add_dig(t1, t1, (dig_t)1);
				break;
#if FP_RDC != MONTY
			case OPT_DIGIT:
				fp_add_dig(t1, t1, ep_curve_get_a()[0]);
				break;
#endif
			default:
				fp_add(t1, t1, ep_curve_get_a());
				break;
		}

		/* t1 = (3 * x1^2 + a)/(2 * y1). */
		fp_mul(t1, t1, t0);

		if (s != NULL) {
			fp_copy(s, t1);
		}

		/* t2 = t1^2. */
		fp_sqr(t2, t1);

		/* x3 = t1^2 - 2 * x1. */
		fp_dbl(t0, p->x);
		fp_sub(t0, t2, t0);

		/* y3 = t1 * (x1 - x3) - y1. */
		fp_sub(t2, p->x, t0);
		fp_mul(t1, t1, t2);
		fp_sub(r->y, t1, p->y);

		fp_copy(r->x, t0);
		fp_copy(r->z, p->z);

		r->norm = 1;
	}
	CATCH_ANY {
		THROW(ERR_CAUGHT);
	}
	FINALLY {
		fp_free(t0);
		fp_free(t1);
		fp_free(t2);
	}
}