aboutsummaryrefslogtreecommitdiffstats
path: root/src/mp_monty.cpp
blob: 00e14a2b4a8e2ff0465856743edee6573909a6d6 (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
/*************************************************
* Montgomery Reduction Source File               *
* (C) 1999-2008 Jack Lloyd                       *
*     2006 Luca Piccarreta                       *
*************************************************/

#include <botan/mp_core.h>
#include <botan/mp_asm.h>
#include <botan/mp_asmi.h>

namespace Botan {

extern "C" {

/*************************************************
* Montgomery Reduction Algorithm                 *
*************************************************/
void bigint_monty_redc(word z[], u32bit z_size,
                       const word x[], u32bit x_size, word u)
   {
   const u32bit blocks_of_8 = x_size - (x_size % 8);

   for(u32bit j = 0; j != x_size; ++j)
      {
      word* z_j = z + j;

      const word y = z_j[0] * u;

      word carry = 0;

      for(u32bit i = 0; i != blocks_of_8; i += 8)
         carry = word8_madd3(z_j + i, x + i, y, carry);

      for(u32bit i = blocks_of_8; i != x_size; ++i)
         z_j[i] = word_madd3(x[i], y, z_j[i], &carry);

      word z_sum = z_j[x_size] + carry;
      carry = (z_sum < z_j[x_size]);
      z_j[x_size] = z_sum;

      for(u32bit k = x_size + 1; carry && k != z_size - j; ++k)
         {
         ++z_j[k];
         carry = !z_j[k];
         }
      }

   // Check if z[x_size...x_size+1] >= x[0...x_size] using bigint_cmp (inlined)
   if(!z[x_size + x_size])
      {
      for(u32bit j = x_size; j > 0; --j)
         {
         if(z[x_size + j - 1] > x[j-1])
            break;

         if(z[x_size + j - 1] < x[j-1])
            return;
         }
      }

   // If the compare above is true, subtract using bigint_sub2 (inlined)
   word carry = 0;

   for(u32bit j = 0; j != blocks_of_8; j += 8)
      carry = word8_sub2(z + x_size + j, x + j, carry);

   for(u32bit j = blocks_of_8; j != x_size; ++j)
      z[x_size + j] = word_sub(z[x_size + j], x[j], &carry);

   if(carry)
      --z[x_size+x_size];
   }

}

}