Skip to content

Commit

Permalink
Another potential optimization.
Browse files Browse the repository at this point in the history
  • Loading branch information
dfaranha committed Sep 12, 2024
1 parent 285e474 commit 04000a8
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/pc/relic_pc_exp.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ void gt_exp_gls_naf(gt_t c, const gt_t a, const bn_t b, size_t f) {
int8_t n0, *s = RLC_ALLOCA(int8_t, f);
gt_t q, *t = RLC_ALLOCA(gt_t, f * RLC_GT_TABLE);
bn_t n, u, *_b = RLC_ALLOCA(bn_t, f);
size_t l, *_l = RLC_ALLOCA(size_t, f);
size_t l, *_l = RLC_ALLOCA(size_t, f), w = RLC_WIDTH;

if (naf == NULL || t == NULL || _b == NULL || _l == NULL) {
RLC_THROW(ERR_NO_MEMORY);
Expand Down Expand Up @@ -167,6 +167,14 @@ void gt_exp_gls_naf(gt_t c, const gt_t a, const bn_t b, size_t f) {
}
bn_rec_frb(_b, f, _b[0], u, n, ep_curve_is_pairf() == EP_BN);

l = 0;
for (size_t i = 0; i < f; i++) {
l = RLC_MAX(l, bn_bits(_b[i]));
}
if (l < bn_bits(u) / 2) {
w = 2;
}

l = 0;
for (size_t i = 0; i < f; i++) {
s[i] = bn_sign(_b[i]);
Expand All @@ -186,7 +194,7 @@ void gt_exp_gls_naf(gt_t c, const gt_t a, const bn_t b, size_t f) {
if (s[i] == RLC_NEG) {
gt_inv(q, t[i * RLC_GT_TABLE]);
}
if (RLC_WIDTH > 2) {
if (w > 2) {
gt_sqr(t[i * RLC_GT_TABLE], q);
gt_mul(t[i * RLC_GT_TABLE + 1], t[i * RLC_GT_TABLE], q);
for (size_t j = 2; j < RLC_GT_TABLE; j++) {
Expand All @@ -201,7 +209,7 @@ void gt_exp_gls_naf(gt_t c, const gt_t a, const bn_t b, size_t f) {
if (bn_sign(_b[0]) == RLC_NEG) {
gt_inv(q, q);
}
if (RLC_WIDTH > 2) {
if (w > 2) {
gt_sqr(t[0], q);
gt_mul(t[1], t[0], q);
for (size_t j = 2; j < RLC_GT_TABLE; j++) {
Expand All @@ -210,10 +218,10 @@ void gt_exp_gls_naf(gt_t c, const gt_t a, const bn_t b, size_t f) {
}
gt_copy(t[0], q);
for (size_t i = 1; i < f; i++) {
for (size_t j = 0; j < RLC_GT_TABLE; j++) {
gt_psi(t[i * RLC_GT_TABLE + j], t[(i - 1) * RLC_GT_TABLE + j]);
for (size_t j = 0; j < (1 << (w - 2)); j++) {
gt_psi(t[i * (1 << (w - 2)) + j], t[(i - 1) * (1 << (w - 2)) + j]);
if (s[i] != s[i - 1]) {
gt_inv(t[i * RLC_GT_TABLE + j], t[i * RLC_GT_TABLE + j]);
gt_inv(t[i * (1 << (w - 2)) + j], t[i * (1 << (w - 2)) + j]);
}
}
}
Expand All @@ -226,10 +234,10 @@ void gt_exp_gls_naf(gt_t c, const gt_t a, const bn_t b, size_t f) {
for (size_t i = 0; i < f; i++) {
n0 = naf[i * (RLC_FP_BITS + 1) + j];
if (n0 > 0) {
gt_mul(c, c, t[i * RLC_GT_TABLE + n0 / 2]);
gt_mul(c, c, t[i * (1 << (w - 2)) + n0 / 2]);
}
if (n0 < 0) {
gt_inv(q, t[i * RLC_GT_TABLE - n0 / 2]);
gt_inv(q, t[i * (1 << (w - 2)) - n0 / 2]);
gt_mul(c, c, q);
}
}
Expand Down

0 comments on commit 04000a8

Please sign in to comment.