aboutsummaryrefslogtreecommitdiffstats
path: root/src/math/gfpmath/gfp_element.cpp
blob: 183d7d4abb2fd7742370de4ac80f7e1a286fcceb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
/******
 * Arithmetic for prime fields GF(p) (source file)
 *
 * (C) 2007 Martin Doering
 *          doering@cdc.informatik.tu-darmstadt.de
 *          Christoph Ludwig
 *          ludwig@fh-worms.de
 *          Falko Strenzke
 *          strenzke@flexsecure.de
 ******/

#include <botan/gfp_element.h>
#include <botan/numthry.h>
#include <botan/def_powm.h>
#include <botan/mp_types.h>
#include <botan/mp_asm.h>
#include <botan/mp_asmi.h>
#include <assert.h>

namespace Botan {

namespace {

void inner_montg_mult_sos(word result[], const word* a_bar, const word* b_bar, const word* n, const word* n_dash, u32bit s)
   {
   SecureVector<word> t;
   t.grow_to(2*s+1);

   // t = a_bar * b_bar
   for (u32bit i=0; i<s; i++)
      {
      word C = 0;
      word S = 0;
      for (u32bit j=0; j<s; j++)
         {
         // we use:
         // word word_madd3(word a, word b, word c, word d, word* carry)
         // returns a * b + c + d and resets the carry (not using it as input)

         S = word_madd3(a_bar[j], b_bar[i], t[i+j], &C);
         t[i+j] = S;
         }
      t[i+s] = C;
      }

   // ???
   for (u32bit i=0; i<s; i++)
      {
      // word word_madd2(word a, word b, word c, word* carry)
      // returns a * b + c, resets the carry

      word C = 0;
      word zero = 0;
      word m = word_madd2(t[i], n_dash[0], &zero);

      for (u32bit j=0; j<s; j++)
         {
         word S = word_madd3(m, n[j], t[i+j], &C);
         t[i+j] = S;
         }

      //// mp_mulop.cpp:
      ////word bigint_mul_add_words(word z[], const word x[], u32bit x_size, word y)
      u32bit cnt = 0;
      while (C > 0)
         {
         // we need not worry here about C > 1, because the other operand is zero
         word tmp = word_add(t[i+s+cnt], 0, &C);
         t[i+s+cnt] = tmp;
         cnt++;
         }
      }

   // u = t
   SecureVector<word> u;
   u.grow_to(s+1);
   for (u32bit j=0; j<s+1; j++)
      {
      u[j] = t[j+s];
      }

   // t = u - n
   word B = 0;
   word D = 0;
   for (u32bit i=0; i<s; i++)
      {
      D = word_sub(u[i], n[i], &B);
      t[i] = D;
      }
   D = word_sub(u[s], 0, &B);
   t[s] = D;

   // if t >= 0 (B == 0 -> no borrow), return t
   if(B == 0)
      {
      for (u32bit i=0; i<s; i++)
         {
         result[i] = t[i];
         }
      }
   else // else return u
      {
      for (u32bit i=0; i<s; i++)
         {
         result[i] = u[i];
         }
      }
   }

void montg_mult(BigInt& result, BigInt& a_bar, BigInt& b_bar, const BigInt& m, const BigInt& m_dash, const BigInt)
   {
   if(m.is_zero() || m_dash.is_zero())
      throw Invalid_Argument("montg_mult(): neither modulus nor m_dash may be zero (and one of them was)");

   if(a_bar.is_zero() || b_bar.is_zero())
      result = 0;

   u32bit s = m.sig_words();
   a_bar.grow_to(s);
   b_bar.grow_to(s);
   result.grow_to(s);

   inner_montg_mult_sos(result.get_reg(), a_bar.data(), b_bar.data(),
                        m.data(), m_dash.data(), s);
   }

/**
*calculates R=b^n (here b=2) with R>m (and R beeing as small as possible) for an odd modulus m.
* no check for oddity is performed!
*
* Distributed under the terms of the Botan license
*/
BigInt montgm_calc_r_oddmod(const BigInt& prime)
   {
   u32bit n = prime.sig_words();
   BigInt result(1);
   result <<= n*BOTAN_MP_WORD_BITS;
   return result;
   }

/**
*calculates m' with r*r^-1 - m*m' = 1
* where r^-1 is the multiplicative inverse of r to the modulus m
*/
BigInt montgm_calc_m_dash(const BigInt& r, const BigInt& m, const BigInt& r_inv)
   {
   BigInt result = ((r * r_inv) - BigInt(1))/m;
   return result;
   }

BigInt montg_trf_to_mres(const BigInt& ord_res, const BigInt& r, const BigInt& m)
   {
   BigInt result(ord_res);
   result *= r;
   result %= m;
   return result;
   }

BigInt montg_trf_to_ordres(const BigInt& m_res, const BigInt& m, const BigInt& r_inv)
   {
   BigInt result(m_res);
   result *= r_inv;
   result %= m;
   return result;
   }

}

GFpElement::GFpElement(const BigInt& p, const BigInt& value, bool use_montgm)
   : mp_mod(),
     m_value(value %p),
     m_use_montgm(use_montgm),
     m_is_trf(false)
   {
   assert(mp_mod.get() == 0);
   mp_mod = std::tr1::shared_ptr<GFpModulus>(new GFpModulus(p));
   assert(mp_mod->m_p_dash == 0);
   if(m_use_montgm)
      ensure_montgm_precomp();
   }

GFpElement::GFpElement(std::tr1::shared_ptr<GFpModulus> const mod, const BigInt& value, bool use_montgm)
   : mp_mod(),
     m_value(value % mod->m_p),
     m_use_montgm(use_montgm),
     m_is_trf(false)
   {
   assert(mp_mod.get() == 0);
   mp_mod = mod;
   }

GFpElement::GFpElement(const GFpElement& other)
   : m_value(other.m_value),
     m_use_montgm(other.m_use_montgm),
     m_is_trf(other.m_is_trf)

