/src/botan/build/include/public/botan/reducer.h
Line | Count | Source (jump to first uncovered line) |
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_FUTURE_INTERNAL_HEADER(reducer.h) |
14 | | |
15 | | namespace Botan { |
16 | | |
17 | | /** |
18 | | * Modular Reducer (using Barrett's technique) |
19 | | */ |
20 | | class BOTAN_PUBLIC_API(2, 0) Modular_Reducer final { |
21 | | public: |
22 | 45 | const BigInt& get_modulus() const { return m_modulus; } |
23 | | |
24 | | BigInt reduce(const BigInt& x) const; |
25 | | |
26 | | /** |
27 | | * Multiply mod p |
28 | | * @param x the first operand |
29 | | * @param y the second operand |
30 | | * @return (x * y) % p |
31 | | */ |
32 | 639k | BigInt multiply(const BigInt& x, const BigInt& y) const { return reduce(x * y); } |
33 | | |
34 | | /** |
35 | | * Multiply mod p |
36 | | * @return (x * y * z) % p |
37 | | */ |
38 | 0 | BigInt multiply(const BigInt& x, const BigInt& y, const BigInt& z) const { return multiply(x, multiply(y, z)); } |
39 | | |
40 | | /** |
41 | | * Square mod p |
42 | | * @param x the value to square |
43 | | * @return (x * x) % p |
44 | | */ |
45 | | BigInt square(const BigInt& x) const; |
46 | | |
47 | | /** |
48 | | * Cube mod p |
49 | | * @param x the value to cube |
50 | | * @return (x * x * x) % p |
51 | | */ |
52 | 38 | BigInt cube(const BigInt& x) const { return multiply(x, this->square(x)); } |
53 | | |
54 | | /** |
55 | | * Low level reduction function. Mostly for internal use. |
56 | | * Sometimes useful for performance by reducing temporaries |
57 | | * Reduce x mod p and place the output in out. |
58 | | * |
59 | | * @warning X and out must not reference each other |
60 | | * |
61 | | * ws is a temporary workspace. |
62 | | */ |
63 | | void reduce(BigInt& out, const BigInt& x, secure_vector<word>& ws) const; |
64 | | |
65 | 0 | bool initialized() const { return (m_mod_words != 0); } |
66 | | |
67 | 0 | BOTAN_DEPRECATED("Use for_public_modulus or for_secret_modulus") Modular_Reducer() { m_mod_words = 0; } |
68 | | |
69 | | /** |
70 | | * Accepts m == 0 and leaves the Modular_Reducer in an uninitialized state |
71 | | */ |
72 | | BOTAN_DEPRECATED("Use for_public_modulus or for_secret_modulus") explicit Modular_Reducer(const BigInt& mod); |
73 | | |
74 | | /** |
75 | | * Requires that m > 0 |
76 | | */ |
77 | | static Modular_Reducer for_public_modulus(const BigInt& m); |
78 | | |
79 | | /** |
80 | | * Requires that m > 0 |
81 | | */ |
82 | | static Modular_Reducer for_secret_modulus(const BigInt& m); |
83 | | |
84 | | private: |
85 | 13.9k | Modular_Reducer(const BigInt& m, BigInt mu, size_t mw) : m_modulus(m), m_mu(std::move(mu)), m_mod_words(mw) {} |
86 | | |
87 | | BigInt m_modulus, m_mu; |
88 | | size_t m_mod_words; |
89 | | }; |
90 | | |
91 | | } // namespace Botan |
92 | | |
93 | | #endif |