• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Originally written by Bodo Moeller and Nils Larsch 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 <openssl/bn.h>
71 #include <openssl/err.h>
72 #include <openssl/mem.h>
73 
74 #include "../bn/internal.h"
75 #include "../delocate.h"
76 #include "internal.h"
77 
78 
ec_GFp_mont_group_init(EC_GROUP * group)79 int ec_GFp_mont_group_init(EC_GROUP *group) {
80   int ok;
81 
82   ok = ec_GFp_simple_group_init(group);
83   group->mont = NULL;
84   return ok;
85 }
86 
ec_GFp_mont_group_finish(EC_GROUP * group)87 void ec_GFp_mont_group_finish(EC_GROUP *group) {
88   BN_MONT_CTX_free(group->mont);
89   group->mont = NULL;
90   ec_GFp_simple_group_finish(group);
91 }
92 
ec_GFp_mont_group_set_curve(EC_GROUP * group,const BIGNUM * p,const BIGNUM * a,const BIGNUM * b,BN_CTX * ctx)93 int ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p,
94                                 const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) {
95   BN_MONT_CTX_free(group->mont);
96   group->mont = BN_MONT_CTX_new_for_modulus(p, ctx);
97   if (group->mont == NULL) {
98     OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
99     return 0;
100   }
101 
102   if (!ec_GFp_simple_group_set_curve(group, p, a, b, ctx)) {
103     BN_MONT_CTX_free(group->mont);
104     group->mont = NULL;
105     return 0;
106   }
107 
108   return 1;
109 }
110 
ec_GFp_mont_felem_to_montgomery(const EC_GROUP * group,EC_FELEM * out,const EC_FELEM * in)111 static void ec_GFp_mont_felem_to_montgomery(const EC_GROUP *group,
112                                             EC_FELEM *out, const EC_FELEM *in) {
113   bn_to_montgomery_small(out->words, in->words, group->field.width,
114                          group->mont);
115 }
116 
ec_GFp_mont_felem_from_montgomery(const EC_GROUP * group,EC_FELEM * out,const EC_FELEM * in)117 static void ec_GFp_mont_felem_from_montgomery(const EC_GROUP *group,
118                                               EC_FELEM *out,
119                                               const EC_FELEM *in) {
120   bn_from_montgomery_small(out->words, group->field.width, in->words,
121                            group->field.width, group->mont);
122 }
123 
ec_GFp_mont_felem_inv0(const EC_GROUP * group,EC_FELEM * out,const EC_FELEM * a)124 static void ec_GFp_mont_felem_inv0(const EC_GROUP *group, EC_FELEM *out,
125                                    const EC_FELEM *a) {
126   bn_mod_inverse0_prime_mont_small(out->words, a->words, group->field.width,
127                                    group->mont);
128 }
129 
ec_GFp_mont_felem_mul(const EC_GROUP * group,EC_FELEM * r,const EC_FELEM * a,const EC_FELEM * b)130 void ec_GFp_mont_felem_mul(const EC_GROUP *group, EC_FELEM *r,
131                            const EC_FELEM *a, const EC_FELEM *b) {
132   bn_mod_mul_montgomery_small(r->words, a->words, b->words, group->field.width,
133                               group->mont);
134 }
135 
ec_GFp_mont_felem_sqr(const EC_GROUP * group,EC_FELEM * r,const EC_FELEM * a)136 void ec_GFp_mont_felem_sqr(const EC_GROUP *group, EC_FELEM *r,
137                            const EC_FELEM *a) {
138   bn_mod_mul_montgomery_small(r->words, a->words, a->words, group->field.width,
139                               group->mont);
140 }
141 
ec_GFp_mont_felem_to_bytes(const EC_GROUP * group,uint8_t * out,size_t * out_len,const EC_FELEM * in)142 void ec_GFp_mont_felem_to_bytes(const EC_GROUP *group, uint8_t *out,
143                                 size_t *out_len, const EC_FELEM *in) {
144   EC_FELEM tmp;
145   ec_GFp_mont_felem_from_montgomery(group, &tmp, in);
146   ec_GFp_simple_felem_to_bytes(group, out, out_len, &tmp);
147 }
148 
ec_GFp_mont_felem_from_bytes(const EC_GROUP * group,EC_FELEM * out,const uint8_t * in,size_t len)149 int ec_GFp_mont_felem_from_bytes(const EC_GROUP *group, EC_FELEM *out,
150                                  const uint8_t *in, size_t len) {
151   if (!ec_GFp_simple_felem_from_bytes(group, out, in, len)) {
152     return 0;
153   }
154 
155   ec_GFp_mont_felem_to_montgomery(group, out, out);
156   return 1;
157 }
158 
ec_GFp_mont_felem_reduce(const EC_GROUP * group,EC_FELEM * out,const BN_ULONG * words,size_t num)159 static void ec_GFp_mont_felem_reduce(const EC_GROUP *group, EC_FELEM *out,
160                                      const BN_ULONG *words, size_t num) {
161   // Convert "from" Montgomery form so the value is reduced mod p.
162   bn_from_montgomery_small(out->words, group->field.width, words, num,
163                            group->mont);
164   // Convert "to" Montgomery form to remove the R^-1 factor added.
165   ec_GFp_mont_felem_to_montgomery(group, out, out);
166   // Convert to Montgomery form to match this implementation's representation.
167   ec_GFp_mont_felem_to_montgomery(group, out, out);
168 }
169 
ec_GFp_mont_felem_exp(const EC_GROUP * group,EC_FELEM * out,const EC_FELEM * a,const BN_ULONG * exp,size_t num_exp)170 static void ec_GFp_mont_felem_exp(const EC_GROUP *group, EC_FELEM *out,
171                                   const EC_FELEM *a, const BN_ULONG *exp,
172                                   size_t num_exp) {
173   bn_mod_exp_mont_small(out->words, a->words, group->field.width, exp, num_exp,
174                         group->mont);
175 }
176 
ec_GFp_mont_point_get_affine_coordinates(const EC_GROUP * group,const EC_RAW_POINT * point,EC_FELEM * x,EC_FELEM * y)177 static int ec_GFp_mont_point_get_affine_coordinates(const EC_GROUP *group,
178                                                     const EC_RAW_POINT *point,
179                                                     EC_FELEM *x, EC_FELEM *y) {
180   if (ec_GFp_simple_is_at_infinity(group, point)) {
181     OPENSSL_PUT_ERROR(EC, EC_R_POINT_AT_INFINITY);
182     return 0;
183   }
184 
185   // Transform (X, Y, Z) into (x, y) := (X/Z^2, Y/Z^3). Note the check above
186   // ensures |point->Z| is non-zero, so the inverse always exists.
187   EC_FELEM z1, z2;
188   ec_GFp_mont_felem_inv0(group, &z2, &point->Z);
189   ec_GFp_mont_felem_sqr(group, &z1, &z2);
190 
191   if (x != NULL) {
192     ec_GFp_mont_felem_mul(group, x, &point->X, &z1);
193   }
194 
195   if (y != NULL) {
196     ec_GFp_mont_felem_mul(group, &z1, &z1, &z2);
197     ec_GFp_mont_felem_mul(group, y, &point->Y, &z1);
198   }
199 
200   return 1;
201 }
202 
ec_GFp_mont_jacobian_to_affine_batch(const EC_GROUP * group,EC_AFFINE * out,const EC_RAW_POINT * in,size_t num)203 static int ec_GFp_mont_jacobian_to_affine_batch(const EC_GROUP *group,
204                                                 EC_AFFINE *out,
205                                                 const EC_RAW_POINT *in,
206                                                 size_t num) {
207   if (num == 0) {
208     return 1;
209   }
210 
211   // Compute prefix products of all Zs. Use |out[i].X| as scratch space
212   // to store these values.
213   out[0].X = in[0].Z;
214   for (size_t i = 1; i < num; i++) {
215     ec_GFp_mont_felem_mul(group, &out[i].X, &out[i - 1].X, &in[i].Z);
216   }
217 
218   // Some input was infinity iff the product of all Zs is zero.
219   if (ec_felem_non_zero_mask(group, &out[num - 1].X) == 0) {
220     OPENSSL_PUT_ERROR(EC, EC_R_POINT_AT_INFINITY);
221     return 0;
222   }
223 
224   // Invert the product of all Zs.
225   EC_FELEM zinvprod;
226   ec_GFp_mont_felem_inv0(group, &zinvprod, &out[num - 1].X);
227   for (size_t i = num - 1; i < num; i--) {
228     // Our loop invariant is that |zinvprod| is Z0^-1 * Z1^-1 * ... * Zi^-1.
229     // Recover Zi^-1 by multiplying by the previous product.
230     EC_FELEM zinv, zinv2;
231     if (i == 0) {
232       zinv = zinvprod;
233     } else {
234       ec_GFp_mont_felem_mul(group, &zinv, &zinvprod, &out[i - 1].X);
235       // Maintain the loop invariant for the next iteration.
236       ec_GFp_mont_felem_mul(group, &zinvprod, &zinvprod, &in[i].Z);
237     }
238 
239     // Compute affine coordinates: x = X * Z^-2 and y = Y * Z^-3.
240     ec_GFp_mont_felem_sqr(group, &zinv2, &zinv);
241     ec_GFp_mont_felem_mul(group, &out[i].X, &in[i].X, &zinv2);
242     ec_GFp_mont_felem_mul(group, &out[i].Y, &in[i].Y, &zinv2);
243     ec_GFp_mont_felem_mul(group, &out[i].Y, &out[i].Y, &zinv);
244   }
245 
246   return 1;
247 }
248 
ec_GFp_mont_add(const EC_GROUP * group,EC_RAW_POINT * out,const EC_RAW_POINT * a,const EC_RAW_POINT * b)249 void ec_GFp_mont_add(const EC_GROUP *group, EC_RAW_POINT *out,
250                      const EC_RAW_POINT *a, const EC_RAW_POINT *b) {
251   if (a == b) {
252     ec_GFp_mont_dbl(group, out, a);
253     return;
254   }
255 
256   // The method is taken from:
257   //   http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html#addition-add-2007-bl
258   //
259   // Coq transcription and correctness proof:
260   // <https://github.com/davidben/fiat-crypto/blob/c7b95f62b2a54b559522573310e9b487327d219a/src/Curves/Weierstrass/Jacobian.v#L467>
261   // <https://github.com/davidben/fiat-crypto/blob/c7b95f62b2a54b559522573310e9b487327d219a/src/Curves/Weierstrass/Jacobian.v#L544>
262   EC_FELEM x_out, y_out, z_out;
263   BN_ULONG z1nz = ec_felem_non_zero_mask(group, &a->Z);
264   BN_ULONG z2nz = ec_felem_non_zero_mask(group, &b->Z);
265 
266   // z1z1 = z1z1 = z1**2
267   EC_FELEM z1z1;
268   ec_GFp_mont_felem_sqr(group, &z1z1, &a->Z);
269 
270   // z2z2 = z2**2
271   EC_FELEM z2z2;
272   ec_GFp_mont_felem_sqr(group, &z2z2, &b->Z);
273 
274   // u1 = x1*z2z2
275   EC_FELEM u1;
276   ec_GFp_mont_felem_mul(group, &u1, &a->X, &z2z2);
277 
278   // two_z1z2 = (z1 + z2)**2 - (z1z1 + z2z2) = 2z1z2
279   EC_FELEM two_z1z2;
280   ec_felem_add(group, &two_z1z2, &a->Z, &b->Z);
281   ec_GFp_mont_felem_sqr(group, &two_z1z2, &two_z1z2);
282   ec_felem_sub(group, &two_z1z2, &two_z1z2, &z1z1);
283   ec_felem_sub(group, &two_z1z2, &two_z1z2, &z2z2);
284 
285   // s1 = y1 * z2**3
286   EC_FELEM s1;
287   ec_GFp_mont_felem_mul(group, &s1, &b->Z, &z2z2);
288   ec_GFp_mont_felem_mul(group, &s1, &s1, &a->Y);
289 
290   // u2 = x2*z1z1
291   EC_FELEM u2;
292   ec_GFp_mont_felem_mul(group, &u2, &b->X, &z1z1);
293 
294   // h = u2 - u1
295   EC_FELEM h;
296   ec_felem_sub(group, &h, &u2, &u1);
297 
298   BN_ULONG xneq = ec_felem_non_zero_mask(group, &h);
299 
300   // z_out = two_z1z2 * h
301   ec_GFp_mont_felem_mul(group, &z_out, &h, &two_z1z2);
302 
303   // z1z1z1 = z1 * z1z1
304   EC_FELEM z1z1z1;
305   ec_GFp_mont_felem_mul(group, &z1z1z1, &a->Z, &z1z1);
306 
307   // s2 = y2 * z1**3
308   EC_FELEM s2;
309   ec_GFp_mont_felem_mul(group, &s2, &b->Y, &z1z1z1);
310 
311   // r = (s2 - s1)*2
312   EC_FELEM r;
313   ec_felem_sub(group, &r, &s2, &s1);
314   ec_felem_add(group, &r, &r, &r);
315 
316   BN_ULONG yneq = ec_felem_non_zero_mask(group, &r);
317 
318   // This case will never occur in the constant-time |ec_GFp_mont_mul|.
319   BN_ULONG is_nontrivial_double = ~xneq & ~yneq & z1nz & z2nz;
320   if (is_nontrivial_double) {
321     ec_GFp_mont_dbl(group, out, a);
322     return;
323   }
324 
325   // I = (2h)**2
326   EC_FELEM i;
327   ec_felem_add(group, &i, &h, &h);
328   ec_GFp_mont_felem_sqr(group, &i, &i);
329 
330   // J = h * I
331   EC_FELEM j;
332   ec_GFp_mont_felem_mul(group, &j, &h, &i);
333 
334   // V = U1 * I
335   EC_FELEM v;
336   ec_GFp_mont_felem_mul(group, &v, &u1, &i);
337 
338   // x_out = r**2 - J - 2V
339   ec_GFp_mont_felem_sqr(group, &x_out, &r);
340   ec_felem_sub(group, &x_out, &x_out, &j);
341   ec_felem_sub(group, &x_out, &x_out, &v);
342   ec_felem_sub(group, &x_out, &x_out, &v);
343 
344   // y_out = r(V-x_out) - 2 * s1 * J
345   ec_felem_sub(group, &y_out, &v, &x_out);
346   ec_GFp_mont_felem_mul(group, &y_out, &y_out, &r);
347   EC_FELEM s1j;
348   ec_GFp_mont_felem_mul(group, &s1j, &s1, &j);
349   ec_felem_sub(group, &y_out, &y_out, &s1j);
350   ec_felem_sub(group, &y_out, &y_out, &s1j);
351 
352   ec_felem_select(group, &x_out, z1nz, &x_out, &b->X);
353   ec_felem_select(group, &out->X, z2nz, &x_out, &a->X);
354   ec_felem_select(group, &y_out, z1nz, &y_out, &b->Y);
355   ec_felem_select(group, &out->Y, z2nz, &y_out, &a->Y);
356   ec_felem_select(group, &z_out, z1nz, &z_out, &b->Z);
357   ec_felem_select(group, &out->Z, z2nz, &z_out, &a->Z);
358 }
359 
ec_GFp_mont_dbl(const EC_GROUP * group,EC_RAW_POINT * r,const EC_RAW_POINT * a)360 void ec_GFp_mont_dbl(const EC_GROUP *group, EC_RAW_POINT *r,
361                      const EC_RAW_POINT *a) {
362   if (group->a_is_minus3) {
363     // The method is taken from:
364     //   http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#doubling-dbl-2001-b
365     //
366     // Coq transcription and correctness proof:
367     // <https://github.com/mit-plv/fiat-crypto/blob/79f8b5f39ed609339f0233098dee1a3c4e6b3080/src/Curves/Weierstrass/Jacobian.v#L93>
368     // <https://github.com/mit-plv/fiat-crypto/blob/79f8b5f39ed609339f0233098dee1a3c4e6b3080/src/Curves/Weierstrass/Jacobian.v#L201>
369     EC_FELEM delta, gamma, beta, ftmp, ftmp2, tmptmp, alpha, fourbeta;
370     // delta = z^2
371     ec_GFp_mont_felem_sqr(group, &delta, &a->Z);
372     // gamma = y^2
373     ec_GFp_mont_felem_sqr(group, &gamma, &a->Y);
374     // beta = x*gamma
375     ec_GFp_mont_felem_mul(group, &beta, &a->X, &gamma);
376 
377     // alpha = 3*(x-delta)*(x+delta)
378     ec_felem_sub(group, &ftmp, &a->X, &delta);
379     ec_felem_add(group, &ftmp2, &a->X, &delta);
380 
381     ec_felem_add(group, &tmptmp, &ftmp2, &ftmp2);
382     ec_felem_add(group, &ftmp2, &ftmp2, &tmptmp);
383     ec_GFp_mont_felem_mul(group, &alpha, &ftmp, &ftmp2);
384 
385     // x' = alpha^2 - 8*beta
386     ec_GFp_mont_felem_sqr(group, &r->X, &alpha);
387     ec_felem_add(group, &fourbeta, &beta, &beta);
388     ec_felem_add(group, &fourbeta, &fourbeta, &fourbeta);
389     ec_felem_add(group, &tmptmp, &fourbeta, &fourbeta);
390     ec_felem_sub(group, &r->X, &r->X, &tmptmp);
391 
392     // z' = (y + z)^2 - gamma - delta
393     ec_felem_add(group, &delta, &gamma, &delta);
394     ec_felem_add(group, &ftmp, &a->Y, &a->Z);
395     ec_GFp_mont_felem_sqr(group, &r->Z, &ftmp);
396     ec_felem_sub(group, &r->Z, &r->Z, &delta);
397 
398     // y' = alpha*(4*beta - x') - 8*gamma^2
399     ec_felem_sub(group, &r->Y, &fourbeta, &r->X);
400     ec_felem_add(group, &gamma, &gamma, &gamma);
401     ec_GFp_mont_felem_sqr(group, &gamma, &gamma);
402     ec_GFp_mont_felem_mul(group, &r->Y, &alpha, &r->Y);
403     ec_felem_add(group, &gamma, &gamma, &gamma);
404     ec_felem_sub(group, &r->Y, &r->Y, &gamma);
405   } else {
406     // The method is taken from:
407     //   http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html#doubling-dbl-2007-bl
408     //
409     // Coq transcription and correctness proof:
410     // <https://github.com/davidben/fiat-crypto/blob/c7b95f62b2a54b559522573310e9b487327d219a/src/Curves/Weierstrass/Jacobian.v#L102>
411     // <https://github.com/davidben/fiat-crypto/blob/c7b95f62b2a54b559522573310e9b487327d219a/src/Curves/Weierstrass/Jacobian.v#L534>
412     EC_FELEM xx, yy, yyyy, zz;
413     ec_GFp_mont_felem_sqr(group, &xx, &a->X);
414     ec_GFp_mont_felem_sqr(group, &yy, &a->Y);
415     ec_GFp_mont_felem_sqr(group, &yyyy, &yy);
416     ec_GFp_mont_felem_sqr(group, &zz, &a->Z);
417 
418     // s = 2*((x_in + yy)^2 - xx - yyyy)
419     EC_FELEM s;
420     ec_felem_add(group, &s, &a->X, &yy);
421     ec_GFp_mont_felem_sqr(group, &s, &s);
422     ec_felem_sub(group, &s, &s, &xx);
423     ec_felem_sub(group, &s, &s, &yyyy);
424     ec_felem_add(group, &s, &s, &s);
425 
426     // m = 3*xx + a*zz^2
427     EC_FELEM m;
428     ec_GFp_mont_felem_sqr(group, &m, &zz);
429     ec_GFp_mont_felem_mul(group, &m, &group->a, &m);
430     ec_felem_add(group, &m, &m, &xx);
431     ec_felem_add(group, &m, &m, &xx);
432     ec_felem_add(group, &m, &m, &xx);
433 
434     // x_out = m^2 - 2*s
435     ec_GFp_mont_felem_sqr(group, &r->X, &m);
436     ec_felem_sub(group, &r->X, &r->X, &s);
437     ec_felem_sub(group, &r->X, &r->X, &s);
438 
439     // z_out = (y_in + z_in)^2 - yy - zz
440     ec_felem_add(group, &r->Z, &a->Y, &a->Z);
441     ec_GFp_mont_felem_sqr(group, &r->Z, &r->Z);
442     ec_felem_sub(group, &r->Z, &r->Z, &yy);
443     ec_felem_sub(group, &r->Z, &r->Z, &zz);
444 
445     // y_out = m*(s-x_out) - 8*yyyy
446     ec_felem_add(group, &yyyy, &yyyy, &yyyy);
447     ec_felem_add(group, &yyyy, &yyyy, &yyyy);
448     ec_felem_add(group, &yyyy, &yyyy, &yyyy);
449     ec_felem_sub(group, &r->Y, &s, &r->X);
450     ec_GFp_mont_felem_mul(group, &r->Y, &r->Y, &m);
451     ec_felem_sub(group, &r->Y, &r->Y, &yyyy);
452   }
453 }
454 
ec_GFp_mont_cmp_x_coordinate(const EC_GROUP * group,const EC_RAW_POINT * p,const EC_SCALAR * r)455 static int ec_GFp_mont_cmp_x_coordinate(const EC_GROUP *group,
456                                         const EC_RAW_POINT *p,
457                                         const EC_SCALAR *r) {
458   if (!group->field_greater_than_order ||
459       group->field.width != group->order.width) {
460     // Do not bother optimizing this case. p > order in all commonly-used
461     // curves.
462     return ec_GFp_simple_cmp_x_coordinate(group, p, r);
463   }
464 
465   if (ec_GFp_simple_is_at_infinity(group, p)) {
466     return 0;
467   }
468 
469   // We wish to compare X/Z^2 with r. This is equivalent to comparing X with
470   // r*Z^2. Note that X and Z are represented in Montgomery form, while r is
471   // not.
472   EC_FELEM r_Z2, Z2_mont, X;
473   ec_GFp_mont_felem_mul(group, &Z2_mont, &p->Z, &p->Z);
474   // r < order < p, so this is valid.
475   OPENSSL_memcpy(r_Z2.words, r->words, group->field.width * sizeof(BN_ULONG));
476   ec_GFp_mont_felem_mul(group, &r_Z2, &r_Z2, &Z2_mont);
477   ec_GFp_mont_felem_from_montgomery(group, &X, &p->X);
478 
479   if (ec_felem_equal(group, &r_Z2, &X)) {
480     return 1;
481   }
482 
483   // During signing the x coefficient is reduced modulo the group order.
484   // Therefore there is a small possibility, less than 1/2^128, that group_order
485   // < p.x < P. in that case we need not only to compare against |r| but also to
486   // compare against r+group_order.
487   if (bn_less_than_words(r->words, group->field_minus_order.words,
488                          group->field.width)) {
489     // We can ignore the carry because: r + group_order < p < 2^256.
490     bn_add_words(r_Z2.words, r->words, group->order.d, group->field.width);
491     ec_GFp_mont_felem_mul(group, &r_Z2, &r_Z2, &Z2_mont);
492     if (ec_felem_equal(group, &r_Z2, &X)) {
493       return 1;
494     }
495   }
496 
497   return 0;
498 }
499 
DEFINE_METHOD_FUNCTION(EC_METHOD,EC_GFp_mont_method)500 DEFINE_METHOD_FUNCTION(EC_METHOD, EC_GFp_mont_method) {
501   out->group_init = ec_GFp_mont_group_init;
502   out->group_finish = ec_GFp_mont_group_finish;
503   out->group_set_curve = ec_GFp_mont_group_set_curve;
504   out->point_get_affine_coordinates = ec_GFp_mont_point_get_affine_coordinates;
505   out->jacobian_to_affine_batch = ec_GFp_mont_jacobian_to_affine_batch;
506   out->add = ec_GFp_mont_add;
507   out->dbl = ec_GFp_mont_dbl;
508   out->mul = ec_GFp_mont_mul;
509   out->mul_base = ec_GFp_mont_mul_base;
510   out->mul_batch = ec_GFp_mont_mul_batch;
511   out->mul_public_batch = ec_GFp_mont_mul_public_batch;
512   out->init_precomp = ec_GFp_mont_init_precomp;
513   out->mul_precomp = ec_GFp_mont_mul_precomp;
514   out->felem_mul = ec_GFp_mont_felem_mul;
515   out->felem_sqr = ec_GFp_mont_felem_sqr;
516   out->felem_to_bytes = ec_GFp_mont_felem_to_bytes;
517   out->felem_from_bytes = ec_GFp_mont_felem_from_bytes;
518   out->felem_reduce = ec_GFp_mont_felem_reduce;
519   out->felem_exp = ec_GFp_mont_felem_exp;
520   out->scalar_inv0_montgomery = ec_simple_scalar_inv0_montgomery;
521   out->scalar_to_montgomery_inv_vartime =
522       ec_simple_scalar_to_montgomery_inv_vartime;
523   out->cmp_x_coordinate = ec_GFp_mont_cmp_x_coordinate;
524 }
525