ec_mult.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/* ====================================================================
* Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
*
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* openssl-core@openssl.org.
*
* 5. Products derived from this software may not be called "OpenSSL"
* nor may "OpenSSL" appear in their names without prior written
* permission of the OpenSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit (http://www.openssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
* This product includes cryptographic software written by Eric Young
* (eay@cryptsoft.com). This product includes software written by Tim
* Hudson (tjh@cryptsoft.com).
*
*/
#include "ec_lcl.h"
/* TODO: optional precomputation of multiples of the generator */
/*
* wNAF-based interleaving multi-exponentation method
* (<URL:http://www.informatik.tu-darmstadt.de/TI/Mitarbeiter/moeller.html#multiexp>)
*/
/* Determine the width-(w+1) Non-Adjacent Form (wNAF) of 'scalar'.
* This is an array r[] of values that are either zero or odd with an
* absolute value less than 2^w satisfying
* scalar = \sum_j r[j]*2^j
* where at most one of any w+1 consecutive digits is non-zero.
*/
{
BIGNUM *c;
int ok = 0;
signed char *r = NULL;
int sign = 1;
c = BN_CTX_get(ctx);
if (w <= 0 || w > 7) /* 'signed char' can represent integers with absolute values less than 2^7 */
{
goto err;
}
if (c->neg)
{
sign = -1;
c->neg = 0;
}
r = OPENSSL_malloc(len);
j = 0;
while (!BN_is_zero(c))
{
int u = 0;
if (BN_is_odd(c))
{
{
goto err;
}
u = c->d[0] & mask;
if (u & bit)
{
u -= next_bit;
/* u < 0 */
if (!BN_add_word(c, -u)) goto err;
}
else
{
/* u > 0 */
if (!BN_sub_word(c, u)) goto err;
}
{
goto err;
}
}
r[j++] = sign * u;
if (BN_is_odd(c))
{
goto err;
}
if (!BN_rshift1(c, c)) goto err;
}
if (j > len)
{
goto err;
}
len = j;
ok = 1;
err:
if (!ok)
{
OPENSSL_free(r);
r = NULL;
}
if (ok)
return r;
}
/* TODO: table should be optimised for the wNAF-based implementation,
* sometimes smaller windows will give better performance
* (thus the boundaries should be increased)
*/
#define EC_window_bits_for_scalar_size(b) \
((size_t) \
((b) >= 2000 ? 6 : \
(b) >= 800 ? 5 : \
(b) >= 300 ? 4 : \
(b) >= 70 ? 3 : \
(b) >= 20 ? 2 : \
1))
/* Compute
* \sum scalars[i]*points[i],
* also including
* scalar*generator
* in the addition if scalar != NULL
*/
{
size_t i, j;
int k;
int r_is_inverted = 0;
int r_is_at_infinity = 1;
EC_POINT **v;
int ret = 0;
{
return 0;
}
{
return EC_POINT_set_to_infinity(group, r);
}
{
{
return 0;
}
}
for (i = 0; i < num; i++)
{
{
return 0;
}
}
{
}
/* num_val := total number of points to precompute */
num_val = 0;
for (i = 0; i < totalnum; i++)
{
}
/* all precomputed points go into a single array 'val',
* 'val_sub[i]' is a pointer to the subarray for the i-th point */
/* allocate points for precomputation */
v = val;
for (i = 0; i < totalnum; i++)
{
val_sub[i] = v;
{
*v = EC_POINT_new(group);
v++;
}
}
{
goto err;
}
{
goto err;
}
/* prepare precomputed values:
* val_sub[i][0] := points[i]
* val_sub[i][1] := 3 * points[i]
* val_sub[i][2] := 5 * points[i]
* ...
*/
for (i = 0; i < totalnum; i++)
{
if (i < num)
{
}
else
{
}
if (wsize[i] > 1)
{
{
}
}
}
#if 1 /* optional; EC_window_bits_for_scalar_size assumes we do this step */
#endif
r_is_at_infinity = 1;
for (k = max_len - 1; k >= 0; k--)
{
if (!r_is_at_infinity)
{
}
for (i = 0; i < totalnum; i++)
{
{
int is_neg;
if (digit)
{
if (is_neg)
if (is_neg != r_is_inverted)
{
if (!r_is_at_infinity)
{
}
}
/* digit > 0 */
if (r_is_at_infinity)
{
r_is_at_infinity = 0;
}
else
{
}
}
}
}
}
if (r_is_at_infinity)
{
}
else
{
if (r_is_inverted)
}
ret = 1;
err:
{
signed char **w;
OPENSSL_free(*w);
}
{
EC_POINT_clear_free(*v);
}
{
}
return ret;
}
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)
{
return EC_POINTs_mul(group, r, g_scalar, (point != NULL && p_scalar != NULL), points, scalars, ctx);
}
{
int ret = 0;
{
return 0;
}
{
return 0;
}
if (BN_is_zero(order))
{
goto err;
}
/* TODO */
ret = 1;
err:
return ret;
}