Coverage Report

Created: 2020-10-17 06:46

/src/botan/src/lib/pubkey/ec_group/curve_gfp.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* Elliptic curves over GF(p) Montgomery Representation
3
* (C) 2014,2015,2018 Jack Lloyd
4
*     2016 Matthias Gierlings
5
*
6
* Botan is released under the Simplified BSD License (see license.txt)
7
*/
8
9
#include <botan/curve_gfp.h>
10
#include <botan/curve_nistp.h>
11
#include <botan/numthry.h>
12
#include <botan/reducer.h>
13
#include <botan/internal/mp_core.h>
14
#include <botan/internal/mp_asmi.h>
15
16
namespace Botan {
17
18
namespace {
19
20
class CurveGFp_Montgomery final : public CurveGFp_Repr
21
   {
22
   public:
23
      CurveGFp_Montgomery(const BigInt& p, const BigInt& a, const BigInt& b) :
24
         m_p(p), m_a(a), m_b(b),
25
         m_p_words(m_p.sig_words()),
26
         m_p_dash(monty_inverse(m_p.word_at(0)))
27
646
         {
28
646
         Modular_Reducer mod_p(m_p);
29
646
30
646
         m_r.set_bit(m_p_words * BOTAN_MP_WORD_BITS);
31
646
         m_r = mod_p.reduce(m_r);
32
646
33
646
         m_r2  = mod_p.square(m_r);
34
646
         m_r3  = mod_p.multiply(m_r, m_r2);
35
646
         m_a_r = mod_p.multiply(m_r, m_a);
36
646
         m_b_r = mod_p.multiply(m_r, m_b);
37
646
38
646
         m_a_is_zero = m_a.is_zero();
39
646
         m_a_is_minus_3 = (m_a + 3 == m_p);
40
646
         }
41
42
2.82M
      bool a_is_zero() const override { return m_a_is_zero; }
43
2.67M
      bool a_is_minus_3() const override { return m_a_is_minus_3; }
44
45
5.90k
      const BigInt& get_a() const override { return m_a; }
46
47
5.90k
      const BigInt& get_b() const override { return m_b; }
48
49
4.73M
      const BigInt& get_p() const override { return m_p; }
50
51
2.60M
      const BigInt& get_a_rep() const override { return m_a_r; }
52
53
12.9k
      const BigInt& get_b_rep() const override { return m_b_r; }
54
55
48.6k
      const BigInt& get_1_rep() const override { return m_r; }
56
57
34.2k
      bool is_one(const BigInt& x) const override { return x == m_r; }
58
59
433k
      size_t get_p_words() const override { return m_p_words; }
60
61
61.4M
      size_t get_ws_size() const override { return 2*m_p_words + 4; }
62
63
      BigInt invert_element(const BigInt& x, secure_vector<word>& ws) const override;
64
65
      void to_curve_rep(BigInt& x, secure_vector<word>& ws) const override;
66
67
      void from_curve_rep(BigInt& x, secure_vector<word>& ws) const override;
68
69
      void curve_mul_words(BigInt& z,
70
                           const word x_words[],
71
                           const size_t x_size,
72
                           const BigInt& y,
73
                           secure_vector<word>& ws) const override;
74
75
      void curve_sqr_words(BigInt& z,
76
                           const word x_words[],
77
                           size_t x_size,
78
                           secure_vector<word>& ws) const override;
79
80
   private:
81
      BigInt m_p;
82
      BigInt m_a, m_b;
83
      BigInt m_a_r, m_b_r;
84
      size_t m_p_words; // cache of m_p.sig_words()
85
86
      // Montgomery parameters
87
      BigInt m_r, m_r2, m_r3;
88
      word m_p_dash;
89
90
      bool m_a_is_zero;
91
      bool m_a_is_minus_3;
92
   };
93
94
BigInt CurveGFp_Montgomery::invert_element(const BigInt& x, secure_vector<word>& ws) const
95
34.5k
   {
96
   // Should we use Montgomery inverse instead?
97
34.5k
   const BigInt inv = inverse_mod(x, m_p);
98
34.5k
   BigInt res;
99
34.5k
   curve_mul(res, inv, m_r3, ws);
100
34.5k
   return res;
101
34.5k
   }
102
103
void CurveGFp_Montgomery::to_curve_rep(BigInt& x, secure_vector<word>& ws) const
104
8.97k
   {
105
8.97k
   const BigInt tx = x;
106
8.97k
   curve_mul(x, tx, m_r2, ws);
107
8.97k
   }
108
109
void CurveGFp_Montgomery::from_curve_rep(BigInt& z, secure_vector<word>& ws) const
110
56.7k
   {
111
56.7k
   if(ws.size() < get_ws_size())
112
498
      ws.resize(get_ws_size());
113
56.7k
114
56.7k
   const size_t output_size = 2*m_p_words + 2;
115
56.7k
   if(z.size() < output_size)
116
12.5k
      z.grow_to(output_size);
117
56.7k
118
56.7k
   bigint_monty_redc(z.mutable_data(),
119
56.7k
                     m_p.data(), m_p_words, m_p_dash,
120
56.7k
                     ws.data(), ws.size());
121
56.7k
   }
122
123
void CurveGFp_Montgomery::curve_mul_words(BigInt& z,
124
                                          const word x_w[],
125
                                          size_t x_size,
126
                                          const BigInt& y,
127
                                          secure_vector<word>& ws) const
128
33.0M
   {
129
33.0M
   BOTAN_DEBUG_ASSERT(y.sig_words() <= m_p_words);
130
33.0M
131
33.0M
   if(ws.size() < get_ws_size())
132
0
      ws.resize(get_ws_size());
133
33.0M
134
33.0M
   const size_t output_size = 2*m_p_words + 2;
135
33.0M
   if(z.size() < output_size)
136
1.96M
      z.grow_to(output_size);
137
33.0M
138
33.0M
   bigint_mul(z.mutable_data(), z.size(),
139
33.0M
              x_w, x_size, std::min(m_p_words, x_size),
140
33.0M
              y.data(), y.size(), std::min(m_p_words, y.size()),
141
33.0M
              ws.data(), ws.size());
142
33.0M
143
33.0M
   bigint_monty_redc(z.mutable_data(),
144
33.0M
                     m_p.data(), m_p_words, m_p_dash,
145
33.0M
                     ws.data(), ws.size());
146
33.0M
   }
147
148
void CurveGFp_Montgomery::curve_sqr_words(BigInt& z,
149
                                          const word x[],
150
                                          size_t x_size,
151
                                          secure_vector<word>& ws) const
152
23.6M
   {
153
23.6M
   if(ws.size() < get_ws_size())
154
52.5k
      ws.resize(get_ws_size());
155
23.6M
156
23.6M
   const size_t output_size = 2*m_p_words + 2;
157
23.6M
   if(z.size() < output_size)
158
172k
      z.grow_to(output_size);
159
23.6M
160
23.6M
   bigint_sqr(z.mutable_data(), z.size(),
161
23.6M
              x, x_size, std::min(m_p_words, x_size),
162
23.6M
              ws.data(), ws.size());
163
23.6M
164
23.6M
   bigint_monty_redc(z.mutable_data(),
165
23.6M
                     m_p.data(), m_p_words, m_p_dash,
166
23.6M
                     ws.data(), ws.size());
167
23.6M
   }
168
169
class CurveGFp_NIST : public CurveGFp_Repr
170
   {
171
   public:
172
      CurveGFp_NIST(size_t p_bits, const BigInt& a, const BigInt& b) :
173
         m_1(1), m_a(a), m_b(b), m_p_words((p_bits + BOTAN_MP_WORD_BITS - 1) / BOTAN_MP_WORD_BITS)
174
646
         {
175
         // All Solinas prime curves are assumed a == -3
176
646
         }
177
178
11.2M
      bool a_is_zero() const override { return false; }
179
11.2M
      bool a_is_minus_3() const override { return true; }
180
181
18.4k
      const BigInt& get_a() const override { return m_a; }
182
183
18.4k
      const BigInt& get_b() const override { return m_b; }
184
185
116k
      const BigInt& get_1_rep() const override { return m_1; }
186
187
1.15M
      size_t get_p_words() const override { return m_p_words; }
188
189
270M
      size_t get_ws_size() const override { return 2*m_p_words + 4; }
190
191
35.0k
      const BigInt& get_a_rep() const override { return m_a; }
192
193
46.7k
      const BigInt& get_b_rep() const override { return m_b; }
194
195
115k
      bool is_one(const BigInt& x) const override { return x == 1; }
196
197
      void to_curve_rep(BigInt& x, secure_vector<word>& ws) const override
198
27.7k
         { redc_mod_p(x, ws); }
199
200
      void from_curve_rep(BigInt& x, secure_vector<word>& ws) const override
201
197k
         { redc_mod_p(x, ws); }
202
203
      virtual void redc_mod_p(BigInt& z, secure_vector<word>& ws) const = 0;
204
205
      BigInt invert_element(const BigInt& x, secure_vector<word>& ws) const override;
206
207
      void curve_mul_words(BigInt& z,
208
                           const word x_words[],
209
                           const size_t x_size,
210
                           const BigInt& y,
211
                           secure_vector<word>& ws) const override;
212
213
      void curve_mul_tmp(BigInt& x, const BigInt& y, BigInt& tmp, secure_vector<word>& ws) const
214
1.39M
         {
215
1.39M
         curve_mul(tmp, x, y, ws);
216
1.39M
         x.swap(tmp);
217
1.39M
         }
218
219
      void curve_sqr_tmp(BigInt& x, BigInt& tmp, secure_vector<word>& ws) const
220
48.1M
         {
221
48.1M
         curve_sqr(tmp, x, ws);
222
48.1M
         x.swap(tmp);
223
48.1M
         }
224
225
      void curve_sqr_words(BigInt& z,
226
                           const word x_words[],
227
                           size_t x_size,
228
                           secure_vector<word>& ws) const override;
229
   private:
230
      // Curve parameters
231
      BigInt m_1;
232
      BigInt m_a, m_b;
233
      size_t m_p_words; // cache of m_p.sig_words()
234
   };
235
236
BigInt CurveGFp_NIST::invert_element(const BigInt& x, secure_vector<word>& ws) const
237
383
   {
238
383
   BOTAN_UNUSED(ws);
239
383
   return inverse_mod(x, get_p());
240
383
   }
241
242
void CurveGFp_NIST::curve_mul_words(BigInt& z,
243
                                    const word x_w[],
244
                                    size_t x_size,
245
                                    const BigInt& y,
246
                                    secure_vector<word>& ws) const
247
129M
   {
248
129M
   BOTAN_DEBUG_ASSERT(y.sig_words() <= m_p_words);
249
129M
250
129M
   if(ws.size() < get_ws_size())
251
0
      ws.resize(get_ws_size());
252
129M
253
129M
   const size_t output_size = 2*m_p_words + 2;
254
129M
   if(z.size() < output_size)
255
4.20M
      z.grow_to(output_size);
256
129M
257
129M
   bigint_mul(z.mutable_data(), z.size(),
258
129M
              x_w, x_size, std::min(m_p_words, x_size),
259
129M
              y.data(), y.size(), std::min(m_p_words, y.size()),
260
129M
              ws.data(), ws.size());
261
129M
262
129M
   this->redc_mod_p(z, ws);
263
129M
   }
264
265
void CurveGFp_NIST::curve_sqr_words(BigInt& z, const word x[], size_t x_size,
266
                                    secure_vector<word>& ws) const
267
121M
   {
268
121M
   if(ws.size() < get_ws_size())
269
183k
      ws.resize(get_ws_size());
270
121M
271
121M
   const size_t output_size = 2*m_p_words + 2;
272
121M
   if(z.size() < output_size)
273
11.7M
      z.grow_to(output_size);
274
121M
275
121M
   bigint_sqr(z.mutable_data(), output_size,
276
121M
              x, x_size, std::min(m_p_words, x_size),
277
121M
              ws.data(), ws.size());
278
121M
279
121M
   this->redc_mod_p(z, ws);
280
121M
   }
281
282
/**
283
* The NIST P-192 curve
284
*/
285
class CurveGFp_P192 final : public CurveGFp_NIST
286
   {
287
   public:
288
21
      CurveGFp_P192(const BigInt& a, const BigInt& b) : CurveGFp_NIST(192, a, b) {}
289
18.8k
      const BigInt& get_p() const override { return prime_p192(); }
290
   private:
291
315k
      void redc_mod_p(BigInt& x, secure_vector<word>& ws) const override { redc_p192(x, ws); }
292
   };
293
294
/**
295
* The NIST P-224 curve
296
*/
297
class CurveGFp_P224 final : public CurveGFp_NIST
298
   {
299
   public:
300
257
      CurveGFp_P224(const BigInt& a, const BigInt& b) : CurveGFp_NIST(224, a, b) {}
301
414k
      const BigInt& get_p() const override { return prime_p224(); }
302
   private:
303
6.45M
      void redc_mod_p(BigInt& x, secure_vector<word>& ws) const override { redc_p224(x, ws); }
304
   };
305
306
/**
307
* The NIST P-256 curve
308
*/
309
class CurveGFp_P256 final : public CurveGFp_NIST
310
   {
311
   public:
312
22
      CurveGFp_P256(const BigInt& a, const BigInt& b) : CurveGFp_NIST(256, a, b) {}
313
2.81M
      const BigInt& get_p() const override { return prime_p256(); }
314
   private:
315
36.6M
      void redc_mod_p(BigInt& x, secure_vector<word>& ws) const override { redc_p256(x, ws); }
316
      BigInt invert_element(const BigInt& x, secure_vector<word>& ws) const override;
317
   };
318
319
BigInt CurveGFp_P256::invert_element(const BigInt& x, secure_vector<word>& ws) const
320
30.1k
   {
321
30.1k
   BigInt r, p2, p4, p8, p16, p32, tmp;
322
30.1k
323
30.1k
   curve_sqr(r, x, ws);
324
30.1k
325
30.1k
   curve_mul(p2, r, x, ws);
326
30.1k
   curve_sqr(r, p2, ws);
327
30.1k
   curve_sqr_tmp(r, tmp, ws);
328
30.1k
329
30.1k
   curve_mul(p4, r, p2, ws);
330
30.1k
331
30.1k
   curve_sqr(r, p4, ws);
332
120k
   for(size_t i = 0; i != 3; ++i)
333
90.5k
      curve_sqr_tmp(r, tmp, ws);
334
30.1k
   curve_mul(p8, r, p4, ws);
335
30.1k
336
30.1k
   curve_sqr(r, p8, ws);
337
241k
   for(size_t i = 0; i != 7; ++i)
338
211k
      curve_sqr_tmp(r, tmp, ws);
339
30.1k
   curve_mul(p16, r, p8, ws);
340
30.1k
341
30.1k
   curve_sqr(r, p16, ws);
342
482k
   for(size_t i = 0; i != 15; ++i)
343
452k
      curve_sqr_tmp(r, tmp, ws);
344
30.1k
   curve_mul(p32, r, p16, ws);
345
30.1k
346
30.1k
   curve_sqr(r, p32, ws);
347
965k
   for(size_t i = 0; i != 31; ++i)
348
935k
      curve_sqr_tmp(r, tmp, ws);
349
30.1k
   curve_mul_tmp(r, x, tmp, ws);
350
30.1k
351
3.89M
   for(size_t i = 0; i != 32*4; ++i)
352
3.86M
      curve_sqr_tmp(r, tmp, ws);
353
30.1k
   curve_mul_tmp(r, p32, tmp, ws);
354
30.1k
355
996k
   for(size_t i = 0; i != 32; ++i)
356
965k
      curve_sqr_tmp(r, tmp, ws);
357
30.1k
   curve_mul_tmp(r, p32, tmp, ws);
358
30.1k
359
513k
   for(size_t i = 0; i != 16; ++i)
360
482k
      curve_sqr_tmp(r, tmp, ws);
361
30.1k
   curve_mul_tmp(r, p16, tmp, ws);
362
271k
   for(size_t i = 0; i != 8; ++i)
363
241k
      curve_sqr_tmp(r, tmp, ws);
364
30.1k
   curve_mul_tmp(r, p8, tmp, ws);
365
30.1k
366
150k
   for(size_t i = 0; i != 4; ++i)
367
120k
      curve_sqr_tmp(r, tmp, ws);
368
30.1k
   curve_mul_tmp(r, p4, tmp, ws);
369
30.1k
370
90.5k
   for(size_t i = 0; i != 2; ++i)
371
60.3k
      curve_sqr_tmp(r, tmp, ws);
372
30.1k
   curve_mul_tmp(r, p2, tmp, ws);
373
30.1k
374
90.5k
   for(size_t i = 0; i != 2; ++i)
375
60.3k
      curve_sqr_tmp(r, tmp, ws);
376
30.1k
   curve_mul_tmp(r, x, tmp, ws);
377
30.1k
378
30.1k
   return r;
379
30.1k
   }
380
381
/**
382
* The NIST P-384 curve
383
*/
384
class CurveGFp_P384 final : public CurveGFp_NIST
385
   {
386
   public:
387
65
      CurveGFp_P384(const BigInt& a, const BigInt& b) : CurveGFp_NIST(384, a, b) {}
388
4.12M
      const BigInt& get_p() const override { return prime_p384(); }
389
   private:
390
54.1M
      void redc_mod_p(BigInt& x, secure_vector<word>& ws) const override { redc_p384(x, ws); }
391
      BigInt invert_element(const BigInt& x, secure_vector<word>& ws) const override;
392
   };
393
394
BigInt CurveGFp_P384::invert_element(const BigInt& x, secure_vector<word>& ws) const
395
30.2k
   {
396
   // From https://briansmith.org/ecc-inversion-addition-chains-01
397
30.2k
398
30.2k
   BigInt r, x2, x3, x15, x30, tmp, rl;
399
30.2k
400
30.2k
   r = x;
401
30.2k
   curve_sqr_tmp(r, tmp, ws);
402
30.2k
   curve_mul_tmp(r, x, tmp, ws);
403
30.2k
   x2 = r;
404
30.2k
405
30.2k
   curve_sqr_tmp(r, tmp, ws);
406
30.2k
   curve_mul_tmp(r, x, tmp, ws);
407
30.2k
408
30.2k
   x3 = r;
409
30.2k
410
121k
   for(size_t i = 0; i != 3; ++i)
411
90.8k
      curve_sqr_tmp(r, tmp, ws);
412
30.2k
   curve_mul_tmp(r, x3, tmp, ws);
413
30.2k
414
30.2k
   rl = r;
415
212k
   for(size_t i = 0; i != 6; ++i)
416
181k
      curve_sqr_tmp(r, tmp, ws);
417
30.2k
   curve_mul_tmp(r, rl, tmp, ws);
418
30.2k
419
121k
   for(size_t i = 0; i != 3; ++i)
420
90.8k
      curve_sqr_tmp(r, tmp, ws);
421
30.2k
   curve_mul_tmp(r, x3, tmp, ws);
422
30.2k
423
30.2k
   x15 = r;
424
484k
   for(size_t i = 0; i != 15; ++i)
425
454k
      curve_sqr_tmp(r, tmp, ws);
426
30.2k
   curve_mul_tmp(r, x15, tmp, ws);
427
30.2k
428
30.2k
   x30 = r;
429
939k
   for(size_t i = 0; i != 30; ++i)
430
908k
      curve_sqr_tmp(r, tmp, ws);
431
30.2k
   curve_mul_tmp(r, x30, tmp, ws);
432
30.2k
433
30.2k
   rl = r;
434
1.84M
   for(size_t i = 0; i != 60; ++i)
435
1.81M
      curve_sqr_tmp(r, tmp, ws);
436
30.2k
   curve_mul_tmp(r, rl, tmp, ws);
437
30.2k
438
30.2k
   rl = r;
439
3.66M
   for(size_t i = 0; i != 120; ++i)
440
3.63M
      curve_sqr_tmp(r, tmp, ws);
441
30.2k
   curve_mul_tmp(r, rl, tmp, ws);
442
30.2k
443
484k
   for(size_t i = 0; i != 15; ++i)
444
454k
      curve_sqr_tmp(r, tmp, ws);
445
30.2k
   curve_mul_tmp(r, x15, tmp, ws);
446
30.2k
447
969k
   for(size_t i = 0; i != 31; ++i)
448
939k
      curve_sqr_tmp(r, tmp, ws);
449
30.2k
   curve_mul_tmp(r, x30, tmp, ws);
450
30.2k
451
90.8k
   for(size_t i = 0; i != 2; ++i)
452
60.5k
      curve_sqr_tmp(r, tmp, ws);
453
30.2k
   curve_mul_tmp(r, x2, tmp, ws);
454
30.2k
455
2.87M
   for(size_t i = 0; i != 94; ++i)
456
2.84M
      curve_sqr_tmp(r, tmp, ws);
457
30.2k
   curve_mul_tmp(r, x30, tmp, ws);
458
30.2k
459
90.8k
   for(size_t i = 0; i != 2; ++i)
460
60.5k
      curve_sqr_tmp(r, tmp, ws);
461
30.2k
462
30.2k
   curve_mul_tmp(r, x, tmp, ws);
463
30.2k
464
30.2k
   return r;
465
30.2k
   }
466
467
/**
468
* The NIST P-521 curve
469
*/
470
class CurveGFp_P521 final : public CurveGFp_NIST
471
   {
472
   public:
473
281
      CurveGFp_P521(const BigInt& a, const BigInt& b) : CurveGFp_NIST(521, a, b) {}
474
11.8M
      const BigInt& get_p() const override { return prime_p521(); }
475
   private:
476
154M
      void redc_mod_p(BigInt& x, secure_vector<word>& ws) const override { redc_p521(x, ws); }
477
      BigInt invert_element(const BigInt& x, secure_vector<word>& ws) const override;
478
   };
479
480
BigInt CurveGFp_P521::invert_element(const BigInt& x, secure_vector<word>& ws) const
481
55.9k
   {
482
   // Addition chain from https://eprint.iacr.org/2014/852.pdf section
483
55.9k
484
55.9k
   BigInt r;
485
55.9k
   BigInt rl;
486
55.9k
   BigInt a7;
487
55.9k
   BigInt tmp;
488
55.9k
489
55.9k
   curve_sqr(r, x, ws);
490
55.9k
   curve_mul_tmp(r, x, tmp, ws);
491
55.9k
492
55.9k
   curve_sqr_tmp(r, tmp, ws);
493
55.9k
   curve_mul_tmp(r, x, tmp, ws);
494
55.9k
495
55.9k
   rl = r;
496
55.9k
497
223k
   for(size_t i = 0; i != 3; ++i)
498
167k
      curve_sqr_tmp(r, tmp, ws);
499
55.9k
   curve_mul_tmp(r, rl, tmp, ws);
500
55.9k
501
55.9k
   curve_sqr_tmp(r, tmp, ws);
502
55.9k
   curve_mul_tmp(r, x, tmp, ws);
503
55.9k
   a7 = r; // need this value later
504
55.9k
505
55.9k
   curve_sqr_tmp(r, tmp, ws);
506
55.9k
   curve_mul_tmp(r, x, tmp, ws);
507
55.9k
508
55.9k
   rl = r;
509
503k
   for(size_t i = 0; i != 8; ++i)
510
447k
      curve_sqr_tmp(r, tmp, ws);
511
55.9k
   curve_mul_tmp(r, rl, tmp, ws);
512
55.9k
513
55.9k
   rl = r;
514
950k
   for(size_t i = 0; i != 16; ++i)
515
894k
      curve_sqr_tmp(r, tmp, ws);
516
55.9k
   curve_mul_tmp(r, rl, tmp, ws);
517
55.9k
518
55.9k
    rl = r;
519
1.84M
    for(size_t i = 0; i != 32; ++i)
520
1.78M
        curve_sqr_tmp(r, tmp, ws);
521
55.9k
    curve_mul_tmp(r, rl, tmp, ws);
522
55.9k
523
55.9k
    rl = r;
524
3.63M
    for(size_t i = 0; i != 64; ++i)
525
3.57M
        curve_sqr_tmp(r, tmp, ws);
526
55.9k
    curve_mul_tmp(r, rl, tmp, ws);
527
55.9k
528
55.9k
    rl = r;
529
7.21M
    for(size_t i = 0; i != 128; ++i)
530
7.15M
        curve_sqr_tmp(r, tmp, ws);
531
55.9k
    curve_mul_tmp(r, rl, tmp, ws);
532
55.9k
533
55.9k
    rl = r;
534
14.3M
    for(size_t i = 0; i != 256; ++i)
535
14.3M
        curve_sqr_tmp(r, tmp, ws);
536
55.9k
    curve_mul_tmp(r, rl, tmp, ws);
537
55.9k
538
447k
    for(size_t i = 0; i != 7; ++i)
539
391k
        curve_sqr_tmp(r, tmp, ws);
540
55.9k
    curve_mul_tmp(r, a7, tmp, ws);
541
55.9k
542
167k
    for(size_t i = 0; i != 2; ++i)
543
111k
       curve_sqr_tmp(r, tmp, ws);
544
55.9k
    curve_mul_tmp(r, x, tmp, ws);
545
55.9k
546
55.9k
    return r;
547
55.9k
   }
548
549
}
550
551
std::shared_ptr<CurveGFp_Repr>
552
CurveGFp::choose_repr(const BigInt& p, const BigInt& a, const BigInt& b)
553
1.29k
   {
554
1.29k
   if(p == prime_p192())
555
21
      return std::shared_ptr<CurveGFp_Repr>(new CurveGFp_P192(a, b));
556
1.27k
   if(p == prime_p224())
557
257
      return std::shared_ptr<CurveGFp_Repr>(new CurveGFp_P224(a, b));
558
1.01k
   if(p == prime_p256())
559
22
      return std::shared_ptr<CurveGFp_Repr>(new CurveGFp_P256(a, b));
560
992
   if(p == prime_p384())
561
65
      return std::shared_ptr<CurveGFp_Repr>(new CurveGFp_P384(a, b));
562
927
   if(p == prime_p521())
563
281
      return std::shared_ptr<CurveGFp_Repr>(new CurveGFp_P521(a, b));
564
646
565
646
   return std::shared_ptr<CurveGFp_Repr>(new CurveGFp_Montgomery(p, a, b));
566
646
   }
567
568
}