/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 |
19 | | { |
20 | | public: |
21 | 2.22k | const BigInt& get_modulus() const { return m_modulus; } |
22 | | |
23 | | BigInt reduce(const BigInt& x) const; |
24 | | |
25 | | /** |
26 | | * Multiply mod p |
27 | | * @param x the first operand |
28 | | * @param y the second operand |
29 | | * @return (x * y) % p |
30 | | */ |
31 | | BigInt multiply(const BigInt& x, const BigInt& y) const |
32 | 1.50M | { return reduce(x * y); } |
33 | | |
34 | | /** |
35 | | * Multiply mod p |
36 | | * @return (x * y * z) % p |
37 | | */ |
38 | | BigInt multiply(const BigInt& x, const BigInt& y, const BigInt& z) const |
39 | 0 | { return multiply(x, multiply(y, z)); } |
40 | | |
41 | | /** |
42 | | * Square mod p |
43 | | * @param x the value to square |
44 | | * @return (x * x) % p |
45 | | */ |
46 | | BigInt square(const BigInt& x) const |
47 | 4.90M | { return reduce(Botan::square(x)); } |
48 | | |
49 | | /** |
50 | | * Cube mod p |
51 | | * @param x the value to cube |
52 | | * @return (x * x * x) % p |
53 | | */ |
54 | | BigInt cube(const BigInt& x) const |
55 | 0 | { return multiply(x, this->square(x)); } |
56 | | |
57 | | /** |
58 | | * Low level reduction function. Mostly for internal use. |
59 | | * Sometimes useful for performance by reducing temporaries |
60 | | * Reduce x mod p and place the output in out. ** X and out must not reference each other ** |
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 | Modular_Reducer() { m_mod_words = 0; } |
68 | | explicit Modular_Reducer(const BigInt& mod); |
69 | | private: |
70 | | BigInt m_modulus, m_mu; |
71 | | size_t m_mod_words; |
72 | | }; |
73 | | |
74 | | } |
75 | | |
76 | | #endif |