Skip to content

Commit

Permalink
Fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
dfaranha committed Aug 14, 2024
1 parent 76d59a7 commit 7193ea6
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 129 deletions.
4 changes: 2 additions & 2 deletions src/epx/relic_ep2_mul.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ static void ep2_mul_gls_imp(ep2_t r, const ep2_t p, const bn_t k) {
static void ep2_mul_reg_gls(ep2_t r, const ep2_t p, const bn_t k) {
size_t l;
bn_t n, _k[4], u;
int8_t even, col, sac[4 * (RLC_FP_BITS + 1)];
int8_t even, col, sac[4 * RLC_FP_BITS];
ep2_t q[4], t[1 << 3];

bn_null(n);
Expand Down Expand Up @@ -171,7 +171,7 @@ static void ep2_mul_reg_gls(ep2_t r, const ep2_t p, const bn_t k) {
ep2_add(t[i], t[i ^ (1 << (l - 1))], q[l]);
}

l = RLC_FP_BITS + 1;
l = RLC_FP_BITS;
bn_rec_sac(sac, &l, _k, 4, bn_bits(n));

#if defined(EP_MIXED)
Expand Down
15 changes: 7 additions & 8 deletions src/epx/relic_ep4_mul.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ static void ep4_mul_reg_gls(ep4_t r, const ep4_t p, const bn_t k) {
ep4_new(q[i]);
}
for (size_t i = 0; i < c; i++) {
for (int j = 0; j < (j << 3); i++) {
for (int j = 0; j < (1 << 3); j++) {
ep4_null(t[i][j]);
ep4_new(t[i][j]);
}
Expand All @@ -208,11 +208,6 @@ static void ep4_mul_reg_gls(ep4_t r, const ep4_t p, const bn_t k) {
bn_mod(_k[0], k, n);
bn_rec_frb(_k, 8, _k[0], u, n, ep_curve_is_pairf() == EP_BN);

for (size_t i = 0; i < c; i++) {
even[i] = bn_is_even(_k[i * m / c]);
bn_add_dig(_k[i * m / c], _k[i * m / c], even[i]);
}

ep4_norm(q[0], p);
for (size_t i = 1; i < 8; i++) {
ep4_psi(q[i], q[i - 1]);
Expand All @@ -222,7 +217,11 @@ static void ep4_mul_reg_gls(ep4_t r, const ep4_t p, const bn_t k) {
fp4_copy_sec(q[i]->y, r->y, bn_sign(_k[i]) == RLC_NEG);
bn_abs(_k[i], _k[i]);
}

for (size_t i = 0; i < c; i++) {
even[i] = bn_is_even(_k[i * m / c]);
bn_add_dig(_k[i * m / c], _k[i * m / c], even[i]);
}

for (size_t i = 0; i < c; i++) {
ep4_copy(t[i][0], q[i * m / c]);
for (size_t j = 1; j < (1 << 3); j++) {
Expand Down Expand Up @@ -307,7 +306,7 @@ static void ep4_mul_reg_gls(ep4_t r, const ep4_t p, const bn_t k) {
ep4_free(q[i]);
}
for (size_t i = 0; i < c; i++) {
for (int j = 0; j < (j << 3); i++) {
for (int j = 0; j < (1 << 3); j++) {
ep4_free(t[i][j]);
}
}
Expand Down
15 changes: 7 additions & 8 deletions src/epx/relic_ep8_mul.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ static void ep8_mul_reg_gls(ep8_t r, const ep8_t p, const bn_t k) {
ep8_new(q[i]);
}
for (size_t i = 0; i < c; i++) {
for (int j = 0; j < (j << 3); i++) {
for (int j = 0; j < (1 << 3); j++) {
ep8_null(t[i][j]);
ep8_new(t[i][j]);
}
Expand All @@ -156,11 +156,6 @@ static void ep8_mul_reg_gls(ep8_t r, const ep8_t p, const bn_t k) {
bn_mod(_k[0], k, n);
bn_rec_frb(_k, 16, _k[0], u, n, ep_curve_is_pairf() == EP_BN);

for (size_t i = 0; i < c; i++) {
even[i] = bn_is_even(_k[i * m / c]);
bn_add_dig(_k[i * m / c], _k[i * m / c], even[i]);
}

ep8_norm(q[0], p);
for (size_t i = 1; i < 16; i++) {
ep8_frb(q[i], q[i - 1], 1);
Expand All @@ -170,7 +165,11 @@ static void ep8_mul_reg_gls(ep8_t r, const ep8_t p, const bn_t k) {
fp8_copy_sec(q[i]->y, r->y, bn_sign(_k[i]) == RLC_NEG);
bn_abs(_k[i], _k[i]);
}

for (size_t i = 0; i < c; i++) {
even[i] = bn_is_even(_k[i * m / c]);
bn_add_dig(_k[i * m / c], _k[i * m / c], even[i]);
}

for (size_t i = 0; i < c; i++) {
ep8_copy(t[i][0], q[i * m / c]);
for (size_t j = 1; j < (1 << 3); j++) {
Expand Down Expand Up @@ -255,7 +254,7 @@ static void ep8_mul_reg_gls(ep8_t r, const ep8_t p, const bn_t k) {
ep8_free(q[i]);
}
for (size_t i = 0; i < c; i++) {
for (int j = 0; j < (j << 3); i++) {
for (int j = 0; j < (1 << 3); j++) {
ep8_free(t[i][j]);
}
}
Expand Down
177 changes: 67 additions & 110 deletions src/pc/relic_pc_exp.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,141 +263,96 @@ void gt_exp_gls_imp(gt_t c, const gt_t a, const bn_t b, size_t f) {
* @param[in] b - the exponent.
* @param[in] f - the maximum Frobenius power.
*/
void gt_exp_reg_gls(gt_t c, const gt_t a, const bn_t b, size_t f) {
int8_t c0, n0, *reg = RLC_ALLOCA(int8_t, f * (RLC_FP_BITS + 1));
int8_t *e = RLC_ALLOCA(int8_t, f), *s = RLC_ALLOCA(int8_t, f);
gt_t q, w, *t = RLC_ALLOCA(gt_t, f * RLC_GT_TABLE);
bn_t n, u, *_b = RLC_ALLOCA(bn_t, f);
size_t l, len, *_l = RLC_ALLOCA(size_t, f);

if (reg == NULL || e == NULL || t == NULL || _b == NULL || _l == NULL) {
static void gt_exp_reg_gls(gt_t c, const gt_t a, const bn_t b, size_t d,

Check warning

Code scanning / CodeQL

Poorly documented large function Warning

Poorly documented function: fewer than 2% comments for a function of 114 lines.
size_t f) {
size_t l, s = (1 << (f / d - 1));
bn_t n, *_b = RLC_ALLOCA(bn_t, f), u;
int8_t col, *e = RLC_ALLOCA(int8_t, d);
int8_t *sac = RLC_ALLOCA(int8_t, d * f * RLC_FP_BITS);
gt_t *q = RLC_ALLOCA(gt_t, f), *t = RLC_ALLOCA(gt_t, d * s);

if (sac == NULL || e == NULL || t == NULL || _b == NULL || q == NULL) {
RLC_THROW(ERR_NO_MEMORY);
return;
}

if (bn_is_zero(b)) {
RLC_FREE(reg);
RLC_FREE(e);
RLC_FREE(s);
RLC_FREE(t);
RLC_FREE(_b);
RLC_FREE(_l);
return gt_set_unity(c);
}

bn_null(n);
bn_null(u);
gt_null(q);
gt_null(w);

RLC_TRY {
bn_new(n);
bn_new(u);
gt_new(q);
gt_new(w);
for (size_t i = 0; i < f; i++) {
for (int i = 0; i < f; i++) {
bn_null(_b[i]);
gt_null(q[i]);
bn_new(_b[i]);
for (size_t j = 0; j < RLC_GT_TABLE; j++) {
gt_null(t[i * RLC_GT_TABLE + j]);
gt_new(t[i * RLC_GT_TABLE + j]);
gt_new(q[i]);
}
for (size_t i = 0; i < d; i++) {
for (int j = 0; j < s; j++) {
gt_null(t[i * s + j]);
gt_new(t[i * s + j]);
}
}

gt_get_ord(n);
fp_prime_get_par(u);
if (ep_curve_is_pairf() == EP_SG18) {
/* Compute base -3*u for the recoding below. */
bn_dbl(n, u);
bn_add(u, u, n);
bn_neg(u, u);
}
gt_get_ord(n);
bn_abs(_b[0], b);
bn_mod(_b[0], _b[0], n);
if (bn_sign(b) == RLC_NEG) {
bn_neg(_b[0], _b[0]);
}
bn_mod(_b[0], b, n);
bn_rec_frb(_b, f, _b[0], u, n, ep_curve_is_pairf() == EP_BN);

l = 0;
len = bn_bits(u) + (ep_curve_is_pairf() == EP_BN);
gt_copy(q[0], a);
for (size_t i = 1; i < f; i++) {
gt_psi(q[i], q[i - 1]);
}
for (size_t i = 0; i < f; i++) {
s[i] = bn_sign(_b[i]);
e[i] = bn_is_even(_b[i]);
_b[i]->dp[0] |= e[i];

_l[i] = RLC_FP_BITS + 1;
bn_rec_reg(reg + i * (RLC_FP_BITS + 1), &_l[i], _b[i], len, RLC_WIDTH);
l = RLC_MAX(l, _l[i]);
gt_inv(c, q[i]);
gt_copy_sec(q[i], c, bn_sign(_b[i]) == RLC_NEG);
bn_abs(_b[i], _b[i]);
}

if (ep_curve_is_pairf() == EP_K16 || ep_curve_embed() == 18) {
gt_copy(t[0], a);
for (size_t i = 1; i < f; i++) {
gt_psi(t[i * RLC_GT_TABLE], t[(i - 1) * RLC_GT_TABLE]);
}
for (size_t i = 0; i < f; i++) {
gt_inv(q, t[i * RLC_GT_TABLE]);
gt_copy_sec(q, t[i * RLC_GT_TABLE], s[i] == RLC_POS);
if (RLC_WIDTH > 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++) {
gt_mul(t[i * RLC_GT_TABLE + j], t[i * RLC_GT_TABLE + j - 1],
t[i * (RLC_GT_TABLE)]);
}
}
gt_copy(t[i * RLC_GT_TABLE], q);
}
} else {
gt_copy(t[0], a);
gt_inv(q, t[0]);
gt_copy_sec(q, t[0], bn_sign(_b[0]) == RLC_POS);
if (RLC_WIDTH > 2) {
gt_sqr(t[0], q);
gt_mul(t[1], t[0], q);
for (size_t j = 2; j < RLC_GT_TABLE; j++) {
gt_mul(t[j], t[j - 1], t[0]);
}
}
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]);
if (s[i] != s[i - 1]) {
gt_inv(t[i * RLC_GT_TABLE + j], t[i * RLC_GT_TABLE + j]);
}
}
for (size_t i = 0; i < d; i++) {
e[i] = bn_is_even(_b[i * f / d]);
bn_add_dig(_b[i * f / d], _b[i * f / d], e[i]);
}

for (size_t i = 0; i < d; i++) {
gt_copy(t[i * s], q[i * f / d]);
for (size_t j = 1; j < s; j++) {
l = util_bits_dig(j);
gt_mul(t[i * s + j], t[i * s + (j ^ (1 << (l - 1)))], q[l + i * f / d]);
}
l = RLC_FP_BITS;
bn_rec_sac(sac + i * f * RLC_FP_BITS, &l, _b + i * f / d, f / d, bn_bits(n));
}

gt_set_unity(c);
for (int j = l - 1; j >= 0; j--) {
for (size_t i = 0; i < RLC_WIDTH - 1; i++) {
gt_sqr(c, c);
}

for (size_t i = 0; i < f; i++) {
n0 = reg[i * (RLC_FP_BITS + 1) + j];
c0 = (n0 >> 7);
n0 = ((n0 ^ c0) - c0) >> 1;

for (size_t m = 0; m < RLC_GT_TABLE; m++) {
gt_copy_sec(w, t[i * RLC_GT_TABLE + m], m == n0);
gt_sqr(c, c);
for (size_t i = 0; i < d; i++) {
col = 0;
for (int k = f / d - 1; k > 0; k--) {
col <<= 1;
col += sac[i * f * RLC_FP_BITS + k * l + j];
}

gt_inv(q, w);
gt_copy_sec(q, w, c0 == 0);
gt_mul(c, c, q);


for (size_t m = 0; m < s; m++) {
gt_copy_sec(q[1], t[i * s + m], m == col);
}
gt_inv(q[2], q[1]);
gt_copy_sec(q[1], q[2], sac[i * f * RLC_FP_BITS + j]);
gt_mul(c, c, q[1]);
}
}

for (size_t i = 0; i < f; i++) {
/* Tables are built with points already negated, so no need here. */
gt_inv(q, t[i * RLC_GT_TABLE]);
gt_mul(q, c, q);
gt_copy_sec(c, q, e[i]);
for (size_t i = 0; i < d; i++) {
gt_inv(q[1], q[i * f / d]);
gt_mul(q[1], q[1], c);
gt_copy_sec(c, q[1], e[i]);
}
}
RLC_CATCH_ANY {
Expand All @@ -406,20 +361,20 @@ void gt_exp_reg_gls(gt_t c, const gt_t a, const bn_t b, size_t f) {
RLC_FINALLY {
bn_free(n);
bn_free(u);
gt_free(q);
gt_free(w);
for (size_t i = 0; i < f; i++) {
for (int i = 0; i < f; i++) {
bn_free(_b[i]);
for (size_t j = 0; j < RLC_GT_TABLE; j++) {
gt_free(t[i * RLC_GT_TABLE + j]);
gt_free(q[i]);
}
for (size_t i = 0; i < d; i++) {
for (int j = 0; j < s; j++) {
gt_free(t[i * d + j]);
}
}
RLC_FREE(reg);
RLC_FREE(e);
RLC_FREE(s);
RLC_FREE(t);
RLC_FREE(_b);
RLC_FREE(_l);
RLC_FREE(q);
RLC_FREE(t);
RLC_FREE(sac);
}
}

Expand Down Expand Up @@ -559,7 +514,9 @@ void gt_exp_sec(gt_t c, const gt_t a, const bn_t b) {
}

#if FP_PRIME <= 1536
gt_exp_reg_gls(c, a, b, ep_curve_frdim());
size_t d = ep_curve_frdim();
d = (d > 4 ? d / 4 : 1);
gt_exp_reg_gls(c, a, b, d, ep_curve_frdim());
#else
RLC_CAT(RLC_GT_LOWER, exp_monty)(c, a, b);
#endif
Expand Down
1 change: 0 additions & 1 deletion src/pp/relic_pp_map_k54.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ void pp_map_k54(fp54_t r, const ep_t p, const fp9_t qx, const fp9_t qy) {
if (bn_sign(a) == RLC_NEG) {
fp54_inv_cyc(r, r);
}
fp18_print(r[0]);
pp_exp_k54(r, r);
break;
}
Expand Down

0 comments on commit 7193ea6

Please sign in to comment.