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