Skip to content

Commit

Permalink
Restored sparse multiplication.
Browse files Browse the repository at this point in the history
  • Loading branch information
dfaranha committed Jul 11, 2011
1 parent eb5af8f commit 23c765a
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 16 deletions.
4 changes: 0 additions & 4 deletions src/pp/relic_ep2_add.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ static void ep2_add_basic_imp(ep2_t r, fp2_t s, ep2_t p, ep2_t q) {
fp2_new(t1);
fp2_new(t2);

if (!q->norm) {
THROW(ERR_INVALID);
}

/* t0 = x2 - x1. */
fp2_sub(t0, q->x, p->x);
/* t1 = y2 - y1. */
Expand Down
40 changes: 40 additions & 0 deletions src/pp/relic_pp_fp12.c
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,46 @@ void fp12_sqr_pck_lazyr(fp12_t c, fp12_t a) {

#endif

void fp12_mul_dxs(fp12_t c, fp12_t a, fp12_t b) {
fp6_t v0, v1, t0;

fp6_null(v0);
fp6_null(v1);
fp6_null(t0);

TRY {
fp6_new(v0);
fp6_new(v1);
fp6_new(t0);

/* c1 = (a0 + a1)(b0 + b1) */
fp6_add(v0, a[0], a[1]);
fp2_add(v1[0], b[0][0], b[1][0]);
fp2_copy(v1[1], b[1][1]);
fp6_mul_dxs(t0, v0, v1);

/* v0 = a0b0 */
fp6_mul_dxq(v0, a[0], b[0][0]);

/* v1 = a1b1 */
fp6_mul_dxs(v1, a[1], b[1]);

/* c1 = c1 - v0 - v1 */
fp6_sub(c[1], t0, v0);
fp6_sub(c[1], c[1], v1);

/* c0 = v0 + v * v1 */
fp6_mul_art(v1, v1);
fp6_add(c[0], v0, v1);
} CATCH_ANY {
THROW(ERR_CAUGHT);
} FINALLY {
fp6_free(v0);
fp6_free(v1);
fp6_free(t0);
}
}

void fp12_inv(fp12_t c, fp12_t a) {
fp6_t t0;
fp6_t t1;
Expand Down
63 changes: 63 additions & 0 deletions src/pp/relic_pp_fp6.c
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,69 @@ void fp6_sqr_lazyr2(fp6_t c, fp6_t a) {

#endif

void fp6_mul_dxs(fp6_t c, fp6_t a, fp6_t b) {
fp2_t v0, v1, t0, t1, t2;

fp2_null(v0);
fp2_null(v1);
fp2_null(t0);
fp2_null(t1);
fp2_null(t2);

TRY {
fp2_new(v0);
fp2_new(v1);
fp2_new(t0);
fp2_new(t1);
fp2_new(t2);

/* v0 = a0b0 */
fp2_mul(v0, a[0], b[0]);

/* v1 = a1b1 */
fp2_mul(v1, a[1], b[1]);

/* v2 = a2b2 = 0 */

/* t2 (c0) = v0 + E((a1 + a2)(b1 + b2) - v1 - v2) */
fp2_add(t0, a[1], a[2]);
fp2_mul(t2, t0, b[1]);
fp2_sub(t2, t2, v1);
fp2_mul_nor(t2, t2);
fp2_add(t2, t2, v0);

/* c1 = (a0 + a1)(b0 + b1) - v0 - v1 + Ev2 */
fp2_add(t0, a[0], a[1]);
fp2_add(t1, b[0], b[1]);
fp2_mul(c[1], t0, t1);
fp2_sub(c[1], c[1], v0);
fp2_sub(c[1], c[1], v1);

/* c2 = (a0 + a2)(b0 + b2) - v0 + v1 - v2 */
fp2_add(t0, a[0], a[2]);
fp2_mul(c[2], t0, b[0]);
fp2_sub(c[2], c[2], v0);
fp2_add(c[2], c[2], v1);

/* c0 = t2 */
fp2_copy(c[0], t2);
} CATCH_ANY {
THROW(ERR_CAUGHT);
} FINALLY {
fp2_free(v0);
fp2_free(v1);
fp2_free(t0);
fp2_free(t1);
fp2_free(t2);
}
}

void fp6_mul_dxq(fp6_t c, fp6_t a, fp2_t b) {
fp2_mul(c[0], a[0], b);
fp2_mul(c[1], a[1], b);
fp2_mul(c[2], a[2], b);
}

void fp6_mul_art(fp6_t c, fp6_t a) {
fp2_t t0;

Expand Down
24 changes: 12 additions & 12 deletions src/pp/relic_pp_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,10 @@ void pp_miller(fp12_t r, ep2_t t, ep2_t q, bn_t a, ep_t p) {
for (int i = bn_bits(a) - 2; i >= 0; i--) {
fp12_sqr(r, r);
pp_dbl(tmp, t, t, p);
fp12_mul(r, r, tmp);
fp12_mul_dxs(r, r, tmp);
if (bn_test_bit(a, i)) {
pp_add(tmp, t, q, p);
fp12_mul(r, r, tmp);
fp12_mul_dxs(r, r, tmp);
}
}
}
Expand Down Expand Up @@ -226,10 +226,10 @@ void pp_miller(fp12_t r, ep2_t t, ep2_t q, bn_t a, ep_t p) {
for (int i = bn_bits(a0) - 2; i >= 0; i--) {
fp12_sqr(_f[0], _f[0]);
pp_dbl(_t[0], _q[0], _q[0], p);
fp12_mul(_f[0], _f[0], _t[0]);
fp12_mul_dxs(_f[0], _f[0], _t[0]);
if (bn_test_bit(a0, i)) {
pp_add(_t[0], _q[0], q, p);
fp12_mul(_f[0], _f[0], _t[0]);
fp12_mul_dxs(_f[0], _f[0], _t[0]);
}
}
for (int i = PART - 1; i >= 0; i--) {
Expand All @@ -244,10 +244,10 @@ void pp_miller(fp12_t r, ep2_t t, ep2_t q, bn_t a, ep_t p) {
for (int i = PART - 1; i >= 0; i--) {
fp12_sqr(_f[1], _f[1]);
pp_dbl(_t[1], _q[1], _q[1], p);
fp12_mul(_f[1], _f[1], _t[1]);
fp12_mul_dxs(_f[1], _f[1], _t[1]);
if (bn_test_bit(a, i)) {
pp_add(_t[1], _q[1], q, p);
fp12_mul(_f[1], _f[1], _t[1]);
fp12_mul_dxs(_f[1], _f[1], _t[1]);
}
}
ep2_copy(t, _q[1]);
Expand Down Expand Up @@ -395,7 +395,7 @@ void pp_r_ate_mul(fp12_t res, ep2_t t, ep2_t q, ep_t p) {
fp_zero(q1->z[1]);

pp_add(tmp1, r1q, q, p);
fp12_mul(tmp2, res, tmp1);
fp12_mul_dxs(tmp2, res, tmp1);
fp12_frb(tmp2, tmp2);
fp12_mul(res, res, tmp2);

Expand All @@ -407,7 +407,7 @@ void pp_r_ate_mul(fp12_t res, ep2_t t, ep2_t q, ep_t p) {
ep2_copy(r1q, t);

pp_add(tmp1, r1q, q1, p);
fp12_mul(res, res, tmp1);
fp12_mul_dxs(res, res, tmp1);
} CATCH_ANY {
THROW(ERR_CAUGHT);
} FINALLY {
Expand Down Expand Up @@ -450,9 +450,9 @@ void pp_o_ate_mul(fp12_t res, ep2_t t, ep2_t q, ep_t p) {
ep2_neg(q2, q2);

pp_add(tmp, t, q1, p);
fp12_mul(res, res, tmp);
fp12_mul_dxs(res, res, tmp);
pp_add(tmp, t, q2, p);
fp12_mul(res, res, tmp);
fp12_mul_dxs(res, res, tmp);
} CATCH_ANY {
THROW(ERR_CAUGHT);
} FINALLY {
Expand Down Expand Up @@ -526,7 +526,7 @@ void pp_x_ate_mul(fp12_t res, ep2_t t, ep2_t q, ep_t p) {
fp12_zero(tmp);
/* q1 = p*xQ + xQ. */
pp_add(tmp, q1, t, p);
fp12_mul(res, res, tmp);
fp12_mul_dxs(res, res, tmp);

/* q2 = q2 + q3. */
pp_add(tmp, q2, q3, p);
Expand All @@ -536,7 +536,7 @@ void pp_x_ate_mul(fp12_t res, ep2_t t, ep2_t q, ep_t p) {
ep2_norm(q2, q2);
pp_add(tmp, q1, q2, p);

fp12_mul(res, res, tmp);
fp12_mul_dxs(res, res, tmp);
} CATCH_ANY {
THROW(ERR_CAUGHT);
} FINALLY {
Expand Down

0 comments on commit 23c765a

Please sign in to comment.