/src/botan/build/include/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/numthry.h> |
12 | | |
13 | | namespace Botan { |
14 | | |
15 | | /** |
16 | | * Modular Reducer (using Barrett's technique) |
17 | | */ |
18 | | class BOTAN_PUBLIC_API(2, 0) Modular_Reducer final { |
19 | | public: |
20 | 16 | const BigInt& get_modulus() const { return m_modulus; } |
21 | | |
22 | | BigInt reduce(const BigInt& x) const; |
23 | | |
24 | | /** |
25 | | * Multiply mod p |
26 | | * @param x the first operand |
27 | | * @param y the second operand |
28 | | * @return (x * y) % p |
29 | | */ |
30 | 859k | BigInt multiply(const BigInt& x, const BigInt& y) const { return reduce(x * y); } |
31 | | |
32 | | /** |
33 | | * Multiply mod p |
34 | | * @return (x * y * z) % p |
35 | | */ |
36 | 0 | BigInt multiply(const BigInt& x, const BigInt& y, const BigInt& z) const { return multiply(x, multiply(y, z)); } |
37 | | |
38 | | /** |
39 | | * Square mod p |
40 | | * @param x the value to square |
41 | | * @return (x * x) % p |
42 | | */ |
43 | 1.05M | BigInt square(const BigInt& x) const { return reduce(Botan::square(x)); } |
44 | | |
45 | | /** |
46 | | * Cube mod p |
47 | | * @param x the value to cube |
48 | | * @return (x * x * x) % p |
49 | | */ |
50 | 0 | BigInt cube(const BigInt& x) const { return multiply(x, this->square(x)); } |
51 | | |
52 | | /** |
53 | | * Low level reduction function. Mostly for internal use. |
54 | | * Sometimes useful for performance by reducing temporaries |
55 | | * Reduce x mod p and place the output in out. ** X and out must not reference each other ** |
56 | | * ws is a temporary workspace. |
57 | | */ |
58 | | void reduce(BigInt& out, const BigInt& x, secure_vector<word>& ws) const; |
59 | | |
60 | 0 | bool initialized() const { return (m_mod_words != 0); } |
61 | | |
62 | 0 | Modular_Reducer() { m_mod_words = 0; } |
63 | | |
64 | | explicit Modular_Reducer(const BigInt& mod); |
65 | | |
66 | | private: |
67 | | BigInt m_modulus, m_mu; |
68 | | size_t m_mod_words; |
69 | | }; |
70 | | |
71 | | } // namespace Botan |
72 | | |
73 | | #endif |