wnaf.c
Go to the documentation of this file.
1 /* Originally written by Bodo Moeller for the OpenSSL project.
2  * ====================================================================
3  * Copyright (c) 1998-2005 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  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
57  *
58  * Portions of the attached software ("Contribution") are developed by
59  * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
60  *
61  * The Contribution is licensed pursuant to the OpenSSL open source
62  * license provided above.
63  *
64  * The elliptic curve binary polynomial software is originally written by
65  * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems
66  * Laboratories. */
67 
68 #include <openssl/ec.h>
69 
70 #include <assert.h>
71 #include <string.h>
72 
73 #include <openssl/bn.h>
74 #include <openssl/err.h>
75 #include <openssl/mem.h>
76 #include <openssl/thread.h>
77 
78 #include "internal.h"
79 #include "../bn/internal.h"
80 #include "../../internal.h"
81 
82 
83 // This file implements the wNAF-based interleaving multi-exponentiation method
84 // at:
85 // http://link.springer.com/chapter/10.1007%2F3-540-45537-X_13
86 // http://www.bmoeller.de/pdf/TI-01-08.multiexp.pdf
87 
89  const EC_SCALAR *scalar, size_t bits, int w) {
90  // 'int8_t' can represent integers with absolute values less than 2^7.
91  assert(0 < w && w <= 7);
92  assert(bits != 0);
93  int bit = 1 << w; // 2^w, at most 128
94  int next_bit = bit << 1; // 2^(w+1), at most 256
95  int mask = next_bit - 1; // at most 255
96 
97  int window_val = scalar->words[0] & mask;
98  for (size_t j = 0; j < bits + 1; j++) {
99  assert(0 <= window_val && window_val <= next_bit);
100  int digit = 0;
101  if (window_val & 1) {
102  assert(0 < window_val && window_val < next_bit);
103  if (window_val & bit) {
104  digit = window_val - next_bit;
105  // We know -next_bit < digit < 0 and window_val - digit = next_bit.
106 
107  // modified wNAF
108  if (j + w + 1 >= bits) {
109  // special case for generating modified wNAFs:
110  // no new bits will be added into window_val,
111  // so using a positive digit here will decrease
112  // the total length of the representation
113 
114  digit = window_val & (mask >> 1);
115  // We know 0 < digit < bit and window_val - digit = bit.
116  }
117  } else {
118  digit = window_val;
119  // We know 0 < digit < bit and window_val - digit = 0.
120  }
121 
122  window_val -= digit;
123 
124  // Now window_val is 0 or 2^(w+1) in standard wNAF generation.
125  // For modified window NAFs, it may also be 2^w.
126  //
127  // See the comments above for the derivation of each of these bounds.
128  assert(window_val == 0 || window_val == next_bit || window_val == bit);
129  assert(-bit < digit && digit < bit);
130 
131  // window_val was odd, so digit is also odd.
132  assert(digit & 1);
133  }
134 
135  out[j] = digit;
136 
137  // Incorporate the next bit. Previously, |window_val| <= |next_bit|, so if
138  // we shift and add at most one copy of |bit|, this will continue to hold
139  // afterwards.
140  window_val >>= 1;
141  window_val +=
142  bit * bn_is_bit_set_words(scalar->words, group->order.width, j + w + 1);
143  assert(window_val <= next_bit);
144  }
145 
146  // bits + 1 entries should be sufficient to consume all bits.
147  assert(window_val == 0);
148 }
149 
150 // compute_precomp sets |out[i]| to (2*i+1)*p, for i from 0 to |len|.
152  const EC_RAW_POINT *p, size_t len) {
154  EC_RAW_POINT two_p;
155  ec_GFp_mont_dbl(group, &two_p, p);
156  for (size_t i = 1; i < len; i++) {
157  ec_GFp_mont_add(group, &out[i], &out[i - 1], &two_p);
158  }
159 }
160 
162  const EC_RAW_POINT *precomp, int digit) {
163  if (digit < 0) {
164  digit = -digit;
165  ec_GFp_simple_point_copy(out, &precomp[digit >> 1]);
167  } else {
168  ec_GFp_simple_point_copy(out, &precomp[digit >> 1]);
169  }
170 }
171 
172 // EC_WNAF_WINDOW_BITS is the window size to use for |ec_GFp_mont_mul_public|.
173 #define EC_WNAF_WINDOW_BITS 4
174 
175 // EC_WNAF_TABLE_SIZE is the table size to use for |ec_GFp_mont_mul_public|.
176 #define EC_WNAF_TABLE_SIZE (1 << (EC_WNAF_WINDOW_BITS - 1))
177 
178 // EC_WNAF_STACK is the number of points worth of data to stack-allocate and
179 // avoid a malloc.
180 #define EC_WNAF_STACK 3
181 
183  const EC_SCALAR *g_scalar,
184  const EC_RAW_POINT *points,
185  const EC_SCALAR *scalars, size_t num) {
186  size_t bits = BN_num_bits(&group->order);
187  size_t wNAF_len = bits + 1;
188 
189  int ret = 0;
190  int8_t wNAF_stack[EC_WNAF_STACK][EC_MAX_BYTES * 8 + 1];
191  int8_t (*wNAF_alloc)[EC_MAX_BYTES * 8 + 1] = NULL;
192  int8_t (*wNAF)[EC_MAX_BYTES * 8 + 1];
194  EC_RAW_POINT (*precomp_alloc)[EC_WNAF_TABLE_SIZE] = NULL;
195  EC_RAW_POINT (*precomp)[EC_WNAF_TABLE_SIZE];
196  if (num <= EC_WNAF_STACK) {
197  wNAF = wNAF_stack;
198  precomp = precomp_stack;
199  } else {
200  if (num >= ((size_t)-1) / sizeof(wNAF_alloc[0]) ||
201  num >= ((size_t)-1) / sizeof(precomp_alloc[0])) {
203  goto err;
204  }
205  wNAF_alloc = OPENSSL_malloc(num * sizeof(wNAF_alloc[0]));
206  precomp_alloc = OPENSSL_malloc(num * sizeof(precomp_alloc[0]));
207  if (wNAF_alloc == NULL || precomp_alloc == NULL) {
209  goto err;
210  }
211  wNAF = wNAF_alloc;
212  precomp = precomp_alloc;
213  }
214 
215  int8_t g_wNAF[EC_MAX_BYTES * 8 + 1];
216  EC_RAW_POINT g_precomp[EC_WNAF_TABLE_SIZE];
217  assert(wNAF_len <= OPENSSL_ARRAY_SIZE(g_wNAF));
218  const EC_RAW_POINT *g = &group->generator->raw;
219  if (g_scalar != NULL) {
220  ec_compute_wNAF(group, g_wNAF, g_scalar, bits, EC_WNAF_WINDOW_BITS);
222  }
223 
224  for (size_t i = 0; i < num; i++) {
225  assert(wNAF_len <= OPENSSL_ARRAY_SIZE(wNAF[i]));
226  ec_compute_wNAF(group, wNAF[i], &scalars[i], bits, EC_WNAF_WINDOW_BITS);
227  compute_precomp(group, precomp[i], &points[i], EC_WNAF_TABLE_SIZE);
228  }
229 
231  int r_is_at_infinity = 1;
232  for (size_t k = wNAF_len - 1; k < wNAF_len; k--) {
233  if (!r_is_at_infinity) {
235  }
236 
237  if (g_scalar != NULL && g_wNAF[k] != 0) {
238  lookup_precomp(group, &tmp, g_precomp, g_wNAF[k]);
239  if (r_is_at_infinity) {
241  r_is_at_infinity = 0;
242  } else {
243  ec_GFp_mont_add(group, r, r, &tmp);
244  }
245  }
246 
247  for (size_t i = 0; i < num; i++) {
248  if (wNAF[i][k] != 0) {
249  lookup_precomp(group, &tmp, precomp[i], wNAF[i][k]);
250  if (r_is_at_infinity) {
252  r_is_at_infinity = 0;
253  } else {
254  ec_GFp_mont_add(group, r, r, &tmp);
255  }
256  }
257  }
258  }
259 
260  if (r_is_at_infinity) {
262  }
263 
264  ret = 1;
265 
266 err:
267  OPENSSL_free(wNAF_alloc);
268  OPENSSL_free(precomp_alloc);
269  return ret;
270 }
bn.h
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
scalar::words
uint32_t words[8]
Definition: spake25519.c:319
ec_GFp_simple_point_set_to_infinity
#define ec_GFp_simple_point_set_to_infinity
Definition: boringssl_prefix_symbols.h:3086
scalar
Definition: spake25519.c:317
OPENSSL_PUT_ERROR
#define OPENSSL_PUT_ERROR(library, reason)
Definition: err.h:423
string.h
OPENSSL_ARRAY_SIZE
#define OPENSSL_ARRAY_SIZE(array)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:179
error_ref_leak.err
err
Definition: error_ref_leak.py:35
xds_manager.p
p
Definition: xds_manager.py:60
setup.k
k
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
EC_RAW_POINT
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/internal.h:260
ec_GFp_simple_invert
#define ec_GFp_simple_invert
Definition: boringssl_prefix_symbols.h:3081
OPENSSL_malloc
#define OPENSSL_malloc
Definition: boringssl_prefix_symbols.h:1885
EC_MAX_BYTES
#define EC_MAX_BYTES
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/internal.h:91
bn_is_bit_set_words
#define bn_is_bit_set_words
Definition: boringssl_prefix_symbols.h:2860
EC_WNAF_WINDOW_BITS
#define EC_WNAF_WINDOW_BITS
Definition: wnaf.c:173
bits
OPENSSL_EXPORT ASN1_BIT_STRING * bits
Definition: x509v3.h:482
err.h
ec_GFp_simple_point_copy
#define ec_GFp_simple_point_copy
Definition: boringssl_prefix_symbols.h:3084
g
struct @717 g
EC_WNAF_STACK
#define EC_WNAF_STACK
Definition: wnaf.c:180
ERR_R_OVERFLOW
#define ERR_R_OVERFLOW
Definition: err.h:375
BN_num_bits
#define BN_num_bits
Definition: boringssl_prefix_symbols.h:974
compute_precomp
static void compute_precomp(const EC_GROUP *group, EC_RAW_POINT *out, const EC_RAW_POINT *p, size_t len)
Definition: wnaf.c:151
lookup_precomp
static void lookup_precomp(const EC_GROUP *group, EC_RAW_POINT *out, const EC_RAW_POINT *precomp, int digit)
Definition: wnaf.c:161
upload.group
group
Definition: bloaty/third_party/googletest/googlemock/scripts/upload.py:397
internal.h
EC_WNAF_TABLE_SIZE
#define EC_WNAF_TABLE_SIZE
Definition: wnaf.c:176
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
ec_group_st
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/internal.h:573
fix_build_deps.r
r
Definition: fix_build_deps.py:491
xds_manager.num
num
Definition: xds_manager.py:56
ec_GFp_mont_mul_public_batch
int ec_GFp_mont_mul_public_batch(const EC_GROUP *group, EC_RAW_POINT *r, const EC_SCALAR *g_scalar, const EC_RAW_POINT *points, const EC_SCALAR *scalars, size_t num)
Definition: wnaf.c:182
ec_GFp_mont_dbl
#define ec_GFp_mont_dbl
Definition: boringssl_prefix_symbols.h:3059
EC_SCALAR
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/internal.h:103
mem.h
int8_t
signed char int8_t
Definition: stdint-msvc2008.h:75
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
autogen_x86imm.tmp
tmp
Definition: autogen_x86imm.py:12
thread.h
ec.h
OPENSSL_free
#define OPENSSL_free
Definition: boringssl_prefix_symbols.h:1869
ec_compute_wNAF
void ec_compute_wNAF(const EC_GROUP *group, int8_t *out, const EC_SCALAR *scalar, size_t bits, int w)
Definition: wnaf.c:88
ec_GFp_mont_add
#define ec_GFp_mont_add
Definition: boringssl_prefix_symbols.h:3058
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
ERR_R_MALLOC_FAILURE
#define ERR_R_MALLOC_FAILURE
Definition: err.h:371


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:54