xref: /onnv-gate/usr/src/common/openssl/crypto/ec/ec2_mult.c (revision 2139:6243c3338933)
1*2139Sjp161948 /* crypto/ec/ec2_mult.c */
2*2139Sjp161948 /* ====================================================================
3*2139Sjp161948  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
4*2139Sjp161948  *
5*2139Sjp161948  * The Elliptic Curve Public-Key Crypto Library (ECC Code) included
6*2139Sjp161948  * herein is developed by SUN MICROSYSTEMS, INC., and is contributed
7*2139Sjp161948  * to the OpenSSL project.
8*2139Sjp161948  *
9*2139Sjp161948  * The ECC Code is licensed pursuant to the OpenSSL open source
10*2139Sjp161948  * license provided below.
11*2139Sjp161948  *
12*2139Sjp161948  * The software is originally written by Sheueling Chang Shantz and
13*2139Sjp161948  * Douglas Stebila of Sun Microsystems Laboratories.
14*2139Sjp161948  *
15*2139Sjp161948  */
16*2139Sjp161948 /* ====================================================================
17*2139Sjp161948  * Copyright (c) 1998-2003 The OpenSSL Project.  All rights reserved.
18*2139Sjp161948  *
19*2139Sjp161948  * Redistribution and use in source and binary forms, with or without
20*2139Sjp161948  * modification, are permitted provided that the following conditions
21*2139Sjp161948  * are met:
22*2139Sjp161948  *
23*2139Sjp161948  * 1. Redistributions of source code must retain the above copyright
24*2139Sjp161948  *    notice, this list of conditions and the following disclaimer.
25*2139Sjp161948  *
26*2139Sjp161948  * 2. Redistributions in binary form must reproduce the above copyright
27*2139Sjp161948  *    notice, this list of conditions and the following disclaimer in
28*2139Sjp161948  *    the documentation and/or other materials provided with the
29*2139Sjp161948  *    distribution.
30*2139Sjp161948  *
31*2139Sjp161948  * 3. All advertising materials mentioning features or use of this
32*2139Sjp161948  *    software must display the following acknowledgment:
33*2139Sjp161948  *    "This product includes software developed by the OpenSSL Project
34*2139Sjp161948  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
35*2139Sjp161948  *
36*2139Sjp161948  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
37*2139Sjp161948  *    endorse or promote products derived from this software without
38*2139Sjp161948  *    prior written permission. For written permission, please contact
39*2139Sjp161948  *    openssl-core@openssl.org.
40*2139Sjp161948  *
41*2139Sjp161948  * 5. Products derived from this software may not be called "OpenSSL"
42*2139Sjp161948  *    nor may "OpenSSL" appear in their names without prior written
43*2139Sjp161948  *    permission of the OpenSSL Project.
44*2139Sjp161948  *
45*2139Sjp161948  * 6. Redistributions of any form whatsoever must retain the following
46*2139Sjp161948  *    acknowledgment:
47*2139Sjp161948  *    "This product includes software developed by the OpenSSL Project
48*2139Sjp161948  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
49*2139Sjp161948  *
50*2139Sjp161948  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
51*2139Sjp161948  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52*2139Sjp161948  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
53*2139Sjp161948  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
54*2139Sjp161948  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55*2139Sjp161948  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
56*2139Sjp161948  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
57*2139Sjp161948  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58*2139Sjp161948  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
59*2139Sjp161948  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
60*2139Sjp161948  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
61*2139Sjp161948  * OF THE POSSIBILITY OF SUCH DAMAGE.
62*2139Sjp161948  * ====================================================================
63*2139Sjp161948  *
64*2139Sjp161948  * This product includes cryptographic software written by Eric Young
65*2139Sjp161948  * (eay@cryptsoft.com).  This product includes software written by Tim
66*2139Sjp161948  * Hudson (tjh@cryptsoft.com).
67*2139Sjp161948  *
68*2139Sjp161948  */
69*2139Sjp161948 
70*2139Sjp161948 #include <openssl/err.h>
71*2139Sjp161948 
72*2139Sjp161948 #include "ec_lcl.h"
73*2139Sjp161948 
74*2139Sjp161948 
75*2139Sjp161948 /* Compute the x-coordinate x/z for the point 2*(x/z) in Montgomery projective
76*2139Sjp161948  * coordinates.
77*2139Sjp161948  * Uses algorithm Mdouble in appendix of
78*2139Sjp161948  *     Lopez, J. and Dahab, R.  "Fast multiplication on elliptic curves over
79*2139Sjp161948  *     GF(2^m) without precomputation".
80*2139Sjp161948  * modified to not require precomputation of c=b^{2^{m-1}}.
81*2139Sjp161948  */
gf2m_Mdouble(const EC_GROUP * group,BIGNUM * x,BIGNUM * z,BN_CTX * ctx)82*2139Sjp161948 static int gf2m_Mdouble(const EC_GROUP *group, BIGNUM *x, BIGNUM *z, BN_CTX *ctx)
83*2139Sjp161948 	{
84*2139Sjp161948 	BIGNUM *t1;
85*2139Sjp161948 	int ret = 0;
86*2139Sjp161948 
87*2139Sjp161948 	/* Since Mdouble is static we can guarantee that ctx != NULL. */
88*2139Sjp161948 	BN_CTX_start(ctx);
89*2139Sjp161948 	t1 = BN_CTX_get(ctx);
90*2139Sjp161948 	if (t1 == NULL) goto err;
91*2139Sjp161948 
92*2139Sjp161948 	if (!group->meth->field_sqr(group, x, x, ctx)) goto err;
93*2139Sjp161948 	if (!group->meth->field_sqr(group, t1, z, ctx)) goto err;
94*2139Sjp161948 	if (!group->meth->field_mul(group, z, x, t1, ctx)) goto err;
95*2139Sjp161948 	if (!group->meth->field_sqr(group, x, x, ctx)) goto err;
96*2139Sjp161948 	if (!group->meth->field_sqr(group, t1, t1, ctx)) goto err;
97*2139Sjp161948 	if (!group->meth->field_mul(group, t1, &group->b, t1, ctx)) goto err;
98*2139Sjp161948 	if (!BN_GF2m_add(x, x, t1)) goto err;
99*2139Sjp161948 
100*2139Sjp161948 	ret = 1;
101*2139Sjp161948 
102*2139Sjp161948  err:
103*2139Sjp161948 	BN_CTX_end(ctx);
104*2139Sjp161948 	return ret;
105*2139Sjp161948 	}
106*2139Sjp161948 
107*2139Sjp161948 /* Compute the x-coordinate x1/z1 for the point (x1/z1)+(x2/x2) in Montgomery
108*2139Sjp161948  * projective coordinates.
109*2139Sjp161948  * Uses algorithm Madd in appendix of
110*2139Sjp161948  *     Lopex, J. and Dahab, R.  "Fast multiplication on elliptic curves over
111*2139Sjp161948  *     GF(2^m) without precomputation".
112*2139Sjp161948  */
gf2m_Madd(const EC_GROUP * group,const BIGNUM * x,BIGNUM * x1,BIGNUM * z1,const BIGNUM * x2,const BIGNUM * z2,BN_CTX * ctx)113*2139Sjp161948 static int gf2m_Madd(const EC_GROUP *group, const BIGNUM *x, BIGNUM *x1, BIGNUM *z1,
114*2139Sjp161948 	const BIGNUM *x2, const BIGNUM *z2, BN_CTX *ctx)
115*2139Sjp161948 	{
116*2139Sjp161948 	BIGNUM *t1, *t2;
117*2139Sjp161948 	int ret = 0;
118*2139Sjp161948 
119*2139Sjp161948 	/* Since Madd is static we can guarantee that ctx != NULL. */
120*2139Sjp161948 	BN_CTX_start(ctx);
121*2139Sjp161948 	t1 = BN_CTX_get(ctx);
122*2139Sjp161948 	t2 = BN_CTX_get(ctx);
123*2139Sjp161948 	if (t2 == NULL) goto err;
124*2139Sjp161948 
125*2139Sjp161948 	if (!BN_copy(t1, x)) goto err;
126*2139Sjp161948 	if (!group->meth->field_mul(group, x1, x1, z2, ctx)) goto err;
127*2139Sjp161948 	if (!group->meth->field_mul(group, z1, z1, x2, ctx)) goto err;
128*2139Sjp161948 	if (!group->meth->field_mul(group, t2, x1, z1, ctx)) goto err;
129*2139Sjp161948 	if (!BN_GF2m_add(z1, z1, x1)) goto err;
130*2139Sjp161948 	if (!group->meth->field_sqr(group, z1, z1, ctx)) goto err;
131*2139Sjp161948 	if (!group->meth->field_mul(group, x1, z1, t1, ctx)) goto err;
132*2139Sjp161948 	if (!BN_GF2m_add(x1, x1, t2)) goto err;
133*2139Sjp161948 
134*2139Sjp161948 	ret = 1;
135*2139Sjp161948 
136*2139Sjp161948  err:
137*2139Sjp161948 	BN_CTX_end(ctx);
138*2139Sjp161948 	return ret;
139*2139Sjp161948 	}
140*2139Sjp161948 
141*2139Sjp161948 /* Compute the x, y affine coordinates from the point (x1, z1) (x2, z2)
142*2139Sjp161948  * using Montgomery point multiplication algorithm Mxy() in appendix of
143*2139Sjp161948  *     Lopex, J. and Dahab, R.  "Fast multiplication on elliptic curves over
144*2139Sjp161948  *     GF(2^m) without precomputation".
145*2139Sjp161948  * Returns:
146*2139Sjp161948  *     0 on error
147*2139Sjp161948  *     1 if return value should be the point at infinity
148*2139Sjp161948  *     2 otherwise
149*2139Sjp161948  */
gf2m_Mxy(const EC_GROUP * group,const BIGNUM * x,const BIGNUM * y,BIGNUM * x1,BIGNUM * z1,BIGNUM * x2,BIGNUM * z2,BN_CTX * ctx)150*2139Sjp161948 static int gf2m_Mxy(const EC_GROUP *group, const BIGNUM *x, const BIGNUM *y, BIGNUM *x1,
151*2139Sjp161948 	BIGNUM *z1, BIGNUM *x2, BIGNUM *z2, BN_CTX *ctx)
152*2139Sjp161948 	{
153*2139Sjp161948 	BIGNUM *t3, *t4, *t5;
154*2139Sjp161948 	int ret = 0;
155*2139Sjp161948 
156*2139Sjp161948 	if (BN_is_zero(z1))
157*2139Sjp161948 		{
158*2139Sjp161948 		BN_zero(x2);
159*2139Sjp161948 		BN_zero(z2);
160*2139Sjp161948 		return 1;
161*2139Sjp161948 		}
162*2139Sjp161948 
163*2139Sjp161948 	if (BN_is_zero(z2))
164*2139Sjp161948 		{
165*2139Sjp161948 		if (!BN_copy(x2, x)) return 0;
166*2139Sjp161948 		if (!BN_GF2m_add(z2, x, y)) return 0;
167*2139Sjp161948 		return 2;
168*2139Sjp161948 		}
169*2139Sjp161948 
170*2139Sjp161948 	/* Since Mxy is static we can guarantee that ctx != NULL. */
171*2139Sjp161948 	BN_CTX_start(ctx);
172*2139Sjp161948 	t3 = BN_CTX_get(ctx);
173*2139Sjp161948 	t4 = BN_CTX_get(ctx);
174*2139Sjp161948 	t5 = BN_CTX_get(ctx);
175*2139Sjp161948 	if (t5 == NULL) goto err;
176*2139Sjp161948 
177*2139Sjp161948 	if (!BN_one(t5)) goto err;
178*2139Sjp161948 
179*2139Sjp161948 	if (!group->meth->field_mul(group, t3, z1, z2, ctx)) goto err;
180*2139Sjp161948 
181*2139Sjp161948 	if (!group->meth->field_mul(group, z1, z1, x, ctx)) goto err;
182*2139Sjp161948 	if (!BN_GF2m_add(z1, z1, x1)) goto err;
183*2139Sjp161948 	if (!group->meth->field_mul(group, z2, z2, x, ctx)) goto err;
184*2139Sjp161948 	if (!group->meth->field_mul(group, x1, z2, x1, ctx)) goto err;
185*2139Sjp161948 	if (!BN_GF2m_add(z2, z2, x2)) goto err;
186*2139Sjp161948 
187*2139Sjp161948 	if (!group->meth->field_mul(group, z2, z2, z1, ctx)) goto err;
188*2139Sjp161948 	if (!group->meth->field_sqr(group, t4, x, ctx)) goto err;
189*2139Sjp161948 	if (!BN_GF2m_add(t4, t4, y)) goto err;
190*2139Sjp161948 	if (!group->meth->field_mul(group, t4, t4, t3, ctx)) goto err;
191*2139Sjp161948 	if (!BN_GF2m_add(t4, t4, z2)) goto err;
192*2139Sjp161948 
193*2139Sjp161948 	if (!group->meth->field_mul(group, t3, t3, x, ctx)) goto err;
194*2139Sjp161948 	if (!group->meth->field_div(group, t3, t5, t3, ctx)) goto err;
195*2139Sjp161948 	if (!group->meth->field_mul(group, t4, t3, t4, ctx)) goto err;
196*2139Sjp161948 	if (!group->meth->field_mul(group, x2, x1, t3, ctx)) goto err;
197*2139Sjp161948 	if (!BN_GF2m_add(z2, x2, x)) goto err;
198*2139Sjp161948 
199*2139Sjp161948 	if (!group->meth->field_mul(group, z2, z2, t4, ctx)) goto err;
200*2139Sjp161948 	if (!BN_GF2m_add(z2, z2, y)) goto err;
201*2139Sjp161948 
202*2139Sjp161948 	ret = 2;
203*2139Sjp161948 
204*2139Sjp161948  err:
205*2139Sjp161948 	BN_CTX_end(ctx);
206*2139Sjp161948 	return ret;
207*2139Sjp161948 	}
208*2139Sjp161948 
209*2139Sjp161948 /* Computes scalar*point and stores the result in r.
210*2139Sjp161948  * point can not equal r.
211*2139Sjp161948  * Uses algorithm 2P of
212*2139Sjp161948  *     Lopex, J. and Dahab, R.  "Fast multiplication on elliptic curves over
213*2139Sjp161948  *     GF(2^m) without precomputation".
214*2139Sjp161948  */
ec_GF2m_montgomery_point_multiply(const EC_GROUP * group,EC_POINT * r,const BIGNUM * scalar,const EC_POINT * point,BN_CTX * ctx)215*2139Sjp161948 static int ec_GF2m_montgomery_point_multiply(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
216*2139Sjp161948 	const EC_POINT *point, BN_CTX *ctx)
217*2139Sjp161948 	{
218*2139Sjp161948 	BIGNUM *x1, *x2, *z1, *z2;
219*2139Sjp161948 	int ret = 0, i, j;
220*2139Sjp161948 	BN_ULONG mask;
221*2139Sjp161948 
222*2139Sjp161948 	if (r == point)
223*2139Sjp161948 		{
224*2139Sjp161948 		ECerr(EC_F_EC_GF2M_MONTGOMERY_POINT_MULTIPLY, EC_R_INVALID_ARGUMENT);
225*2139Sjp161948 		return 0;
226*2139Sjp161948 		}
227*2139Sjp161948 
228*2139Sjp161948 	/* if result should be point at infinity */
229*2139Sjp161948 	if ((scalar == NULL) || BN_is_zero(scalar) || (point == NULL) ||
230*2139Sjp161948 		EC_POINT_is_at_infinity(group, point))
231*2139Sjp161948 		{
232*2139Sjp161948 		return EC_POINT_set_to_infinity(group, r);
233*2139Sjp161948 		}
234*2139Sjp161948 
235*2139Sjp161948 	/* only support affine coordinates */
236*2139Sjp161948 	if (!point->Z_is_one) return 0;
237*2139Sjp161948 
238*2139Sjp161948 	/* Since point_multiply is static we can guarantee that ctx != NULL. */
239*2139Sjp161948 	BN_CTX_start(ctx);
240*2139Sjp161948 	x1 = BN_CTX_get(ctx);
241*2139Sjp161948 	z1 = BN_CTX_get(ctx);
242*2139Sjp161948 	if (z1 == NULL) goto err;
243*2139Sjp161948 
244*2139Sjp161948 	x2 = &r->X;
245*2139Sjp161948 	z2 = &r->Y;
246*2139Sjp161948 
247*2139Sjp161948 	if (!BN_GF2m_mod_arr(x1, &point->X, group->poly)) goto err; /* x1 = x */
248*2139Sjp161948 	if (!BN_one(z1)) goto err; /* z1 = 1 */
249*2139Sjp161948 	if (!group->meth->field_sqr(group, z2, x1, ctx)) goto err; /* z2 = x1^2 = x^2 */
250*2139Sjp161948 	if (!group->meth->field_sqr(group, x2, z2, ctx)) goto err;
251*2139Sjp161948 	if (!BN_GF2m_add(x2, x2, &group->b)) goto err; /* x2 = x^4 + b */
252*2139Sjp161948 
253*2139Sjp161948 	/* find top most bit and go one past it */
254*2139Sjp161948 	i = scalar->top - 1; j = BN_BITS2 - 1;
255*2139Sjp161948 	mask = BN_TBIT;
256*2139Sjp161948 	while (!(scalar->d[i] & mask)) { mask >>= 1; j--; }
257*2139Sjp161948 	mask >>= 1; j--;
258*2139Sjp161948 	/* if top most bit was at word break, go to next word */
259*2139Sjp161948 	if (!mask)
260*2139Sjp161948 		{
261*2139Sjp161948 		i--; j = BN_BITS2 - 1;
262*2139Sjp161948 		mask = BN_TBIT;
263*2139Sjp161948 		}
264*2139Sjp161948 
265*2139Sjp161948 	for (; i >= 0; i--)
266*2139Sjp161948 		{
267*2139Sjp161948 		for (; j >= 0; j--)
268*2139Sjp161948 			{
269*2139Sjp161948 			if (scalar->d[i] & mask)
270*2139Sjp161948 				{
271*2139Sjp161948 				if (!gf2m_Madd(group, &point->X, x1, z1, x2, z2, ctx)) goto err;
272*2139Sjp161948 				if (!gf2m_Mdouble(group, x2, z2, ctx)) goto err;
273*2139Sjp161948 				}
274*2139Sjp161948 			else
275*2139Sjp161948 				{
276*2139Sjp161948 				if (!gf2m_Madd(group, &point->X, x2, z2, x1, z1, ctx)) goto err;
277*2139Sjp161948 				if (!gf2m_Mdouble(group, x1, z1, ctx)) goto err;
278*2139Sjp161948 				}
279*2139Sjp161948 			mask >>= 1;
280*2139Sjp161948 			}
281*2139Sjp161948 		j = BN_BITS2 - 1;
282*2139Sjp161948 		mask = BN_TBIT;
283*2139Sjp161948 		}
284*2139Sjp161948 
285*2139Sjp161948 	/* convert out of "projective" coordinates */
286*2139Sjp161948 	i = gf2m_Mxy(group, &point->X, &point->Y, x1, z1, x2, z2, ctx);
287*2139Sjp161948 	if (i == 0) goto err;
288*2139Sjp161948 	else if (i == 1)
289*2139Sjp161948 		{
290*2139Sjp161948 		if (!EC_POINT_set_to_infinity(group, r)) goto err;
291*2139Sjp161948 		}
292*2139Sjp161948 	else
293*2139Sjp161948 		{
294*2139Sjp161948 		if (!BN_one(&r->Z)) goto err;
295*2139Sjp161948 		r->Z_is_one = 1;
296*2139Sjp161948 		}
297*2139Sjp161948 
298*2139Sjp161948 	/* GF(2^m) field elements should always have BIGNUM::neg = 0 */
299*2139Sjp161948 	BN_set_negative(&r->X, 0);
300*2139Sjp161948 	BN_set_negative(&r->Y, 0);
301*2139Sjp161948 
302*2139Sjp161948 	ret = 1;
303*2139Sjp161948 
304*2139Sjp161948  err:
305*2139Sjp161948 	BN_CTX_end(ctx);
306*2139Sjp161948 	return ret;
307*2139Sjp161948 	}
308*2139Sjp161948 
309*2139Sjp161948 
310*2139Sjp161948 /* Computes the sum
311*2139Sjp161948  *     scalar*group->generator + scalars[0]*points[0] + ... + scalars[num-1]*points[num-1]
312*2139Sjp161948  * gracefully ignoring NULL scalar values.
313*2139Sjp161948  */
ec_GF2m_simple_mul(const EC_GROUP * group,EC_POINT * r,const BIGNUM * scalar,size_t num,const EC_POINT * points[],const BIGNUM * scalars[],BN_CTX * ctx)314*2139Sjp161948 int ec_GF2m_simple_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
315*2139Sjp161948 	size_t num, const EC_POINT *points[], const BIGNUM *scalars[], BN_CTX *ctx)
316*2139Sjp161948 	{
317*2139Sjp161948 	BN_CTX *new_ctx = NULL;
318*2139Sjp161948 	int ret = 0;
319*2139Sjp161948 	size_t i;
320*2139Sjp161948 	EC_POINT *p=NULL;
321*2139Sjp161948 
322*2139Sjp161948 	if (ctx == NULL)
323*2139Sjp161948 		{
324*2139Sjp161948 		ctx = new_ctx = BN_CTX_new();
325*2139Sjp161948 		if (ctx == NULL)
326*2139Sjp161948 			return 0;
327*2139Sjp161948 		}
328*2139Sjp161948 
329*2139Sjp161948 	/* This implementation is more efficient than the wNAF implementation for 2
330*2139Sjp161948 	 * or fewer points.  Use the ec_wNAF_mul implementation for 3 or more points,
331*2139Sjp161948 	 * or if we can perform a fast multiplication based on precomputation.
332*2139Sjp161948 	 */
333*2139Sjp161948 	if ((scalar && (num > 1)) || (num > 2) || (num == 0 && EC_GROUP_have_precompute_mult(group)))
334*2139Sjp161948 		{
335*2139Sjp161948 		ret = ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
336*2139Sjp161948 		goto err;
337*2139Sjp161948 		}
338*2139Sjp161948 
339*2139Sjp161948 	if ((p = EC_POINT_new(group)) == NULL) goto err;
340*2139Sjp161948 
341*2139Sjp161948 	if (!EC_POINT_set_to_infinity(group, r)) goto err;
342*2139Sjp161948 
343*2139Sjp161948 	if (scalar)
344*2139Sjp161948 		{
345*2139Sjp161948 		if (!ec_GF2m_montgomery_point_multiply(group, p, scalar, group->generator, ctx)) goto err;
346*2139Sjp161948 		if (BN_is_negative(scalar))
347*2139Sjp161948 			if (!group->meth->invert(group, p, ctx)) goto err;
348*2139Sjp161948 		if (!group->meth->add(group, r, r, p, ctx)) goto err;
349*2139Sjp161948 		}
350*2139Sjp161948 
351*2139Sjp161948 	for (i = 0; i < num; i++)
352*2139Sjp161948 		{
353*2139Sjp161948 		if (!ec_GF2m_montgomery_point_multiply(group, p, scalars[i], points[i], ctx)) goto err;
354*2139Sjp161948 		if (BN_is_negative(scalars[i]))
355*2139Sjp161948 			if (!group->meth->invert(group, p, ctx)) goto err;
356*2139Sjp161948 		if (!group->meth->add(group, r, r, p, ctx)) goto err;
357*2139Sjp161948 		}
358*2139Sjp161948 
359*2139Sjp161948 	ret = 1;
360*2139Sjp161948 
361*2139Sjp161948   err:
362*2139Sjp161948 	if (p) EC_POINT_free(p);
363*2139Sjp161948 	if (new_ctx != NULL)
364*2139Sjp161948 		BN_CTX_free(new_ctx);
365*2139Sjp161948 	return ret;
366*2139Sjp161948 	}
367*2139Sjp161948 
368*2139Sjp161948 
369*2139Sjp161948 /* Precomputation for point multiplication: fall back to wNAF methods
370*2139Sjp161948  * because ec_GF2m_simple_mul() uses ec_wNAF_mul() if appropriate */
371*2139Sjp161948 
ec_GF2m_precompute_mult(EC_GROUP * group,BN_CTX * ctx)372*2139Sjp161948 int ec_GF2m_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
373*2139Sjp161948 	{
374*2139Sjp161948 	return ec_wNAF_precompute_mult(group, ctx);
375*2139Sjp161948  	}
376*2139Sjp161948 
ec_GF2m_have_precompute_mult(const EC_GROUP * group)377*2139Sjp161948 int ec_GF2m_have_precompute_mult(const EC_GROUP *group)
378*2139Sjp161948 	{
379*2139Sjp161948 	return ec_wNAF_have_precompute_mult(group);
380*2139Sjp161948  	}
381