/src/Botan-3.13.0/build/include/public/botan/reducer.h
Line | Count | Source |
1 | | /* |
2 | | * Modular Reducer |
3 | | * (C) 1999-2010 Jack Lloyd |
4 | | * |
5 | | * Botan is released under the Simplified BSD License (see license.txt) |
6 | | */ |
7 | | |
8 | | #ifndef BOTAN_MODULAR_REDUCER_H_ |
9 | | #define BOTAN_MODULAR_REDUCER_H_ |
10 | | |
11 | | #include <botan/bigint.h> |
12 | | |
13 | | BOTAN_DEPRECATED_HEADER("reducer.h") |
14 | | |
15 | | namespace Botan { |
16 | | |
17 | | /** |
18 | | * Modular Reducer |
19 | | * |
20 | | * This class is deprecated without replacement |
21 | | */ |
22 | | class BOTAN_PUBLIC_API(2, 0) Modular_Reducer final { |
23 | | public: |
24 | | /** |
25 | | * Return the modulus of this reducer |
26 | | * @return the modulus |
27 | | */ |
28 | 0 | const BigInt& get_modulus() const { return m_modulus; } |
29 | | |
30 | | /** |
31 | | * Reduce a value modulo p |
32 | | * @param x the value to reduce |
33 | | * @return (x % p) |
34 | | */ |
35 | | BigInt reduce(const BigInt& x) const; |
36 | | |
37 | | /** |
38 | | * Multiply mod p |
39 | | * @param x the first operand |
40 | | * @param y the second operand |
41 | | * @return (x * y) % p |
42 | | */ |
43 | 0 | BigInt multiply(const BigInt& x, const BigInt& y) const { return reduce(x * y); } |
44 | | |
45 | | /** |
46 | | * Multiply mod p |
47 | | * @return (x * y * z) % p |
48 | | */ |
49 | 0 | BigInt multiply(const BigInt& x, const BigInt& y, const BigInt& z) const { return multiply(x, multiply(y, z)); } |
50 | | |
51 | | /** |
52 | | * Square mod p |
53 | | * @param x the value to square |
54 | | * @return (x * x) % p |
55 | | */ |
56 | 0 | BigInt square(const BigInt& x) const { return reduce(x * x); } |
57 | | |
58 | | /** |
59 | | * Cube mod p |
60 | | * @param x the value to cube |
61 | | * @return (x * x * x) % p |
62 | | */ |
63 | 0 | BigInt cube(const BigInt& x) const { return multiply(x, this->square(x)); } |
64 | | |
65 | | /** |
66 | | * Low level reduction function. Mostly for internal use. |
67 | | * Sometimes useful for performance by reducing temporaries |
68 | | * Reduce x mod p and place the output in out. |
69 | | * |
70 | | * @warning X and out must not reference each other |
71 | | * |
72 | | * ws is an (ignored) a temporary workspace. |
73 | | */ |
74 | 0 | void reduce(BigInt& out, const BigInt& x, secure_vector<word>& /*ws*/) const { out = reduce(x); } |
75 | | |
76 | | /** |
77 | | * Test whether this reducer was initialized with a non-zero modulus |
78 | | * @return true if a modulus is set |
79 | | */ |
80 | 0 | bool initialized() const { return (m_mod_words != 0); } |
81 | | |
82 | | /** |
83 | | * Create an uninitialized Modular_Reducer |
84 | | */ |
85 | 0 | BOTAN_DEPRECATED("Use for_public_modulus or for_secret_modulus") Modular_Reducer() : m_mod_words(0) {} |
86 | | |
87 | | /** |
88 | | * Accepts m == 0 and leaves the Modular_Reducer in an uninitialized state |
89 | | */ |
90 | | explicit Modular_Reducer(const BigInt& mod); |
91 | | |
92 | | /** |
93 | | * Requires that m > 0 |
94 | | */ |
95 | 0 | static Modular_Reducer for_public_modulus(const BigInt& m) { return Modular_Reducer(m); } |
96 | | |
97 | | /** |
98 | | * Requires that m > 0 |
99 | | */ |
100 | 0 | static Modular_Reducer for_secret_modulus(const BigInt& m) { return Modular_Reducer(m); } |
101 | | |
102 | | private: |
103 | 0 | Modular_Reducer(const BigInt& m, BigInt mu, size_t mw) : m_modulus(m), m_mu(std::move(mu)), m_mod_words(mw) {} |
104 | | |
105 | | BigInt m_modulus, m_mu; |
106 | | size_t m_mod_words; |
107 | | }; |
108 | | |
109 | | } // namespace Botan |
110 | | |
111 | | #endif |