#include <libecc/curves/aff_pt.h>
#define AFF_PT_EDWARDS_MAGIC ((word_t)(0x8390a9bc43d9ffabULL))
int aff_pt_edwards_check_initialized(aff_pt_edwards_src_t in)
{
int ret;
MUST_HAVE(((in != NULL) && (in->magic == AFF_PT_EDWARDS_MAGIC)), ret, err);
ret = ec_edwards_crv_check_initialized(in->crv);
err:
return ret;
}
int aff_pt_edwards_init(aff_pt_edwards_t in, ec_edwards_crv_src_t curve)
{
int ret;
MUST_HAVE((in != NULL), ret, err);
ret = ec_edwards_crv_check_initialized(curve); EG(ret, err);
ret = fp_init(&(in->x), curve->a.ctx); EG(ret, err);
ret = fp_init(&(in->y), curve->a.ctx); EG(ret, err);
in->crv = curve;
in->magic = AFF_PT_EDWARDS_MAGIC;
err:
return ret;
}
int aff_pt_edwards_init_from_coords(aff_pt_edwards_t in,
ec_edwards_crv_src_t curve,
fp_src_t xcoord, fp_src_t ycoord)
{
int ret;
ret = aff_pt_edwards_init(in, curve); EG(ret, err);
ret = fp_copy(&(in->x), xcoord); EG(ret, err);
ret = fp_copy(&(in->y), ycoord);
err:
return ret;
}
void aff_pt_edwards_uninit(aff_pt_edwards_t in)
{
if ((in != NULL) && (in->magic == AFF_PT_EDWARDS_MAGIC) && (in->crv != NULL)) {
fp_uninit(&(in->x));
fp_uninit(&(in->y));
in->crv = NULL;
in->magic = WORD(0);
}
return;
}
int is_on_edwards_curve(fp_src_t x, fp_src_t y,
ec_edwards_crv_src_t curve,
int *on_curve)
{
fp x2, y2, tmp1, tmp2;
int ret, cmp;
x2.magic = y2.magic = tmp1.magic = tmp2.magic = WORD(0);
MUST_HAVE((on_curve != NULL), ret, err);
ret = ec_edwards_crv_check_initialized(curve); EG(ret, err);
ret = fp_check_initialized(x); EG(ret, err);
ret = fp_check_initialized(y); EG(ret, err);
MUST_HAVE((x->ctx == y->ctx), ret, err);
MUST_HAVE((x->ctx == curve->a.ctx), ret, err);
ret = fp_init(&x2, x->ctx); EG(ret, err);
ret = fp_sqr(&x2, x); EG(ret, err);
ret = fp_init(&y2, x->ctx); EG(ret, err);
ret = fp_sqr(&y2, y); EG(ret, err);
ret = fp_init(&tmp1, x->ctx); EG(ret, err);
ret = fp_init(&tmp2, x->ctx); EG(ret, err);
ret = fp_mul(&tmp1, &x2, &y2); EG(ret, err);
ret = fp_mul(&tmp1, &tmp1, &(curve->d)); EG(ret, err);
ret = fp_inc(&tmp1, &tmp1); EG(ret, err);
ret = fp_mul(&tmp2, &x2, &(curve->a)); EG(ret, err);
ret = fp_add(&tmp2, &tmp2, &y2); EG(ret, err);
ret = fp_cmp(&tmp1, &tmp2, &cmp);
if (!ret) {
(*on_curve) = (!cmp);
}
err:
fp_uninit(&x2);
fp_uninit(&y2);
fp_uninit(&tmp1);
fp_uninit(&tmp2);
return ret;
}
int aff_pt_edwards_is_on_curve(aff_pt_edwards_src_t pt, int *on_curve)
{
int ret;
ret = aff_pt_edwards_check_initialized(pt); EG(ret, err);
ret = is_on_edwards_curve(&(pt->x), &(pt->y), pt->crv, on_curve);
err:
return ret;
}
int ec_edwards_aff_copy(aff_pt_edwards_t out, aff_pt_edwards_src_t in)
{
int ret;
ret = aff_pt_edwards_check_initialized(in); EG(ret, err);
ret = aff_pt_edwards_init(out, in->crv); EG(ret, err);
ret = fp_copy(&(out->x), &(in->x)); EG(ret, err);
ret = fp_copy(&(out->y), &(in->y));
err:
return ret;
}
int ec_edwards_aff_cmp(aff_pt_edwards_src_t in1, aff_pt_edwards_src_t in2,
int *cmp)
{
int ret, cmp1, cmp2;
MUST_HAVE((cmp != NULL), ret, err);
ret = aff_pt_edwards_check_initialized(in1); EG(ret, err);
ret = aff_pt_edwards_check_initialized(in2); EG(ret, err);
MUST_HAVE((in1->crv == in2->crv), ret, err);
ret = fp_cmp(&(in1->x), &(in2->x), &cmp1); EG(ret, err);
ret = fp_cmp(&(in1->y), &(in2->y), &cmp2);
if (!ret) {
(*cmp) = (cmp1 | cmp2);
}
err:
return ret;
}
int aff_pt_edwards_import_from_buf(aff_pt_edwards_t pt,
const u8 *pt_buf,
u16 pt_buf_len, ec_edwards_crv_src_t crv)
{
fp_ctx_src_t ctx;
u16 coord_len;
int ret, on_curve;
ret = ec_edwards_crv_check_initialized(crv); EG(ret, err);
MUST_HAVE(((pt_buf != NULL) && (pt != NULL)), ret, err);
ctx = crv->a.ctx;
coord_len = (u16)BYTECEIL(ctx->p_bitlen);
MUST_HAVE((pt_buf_len == (2 * coord_len)), ret, err);
ret = fp_init_from_buf(&(pt->x), ctx, pt_buf, coord_len); EG(ret, err);
ret = fp_init_from_buf(&(pt->y), ctx, pt_buf + coord_len, coord_len); EG(ret, err);
pt->crv = crv;
pt->magic = AFF_PT_EDWARDS_MAGIC;
ret = aff_pt_edwards_is_on_curve(pt, &on_curve); EG(ret, err);
if (!on_curve) {
aff_pt_edwards_uninit(pt);
ret = -1;
}
err:
return ret;
}
int aff_pt_edwards_export_to_buf(aff_pt_edwards_src_t pt,
u8 *pt_buf, u32 pt_buf_len)
{
fp_ctx_src_t ctx;
u16 coord_len;
int ret, on_curve;
ret = aff_pt_edwards_check_initialized(pt); EG(ret, err);
MUST_HAVE((pt_buf != NULL), ret, err);
ret = aff_pt_edwards_is_on_curve(pt, &on_curve); EG(ret, err);
MUST_HAVE(on_curve, ret, err);
ctx = pt->crv->a.ctx;
coord_len = (u16)BYTECEIL(ctx->p_bitlen);
MUST_HAVE((pt_buf_len == (2 * coord_len)), ret, err);
ret = fp_export_to_buf(pt_buf, coord_len, &(pt->x)); EG(ret, err);
ret = fp_export_to_buf(pt_buf + coord_len, coord_len, &(pt->y));
err:
return ret;
}
int curve_edwards_to_montgomery(ec_edwards_crv_src_t edwards_crv,
ec_montgomery_crv_t montgomery_crv,
fp_src_t alpha_edwards)
{
fp tmp1, tmp2, A, B;
int ret;
tmp1.magic = tmp2.magic = A.magic = B.magic = WORD(0);
ret = ec_edwards_crv_check_initialized(edwards_crv); EG(ret, err);
ret = fp_check_initialized(alpha_edwards); EG(ret, err);
MUST_HAVE((edwards_crv->a.ctx == alpha_edwards->ctx), ret, err);
ret = fp_init(&tmp1, edwards_crv->a.ctx); EG(ret, err);
ret = fp_init(&tmp2, edwards_crv->a.ctx); EG(ret, err);
ret = fp_init(&A, edwards_crv->a.ctx); EG(ret, err);
ret = fp_init(&B, edwards_crv->a.ctx); EG(ret, err);
ret = fp_sqr(&tmp1, alpha_edwards); EG(ret, err);
ret = fp_sub(&tmp2, &(edwards_crv->a), &(edwards_crv->d)); EG(ret, err);
ret = fp_mul(&tmp2, &tmp2, &tmp1); EG(ret, err);
ret = fp_inv(&tmp2, &tmp2); EG(ret, err);
ret = fp_set_word_value(&B, WORD(2)); EG(ret, err);
ret = fp_mul(&tmp2, &tmp2, &B); EG(ret, err);
ret = fp_add(&A, &(edwards_crv->a), &(edwards_crv->d)); EG(ret, err);
ret = fp_mul(&A, &A, &tmp1); EG(ret, err);
ret = fp_mul(&A, &A, &tmp2); EG(ret, err);
ret = fp_mul(&B, &B, &tmp2); EG(ret, err);
ret = ec_montgomery_crv_init(montgomery_crv, &A, &B, &(edwards_crv->order));
err:
fp_uninit(&tmp1);
fp_uninit(&tmp2);
fp_uninit(&A);
fp_uninit(&B);
return ret;
}
int curve_edwards_montgomery_check(ec_edwards_crv_src_t e_crv,
ec_montgomery_crv_src_t m_crv,
fp_src_t alpha_edwards)
{
int ret, cmp;
ec_montgomery_crv check;
check.magic = WORD(0);
ret = ec_montgomery_crv_check_initialized(m_crv); EG(ret, err);
ret = curve_edwards_to_montgomery(e_crv, &check, alpha_edwards); EG(ret, err);
MUST_HAVE((!fp_cmp(&(check.A), &(m_crv->A), &cmp)) && (!cmp), ret, err);
MUST_HAVE((!fp_cmp(&(check.B), &(m_crv->B), &cmp)) && (!cmp), ret, err);
MUST_HAVE((!nn_cmp(&(check.order), &(m_crv->order), &cmp)) && (!cmp), ret, err);
err:
ec_montgomery_crv_uninit(&check);
return ret;
}
int curve_montgomery_to_edwards(ec_montgomery_crv_src_t m_crv,
ec_edwards_crv_t e_crv,
fp_src_t alpha_edwards)
{
int ret, cmp;
fp tmp, tmp2, a, d;
tmp.magic = tmp2.magic = a.magic = d.magic = WORD(0);
ret = ec_montgomery_crv_check_initialized(m_crv); EG(ret, err);
ret = fp_check_initialized(alpha_edwards); EG(ret, err);
MUST_HAVE((m_crv->A.ctx == alpha_edwards->ctx), ret, err);
ret = fp_init(&tmp, m_crv->A.ctx); EG(ret, err);
ret = fp_init(&tmp2, m_crv->A.ctx); EG(ret, err);
ret = fp_init(&a, m_crv->A.ctx); EG(ret, err);
ret = fp_init(&d, m_crv->A.ctx); EG(ret, err);
ret = fp_set_word_value(&tmp, WORD(2)); EG(ret, err);
ret = fp_mul(&tmp2, &(m_crv->B), alpha_edwards); EG(ret, err);
ret = fp_mul(&tmp2, &tmp2, alpha_edwards); EG(ret, err);
ret = fp_inv(&tmp2, &tmp2); EG(ret, err);
ret = fp_add(&a, &(m_crv->A), &tmp); EG(ret, err);
ret = fp_mul(&a, &a, &tmp2); EG(ret, err);
ret = fp_sub(&d, &(m_crv->A), &tmp); EG(ret, err);
ret = fp_mul(&d, &d, &tmp2); EG(ret, err);
ret = fp_one(&tmp); EG(ret, err);
ret = fp_cmp(&d, &tmp, &cmp); EG(ret, err);
if (cmp == 0) {
ret = ec_edwards_crv_init(e_crv, &d, &a, &(m_crv->order));
} else {
ret = ec_edwards_crv_init(e_crv, &a, &d, &(m_crv->order));
}
err:
fp_uninit(&tmp);
fp_uninit(&tmp2);
fp_uninit(&a);
fp_uninit(&d);
return ret;
}
int curve_edwards_to_shortw(ec_edwards_crv_src_t edwards_crv,
ec_shortw_crv_t shortw_crv,
fp_src_t alpha_edwards)
{
int ret;
ec_montgomery_crv montgomery_crv;
montgomery_crv.magic = WORD(0);
ret = curve_edwards_to_montgomery(edwards_crv, &montgomery_crv, alpha_edwards); EG(ret, err);
ret = curve_montgomery_to_shortw(&montgomery_crv, shortw_crv);
err:
ec_montgomery_crv_uninit(&montgomery_crv);
return ret;
}
int curve_edwards_shortw_check(ec_edwards_crv_src_t edwards_crv,
ec_shortw_crv_src_t shortw_crv,
fp_src_t alpha_edwards)
{
int ret;
ec_montgomery_crv montgomery_crv;
montgomery_crv.magic = WORD(0);
ret = curve_edwards_to_montgomery(edwards_crv, &montgomery_crv, alpha_edwards); EG(ret, err);
ret = curve_montgomery_shortw_check(&montgomery_crv, shortw_crv);
err:
ec_montgomery_crv_uninit(&montgomery_crv);
return ret;
}
int curve_shortw_to_edwards(ec_shortw_crv_src_t shortw_crv,
ec_edwards_crv_t edwards_crv,
fp_src_t alpha_montgomery,
fp_src_t gamma_montgomery,
fp_src_t alpha_edwards)
{
int ret;
ec_montgomery_crv montgomery_crv;
montgomery_crv.magic = WORD(0);
ret = curve_shortw_to_montgomery(shortw_crv, &montgomery_crv, alpha_montgomery, gamma_montgomery); EG(ret, err);
ret = curve_montgomery_to_edwards(&montgomery_crv, edwards_crv, alpha_edwards);
err:
ec_montgomery_crv_uninit(&montgomery_crv);
return ret;
}
int aff_pt_edwards_to_montgomery(aff_pt_edwards_src_t in_edwards,
ec_montgomery_crv_src_t montgomery_crv,
aff_pt_montgomery_t out_montgomery,
fp_src_t alpha_edwards)
{
int ret, iszero, on_curve, cmp;
fp tmp, tmp2, x, y;
fp tab_x[2];
fp_src_t tab_x_t[2] = { &tab_x[0], &tab_x[1] };
fp tab_y[2];
fp_src_t tab_y_t[2] = { &tab_y[0], &tab_y[1] };
u8 idx = 0;
tmp.magic = tmp2.magic = x.magic = y.magic = WORD(0);
tab_x[0].magic = tab_x[1].magic = WORD(0);
tab_y[0].magic = tab_y[1].magic = WORD(0);
ret = ec_montgomery_crv_check_initialized(montgomery_crv); EG(ret, err);
ret = aff_pt_edwards_is_on_curve(in_edwards, &on_curve); EG(ret, err);
MUST_HAVE(on_curve, ret, err);
ret = curve_edwards_montgomery_check(in_edwards->crv, montgomery_crv, alpha_edwards); EG(ret, err);
ret = fp_init(&tmp, in_edwards->crv->a.ctx); EG(ret, err);
ret = fp_init(&tmp2, in_edwards->crv->a.ctx); EG(ret, err);
ret = fp_init(&x, in_edwards->crv->a.ctx); EG(ret, err);
ret = fp_init(&y, in_edwards->crv->a.ctx); EG(ret, err);
ret = fp_init(&tab_x[0], in_edwards->crv->a.ctx); EG(ret, err);
ret = fp_init(&tab_x[1], in_edwards->crv->a.ctx); EG(ret, err);
ret = fp_init(&tab_y[0], in_edwards->crv->a.ctx); EG(ret, err);
ret = fp_init(&tab_y[1], in_edwards->crv->a.ctx); EG(ret, err);
ret = fp_one(&tmp); EG(ret, err);
ret = fp_iszero(&(in_edwards->x), &iszero); EG(ret, err);
ret = fp_cmp(&(in_edwards->y), &tmp, &cmp); EG(ret, err);
MUST_HAVE(!(iszero && (cmp == 0)), ret, err);
ret = fp_zero(&tmp2); EG(ret, err);
ret = fp_sub(&tmp2, &tmp2, &tmp); EG(ret, err);
ret = fp_one(&tab_x[0]); EG(ret, err);
ret = fp_copy(&tab_x[1], &(in_edwards->x)); EG(ret, err);
ret = fp_copy(&tab_y[0], &tmp2); EG(ret, err);
ret = fp_copy(&tab_y[1], &(in_edwards->y)); EG(ret, err);
ret = fp_iszero(&(in_edwards->x), &iszero); EG(ret, err);
ret = fp_cmp(&(in_edwards->y), &tmp2, &cmp); EG(ret, err);
idx = !(iszero && cmp);
ret = fp_tabselect(&x, idx, tab_x_t, 2); EG(ret, err);
ret = fp_tabselect(&y, idx, tab_y_t, 2); EG(ret, err);
ret = aff_pt_montgomery_init(out_montgomery, montgomery_crv); EG(ret, err);
ret = fp_copy(&tmp2, &tmp); EG(ret, err);
ret = fp_sub(&tmp, &tmp, &y); EG(ret, err);
ret = fp_inv(&tmp, &tmp); EG(ret, err);
ret = fp_add(&tmp2, &tmp2, &y); EG(ret, err);
ret = fp_mul(&(out_montgomery->u), &tmp, &tmp2); EG(ret, err);
ret = fp_inv(&(out_montgomery->v), &x); EG(ret, err);
ret = fp_mul(&(out_montgomery->v), &(out_montgomery->v), alpha_edwards); EG(ret, err);
ret = fp_mul(&(out_montgomery->v), &(out_montgomery->u), &(out_montgomery->v)); EG(ret, err);
ret = aff_pt_montgomery_is_on_curve(out_montgomery, &on_curve); EG(ret, err);
if (!on_curve) {
ret = -1;
}
err:
fp_uninit(&tmp);
fp_uninit(&tmp2);
fp_uninit(&x);
fp_uninit(&y);
fp_uninit(&tab_x[0]);
fp_uninit(&tab_x[1]);
fp_uninit(&tab_y[0]);
fp_uninit(&tab_y[1]);
return ret;
}
int aff_pt_montgomery_to_edwards(aff_pt_montgomery_src_t in_montgomery,
ec_edwards_crv_src_t edwards_crv,
aff_pt_edwards_t out_edwards,
fp_src_t alpha)
{
int ret, iszero1, iszero2, on_curve;
fp tmp, u, v;
fp tab_u[2];
fp_src_t tab_u_t[2] = { &tab_u[0], &tab_u[1] };
fp tab_v[2];
fp_src_t tab_v_t[2] = { &tab_v[0], &tab_v[1] };
u8 idx = 0;
tmp.magic = u.magic = v.magic = 0;
tab_u[0].magic = tab_u[1].magic = WORD(0);
tab_v[0].magic = tab_v[1].magic = WORD(0);
ret = ec_edwards_crv_check_initialized(edwards_crv); EG(ret, err);
ret = aff_pt_montgomery_is_on_curve(in_montgomery, &on_curve); EG(ret, err);
MUST_HAVE(on_curve, ret, err);
ret = curve_edwards_montgomery_check(edwards_crv, in_montgomery->crv, alpha); EG(ret, err);
ret = fp_init(&tmp, in_montgomery->crv->A.ctx); EG(ret, err);
ret = fp_init(&u, in_montgomery->crv->A.ctx); EG(ret, err);
ret = fp_init(&v, in_montgomery->crv->A.ctx); EG(ret, err);
ret = fp_init(&tab_u[0], in_montgomery->crv->A.ctx); EG(ret, err);
ret = fp_init(&tab_u[1], in_montgomery->crv->A.ctx); EG(ret, err);
ret = fp_init(&tab_v[0], in_montgomery->crv->A.ctx); EG(ret, err);
ret = fp_init(&tab_v[1], in_montgomery->crv->A.ctx); EG(ret, err);
ret = fp_one(&tmp); EG(ret, err);
ret = fp_zero(&tab_u[0]); EG(ret, err);
ret = fp_copy(&tab_u[1], &(in_montgomery->u)); EG(ret, err);
ret = fp_copy(&tab_v[0], &tmp); EG(ret, err);
ret = fp_copy(&tab_v[1], &(in_montgomery->v)); EG(ret, err);
ret = fp_iszero(&(in_montgomery->u), &iszero1); EG(ret, err);
ret = fp_iszero(&(in_montgomery->v), &iszero2); EG(ret, err);
idx = (iszero1 && iszero2) ? 0 : 1;
ret = fp_tabselect(&u, idx, tab_u_t, 2); EG(ret, err);
ret = fp_tabselect(&v, idx, tab_v_t, 2); EG(ret, err);
ret = aff_pt_edwards_init(out_edwards, edwards_crv); EG(ret, err);
ret = fp_inv(&(out_edwards->x), &v); EG(ret, err);
ret = fp_mul(&(out_edwards->x), &(out_edwards->x), alpha); EG(ret, err);
ret = fp_mul(&(out_edwards->x), &(out_edwards->x), &u); EG(ret, err);
ret = fp_add(&(out_edwards->y), &u, &tmp); EG(ret, err);
ret = fp_inv(&(out_edwards->y), &(out_edwards->y)); EG(ret, err);
ret = fp_sub(&tmp, &u, &tmp); EG(ret, err);
ret = fp_mul(&(out_edwards->y), &(out_edwards->y), &tmp); EG(ret, err);
ret = aff_pt_edwards_is_on_curve(out_edwards, &on_curve); EG(ret, err);
if (!on_curve) {
ret = -1;
}
err:
fp_uninit(&tmp);
fp_uninit(&u);
fp_uninit(&v);
fp_uninit(&tab_u[0]);
fp_uninit(&tab_u[1]);
fp_uninit(&tab_v[0]);
fp_uninit(&tab_v[1]);
return ret;
}
int aff_pt_edwards_to_shortw(aff_pt_edwards_src_t in_edwards,
ec_shortw_crv_src_t shortw_crv,
aff_pt_t out_shortw, fp_src_t alpha_edwards)
{
int ret;
aff_pt_montgomery inter_montgomery;
ec_montgomery_crv inter_montgomery_crv;
inter_montgomery.magic = inter_montgomery_crv.magic = WORD(0);
ret = aff_pt_edwards_check_initialized(in_edwards); EG(ret, err);
ret = curve_edwards_to_montgomery(in_edwards->crv, &inter_montgomery_crv, alpha_edwards); EG(ret, err);
ret = aff_pt_edwards_to_montgomery(in_edwards, &inter_montgomery_crv, &inter_montgomery, alpha_edwards); EG(ret, err);
ret = aff_pt_montgomery_to_shortw(&inter_montgomery, shortw_crv, out_shortw);
err:
aff_pt_montgomery_uninit(&inter_montgomery);
ec_montgomery_crv_uninit(&inter_montgomery_crv);
return ret;
}
int aff_pt_shortw_to_edwards(aff_pt_src_t in_shortw,
ec_edwards_crv_src_t edwards_crv,
aff_pt_edwards_t out_edwards,
fp_src_t alpha_edwards)
{
int ret;
aff_pt_montgomery inter_montgomery;
ec_montgomery_crv inter_montgomery_crv;
inter_montgomery.magic = inter_montgomery_crv.magic = WORD(0);
ret = curve_edwards_to_montgomery(edwards_crv, &inter_montgomery_crv, alpha_edwards); EG(ret, err);
ret = aff_pt_shortw_to_montgomery(in_shortw, &inter_montgomery_crv, &inter_montgomery); EG(ret, err);
ret = aff_pt_montgomery_to_edwards(&inter_montgomery, edwards_crv, out_edwards, alpha_edwards);
err:
aff_pt_montgomery_uninit(&inter_montgomery);
ec_montgomery_crv_uninit(&inter_montgomery_crv);
return ret;
}
int aff_pt_edwards_y_from_x(fp_t y1, fp_t y2, fp_src_t x, ec_edwards_crv_src_t crv)
{
int ret;
fp tmp;
tmp.magic = WORD(0);
ret = fp_check_initialized(x); EG(ret, err);
ret = ec_edwards_crv_check_initialized(crv); EG(ret, err);
MUST_HAVE((x->ctx == crv->a.ctx) && (x->ctx == crv->d.ctx), ret, err);
MUST_HAVE((y1 != NULL) && (y2 != NULL), ret, err);
MUST_HAVE((y1 != y2) && (y1 != x), ret, err);
ret = fp_init(y1, x->ctx); EG(ret, err);
ret = fp_init(y2, x->ctx); EG(ret, err);
ret = fp_init(&tmp, x->ctx); EG(ret, err);
ret = fp_one(&tmp); EG(ret, err);
ret = fp_mul(y1, x, &(crv->a)); EG(ret, err);
ret = fp_mul(y1, y1, x); EG(ret, err);
ret = fp_sub(y1, &tmp, y1); EG(ret, err);
ret = fp_mul(y2, x, &(crv->d)); EG(ret, err);
ret = fp_mul(y2, y2, x); EG(ret, err);
ret = fp_sub(y2, &tmp, y2); EG(ret, err);
ret = fp_inv(y2, y2); EG(ret, err);
ret = fp_mul(&tmp, y1, y2); EG(ret, err);
ret = fp_sqrt(y1, y2, &tmp);
err:
fp_uninit(&tmp);
return ret;
}
int aff_pt_edwards_x_from_y(fp_t x1, fp_t x2, fp_src_t y, ec_edwards_crv_src_t crv)
{
int ret;
fp tmp;
tmp.magic = WORD(0);
ret = fp_check_initialized(y); EG(ret, err);
ret = ec_edwards_crv_check_initialized(crv); EG(ret, err);
MUST_HAVE((y->ctx == crv->a.ctx) && (y->ctx == crv->d.ctx), ret, err);
MUST_HAVE((x1 != NULL) && (x2 != NULL), ret, err);
MUST_HAVE((x1 != x2) && (x1 != y), ret, err);
ret = fp_init(x1, y->ctx); EG(ret, err);
ret = fp_init(x2, y->ctx); EG(ret, err);
ret = fp_init(&tmp, y->ctx); EG(ret, err);
ret = fp_one(&tmp); EG(ret, err);
ret = fp_mul(x1, y, y); EG(ret, err);
ret = fp_sub(x1, &tmp, x1); EG(ret, err);
ret = fp_mul(x2, y, &(crv->d)); EG(ret, err);
ret = fp_mul(x2, x2, y); EG(ret, err);
ret = fp_sub(x2, &(crv->a), x2); EG(ret, err);
ret = fp_inv(x2, x2); EG(ret, err);
ret = fp_mul(&tmp, x1, x2); EG(ret, err);
ret = fp_sqrt(x1, x2, &tmp);
err:
fp_uninit(&tmp);
return ret;
}