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