Botan 3.7.1
Crypto and TLS for C&
reducer.h
Go to the documentation of this file.
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
14
15namespace Botan {
16
17/**
18* Modular Reducer (using Barrett's technique)
19*/
21 public:
22 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 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 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 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 bool initialized() const { return (m_mod_words != 0); }
66
67 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 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
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:19
#define BOTAN_FUTURE_INTERNAL_HEADER(hdr)
Definition api.h:84
#define BOTAN_DEPRECATED(msg)
Definition api.h:59
const BigInt & get_modulus() const
Definition reducer.h:22
BigInt cube(const BigInt &x) const
Definition reducer.h:52
BigInt multiply(const BigInt &x, const BigInt &y) const
Definition reducer.h:32
bool initialized() const
Definition reducer.h:65
BigInt multiply(const BigInt &x, const BigInt &y, const BigInt &z) const
Definition reducer.h:38
int(* final)(unsigned char *, CTX *)
BigInt square(const BigInt &x)
Definition numthry.cpp:157
RetT reduce(const std::vector< KeyT > &keys, RetT acc, ReducerT reducer)
Definition stl_util.h:47
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61