Coverage Report

Created: 2020-11-21 08:34

/src/botan/build/include/botan/internal/monty.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
* (C) 2018 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6
7
#ifndef BOTAN_MONTY_INT_H_
8
#define BOTAN_MONTY_INT_H_
9
10
#include <botan/bigint.h>
11
12
namespace Botan {
13
14
class Modular_Reducer;
15
16
class Montgomery_Params;
17
18
/*
19
* Compute -input^-1 mod 2^MP_WORD_BITS. Throws an exception if input
20
* is even. If input is odd, then input and 2^n are relatively prime
21
* and an inverse exists.
22
*/
23
word monty_inverse(word input);
24
25
/**
26
* The Montgomery representation of an integer
27
*/
28
class BOTAN_UNSTABLE_API Montgomery_Int final
29
   {
30
   public:
31
      /**
32
      * Create a zero-initialized Montgomery_Int
33
      */
34
0
      Montgomery_Int(std::shared_ptr<const Montgomery_Params> params) : m_params(params) {}
35
36
      /**
37
      * Create a Montgomery_Int
38
      */
39
      Montgomery_Int(std::shared_ptr<const Montgomery_Params> params,
40
                     const BigInt& v,
41
                     bool redc_needed = true);
42
43
      /**
44
      * Create a Montgomery_Int
45
      */
46
      Montgomery_Int(std::shared_ptr<const Montgomery_Params> params,
47
                     const uint8_t bits[], size_t len,
48
                     bool redc_needed = true);
49
50
      /**
51
      * Create a Montgomery_Int
52
      */
53
      Montgomery_Int(std::shared_ptr<const Montgomery_Params> params,
54
                     const word words[], size_t len,
55
                     bool redc_needed = true);
56
57
      bool operator==(const Montgomery_Int& other) const;
58
0
      bool operator!=(const Montgomery_Int& other) const { return (m_v != other.m_v); }
59
60
      std::vector<uint8_t> serialize() const;
61
62
      size_t size() const;
63
      bool is_one() const;
64
      bool is_zero() const;
65
66
      void fix_size();
67
68
      /**
69
      * Return the value to normal mod-p space
70
      */
71
      BigInt value() const;
72
73
      /**
74
      * Return the Montgomery representation
75
      */
76
92.7M
      const BigInt& repr() const { return m_v; }
77
78
      Montgomery_Int operator+(const Montgomery_Int& other) const;
79
80
      Montgomery_Int operator-(const Montgomery_Int& other) const;
81
82
      Montgomery_Int& operator+=(const Montgomery_Int& other);
83
84
      Montgomery_Int& operator-=(const Montgomery_Int& other);
85
86
      Montgomery_Int operator*(const Montgomery_Int& other) const;
87
88
      Montgomery_Int& operator*=(const Montgomery_Int& other);
89
90
      Montgomery_Int& operator*=(const secure_vector<word>& other);
91
92
      Montgomery_Int& add(const Montgomery_Int& other,
93
                          secure_vector<word>& ws);
94
95
      Montgomery_Int& sub(const Montgomery_Int& other,
96
                          secure_vector<word>& ws);
97
98
      Montgomery_Int mul(const Montgomery_Int& other,
99
                         secure_vector<word>& ws) const;
100
101
      Montgomery_Int& mul_by(const Montgomery_Int& other,
102
                             secure_vector<word>& ws);
103
104
      Montgomery_Int& mul_by(const secure_vector<word>& other,
105
                             secure_vector<word>& ws);
106
107
      Montgomery_Int square(secure_vector<word>& ws) const;
108
109
      Montgomery_Int& square_this(secure_vector<word>& ws);
110
111
      Montgomery_Int& square_this_n_times(secure_vector<word>& ws, size_t n);
112
113
      Montgomery_Int multiplicative_inverse() const;
114
115
      Montgomery_Int additive_inverse() const;
116
117
      Montgomery_Int& mul_by_2(secure_vector<word>& ws);
118
119
      Montgomery_Int& mul_by_3(secure_vector<word>& ws);
120
121
      Montgomery_Int& mul_by_4(secure_vector<word>& ws);
122
123
      Montgomery_Int& mul_by_8(secure_vector<word>& ws);
124
125
1.61M
      void const_time_poison() const { m_v.const_time_poison(); }
126
106k
      void const_time_unpoison() const { return m_v.const_time_unpoison(); }
127
128
   private:
129
      std::shared_ptr<const Montgomery_Params> m_params;
130
      BigInt m_v;
131
   };
132
133
/**
134
* Parameters for Montgomery Reduction
135
*/
136
class BOTAN_UNSTABLE_API Montgomery_Params final
137
   {
138
   public:
139
      /**
140
      * Initialize a set of Montgomery reduction parameters. These values
141
      * can be shared by all values in a specific Montgomery domain.
142
      */
143
      Montgomery_Params(const BigInt& p, const Modular_Reducer& mod_p);
144
145
      /**
146
      * Initialize a set of Montgomery reduction parameters. These values
147
      * can be shared by all values in a specific Montgomery domain.
148
      */
149
      Montgomery_Params(const BigInt& p);
150
151
213k
      const BigInt& p() const { return m_p; }
152
106k
      const BigInt& R1() const { return m_r1; }
153
106k
      const BigInt& R2() const { return m_r2; }
154
0
      const BigInt& R3() const { return m_r3; }
155
156
0
      word p_dash() const { return m_p_dash; }
157
158
1.72M
      size_t p_words() const { return m_p_words; }
159
160
      BigInt redc(const BigInt& x,
161
                  secure_vector<word>& ws) const;
162
163
      BigInt mul(const BigInt& x,
164
                 const BigInt& y,
165
                 secure_vector<word>& ws) const;
166
167
      BigInt mul(const BigInt& x,
168
                 const secure_vector<word>& y,
169
                 secure_vector<word>& ws) const;
170
171
      void mul_by(BigInt& x,
172
                  const secure_vector<word>& y,
173
                  secure_vector<word>& ws) const;
174
175
      void mul_by(BigInt& x, const BigInt& y,
176
                  secure_vector<word>& ws) const;
177
178
      BigInt sqr(const BigInt& x,
179
                 secure_vector<word>& ws) const;
180
181
      void square_this(BigInt& x,
182
                       secure_vector<word>& ws) const;
183
184
      BigInt inv_mod_p(const BigInt& x) const;
185
186
   private:
187
      BigInt m_p;
188
      BigInt m_r1;
189
      BigInt m_r2;
190
      BigInt m_r3;
191
      word m_p_dash;
192
      size_t m_p_words;
193
   };
194
195
}
196
197
#endif