Coverage Report

Created: 2024-11-29 06:10

/src/botan/build/include/internal/botan/internal/monty.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
* (C) 2018,2024 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
#include <botan/internal/ct_utils.h>
13
14
namespace Botan {
15
16
class Modular_Reducer;
17
18
class Montgomery_Params;
19
20
/**
21
* The Montgomery representation of an integer
22
*/
23
class BOTAN_TEST_API Montgomery_Int final {
24
   public:
25
      /**
26
      * Create a zero-initialized Montgomery_Int
27
      */
28
0
      Montgomery_Int(std::shared_ptr<const Montgomery_Params> params) : m_params(std::move(params)) {}
29
30
      /**
31
      * Create a Montgomery_Int
32
      */
33
      Montgomery_Int(const std::shared_ptr<const Montgomery_Params>& params, const BigInt& v, bool redc_needed = true);
34
35
      /**
36
      * Create a Montgomery_Int
37
      */
38
      Montgomery_Int(const std::shared_ptr<const Montgomery_Params>& params,
39
                     const uint8_t bits[],
40
                     size_t len,
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 word words[],
48
                     size_t len,
49
                     bool redc_needed = true);
50
51
      bool operator==(const Montgomery_Int& other) const;
52
53
0
      bool operator!=(const Montgomery_Int& other) const { return (m_v != other.m_v); }
54
55
      std::vector<uint8_t> serialize() const;
56
57
      size_t size() const;
58
      bool is_one() const;
59
      bool is_zero() const;
60
61
      void fix_size();
62
63
      /**
64
      * Return the value to normal mod-p space
65
      */
66
      BigInt value() const;
67
68
      /**
69
      * Return the Montgomery representation
70
      */
71
3.34M
      const BigInt& repr() const { return m_v; }
72
73
      Montgomery_Int operator+(const Montgomery_Int& other) const;
74
75
      Montgomery_Int operator-(const Montgomery_Int& other) const;
76
77
      Montgomery_Int& operator+=(const Montgomery_Int& other);
78
79
      Montgomery_Int& operator-=(const Montgomery_Int& other);
80
81
      Montgomery_Int operator*(const Montgomery_Int& other) const;
82
83
      Montgomery_Int& operator*=(const Montgomery_Int& other);
84
85
      Montgomery_Int& operator*=(const secure_vector<word>& other);
86
87
      Montgomery_Int& add(const Montgomery_Int& other, secure_vector<word>& ws);
88
89
      Montgomery_Int& sub(const Montgomery_Int& other, secure_vector<word>& ws);
90
91
      Montgomery_Int mul(const Montgomery_Int& other, secure_vector<word>& ws) const;
92
93
      Montgomery_Int& mul_by(const Montgomery_Int& other, secure_vector<word>& ws);
94
95
      Montgomery_Int& mul_by(const secure_vector<word>& other, secure_vector<word>& ws);
96
97
      Montgomery_Int square(secure_vector<word>& ws) const;
98
99
      Montgomery_Int cube(secure_vector<word>& ws) const;
100
101
      Montgomery_Int& square_this(secure_vector<word>& ws);
102
103
      Montgomery_Int& square_this_n_times(secure_vector<word>& ws, size_t n);
104
105
      Montgomery_Int multiplicative_inverse() const;
106
107
      Montgomery_Int additive_inverse() const;
108
109
      Montgomery_Int& mul_by_2(secure_vector<word>& ws);
110
111
      Montgomery_Int& mul_by_3(secure_vector<word>& ws);
112
113
      Montgomery_Int& mul_by_4(secure_vector<word>& ws);
114
115
      Montgomery_Int& mul_by_8(secure_vector<word>& ws);
116
117
39.0k
      void _const_time_poison() const { CT::poison(m_v); }
118
119
19.7k
      void _const_time_unpoison() const { CT::unpoison(m_v); }
120
121
   private:
122
      std::shared_ptr<const Montgomery_Params> m_params;
123
      BigInt m_v;
124
};
125
126
/**
127
* Parameters for Montgomery Reduction
128
*/
129
class BOTAN_TEST_API Montgomery_Params final {
130
   public:
131
      /**
132
      * Initialize a set of Montgomery reduction parameters. These values
133
      * can be shared by all values in a specific Montgomery domain.
134
      */
135
      Montgomery_Params(const BigInt& p, const Modular_Reducer& mod_p);
136
137
      /**
138
      * Initialize a set of Montgomery reduction parameters. These values
139
      * can be shared by all values in a specific Montgomery domain.
140
      */
141
      Montgomery_Params(const BigInt& p);
142
143
119k
      const BigInt& p() const { return m_p; }
144
145
221k
      const BigInt& R1() const { return m_r1; }
146
147
95.7k
      const BigInt& R2() const { return m_r2; }
148
149
79.1k
      const BigInt& R3() const { return m_r3; }
150
151
0
      word p_dash() const { return m_p_dash; }
152
153
217k
      size_t p_words() const { return m_p_words; }
154
155
      BigInt redc(const BigInt& x, secure_vector<word>& ws) const;
156
157
      void redc_in_place(BigInt& x, secure_vector<word>& ws) const;
158
159
      void mul(BigInt& z, const BigInt& x, const BigInt& y, secure_vector<word>& ws) const;
160
161
      void mul(BigInt& z, const BigInt& x, std::span<const word> y, secure_vector<word>& ws) const;
162
163
      BigInt mul(const BigInt& x, const BigInt& y, secure_vector<word>& ws) const;
164
165
      BigInt mul(const BigInt& x, std::span<const word> y, secure_vector<word>& ws) const;
166
167
      void mul_by(BigInt& x, std::span<const word> y, secure_vector<word>& ws) const;
168
169
      void mul_by(BigInt& x, const BigInt& y, secure_vector<word>& ws) const;
170
171
      BigInt sqr(const BigInt& x, secure_vector<word>& ws) const;
172
173
      BigInt sqr(std::span<const word> x, secure_vector<word>& ws) const;
174
175
      void sqr(BigInt& z, const BigInt& x, secure_vector<word>& ws) const;
176
177
      void sqr(BigInt& z, std::span<const word> x, secure_vector<word>& ws) const;
178
179
      void square_this(BigInt& x, secure_vector<word>& ws) const;
180
181
      BigInt inv_mod_p(const BigInt& x, secure_vector<word>& ws) const;
182
183
   private:
184
      BigInt m_p;
185
      BigInt m_r1;
186
      BigInt m_r2;
187
      BigInt m_r3;
188
      word m_p_dash;
189
      size_t m_p_words;
190
};
191
192
}  // namespace Botan
193
194
#endif