File: pow_mod.cpp

package info (click to toggle)
monotone 0.31-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 20,680 kB
  • ctags: 14,801
  • sloc: cpp: 87,711; ansic: 64,862; sh: 5,691; lisp: 954; perl: 783; makefile: 509; python: 265; sql: 98; sed: 16
file content (142 lines) | stat: -rw-r--r-- 4,118 bytes parent folder | download
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
/*************************************************
* Modular Exponentiation Source File             *
* (C) 1999-2005 The Botan Project                *
*************************************************/

#include <botan/numthry.h>
#include <vector>

namespace Botan {

namespace {

/*************************************************
* Exponentiation Window Size                     *
*************************************************/
u32bit window_size(u32bit exp_bits)
   {
   struct mapping { u32bit bits; u32bit window_size; };

   static const mapping wsize[] = {
      { 2048, 7 },
      { 1024, 6 },
      {  256, 5 },
      {  128, 4 },
      {   64, 3 },
      {    0, 0 }
   };

   for(u32bit j = 0; wsize[j].bits; j++)
      {
      if(exp_bits >= wsize[j].bits)
         return wsize[j].window_size;
      }
   return 1;
   }

/*************************************************
* Left-to-Right Binary Modular Exponentiation    *
*************************************************/
BigInt power_mod_l2r(const BigInt& basex, const BigInt& exp,
                     ModularReducer* reducer)
   {
   const BigInt base = reducer->convert_in(basex);
   const u32bit exp_bits = exp.bits();

   BigInt x = reducer->convert_in(1);
   for(u32bit j = exp_bits; j > 0; j--)
      {
      x = reducer->square(x);
      if(exp.get_bit(j-1))
         x = reducer->multiply(x, base);
      }
   return reducer->convert_out(x);
   }

/*************************************************
* Modular Exponentiation with g = 2              *
*************************************************/
BigInt power_mod_g2(const BigInt& exp, ModularReducer* reducer)
   {
   if(reducer->must_convert())
      throw Internal_Error("power_mod_g2: Can't use this reducer");

   const u32bit exp_bits = exp.bits();
   BigInt x = 1;
   for(u32bit j = exp_bits; j > 0; j--)
      {
      x = reducer->square(x);
      if(exp.get_bit(j-1))
         {
         x <<= 1;
         x = reducer->reduce(x);
         }
      }
   return x;
   }

/*************************************************
* Window Modular Exponentiation                  *
*************************************************/
BigInt power_mod_window(const BigInt& base, const BigInt& exp,
                        ModularReducer* reducer, u32bit window_bits)
   {
   if(window_bits < 2)
      throw Internal_Error("power_mod_window: Window size too small");

   std::vector<BigInt> g((1 << window_bits) - 1);

   g[0] = reducer->convert_in(base);
   for(u32bit j = 1; j != g.size(); j++)
      g[j] = reducer->multiply(g[j-1], g[0]);

   const u32bit exp_nibbles = (exp.bits() + window_bits - 1) / window_bits;

   BigInt x = reducer->convert_in(1);
   for(u32bit j = exp_nibbles; j > 0; j--)
      {
      for(u32bit k = 0; k != window_bits; k++)
         x = reducer->square(x);
      u32bit nibble = exp.get_nibble(j-1, window_bits);
      if(nibble)
         x = reducer->multiply(x, g[nibble-1]);
      }
   return reducer->convert_out(x);
   }

}

/*************************************************
* Modular Exponentiation                         *
*************************************************/
BigInt power_mod(const BigInt& base, const BigInt& exp, const BigInt& mod)
   {
   ModularReducer* reducer = get_reducer(mod);
   BigInt x = power_mod(base, exp, reducer);
   delete reducer;
   return x;
   }

/*************************************************
* Modular Exponentiation Algorithm Dispatch      *
*************************************************/
BigInt power_mod(const BigInt& base, const BigInt& exp,
                 ModularReducer* reducer)
   {
   if(base.is_negative())
      throw Invalid_Argument("power_mod: base must be positive");
   if(exp.is_negative())
      throw Invalid_Argument("power_mod: exponent must be positive");
   if(exp.is_zero())
      return 1;

   const u32bit window_bits = window_size(exp.bits());

   if(base == 2 && !reducer->must_convert())
      return power_mod_g2(exp, reducer);
   if(window_bits > 1)
      return power_mod_window(base, exp, reducer, window_bits);
   return power_mod_l2r(base, exp, reducer);
   }

}