60429e0d1e78372a9b93b0d2f574f4b54403e6f1
[openssl.git] / crypto / ec / ec_mult.c
1 /* crypto/ec/ec_mult.c */
2 /* ====================================================================
3  * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer. 
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in
14  *    the documentation and/or other materials provided with the
15  *    distribution.
16  *
17  * 3. All advertising materials mentioning features or use of this
18  *    software must display the following acknowledgment:
19  *    "This product includes software developed by the OpenSSL Project
20  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21  *
22  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23  *    endorse or promote products derived from this software without
24  *    prior written permission. For written permission, please contact
25  *    openssl-core@openssl.org.
26  *
27  * 5. Products derived from this software may not be called "OpenSSL"
28  *    nor may "OpenSSL" appear in their names without prior written
29  *    permission of the OpenSSL Project.
30  *
31  * 6. Redistributions of any form whatsoever must retain the following
32  *    acknowledgment:
33  *    "This product includes software developed by the OpenSSL Project
34  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35  *
36  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47  * OF THE POSSIBILITY OF SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This product includes cryptographic software written by Eric Young
51  * (eay@cryptsoft.com).  This product includes software written by Tim
52  * Hudson (tjh@cryptsoft.com).
53  *
54  */
55
56 #include <openssl/err.h>
57
58 #include "ec_lcl.h"
59
60
61 /* TODO: optional precomputation of multiples of the generator */
62
63
64 #if 1
65 /*
66  * wNAF-based interleaving multi-exponentation method
67  */
68
69
70
71 /* Determine the width-(w+1) Non-Adjacent Form of 'scalar'.
72  * This is an array  r[]  of values that are either zero or odd with an
73  * absolute value less than  2^w  satisfying
74  *     scalar = \sum_j r[j]*2^j
75  * where at most one of any  w+1  consecutive digits is non-zero.
76  */
77 static signed char *compute_wNAF(const BIGNUM *scalar, int w, size_t *ret_len, BN_CTX *ctx)
78         {
79         BIGNUM *c;
80         int ok = 0;
81         signed char *r = NULL;
82         int sign = 1;
83         int bit, next_bit, mask;
84         size_t len, j;
85         
86         BN_CTX_start(ctx);
87         c = BN_CTX_get(ctx);
88         if (c == NULL) goto err;
89         
90         if (w <= 0 || w > 7) /* 'unsigned char' can represent integers with absolute values less than 2^7 */
91                 {
92                 ECerr(EC_F_COMPUTE_WNAF, ERR_R_INTERNAL_ERROR);
93                 goto err;
94                 }
95         bit = 1 << w; /* at most 128 */
96         next_bit = bit << 1; /* at most 256 */
97         mask = next_bit - 1; /* at most 255 */
98
99         if (!BN_copy(c, scalar)) goto err;
100         if (c->neg)
101                 {
102                 sign = -1;
103                 c->neg = 0;
104                 }
105
106         len = BN_num_bits(c) + 1; /* wNAF may be one digit longer than binary representation */
107         r = OPENSSL_malloc(len);
108         if (r == NULL) goto err;
109
110         j = 0;
111         while (!BN_is_zero(c))
112                 {
113                 int u = 0;
114
115                 if (BN_is_odd(c)) 
116                         {
117                         if (c->d == NULL || c->top == 0)
118                                 {
119                                 ECerr(EC_F_COMPUTE_WNAF, ERR_R_INTERNAL_ERROR);
120                                 goto err;
121                                 }
122                         u = c->d[0] & mask;
123                         if (u & bit)
124                                 {
125                                 u -= next_bit;
126                                 /* u < 0 */
127                                 if (!BN_add_word(c, -u)) goto err;
128                                 }
129                         else
130                                 {
131                                 /* u > 0 */
132                                 if (!BN_sub_word(c, u)) goto err;
133                                 }
134
135                         if (u <= -bit || u >= bit || !(u & 1) || c->neg)
136                                 {
137                                 ECerr(EC_F_COMPUTE_WNAF, ERR_R_INTERNAL_ERROR);
138                                 goto err;
139                                 }
140                         }
141
142                 r[j++] = sign * u;
143                 
144                 if (BN_is_odd(c))
145                         {
146                         ECerr(EC_F_COMPUTE_WNAF, ERR_R_INTERNAL_ERROR);
147                         goto err;
148                         }
149                 if (!BN_rshift1(c, c)) goto err;
150                 }
151
152         if (j > len)
153                 {
154                 ECerr(EC_F_COMPUTE_WNAF, ERR_R_INTERNAL_ERROR);
155                 goto err;
156                 }
157         len = j;
158         ok = 1;
159
160  err:
161         BN_CTX_end(ctx);
162         if (!ok)
163                 {
164                 OPENSSL_free(r);
165                 r = NULL;
166                 }
167         if (ok)
168                 *ret_len = len;
169         return r;
170         }
171
172
173 /* TODO: table should be optimised for the wNAF-based implementation */
174 #define EC_window_bits_for_scalar_size(b) \
175                 ((b) >= 2000 ? 6 : \
176                  (b) >=  800 ? 5 : \
177                  (b) >=  300 ? 4 : \
178                  (b) >=   70 ? 3 : \
179                  (b) >=   20 ? 2 : \
180                   1)
181
182 /* Compute
183  *      \sum scalars[i]*points[i],
184  * also including
185  *      scalar*generator
186  * in the addition if scalar != NULL
187  */
188 int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
189         size_t num, const EC_POINT *points[], const BIGNUM *scalars[], BN_CTX *ctx)
190         {
191         BN_CTX *new_ctx = NULL;
192         EC_POINT *generator = NULL;
193         EC_POINT *tmp = NULL;
194         size_t totalnum;
195         size_t i, j;
196         int k;
197         int r_is_inverted = 0;
198         int r_is_at_infinity = 1;
199         size_t *wsize = NULL; /* individual window sizes */
200         size_t *wNAF_len = NULL;
201         size_t max_len = 0;
202         signed char **wNAF = NULL; /* individual wNAFs */
203         size_t num_val;
204         EC_POINT **val = NULL; /* precomputation */
205         EC_POINT **v;
206         EC_POINT ***val_sub = NULL; /* pointers to sub-arrays of 'val' */
207         int ret = 0;
208         
209         if (scalar != NULL)
210                 {
211                 generator = EC_GROUP_get0_generator(group);
212                 if (generator == NULL)
213                         {
214                         ECerr(EC_F_EC_POINTS_MUL, EC_R_UNDEFINED_GENERATOR);
215                         return 0;
216                         }
217                 }
218         
219         for (i = 0; i < num; i++)
220                 {
221                 if (group->meth != points[i]->meth)
222                         {
223                         ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);
224                         return 0;
225                         }
226                 }
227
228         totalnum = num + (scalar != NULL);
229
230         wsize = OPENSSL_malloc(totalnum * sizeof wsize[0]);
231         wNAF_len = OPENSSL_malloc(totalnum * sizeof wNAF_len[0]);
232         wNAF = OPENSSL_malloc(totalnum * sizeof wNAF[0] + 1);
233         if (wNAF != NULL)
234                 {
235                 wNAF[0] = NULL; /* preliminary pivot */
236                 }
237         if (wsize == NULL || wNAF_len == NULL || wNAF == NULL) goto err;
238
239         /* num_val := total number of points to precompute */
240         num_val = 0;
241         for (i = 0; i < totalnum; i++)
242                 {
243                 size_t bits;
244
245                 bits = i < num ? BN_num_bits(scalars[i]) : BN_num_bits(scalar);
246                 wsize[i] = EC_window_bits_for_scalar_size(bits);
247                 num_val += 1u << (wsize[i] - 1);
248                 }
249
250         /* all precomputed points go into a single array 'val',
251          * 'val_sub[i]' is a pointer to the subarray for the i-th point */
252         val = OPENSSL_malloc((num_val + 1) * sizeof val[0]);
253         if (val == NULL) goto err;
254         val[num_val] = NULL; /* pivot element */
255
256         val_sub = OPENSSL_malloc(totalnum * sizeof val_sub[0]);
257         if (val_sub == NULL) goto err;
258
259         /* allocate points for precomputation */
260         v = val;
261         for (i = 0; i < totalnum; i++)
262                 {
263                 val_sub[i] = v;
264                 for (j = 0; j < (1u << (wsize[i] - 1)); j++)
265                         {
266                         *v = EC_POINT_new(group);
267                         if (*v == NULL) goto err;
268                         v++;
269                         }
270                 }
271         if (!(v == val + num_val))
272                 {
273                 ECerr(EC_F_EC_POINTS_MUL, ERR_R_INTERNAL_ERROR);
274                 goto err;
275                 }
276
277         if (ctx == NULL)
278                 {
279                 ctx = new_ctx = BN_CTX_new();
280                 if (ctx == NULL)
281                         goto err;
282                 }
283         
284         tmp = EC_POINT_new(group);
285         if (tmp == NULL) goto err;
286
287         /* prepare precomputed values:
288          *    val_sub[i][0] :=     points[i]
289          *    val_sub[i][1] := 3 * points[i]
290          *    val_sub[i][2] := 5 * points[i]
291          *    ...
292          */
293         for (i = 0; i < totalnum; i++)
294                 {
295                 if (i < num)
296                         {
297                         if (!EC_POINT_copy(val_sub[i][0], points[i])) goto err;
298                         }
299                 else
300                         {
301                         if (!EC_POINT_copy(val_sub[i][0], generator)) goto err;
302                         }
303
304                 if (wsize[i] > 1)
305                         {
306                         if (!EC_POINT_dbl(group, tmp, val_sub[i][0], ctx)) goto err;
307                         for (j = 1; j < (1u << (wsize[i] - 1)); j++)
308                                 {
309                                 if (!EC_POINT_add(group, val_sub[i][j], val_sub[i][j - 1], tmp, ctx)) goto err;
310                                 }
311                         }
312
313                 wNAF[i + 1] = NULL; /* make sure we always have a pivot */
314                 wNAF[i] = compute_wNAF((i < num ? scalars[i] : scalar), wsize[i], &wNAF_len[i], ctx);
315                 if (wNAF[i] == NULL) goto err;
316                 if (wNAF_len[i] > max_len)
317                         max_len = wNAF_len[i];
318                 }
319
320 #if 1 /* optional; EC_window_bits_for_scalar_size assumes we do this step */
321         if (!EC_POINTs_make_affine(group, num_val, val, ctx)) goto err;
322 #endif
323
324         r_is_at_infinity = 1;
325
326         for (k = max_len - 1; k >= 0; k--)
327                 {
328                 if (!r_is_at_infinity)
329                         {
330                         if (!EC_POINT_dbl(group, r, r, ctx)) goto err;
331                         }
332                 
333                 for (i = 0; i < totalnum; i++)
334                         {
335                         if (wNAF_len[i] > k)
336                                 {
337                                 int digit = wNAF[i][k];
338                                 int is_neg;
339
340                                 if (digit) 
341                                         {
342                                         is_neg = digit < 0;
343
344                                         if (is_neg)
345                                                 digit = -digit;
346
347                                         if (is_neg != r_is_inverted)
348                                                 {
349                                                 if (!r_is_at_infinity)
350                                                         {
351                                                         if (!EC_POINT_invert(group, r, ctx)) goto err;
352                                                         }
353                                                 r_is_inverted = !r_is_inverted;
354                                                 }
355
356                                         /* digit > 0 */
357
358                                         if (r_is_at_infinity)
359                                                 {
360                                                 if (!EC_POINT_copy(r, val_sub[i][digit >> 1])) goto err;
361                                                 r_is_at_infinity = 0;
362                                                 }
363                                         else
364                                                 {
365                                                 if (!EC_POINT_add(group, r, r, val_sub[i][digit >> 1], ctx)) goto err;
366                                                 }
367                                         }
368                                 }
369                         }
370                 }
371
372         if (r_is_at_infinity)
373                 {
374                 if (!EC_POINT_set_to_infinity(group, r)) goto err;
375                 }
376         else
377                 {
378                 if (r_is_inverted)
379                         if (!EC_POINT_invert(group, r, ctx)) goto err;
380                 }
381         
382         ret = 1;
383
384  err:
385         if (new_ctx != NULL)
386                 BN_CTX_free(new_ctx);
387         if (tmp != NULL)
388                 EC_POINT_free(tmp);
389         if (wsize != NULL)
390                 OPENSSL_free(wsize);
391         if (wNAF_len != NULL)
392                 OPENSSL_free(wNAF_len);
393         if (wNAF != NULL)
394                 {
395                 signed char **w;
396                 
397                 for (w = wNAF; *w != NULL; w++)
398                         OPENSSL_free(*w);
399                 
400                 OPENSSL_free(wNAF);
401                 }
402         if (val != NULL)
403                 {
404                 for (v = val; *v != NULL; v++)
405                         EC_POINT_clear_free(*v);
406
407                 OPENSSL_free(val);
408                 }
409         if (val_sub != NULL)
410                 {
411                 OPENSSL_free(val_sub);
412                 }
413         return ret;
414         }
415
416 #else
417
418 /*
419  * Basic interleaving multi-exponentation method
420  */
421
422
423
424 #define EC_window_bits_for_scalar_size(b) \
425                 ((b) >= 2000 ? 6 : \
426                  (b) >=  800 ? 5 : \
427                  (b) >=  300 ? 4 : \
428                  (b) >=   70 ? 3 : \
429                  (b) >=   20 ? 2 : \
430                   1)
431 /* For window size 'w' (w >= 2), we compute the odd multiples
432  *      1*P .. (2^w-1)*P.
433  * This accounts for  2^(w-1)  point additions (neglecting constants),
434  * each of which requires 16 field multiplications (4 squarings
435  * and 12 general multiplications) in the case of curves defined
436  * over GF(p), which are the only curves we have so far.
437  *
438  * Converting these precomputed points into affine form takes
439  * three field multiplications for inverting Z and one squaring
440  * and three multiplications for adjusting X and Y, i.e.
441  * 7 multiplications in total (1 squaring and 6 general multiplications),
442  * again except for constants.
443  *
444  * The average number of windows for a 'b' bit scalar is roughly
445  *          b/(w+1).
446  * Each of these windows (except possibly for the first one, but
447  * we are ignoring constants anyway) requires one point addition.
448  * As the precomputed table stores points in affine form, these
449  * additions take only 11 field multiplications each (3 squarings
450  * and 8 general multiplications).
451  *
452  * So the total workload, except for constants, is
453  *
454  *        2^(w-1)*[5 squarings + 18 multiplications]
455  *      + (b/(w+1))*[3 squarings + 8 multiplications]
456  *
457  * If we assume that 10 squarings are as costly as 9 multiplications,
458  * our task is to find the 'w' that, given 'b', minimizes
459  *
460  *        2^(w-1)*(5*9 + 18*10) + (b/(w+1))*(3*9 + 8*10)
461  *      = 2^(w-1)*225 +           (b/(w+1))*107.
462  *
463  * Thus optimal window sizes should be roughly as follows:
464  *
465  *    w >= 6  if         b >= 1414
466  *     w = 5  if 1413 >= b >=  505
467  *     w = 4  if  504 >= b >=  169
468  *     w = 3  if  168 >= b >=   51
469  *     w = 2  if   50 >= b >=   13
470  *     w = 1  if   12 >= b
471  *
472  * If we assume instead that squarings are exactly as costly as
473  * multiplications, we have to minimize
474  *      2^(w-1)*23 + (b/(w+1))*11.
475  *
476  * This gives us the following (nearly unchanged) table of optimal
477  * windows sizes:
478  *
479  *    w >= 6  if         b >= 1406
480  *     w = 5  if 1405 >= b >=  502
481  *     w = 4  if  501 >= b >=  168
482  *     w = 3  if  167 >= b >=   51
483  *     w = 2  if   50 >= b >=   13
484  *     w = 1  if   12 >= b
485  *
486  * Note that neither table tries to take into account memory usage
487  * (allocation overhead, code locality etc.).  Actual timings with
488  * NIST curves P-192, P-224, and P-256 with scalars of 192, 224,
489  * and 256 bits, respectively, show that  w = 3  (instead of 4) is
490  * preferrable; timings with NIST curve P-384 and 384-bit scalars
491  * confirm that  w = 4  is optimal for this case; and timings with
492  * NIST curve P-521 and 521-bit scalars show that  w = 4  (instead
493  * of 5) is preferrable.  So we generously round up all the
494  * boundaries and use the following table:
495  *
496  *    w >= 6  if         b >= 2000
497  *     w = 5  if 1999 >= b >=  800
498  *     w = 4  if  799 >= b >=  300
499  *     w = 3  if  299 >= b >=   70
500  *     w = 2  if   69 >= b >=   20
501  *     w = 1  if   19 >= b
502  */
503
504 int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
505         size_t num, const EC_POINT *points[], const BIGNUM *scalars[], BN_CTX *ctx)
506         {
507         BN_CTX *new_ctx = NULL;
508         EC_POINT *generator = NULL;
509         EC_POINT *tmp = NULL;
510         size_t totalnum;
511         size_t i, j;
512         int k, t;
513         int r_is_at_infinity = 1;
514         size_t max_bits = 0;
515         size_t *wsize = NULL; /* individual window sizes */
516         unsigned long *wbits = NULL; /* individual window contents */
517         int *wpos = NULL; /* position of bottom bit of current individual windows
518                            * (wpos[i] is valid if wbits[i] != 0) */
519         size_t num_val;
520         EC_POINT **val = NULL; /* precomputation */
521         EC_POINT **v;
522         EC_POINT ***val_sub = NULL; /* pointers to sub-arrays of 'val' */
523         int ret = 0;
524         
525         if (scalar != NULL)
526                 {
527                 generator = EC_GROUP_get0_generator(group);
528                 if (generator == NULL)
529                         {
530                         ECerr(EC_F_EC_POINTS_MUL, EC_R_UNDEFINED_GENERATOR);
531                         return 0;
532                         }
533                 }
534         
535         for (i = 0; i < num; i++)
536                 {
537                 if (group->meth != points[i]->meth)
538                         {
539                         ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);
540                         return 0;
541                         }
542                 }
543
544         totalnum = num + (scalar != NULL);
545
546         wsize = OPENSSL_malloc(totalnum * sizeof wsize[0]);
547         wbits = OPENSSL_malloc(totalnum * sizeof wbits[0]);
548         wpos = OPENSSL_malloc(totalnum * sizeof wpos[0]);
549         if (wsize == NULL || wbits == NULL || wpos == NULL) goto err;
550
551         /* num_val := total number of points to precompute */
552         num_val = 0;
553         for (i = 0; i < totalnum; i++)
554                 {
555                 size_t bits;
556
557                 bits = i < num ? BN_num_bits(scalars[i]) : BN_num_bits(scalar);
558                 wsize[i] = EC_window_bits_for_scalar_size(bits);
559                 num_val += 1u << (wsize[i] - 1);
560                 if (bits > max_bits)
561                         max_bits = bits;
562                 wbits[i] = 0;
563                 wpos[i] = 0;
564                 }
565
566         /* all precomputed points go into a single array 'val',
567          * 'val_sub[i]' is a pointer to the subarray for the i-th point */
568         val = OPENSSL_malloc((num_val + 1) * sizeof val[0]);
569         if (val == NULL) goto err;
570         val[num_val] = NULL; /* pivot element */
571
572         val_sub = OPENSSL_malloc(totalnum * sizeof val_sub[0]);
573         if (val_sub == NULL) goto err;
574
575         /* allocate points for precomputation */
576         v = val;
577         for (i = 0; i < totalnum; i++)
578                 {
579                 val_sub[i] = v;
580                 for (j = 0; j < (1u << (wsize[i] - 1)); j++)
581                         {
582                         *v = EC_POINT_new(group);
583                         if (*v == NULL) goto err;
584                         v++;
585                         }
586                 }
587         if (!(v == val + num_val))
588                 {
589                 ECerr(EC_F_EC_POINTS_MUL, ERR_R_INTERNAL_ERROR);
590                 goto err;
591                 }
592
593         if (ctx == NULL)
594                 {
595                 ctx = new_ctx = BN_CTX_new();
596                 if (ctx == NULL)
597                         goto err;
598                 }
599         
600         tmp = EC_POINT_new(group);
601         if (tmp == NULL) goto err;
602
603         /* prepare precomputed values:
604          *    val_sub[i][0] :=     points[i]
605          *    val_sub[i][1] := 3 * points[i]
606          *    val_sub[i][2] := 5 * points[i]
607          *    ...
608          */
609         for (i = 0; i < totalnum; i++)
610                 {
611                 if (i < num)
612                         {
613                         if (!EC_POINT_copy(val_sub[i][0], points[i])) goto err;
614                         if (scalars[i]->neg)
615                                 {
616                                 if (!EC_POINT_invert(group, val_sub[i][0], ctx)) goto err;
617                                 }
618                         }
619                 else
620                         {
621                         if (!EC_POINT_copy(val_sub[i][0], generator)) goto err;
622                         if (scalar->neg)
623                                 {
624                                 if (!EC_POINT_invert(group, val_sub[i][0], ctx)) goto err;
625                                 }
626                         }
627
628                 if (wsize[i] > 1)
629                         {
630                         if (!EC_POINT_dbl(group, tmp, val_sub[i][0], ctx)) goto err;
631                         for (j = 1; j < (1u << (wsize[i] - 1)); j++)
632                                 {
633                                 if (!EC_POINT_add(group, val_sub[i][j], val_sub[i][j - 1], tmp, ctx)) goto err;
634                                 }
635                         }
636                 }
637
638 #if 1 /* optional; EC_window_bits_for_scalar_size assumes we do this step */
639         if (!EC_POINTs_make_affine(group, num_val, val, ctx)) goto err;
640 #endif
641
642         r_is_at_infinity = 1;
643
644         for (k = max_bits - 1; k >= 0; k--)
645                 {
646                 if (!r_is_at_infinity)
647                         {
648                         if (!EC_POINT_dbl(group, r, r, ctx)) goto err;
649                         }
650                 
651                 for (i = 0; i < totalnum; i++)
652                         {
653                         if (wbits[i] == 0)
654                                 {
655                                 const BIGNUM *s;
656
657                                 s = i < num ? scalars[i] : scalar;
658
659                                 if (BN_is_bit_set(s, k))
660                                         {
661                                         /* look at bits  k - wsize[i] + 1 .. k  for this window */
662                                         t = k - wsize[i] + 1;
663                                         while (!BN_is_bit_set(s, t)) /* BN_is_bit_set is false for t < 0 */
664                                                 t++;
665                                         wpos[i] = t;
666                                         wbits[i] = 1;
667                                         for (t = k - 1; t >= wpos[i]; t--)
668                                                 {
669                                                 wbits[i] <<= 1;
670                                                 if (BN_is_bit_set(s, t))
671                                                         wbits[i]++;
672                                                 }
673                                         /* now wbits[i] is the odd bit pattern at bits wpos[i] .. k */
674                                         }
675                                 }
676                         
677                         if ((wbits[i] != 0) && (wpos[i] == k))
678                                 {
679                                 if (r_is_at_infinity)
680                                         {
681                                         if (!EC_POINT_copy(r, val_sub[i][wbits[i] >> 1])) goto err;
682                                         r_is_at_infinity = 0;
683                                         }
684                                 else
685                                         {
686                                         if (!EC_POINT_add(group, r, r, val_sub[i][wbits[i] >> 1], ctx)) goto err;
687                                         }
688                                 wbits[i] = 0;
689                                 }
690                         }
691                 }
692
693         if (r_is_at_infinity)
694                 if (!EC_POINT_set_to_infinity(group, r)) goto err;
695         
696         ret = 1;
697
698  err:
699         if (new_ctx != NULL)
700                 BN_CTX_free(new_ctx);
701         if (tmp != NULL)
702                 EC_POINT_free(tmp);
703         if (wsize != NULL)
704                 OPENSSL_free(wsize);
705         if (wbits != NULL)
706                 OPENSSL_free(wbits);
707         if (wpos != NULL)
708                 OPENSSL_free(wpos);
709         if (val != NULL)
710                 {
711                 for (v = val; *v != NULL; v++)
712                         EC_POINT_clear_free(*v);
713
714                 OPENSSL_free(val);
715                 }
716         if (val_sub != NULL)
717                 {
718                 OPENSSL_free(val_sub);
719                 }
720         return ret;
721         }
722 #endif
723
724
725 int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar, const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
726         {
727         const EC_POINT *points[1];
728         const BIGNUM *scalars[1];
729
730         points[0] = point;
731         scalars[0] = p_scalar;
732
733         return EC_POINTs_mul(group, r, g_scalar, (point != NULL && p_scalar != NULL), points, scalars, ctx);
734         }
735
736
737 int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
738         {
739         const EC_POINT *generator;
740         BN_CTX *new_ctx = NULL;
741         BIGNUM *order;
742         int ret = 0;
743
744         generator = EC_GROUP_get0_generator(group);
745         if (generator == NULL)
746                 {
747                 ECerr(EC_F_EC_GROUP_PRECOMPUTE_MULT, EC_R_UNDEFINED_GENERATOR);
748                 return 0;
749                 }
750
751         if (ctx == NULL)
752                 {
753                 ctx = new_ctx = BN_CTX_new();
754                 if (ctx == NULL)
755                         return 0;
756                 }
757         
758         BN_CTX_start(ctx);
759         order = BN_CTX_get(ctx);
760         if (order == NULL) goto err;
761         
762         if (!EC_GROUP_get_order(group, order, ctx)) return 0;
763         if (BN_is_zero(order))
764                 {
765                 ECerr(EC_F_EC_GROUP_PRECOMPUTE_MULT, EC_R_UNKNOWN_ORDER);
766                 goto err;
767                 }
768
769         /* TODO */
770
771         ret = 1;
772         
773  err:
774         BN_CTX_end(ctx);
775         if (new_ctx != NULL)
776                 BN_CTX_free(new_ctx);
777         return ret;
778         }