1 /* crypto/ec/ec_mult.c */
2 /* ====================================================================
3 * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
31 * 6. Redistributions of any form whatsoever must retain the following
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 * ====================================================================
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).
56 #include <openssl/err.h>
61 /* TODO: optional precomputation of multiples of the generator */
66 * wNAF-based interleaving multi-exponentation method
67 * (<URL:http://www.informatik.tu-darmstadt.de/TI/Mitarbeiter/moeller.html#multiexp>)
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.
78 static signed char *compute_wNAF(const BIGNUM
*scalar
, int w
, size_t *ret_len
, BN_CTX
*ctx
)
82 signed char *r
= NULL
;
84 int bit
, next_bit
, mask
;
89 if (c
== NULL
) goto err
;
91 if (w
<= 0 || w
> 7) /* 'signed char' can represent integers with absolute values less than 2^7 */
93 ECerr(EC_F_COMPUTE_WNAF
, ERR_R_INTERNAL_ERROR
);
96 bit
= 1 << w
; /* at most 128 */
97 next_bit
= bit
<< 1; /* at most 256 */
98 mask
= next_bit
- 1; /* at most 255 */
100 if (!BN_copy(c
, scalar
)) goto err
;
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
;
112 while (!BN_is_zero(c
))
118 if (c
->d
== NULL
|| c
->top
== 0)
120 ECerr(EC_F_COMPUTE_WNAF
, ERR_R_INTERNAL_ERROR
);
128 if (!BN_add_word(c
, -u
)) goto err
;
133 if (!BN_sub_word(c
, u
)) goto err
;
136 if (u
<= -bit
|| u
>= bit
|| !(u
& 1) || c
->neg
)
138 ECerr(EC_F_COMPUTE_WNAF
, ERR_R_INTERNAL_ERROR
);
147 ECerr(EC_F_COMPUTE_WNAF
, ERR_R_INTERNAL_ERROR
);
150 if (!BN_rshift1(c
, c
)) goto err
;
155 ECerr(EC_F_COMPUTE_WNAF
, ERR_R_INTERNAL_ERROR
);
174 /* TODO: table should be optimised for the wNAF-based implementation,
175 * sometimes smaller windows will give better performance
176 * (thus the boundaries should be increased)
178 #define EC_window_bits_for_scalar_size(b) \
187 * \sum scalars[i]*points[i],
190 * in the addition if scalar != NULL
192 int EC_POINTs_mul(const EC_GROUP
*group
, EC_POINT
*r
, const BIGNUM
*scalar
,
193 size_t num
, const EC_POINT
*points
[], const BIGNUM
*scalars
[], BN_CTX
*ctx
)
195 BN_CTX
*new_ctx
= NULL
;
196 EC_POINT
*generator
= NULL
;
197 EC_POINT
*tmp
= NULL
;
201 int r_is_inverted
= 0;
202 int r_is_at_infinity
= 1;
203 size_t *wsize
= NULL
; /* individual window sizes */
204 signed char **wNAF
= NULL
; /* individual wNAFs */
205 size_t *wNAF_len
= NULL
;
208 EC_POINT
**val
= NULL
; /* precomputation */
210 EC_POINT
***val_sub
= NULL
; /* pointers to sub-arrays of 'val' */
215 generator
= EC_GROUP_get0_generator(group
);
216 if (generator
== NULL
)
218 ECerr(EC_F_EC_POINTS_MUL
, EC_R_UNDEFINED_GENERATOR
);
223 for (i
= 0; i
< num
; i
++)
225 if (group
->meth
!= points
[i
]->meth
)
227 ECerr(EC_F_EC_POINTS_MUL
, EC_R_INCOMPATIBLE_OBJECTS
);
232 totalnum
= num
+ (scalar
!= NULL
);
234 wsize
= OPENSSL_malloc(totalnum
* sizeof wsize
[0]);
235 wNAF_len
= OPENSSL_malloc(totalnum
* sizeof wNAF_len
[0]);
236 wNAF
= OPENSSL_malloc(totalnum
* sizeof wNAF
[0] + 1);
239 wNAF
[0] = NULL
; /* preliminary pivot */
241 if (wsize
== NULL
|| wNAF_len
== NULL
|| wNAF
== NULL
) goto err
;
243 /* num_val := total number of points to precompute */
245 for (i
= 0; i
< totalnum
; i
++)
249 bits
= i
< num
? BN_num_bits(scalars
[i
]) : BN_num_bits(scalar
);
250 wsize
[i
] = EC_window_bits_for_scalar_size(bits
);
251 num_val
+= 1u << (wsize
[i
] - 1);
254 /* all precomputed points go into a single array 'val',
255 * 'val_sub[i]' is a pointer to the subarray for the i-th point */
256 val
= OPENSSL_malloc((num_val
+ 1) * sizeof val
[0]);
257 if (val
== NULL
) goto err
;
258 val
[num_val
] = NULL
; /* pivot element */
260 val_sub
= OPENSSL_malloc(totalnum
* sizeof val_sub
[0]);
261 if (val_sub
== NULL
) goto err
;
263 /* allocate points for precomputation */
265 for (i
= 0; i
< totalnum
; i
++)
268 for (j
= 0; j
< (1u << (wsize
[i
] - 1)); j
++)
270 *v
= EC_POINT_new(group
);
271 if (*v
== NULL
) goto err
;
275 if (!(v
== val
+ num_val
))
277 ECerr(EC_F_EC_POINTS_MUL
, ERR_R_INTERNAL_ERROR
);
283 ctx
= new_ctx
= BN_CTX_new();
288 tmp
= EC_POINT_new(group
);
289 if (tmp
== NULL
) goto err
;
291 /* prepare precomputed values:
292 * val_sub[i][0] := points[i]
293 * val_sub[i][1] := 3 * points[i]
294 * val_sub[i][2] := 5 * points[i]
297 for (i
= 0; i
< totalnum
; i
++)
301 if (!EC_POINT_copy(val_sub
[i
][0], points
[i
])) goto err
;
305 if (!EC_POINT_copy(val_sub
[i
][0], generator
)) goto err
;
310 if (!EC_POINT_dbl(group
, tmp
, val_sub
[i
][0], ctx
)) goto err
;
311 for (j
= 1; j
< (1u << (wsize
[i
] - 1)); j
++)
313 if (!EC_POINT_add(group
, val_sub
[i
][j
], val_sub
[i
][j
- 1], tmp
, ctx
)) goto err
;
317 wNAF
[i
+ 1] = NULL
; /* make sure we always have a pivot */
318 wNAF
[i
] = compute_wNAF((i
< num
? scalars
[i
] : scalar
), wsize
[i
], &wNAF_len
[i
], ctx
);
319 if (wNAF
[i
] == NULL
) goto err
;
320 if (wNAF_len
[i
] > max_len
)
321 max_len
= wNAF_len
[i
];
324 #if 1 /* optional; EC_window_bits_for_scalar_size assumes we do this step */
325 if (!EC_POINTs_make_affine(group
, num_val
, val
, ctx
)) goto err
;
328 r_is_at_infinity
= 1;
330 for (k
= max_len
- 1; k
>= 0; k
--)
332 if (!r_is_at_infinity
)
334 if (!EC_POINT_dbl(group
, r
, r
, ctx
)) goto err
;
337 for (i
= 0; i
< totalnum
; i
++)
339 if (wNAF_len
[i
] > (size_t)k
)
341 int digit
= wNAF
[i
][k
];
351 if (is_neg
!= r_is_inverted
)
353 if (!r_is_at_infinity
)
355 if (!EC_POINT_invert(group
, r
, ctx
)) goto err
;
357 r_is_inverted
= !r_is_inverted
;
362 if (r_is_at_infinity
)
364 if (!EC_POINT_copy(r
, val_sub
[i
][digit
>> 1])) goto err
;
365 r_is_at_infinity
= 0;
369 if (!EC_POINT_add(group
, r
, r
, val_sub
[i
][digit
>> 1], ctx
)) goto err
;
376 if (r_is_at_infinity
)
378 if (!EC_POINT_set_to_infinity(group
, r
)) goto err
;
383 if (!EC_POINT_invert(group
, r
, ctx
)) goto err
;
390 BN_CTX_free(new_ctx
);
395 if (wNAF_len
!= NULL
)
396 OPENSSL_free(wNAF_len
);
401 for (w
= wNAF
; *w
!= NULL
; w
++)
408 for (v
= val
; *v
!= NULL
; v
++)
409 EC_POINT_clear_free(*v
);
415 OPENSSL_free(val_sub
);
423 * Basic interleaving multi-exponentation method
428 #define EC_window_bits_for_scalar_size(b) \
435 /* For window size 'w' (w >= 2), we compute the odd multiples
437 * This accounts for 2^(w-1) point additions (neglecting constants),
438 * each of which requires 16 field multiplications (4 squarings
439 * and 12 general multiplications) in the case of curves defined
440 * over GF(p), which are the only curves we have so far.
442 * Converting these precomputed points into affine form takes
443 * three field multiplications for inverting Z and one squaring
444 * and three multiplications for adjusting X and Y, i.e.
445 * 7 multiplications in total (1 squaring and 6 general multiplications),
446 * again except for constants.
448 * The average number of windows for a 'b' bit scalar is roughly
450 * Each of these windows (except possibly for the first one, but
451 * we are ignoring constants anyway) requires one point addition.
452 * As the precomputed table stores points in affine form, these
453 * additions take only 11 field multiplications each (3 squarings
454 * and 8 general multiplications).
456 * So the total workload, except for constants, is
458 * 2^(w-1)*[5 squarings + 18 multiplications]
459 * + (b/(w+1))*[3 squarings + 8 multiplications]
461 * If we assume that 10 squarings are as costly as 9 multiplications,
462 * our task is to find the 'w' that, given 'b', minimizes
464 * 2^(w-1)*(5*9 + 18*10) + (b/(w+1))*(3*9 + 8*10)
465 * = 2^(w-1)*225 + (b/(w+1))*107.
467 * Thus optimal window sizes should be roughly as follows:
469 * w >= 6 if b >= 1414
470 * w = 5 if 1413 >= b >= 505
471 * w = 4 if 504 >= b >= 169
472 * w = 3 if 168 >= b >= 51
473 * w = 2 if 50 >= b >= 13
476 * If we assume instead that squarings are exactly as costly as
477 * multiplications, we have to minimize
478 * 2^(w-1)*23 + (b/(w+1))*11.
480 * This gives us the following (nearly unchanged) table of optimal
483 * w >= 6 if b >= 1406
484 * w = 5 if 1405 >= b >= 502
485 * w = 4 if 501 >= b >= 168
486 * w = 3 if 167 >= b >= 51
487 * w = 2 if 50 >= b >= 13
490 * Note that neither table tries to take into account memory usage
491 * (allocation overhead, code locality etc.). Actual timings with
492 * NIST curves P-192, P-224, and P-256 with scalars of 192, 224,
493 * and 256 bits, respectively, show that w = 3 (instead of 4) is
494 * preferrable; timings with NIST curve P-384 and 384-bit scalars
495 * confirm that w = 4 is optimal for this case; and timings with
496 * NIST curve P-521 and 521-bit scalars show that w = 4 (instead
497 * of 5) is preferrable. So we generously round up all the
498 * boundaries and use the following table:
500 * w >= 6 if b >= 2000
501 * w = 5 if 1999 >= b >= 800
502 * w = 4 if 799 >= b >= 300
503 * w = 3 if 299 >= b >= 70
504 * w = 2 if 69 >= b >= 20
508 int EC_POINTs_mul(const EC_GROUP
*group
, EC_POINT
*r
, const BIGNUM
*scalar
,
509 size_t num
, const EC_POINT
*points
[], const BIGNUM
*scalars
[], BN_CTX
*ctx
)
511 BN_CTX
*new_ctx
= NULL
;
512 EC_POINT
*generator
= NULL
;
513 EC_POINT
*tmp
= NULL
;
517 int r_is_at_infinity
= 1;
519 size_t *wsize
= NULL
; /* individual window sizes */
520 unsigned long *wbits
= NULL
; /* individual window contents */
521 int *wpos
= NULL
; /* position of bottom bit of current individual windows
522 * (wpos[i] is valid if wbits[i] != 0) */
524 EC_POINT
**val
= NULL
; /* precomputation */
526 EC_POINT
***val_sub
= NULL
; /* pointers to sub-arrays of 'val' */
531 generator
= EC_GROUP_get0_generator(group
);
532 if (generator
== NULL
)
534 ECerr(EC_F_EC_POINTS_MUL
, EC_R_UNDEFINED_GENERATOR
);
539 for (i
= 0; i
< num
; i
++)
541 if (group
->meth
!= points
[i
]->meth
)
543 ECerr(EC_F_EC_POINTS_MUL
, EC_R_INCOMPATIBLE_OBJECTS
);
548 totalnum
= num
+ (scalar
!= NULL
);
550 wsize
= OPENSSL_malloc(totalnum
* sizeof wsize
[0]);
551 wbits
= OPENSSL_malloc(totalnum
* sizeof wbits
[0]);
552 wpos
= OPENSSL_malloc(totalnum
* sizeof wpos
[0]);
553 if (wsize
== NULL
|| wbits
== NULL
|| wpos
== NULL
) goto err
;
555 /* num_val := total number of points to precompute */
557 for (i
= 0; i
< totalnum
; i
++)
561 bits
= i
< num
? BN_num_bits(scalars
[i
]) : BN_num_bits(scalar
);
562 wsize
[i
] = EC_window_bits_for_scalar_size(bits
);
563 num_val
+= 1u << (wsize
[i
] - 1);
570 /* all precomputed points go into a single array 'val',
571 * 'val_sub[i]' is a pointer to the subarray for the i-th point */
572 val
= OPENSSL_malloc((num_val
+ 1) * sizeof val
[0]);
573 if (val
== NULL
) goto err
;
574 val
[num_val
] = NULL
; /* pivot element */
576 val_sub
= OPENSSL_malloc(totalnum
* sizeof val_sub
[0]);
577 if (val_sub
== NULL
) goto err
;
579 /* allocate points for precomputation */
581 for (i
= 0; i
< totalnum
; i
++)
584 for (j
= 0; j
< (1u << (wsize
[i
] - 1)); j
++)
586 *v
= EC_POINT_new(group
);
587 if (*v
== NULL
) goto err
;
591 if (!(v
== val
+ num_val
))
593 ECerr(EC_F_EC_POINTS_MUL
, ERR_R_INTERNAL_ERROR
);
599 ctx
= new_ctx
= BN_CTX_new();
604 tmp
= EC_POINT_new(group
);
605 if (tmp
== NULL
) goto err
;
607 /* prepare precomputed values:
608 * val_sub[i][0] := points[i]
609 * val_sub[i][1] := 3 * points[i]
610 * val_sub[i][2] := 5 * points[i]
613 for (i
= 0; i
< totalnum
; i
++)
617 if (!EC_POINT_copy(val_sub
[i
][0], points
[i
])) goto err
;
620 if (!EC_POINT_invert(group
, val_sub
[i
][0], ctx
)) goto err
;
625 if (!EC_POINT_copy(val_sub
[i
][0], generator
)) goto err
;
628 if (!EC_POINT_invert(group
, val_sub
[i
][0], ctx
)) goto err
;
634 if (!EC_POINT_dbl(group
, tmp
, val_sub
[i
][0], ctx
)) goto err
;
635 for (j
= 1; j
< (1u << (wsize
[i
] - 1)); j
++)
637 if (!EC_POINT_add(group
, val_sub
[i
][j
], val_sub
[i
][j
- 1], tmp
, ctx
)) goto err
;
642 #if 1 /* optional; EC_window_bits_for_scalar_size assumes we do this step */
643 if (!EC_POINTs_make_affine(group
, num_val
, val
, ctx
)) goto err
;
646 r_is_at_infinity
= 1;
648 for (k
= max_bits
- 1; k
>= 0; k
--)
650 if (!r_is_at_infinity
)
652 if (!EC_POINT_dbl(group
, r
, r
, ctx
)) goto err
;
655 for (i
= 0; i
< totalnum
; i
++)
661 s
= i
< num
? scalars
[i
] : scalar
;
663 if (BN_is_bit_set(s
, k
))
665 /* look at bits k - wsize[i] + 1 .. k for this window */
666 t
= k
- wsize
[i
] + 1;
667 while (!BN_is_bit_set(s
, t
)) /* BN_is_bit_set is false for t < 0 */
671 for (t
= k
- 1; t
>= wpos
[i
]; t
--)
674 if (BN_is_bit_set(s
, t
))
677 /* now wbits[i] is the odd bit pattern at bits wpos[i] .. k */
681 if ((wbits
[i
] != 0) && (wpos
[i
] == k
))
683 if (r_is_at_infinity
)
685 if (!EC_POINT_copy(r
, val_sub
[i
][wbits
[i
] >> 1])) goto err
;
686 r_is_at_infinity
= 0;
690 if (!EC_POINT_add(group
, r
, r
, val_sub
[i
][wbits
[i
] >> 1], ctx
)) goto err
;
697 if (r_is_at_infinity
)
698 if (!EC_POINT_set_to_infinity(group
, r
)) goto err
;
704 BN_CTX_free(new_ctx
);
715 for (v
= val
; *v
!= NULL
; v
++)
716 EC_POINT_clear_free(*v
);
722 OPENSSL_free(val_sub
);
729 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
)
731 const EC_POINT
*points
[1];
732 const BIGNUM
*scalars
[1];
735 scalars
[0] = p_scalar
;
737 return EC_POINTs_mul(group
, r
, g_scalar
, (point
!= NULL
&& p_scalar
!= NULL
), points
, scalars
, ctx
);
741 int EC_GROUP_precompute_mult(EC_GROUP
*group
, BN_CTX
*ctx
)
743 const EC_POINT
*generator
;
744 BN_CTX
*new_ctx
= NULL
;
748 generator
= EC_GROUP_get0_generator(group
);
749 if (generator
== NULL
)
751 ECerr(EC_F_EC_GROUP_PRECOMPUTE_MULT
, EC_R_UNDEFINED_GENERATOR
);
757 ctx
= new_ctx
= BN_CTX_new();
763 order
= BN_CTX_get(ctx
);
764 if (order
== NULL
) goto err
;
766 if (!EC_GROUP_get_order(group
, order
, ctx
)) return 0;
767 if (BN_is_zero(order
))
769 ECerr(EC_F_EC_GROUP_PRECOMPUTE_MULT
, EC_R_UNKNOWN_ORDER
);
780 BN_CTX_free(new_ctx
);