94 static void *ec_pre_comp_dup(
void *);
95 static void ec_pre_comp_free(
void *);
96 static void ec_pre_comp_clear_free(
void *);
121 static void *ec_pre_comp_dup(
void *src_)
132 static void ec_pre_comp_free(
void *pre_)
148 for (p = pre->
points; *p != NULL; p++)
155 static void ec_pre_comp_clear_free(
void *pre_)
171 for (p = pre->
points; *p != NULL; p++)
193 static signed char *compute_wNAF(
const BIGNUM *scalar,
int w,
size_t *ret_len)
197 signed char *r = NULL;
199 int bit, next_bit, mask;
229 if (scalar->
d == NULL || scalar->
top == 0)
244 window_val = scalar->
d[0] & mask;
246 while ((window_val != 0) || (j + w + 1 < len))
256 if (window_val & bit)
258 digit = window_val - next_bit;
261 if (j + w + 1 >= len)
268 digit = window_val & (mask >> 1);
277 if (digit <= -bit || digit >= bit || !(digit & 1))
288 if (window_val != 0 && window_val != next_bit && window_val != bit)
295 r[j++] = sign * digit;
300 if (window_val > next_bit)
331 #define EC_window_bits_for_scalar_size(b) \
353 size_t blocksize = 0, numblocks = 0;
354 size_t pre_points_per_block = 0;
357 int r_is_inverted = 0;
358 int r_is_at_infinity = 1;
359 size_t *wsize = NULL;
360 signed char **wNAF = NULL;
361 size_t *wNAF_len = NULL;
378 if ((scalar == NULL) && (num == 0))
383 for (i = 0; i <
num; i++)
402 if (generator == NULL)
424 pre_points_per_block = (size_t)1 << (pre_comp->
w - 1);
427 if (pre_comp->
num != (pre_comp->
numblocks * pre_points_per_block))
442 totalnum = num + numblocks;
449 if (!wsize || !wNAF_len || !wNAF || !val_sub)
460 for (i = 0; i < num + num_scalar; i++)
466 num_val += (size_t)1 << (wsize[i] - 1);
468 wNAF[i] = compute_wNAF((i < num ? scalars[i] : scalar), wsize[i], &wNAF_len[i]);
471 if (wNAF_len[i] > max_len)
472 max_len = wNAF_len[i];
479 if (pre_comp == NULL)
490 signed char *tmp_wNAF = NULL;
500 wsize[
num] = pre_comp->
w;
501 tmp_wNAF = compute_wNAF(scalar, wsize[num], &tmp_len);
505 if (tmp_len <= max_len)
513 wNAF[
num] = tmp_wNAF;
514 wNAF[num + 1] = NULL;
515 wNAF_len[
num] = tmp_len;
516 if (tmp_len > max_len)
529 if (tmp_len < numblocks * blocksize)
532 numblocks = (tmp_len + blocksize - 1) / blocksize;
538 totalnum = num + numblocks;
543 tmp_points = pre_comp->
points;
545 for (i = num; i < totalnum; i++)
547 if (i < totalnum - 1)
549 wNAF_len[i] = blocksize;
550 if (tmp_len < blocksize)
555 tmp_len -= blocksize;
560 wNAF_len[i] = tmp_len;
570 memcpy(wNAF[i], pp, wNAF_len[i]);
571 if (wNAF_len[i] > max_len)
572 max_len = wNAF_len[i];
574 if (*tmp_points == NULL)
580 val_sub[i] = tmp_points;
581 tmp_points += pre_points_per_block;
602 for (i = 0; i < num + num_scalar; i++)
605 for (j = 0; j < ((size_t)1 << (wsize[i] - 1)); j++)
608 if (*v == NULL)
goto err;
612 if (!(v == val + num_val))
627 for (i = 0; i < num + num_scalar; i++)
640 if (!
EC_POINT_dbl(group, tmp, val_sub[i][0], ctx))
goto err;
641 for (j = 1; j < ((size_t)1 << (wsize[i] - 1)); j++)
643 if (!
EC_POINT_add(group, val_sub[i][j], val_sub[i][j - 1], tmp, ctx))
goto err;
653 r_is_at_infinity = 1;
655 for (k = max_len - 1; k >= 0; k--)
657 if (!r_is_at_infinity)
662 for (i = 0; i < totalnum; i++)
664 if (wNAF_len[i] > (
size_t)k)
666 int digit = wNAF[i][k];
676 if (is_neg != r_is_inverted)
678 if (!r_is_at_infinity)
682 r_is_inverted = !r_is_inverted;
687 if (r_is_at_infinity)
690 r_is_at_infinity = 0;
694 if (!
EC_POINT_add(group, r, r, val_sub[i][digit >> 1], ctx))
goto err;
701 if (r_is_at_infinity)
720 if (wNAF_len != NULL)
726 for (w = wNAF; *w != NULL; w++)
733 for (v = val; *v != NULL; v++)
768 EC_POINT *tmp_point = NULL, *base = NULL, **var;
771 size_t i,
bits, w, pre_points_per_block, blocksize, numblocks,
num;
779 if ((pre_comp = ec_pre_comp_new(group)) == NULL)
783 if (generator == NULL)
798 if (order == NULL)
goto err;
823 numblocks = (bits + blocksize - 1) / blocksize;
825 pre_points_per_block = (size_t)1 << (w - 1);
826 num = pre_points_per_block * numblocks;
837 for (i = 0; i <
num; i++)
856 for (i = 0; i < numblocks; i++)
866 for (j = 1; j < pre_points_per_block; j++, var++)
869 if (!
EC_POINT_add(group, *var, tmp_point, *(var - 1), ctx))
873 if (i < numblocks - 1)
886 for (k = 2; k < blocksize; k++)
897 pre_comp->
group = group;
901 pre_comp->
points = points;
906 ec_pre_comp_dup, ec_pre_comp_free, ec_pre_comp_clear_free))
917 ec_pre_comp_free(pre_comp);
922 for (p = points; *p != NULL; p++)