1 /* crypto/ec/ec_mult.c */
2 /* ====================================================================
3 * Copyright (c) 1998-2002 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>)
71 /* Determine the modified width-(w+1) Non-Adjacent Form (wNAF) 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 * with the exception that the most significant digit may be only
77 * w-1 zeros away from that next non-zero digit.
79 static signed char *compute_wNAF(const BIGNUM *scalar, int w, size_t *ret_len)
83 signed char *r = NULL;
85 int bit, next_bit, mask;
88 if (w <= 0 || w > 7) /* 'signed char' can represent integers with absolute values less than 2^7 */
90 ECerr(EC_F_COMPUTE_WNAF, ERR_R_INTERNAL_ERROR);
93 bit = 1 << w; /* at most 128 */
94 next_bit = bit << 1; /* at most 256 */
95 mask = next_bit - 1; /* at most 255 */
102 len = BN_num_bits(scalar);
103 r = OPENSSL_malloc(len + 1); /* modified wNAF may be one digit longer than binary representation */
104 if (r == NULL) goto err;
106 if (scalar->d == NULL || scalar->top == 0)
108 ECerr(EC_F_COMPUTE_WNAF, ERR_R_INTERNAL_ERROR);
111 window_val = scalar->d[0] & mask;
113 while ((window_val != 0) || (j + w + 1 < len)) /* if j+w+1 >= len, window_val will not increase */
117 /* 0 <= window_val <= 2^(w+1) */
121 /* 0 < window_val < 2^(w+1) */
123 if (window_val & bit)
125 digit = window_val - next_bit; /* -2^w < digit < 0 */
127 #if 1 /* modified wNAF */
128 if (j + w + 1 >= len)
130 /* special case for generating modified wNAFs:
131 * no new bits will be added into window_val,
132 * so using a positive digit here will decrease
133 * the total length of the representation */
135 digit = window_val & (mask >> 1); /* 0 < digit < 2^w */
141 digit = window_val; /* 0 < digit < 2^w */
144 if (digit <= -bit || digit >= bit || !(digit & 1))
146 ECerr(EC_F_COMPUTE_WNAF, ERR_R_INTERNAL_ERROR);
152 /* now window_val is 0 or 2^(w+1) in standard wNAF generation;
153 * for modified window NAFs, it may also be 2^w
155 if (window_val != 0 && window_val != next_bit && window_val != bit)
157 ECerr(EC_F_COMPUTE_WNAF, ERR_R_INTERNAL_ERROR);
162 r[j++] = sign * digit;
165 window_val += bit * BN_is_bit_set(scalar, j + w);
167 if (window_val > next_bit)
169 ECerr(EC_F_COMPUTE_WNAF, ERR_R_INTERNAL_ERROR);
176 ECerr(EC_F_COMPUTE_WNAF, ERR_R_INTERNAL_ERROR);
194 /* TODO: table should be optimised for the wNAF-based implementation,
195 * sometimes smaller windows will give better performance
196 * (thus the boundaries should be increased)
198 #define EC_window_bits_for_scalar_size(b) \
207 * \sum scalars[i]*points[i],
210 * in the addition if scalar != NULL
212 int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
213 size_t num, const EC_POINT *points[], const BIGNUM *scalars[], BN_CTX *ctx)
215 BN_CTX *new_ctx = NULL;
216 EC_POINT *generator = NULL;
217 EC_POINT *tmp = NULL;
221 int r_is_inverted = 0;
222 int r_is_at_infinity = 1;
223 size_t *wsize = NULL; /* individual window sizes */
224 signed char **wNAF = NULL; /* individual wNAFs */
225 size_t *wNAF_len = NULL;
228 EC_POINT **val = NULL; /* precomputation */
230 EC_POINT ***val_sub = NULL; /* pointers to sub-arrays of 'val' */
235 generator = EC_GROUP_get0_generator(group);
236 if (generator == NULL)
238 ECerr(EC_F_EC_POINTS_MUL, EC_R_UNDEFINED_GENERATOR);
243 for (i = 0; i < num; i++)
245 if (group->meth != points[i]->meth)
247 ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);
252 totalnum = num + (scalar != NULL);
254 wsize = OPENSSL_malloc(totalnum * sizeof wsize[0]);
255 wNAF_len = OPENSSL_malloc(totalnum * sizeof wNAF_len[0]);
256 wNAF = OPENSSL_malloc((totalnum + 1) * sizeof wNAF[0]);
259 wNAF[0] = NULL; /* preliminary pivot */
261 if (wsize == NULL || wNAF_len == NULL || wNAF == NULL) goto err;
263 /* num_val := total number of points to precompute */
265 for (i = 0; i < totalnum; i++)
269 bits = i < num ? BN_num_bits(scalars[i]) : BN_num_bits(scalar);
270 wsize[i] = EC_window_bits_for_scalar_size(bits);
271 num_val += 1u << (wsize[i] - 1);
274 /* all precomputed points go into a single array 'val',
275 * 'val_sub[i]' is a pointer to the subarray for the i-th point */
276 val = OPENSSL_malloc((num_val + 1) * sizeof val[0]);
277 if (val == NULL) goto err;
278 val[num_val] = NULL; /* pivot element */
280 val_sub = OPENSSL_malloc(totalnum * sizeof val_sub[0]);
281 if (val_sub == NULL) goto err;
283 /* allocate points for precomputation */
285 for (i = 0; i < totalnum; i++)
288 for (j = 0; j < (1u << (wsize[i] - 1)); j++)
290 *v = EC_POINT_new(group);
291 if (*v == NULL) goto err;
295 if (!(v == val + num_val))
297 ECerr(EC_F_EC_POINTS_MUL, ERR_R_INTERNAL_ERROR);
303 ctx = new_ctx = BN_CTX_new();
308 tmp = EC_POINT_new(group);
309 if (tmp == NULL) goto err;
311 /* prepare precomputed values:
312 * val_sub[i][0] := points[i]
313 * val_sub[i][1] := 3 * points[i]
314 * val_sub[i][2] := 5 * points[i]
317 for (i = 0; i < totalnum; i++)
321 if (!EC_POINT_copy(val_sub[i][0], points[i])) goto err;
325 if (!EC_POINT_copy(val_sub[i][0], generator)) goto err;
330 if (!EC_POINT_dbl(group, tmp, val_sub[i][0], ctx)) goto err;
331 for (j = 1; j < (1u << (wsize[i] - 1)); j++)
333 if (!EC_POINT_add(group, val_sub[i][j], val_sub[i][j - 1], tmp, ctx)) goto err;
337 wNAF[i + 1] = NULL; /* make sure we always have a pivot */
338 wNAF[i] = compute_wNAF((i < num ? scalars[i] : scalar), wsize[i], &wNAF_len[i]);
339 if (wNAF[i] == NULL) goto err;
340 if (wNAF_len[i] > max_len)
341 max_len = wNAF_len[i];
344 #if 1 /* optional; EC_window_bits_for_scalar_size assumes we do this step */
345 if (!EC_POINTs_make_affine(group, num_val, val, ctx)) goto err;
348 r_is_at_infinity = 1;
350 for (k = max_len - 1; k >= 0; k--)
352 if (!r_is_at_infinity)
354 if (!EC_POINT_dbl(group, r, r, ctx)) goto err;
357 for (i = 0; i < totalnum; i++)
359 if (wNAF_len[i] > (size_t)k)
361 int digit = wNAF[i][k];
371 if (is_neg != r_is_inverted)
373 if (!r_is_at_infinity)
375 if (!EC_POINT_invert(group, r, ctx)) goto err;
377 r_is_inverted = !r_is_inverted;
382 if (r_is_at_infinity)
384 if (!EC_POINT_copy(r, val_sub[i][digit >> 1])) goto err;
385 r_is_at_infinity = 0;
389 if (!EC_POINT_add(group, r, r, val_sub[i][digit >> 1], ctx)) goto err;
396 if (r_is_at_infinity)
398 if (!EC_POINT_set_to_infinity(group, r)) goto err;
403 if (!EC_POINT_invert(group, r, ctx)) goto err;
410 BN_CTX_free(new_ctx);
415 if (wNAF_len != NULL)
416 OPENSSL_free(wNAF_len);
421 for (w = wNAF; *w != NULL; w++)
428 for (v = val; *v != NULL; v++)
429 EC_POINT_clear_free(*v);
435 OPENSSL_free(val_sub);
441 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)
443 const EC_POINT *points[1];
444 const BIGNUM *scalars[1];
447 scalars[0] = p_scalar;
449 return EC_POINTs_mul(group, r, g_scalar, (point != NULL && p_scalar != NULL), points, scalars, ctx);
453 int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
455 const EC_POINT *generator;
456 BN_CTX *new_ctx = NULL;
460 generator = EC_GROUP_get0_generator(group);
461 if (generator == NULL)
463 ECerr(EC_F_EC_GROUP_PRECOMPUTE_MULT, EC_R_UNDEFINED_GENERATOR);
469 ctx = new_ctx = BN_CTX_new();
475 order = BN_CTX_get(ctx);
476 if (order == NULL) goto err;
478 if (!EC_GROUP_get_order(group, order, ctx)) return 0;
479 if (BN_is_zero(order))
481 ECerr(EC_F_EC_GROUP_PRECOMPUTE_MULT, EC_R_UNKNOWN_ORDER);
492 BN_CTX_free(new_ctx);