   {
   //creates an independent copy
   assert((other.m_is_trf && other.m_use_montgm) || !other.m_is_trf);
   mp_mod.reset(new GFpModulus(*other.mp_mod)); // copy-ctor of GFpModulus
   }

void GFpElement::turn_on_sp_red_mul() const
   {
   ensure_montgm_precomp();
   m_use_montgm = true;
   }

void GFpElement::turn_off_sp_red_mul() const
   {
   if(m_is_trf)
      {
      trf_to_ordres();
      // will happen soon anyway, so we can do it here already
      // (this is not lazy but way more secure concerning our internal logic here)
      }
   m_use_montgm = false;
   }

void GFpElement::ensure_montgm_precomp() const
   {
   if((!mp_mod->m_r.is_zero()) && (!mp_mod->m_r_inv.is_zero()) && (!mp_mod->m_p_dash.is_zero()))
      {
      // values are already set, nothing more to do
      }
   else
      {
      BigInt tmp_r(montgm_calc_r_oddmod(mp_mod->m_p));

      BigInt tmp_r_inv(inverse_mod(tmp_r, mp_mod->m_p));

      BigInt tmp_p_dash(montgm_calc_m_dash(tmp_r, mp_mod->m_p, tmp_r_inv));

      mp_mod->m_r.grow_reg(tmp_r.size());
      mp_mod->m_r_inv.grow_reg(tmp_r_inv.size());
      mp_mod->m_p_dash.grow_reg(tmp_p_dash.size());

      mp_mod->m_r = tmp_r;
      mp_mod->m_r_inv = tmp_r_inv;
      mp_mod->m_p_dash = tmp_p_dash;

      assert(!mp_mod->m_r.is_zero());
      assert(!mp_mod->m_r_inv.is_zero());
      assert(!mp_mod->m_p_dash.is_zero());
      }

   }

void GFpElement::set_shrd_mod(std::tr1::shared_ptr<GFpModulus> const p_mod)
   {
   mp_mod = p_mod;
   }

void GFpElement::trf_to_mres() const
   {
   if(!m_use_montgm)
      {
      throw Illegal_Transformation("GFpElement is not allowed to be transformed to m-residue");
      }
   assert(m_is_trf == false);
   assert(!mp_mod->m_r_inv.is_zero());
   assert(!mp_mod->m_p_dash.is_zero());
   m_value = montg_trf_to_mres(m_value, mp_mod->m_r, mp_mod->m_p);
   m_is_trf = true;
   }

void GFpElement::trf_to_ordres() const
   {
   assert(m_is_trf == true);
   m_value = montg_trf_to_ordres(m_value, mp_mod->m_p, mp_mod->m_r_inv);
   m_is_trf = false;
   }

bool GFpElement::align_operands_res(const GFpElement& lhs, const GFpElement& rhs) //static
   {
   assert(lhs.mp_mod->m_p == rhs.mp_mod->m_p);
   if(lhs.m_use_montgm && rhs.m_use_montgm)
      {
      assert(rhs.mp_mod->m_p_dash == lhs.mp_mod->m_p_dash);
      assert(rhs.mp_mod->m_r == lhs.mp_mod->m_r);
      assert(rhs.mp_mod->m_r_inv == lhs.mp_mod->m_r_inv);
      if(!lhs.m_is_trf && !rhs.m_is_trf)
         {
         return false;
         }
      else if(lhs.m_is_trf && rhs.m_is_trf)
         {
         return true;
         }
      else // one is transf., the other not
         {
         if(!lhs.m_is_trf)
            {
            lhs.trf_to_mres();
            assert(rhs.m_is_trf==true);
            return true;
            }
         assert(rhs.m_is_trf==false);
         assert(lhs.m_is_trf==true);
         rhs.trf_to_mres(); // the only possibility left...
         return true;
         }
      }
   else // at least one of them does not use mm
      // (so it is impossible that both use it)
      {
      if(lhs.m_is_trf)
         {
         lhs.trf_to_ordres();
         assert(rhs.m_is_trf == false);
         return false;
         }
      if(rhs.m_is_trf)
         {
         rhs.trf_to_ordres();
         assert(lhs.m_is_trf == false);
         return false;
         }
      return false;
      }
   assert(false);
   }

bool GFpElement::is_trf_to_mres() const
   {
   return m_is_trf;
   }

const BigInt& GFpElement::get_p() const
   {
   return (mp_mod->m_p);
   }

const BigInt& GFpElement::get_value() const
   {
   if(m_is_trf)
      {
      assert(m_use_montgm);
      trf_to_ordres();
      }
   return m_value;
   }

const BigInt& GFpElement::get_mres() const
   {
   if(!m_use_montgm)
      {
      // does the following exception really make sense?
      // wouldn´t it be better to simply turn on montg.mult. when
      // this explicit request is made?
      throw Illegal_Transformation("GFpElement is not allowed to be transformed to m-residue");
      }
   if(!m_is_trf)
      {
      trf_to_mres();
      }

   return m_value;
   }

const GFpElement& GFpElement::operator=(const GFpElement& other)
   {
   m_value.grow_reg(other.m_value.size()); // grow first for exception safety

   //m_value = other.m_value;

   //              m_use_montgm = other.m_use_montgm;
   //              m_is_trf = other.m_is_trf;
   // we want to keep the member pointers, which might be part of a "sharing group"
   // but we may not simply overwrite the BigInt values with those of the argument!!
   // if ours already contains precomputations, it would be hazardous to
   // set them back to zero.
   // thus we first check for equality of the moduli,
   // then whether either of the two objects already contains
   // precomputed values.

   // we also deal with the case were the pointers themsevles are equal:
   if(mp_mod.get() == other.mp_mod.get())
      {
      // everything ok, we are in the same sharing group anyway, nothing to do
      m_value = other.m_value; // cannot throw
      m_use_montgm = other.m_use_montgm;
      m_is_trf = other.m_is_trf;
      return *this;
      }
   if(mp_mod->m_p != other.mp_mod->m_p)
      {
      // the moduli are different, this is a special case
      // which will not occur in usual applications,
      // so we don´t hesitate to simply create new objects
      // (we do want to create an independent copy)
      mp_mod.reset(new GFpModulus(*other.mp_mod)); // this could throw,
      // and because of this
      // we haven't modified
      // anything so far
      m_value = other.m_value; // can't throw
      m_use_montgm = other.m_use_montgm;
      m_is_trf = other.m_is_trf;
      return *this;
      }
   // exception safety note: from now on we are on the safe
   // side with respect to the modulus,
   // so we can assign the value now:
   m_value = other.m_value;
   m_use_montgm = other.m_use_montgm;
   m_is_trf = other.m_is_trf;
   // the moduli are equal, but we deal with different sharing groups.
   // we will NOT fuse the sharing goups
   // and we will NOT reset already precomputed values
   if(mp_mod->has_precomputations())
      {
      // our own sharing group already has precomputed values,
      // so nothing to do.
      return *this;
      }
   else
      {
      // let´s see whether the argument has something for us...
      if(other.mp_mod->has_precomputations())
         {
         // fetch them for our sharing group
         // exc. safety note: grow first
         mp_mod->m_p_dash.grow_reg(other.mp_mod->m_p_dash.size());
         mp_mod->m_r.grow_reg(other.mp_mod->m_r.size());
         mp_mod->m_r_inv.grow_reg(other.mp_mod->m_r_inv.size());

         mp_mod->m_p_dash = other.mp_mod->m_p_dash;
         mp_mod->m_r = other.mp_mod->m_r;
         mp_mod->m_r_inv = other.mp_mod->m_r_inv;
         return *this;
         }
      }
   // our precomputations aren´t set, the arguments neither,
   // so we let them alone
   return *this;
   }

void GFpElement::share_assign(const GFpElement& other)
   {
   assert((other.m_is_trf && other.m_use_montgm) || !other.m_is_trf);

   // use grow_to to make it exc safe
   m_value.grow_reg(other.m_value.size());
   m_value = other.m_value;

   m_use_montgm = other.m_use_montgm;
   m_is_trf = other.m_is_trf;
   mp_mod = other.mp_mod; // cannot throw
   }

GFpElement& GFpElement::operator+=(const GFpElement& rhs)
   {
   GFpElement::align_operands_res(*this, rhs);

   workspace = m_value;
   workspace += rhs.m_value;
   if(workspace >= mp_mod->m_p)
      workspace -= mp_mod->m_p;

   m_value = workspace;
   assert(m_value < mp_mod->m_p);
   assert(m_value >= 0);

   return *this;
   }

GFpElement& GFpElement::operator-=(const GFpElement& rhs)
   {
   GFpElement::align_operands_res(*this, rhs);

   workspace = m_value;

   workspace -= rhs.m_value;

   if(workspace.is_negative())
      workspace += mp_mod->m_p;

   m_value = workspace;
   assert(m_value < mp_mod->m_p);
   assert(m_value >= 0);
   return *this;
   }

GFpElement& GFpElement::operator*= (u32bit rhs)
   {
   workspace = m_value;
   workspace *= rhs;
   workspace %= mp_mod->m_p;
   m_value = workspace;
   return *this;
   }

GFpElement& GFpElement::operator*=(const GFpElement& rhs)
   {
   assert(rhs.mp_mod->m_p == mp_mod->m_p);
   // here, we do not use align_operands_res() for one simple reason:
   // we want to enforce the transformation to an m-residue, otherwise it would
  // never happen
   if(m_use_montgm && rhs.m_use_montgm)
      {
      assert(rhs.mp_mod->m_p == mp_mod->m_p); // is montgm. mult is on, then precomps must be there
      assert(rhs.mp_mod->m_p_dash == mp_mod->m_p_dash);
      assert(rhs.mp_mod->m_r == mp_mod->m_r);
      if(!m_is_trf)
         {
         trf_to_mres();
         }
      if(!rhs.m_is_trf)
         {
         rhs.trf_to_mres();
         }
      workspace = m_value;
      montg_mult(m_value, workspace, rhs.m_value, mp_mod->m_p, mp_mod->m_p_dash, mp_mod->m_r);
      }
   else // ordinary multiplication
      {
      if(m_is_trf)
         {
         assert(m_use_montgm);
         trf_to_ordres();
         }
      if(rhs.m_is_trf)
         {
         assert(rhs.m_use_montgm);
         rhs.trf_to_ordres();
         }

      workspace = m_value;
      workspace *= rhs.m_value;
      workspace %= mp_mod->m_p;
      m_value = workspace;
      }
   return *this;
   }

GFpElement& GFpElement::operator/=(const GFpElement& rhs)
   {
   bool use_mres = GFpElement::align_operands_res(*this, rhs);
   assert((this->m_is_trf && rhs.m_is_trf) || !(this->m_is_trf && rhs.m_is_trf));
   // (internal note: see C86)
   if(use_mres)
      {
      assert(m_use_montgm && rhs.m_use_montgm);
      GFpElement rhs_ordres(rhs);
      rhs_ordres.trf_to_ordres();
      rhs_ordres.inverse_in_place();
      workspace = m_value;
      workspace *=  rhs_ordres.get_value();
      workspace %= mp_mod->m_p;
      m_value = workspace;

      }
   else
      {
      GFpElement inv_rhs(rhs);
      inv_rhs.inverse_in_place();
      *this *= inv_rhs;
      }
   return *this;
   }

bool GFpElement::is_zero()
   {
   return (m_value.is_zero());
   // this is correct because x_bar = x * r = x = 0 for x = 0
   }

GFpElement& GFpElement::inverse_in_place()
   {
   m_value = inverse_mod(m_value, mp_mod->m_p);
   if(m_is_trf)
      {
      assert(m_use_montgm);

      m_value *= mp_mod->m_r;
      m_value *= mp_mod->m_r;
      m_value %= mp_mod->m_p;
      }
   assert(m_value <= mp_mod->m_p);
   return *this;
   }

GFpElement& GFpElement::negate()
   {
   m_value = mp_mod->m_p - m_value;
   assert(m_value <= mp_mod->m_p);
   return *this;
   }

void GFpElement::swap(GFpElement& other)
   {
   m_value.swap(other.m_value);
   mp_mod.swap(other.mp_mod);
   std::swap<bool>(m_use_montgm,other.m_use_montgm);
   std::swap<bool>(m_is_trf,other.m_is_trf);
   }

std::ostream& operator<<(std::ostream& output, const GFpElement& elem)
   {
   return output << '(' << elem.get_value() << "," << elem.get_p() << ')';
   }

bool operator==(const GFpElement& lhs, const GFpElement& rhs)
   {
   // for effeciency reasons we firstly check whether
   //the modulus pointers are different in the first place:
   if(lhs.get_ptr_mod() != rhs.get_ptr_mod())
      {
      if(lhs.get_p() != rhs.get_p())
         {
         return false;
         }
      }
   // so the modulus is equal, now check the values
   bool use_mres = GFpElement::align_operands_res(lhs, rhs);

   if(use_mres)
      {
      return (lhs.get_mres() == rhs.get_mres());
      }
   else
      {
      return(lhs.get_value() == rhs.get_value());
      }
   }

GFpElement operator+(const GFpElement& lhs, const GFpElement& rhs)
   {
   // consider the case that lhs and rhs both use montgm:
   // then += returns an element which uses montgm.
   // thus the return value of op+ here will be an element
   // using montgm in this case
   // NOTE: the rhs might be transformed when using op+, the lhs never
   GFpElement result(lhs);
   result += rhs;
   return result;
   }

GFpElement operator-(const GFpElement& lhs, const GFpElement& rhs)
   {
   GFpElement result(lhs);
   result -= rhs;
   return result;
   // NOTE: the rhs might be transformed when using op-, the lhs never
   }

GFpElement operator-(const GFpElement& lhs)
   {
   return(GFpElement(lhs)).negate();
   }

GFpElement operator*(const GFpElement& lhs, const GFpElement& rhs)
   {
   // consider the case that lhs and rhs both use montgm:
   // then *= returns an element which uses montgm.
   // thus the return value of op* here will be an element
   // using montgm in this case
   GFpElement result(lhs);
   result *= rhs;
   return result;
   }

GFpElement operator*(const GFpElement& lhs, u32bit rhs)
   {
   GFpElement result(lhs);
   result *= rhs;
   return result;
   }

GFpElement operator*(u32bit lhs, const GFpElement& rhs)
   {
   return rhs*lhs;
   }

GFpElement operator/(const GFpElement& lhs, const GFpElement& rhs)
   {
   GFpElement result (lhs);
   result /= rhs;
   return result;
   }

SecureVector<byte> FE2OSP(const GFpElement& elem)
   {
   return BigInt::encode_1363(elem.get_value(), elem.get_p().bytes());
   }

GFpElement OS2FEP(MemoryRegion<byte> const& os, BigInt p)
   {
   return GFpElement(p, BigInt::decode(os.begin(), os.size()));
   }

GFpElement inverse(const GFpElement& elem)
   {
   return GFpElement(elem).inverse_in_place();
   }

}