Botan 3.6.1
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
12#include <botan/internal/ct_utils.h>
13
14namespace Botan {
15
16class Modular_Reducer;
17
18class Montgomery_Params;
19
20/**
21* The Montgomery representation of an integer
22*/
24 public:
25 /**
26 * Create a zero-initialized Montgomery_Int
27 */
28 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 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 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
78
80
81 Montgomery_Int operator*(const Montgomery_Int& other) const;
82
84
86
88
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
96
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 void _const_time_poison() const { CT::poison(m_v); }
118
119 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*/
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 const BigInt& p() const { return m_p; }
144
145 const BigInt& R1() const { return m_r1; }
146
147 const BigInt& R2() const { return m_r2; }
148
149 const BigInt& R3() const { return m_r3; }
150
151 word p_dash() const { return m_p_dash; }
152
153 size_t p_words() const { return m_p_words; }
154
155 BigInt redc(const BigInt& x, secure_vector<word>& ws) const;
156
157 BigInt mul(const BigInt& x, const BigInt& y, secure_vector<word>& ws) const;
158
159 BigInt mul(const BigInt& x, const secure_vector<word>& y, secure_vector<word>& ws) const;
160
161 void mul_by(BigInt& x, const secure_vector<word>& y, secure_vector<word>& ws) const;
162
163 void mul_by(BigInt& x, const BigInt& y, secure_vector<word>& ws) const;
164
165 BigInt sqr(const BigInt& x, secure_vector<word>& ws) const;
166
167 void square_this(BigInt& x, secure_vector<word>& ws) const;
168
169 BigInt inv_mod_p(const BigInt& x) const;
170
171 private:
172 BigInt m_p;
173 BigInt m_r1;
174 BigInt m_r2;
175 BigInt m_r3;
176 word m_p_dash;
177 size_t m_p_words;
178};
179
180} // namespace Botan
181
182#endif
void _const_time_unpoison() const
Definition monty.h:119
Montgomery_Int(std::shared_ptr< const Montgomery_Params > params)
Definition monty.h:28
const BigInt & repr() const
Definition monty.h:71
bool operator!=(const Montgomery_Int &other) const
Definition monty.h:53
void _const_time_poison() const
Definition monty.h:117
size_t p_words() const
Definition monty.h:153
const BigInt & R3() const
Definition monty.h:149
const BigInt & R2() const
Definition monty.h:147
const BigInt & R1() const
Definition monty.h:145
word p_dash() const
Definition monty.h:151
const BigInt & p() const
Definition monty.h:143
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:1094
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)