Botan 3.4.0
Crypto and TLS for C&
monty.h
Go to the documentation of this file.
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
12namespace Botan {
13
14class Modular_Reducer;
15
16class Montgomery_Params;
17
18/**
19* The Montgomery representation of an integer
20*/
22 public:
23 /**
24 * Create a zero-initialized Montgomery_Int
25 */
26 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 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 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
76
78
79 Montgomery_Int operator*(const Montgomery_Int& other) const;
80
82
84
86
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
94
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 void const_time_poison() const { m_v.const_time_poison(); }
116
117 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*/
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 const BigInt& p() const { return m_p; }
142
143 const BigInt& R1() const { return m_r1; }
144
145 const BigInt& R2() const { return m_r2; }
146
147 const BigInt& R3() const { return m_r3; }
148
149 word p_dash() const { return m_p_dash; }
150
151 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
Montgomery_Int(std::shared_ptr< const Montgomery_Params > params)
Definition monty.h:26
const BigInt & repr() const
Definition monty.h:69
void const_time_poison() const
Definition monty.h:115
bool operator!=(const Montgomery_Int &other) const
Definition monty.h:51
void const_time_unpoison() const
Definition monty.h:117
size_t p_words() const
Definition monty.h:151
const BigInt & R3() const
Definition monty.h:147
const BigInt & R2() const
Definition monty.h:145
const BigInt & R1() const
Definition monty.h:143
word p_dash() const
Definition monty.h:149
const BigInt & p() const
Definition monty.h:141
int(* final)(unsigned char *, CTX *)
#define BOTAN_TEST_API
Definition compiler.h:51
BigInt operator*(const BigInt &x, const BigInt &y)
Definition big_ops3.cpp:46
BigInt square(const BigInt &x)
Definition numthry.cpp:157
OctetString operator+(const OctetString &k1, const OctetString &k2)
Definition symkey.cpp:99
BigInt operator-(const BigInt &x, const BigInt &y)
Definition bigint.h:963
bool operator==(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition alg_id.cpp:54
std::vector< T, Alloc > & operator+=(std::vector< T, Alloc > &out, const std::vector< T, Alloc2 > &in)
Definition secmem.h:80
constexpr auto operator-=(Strong< T1, Tags... > &a, T2 b)
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61
constexpr auto operator*=(Strong< T1, Tags... > &a, T2 b)