Botan 3.0.0-alpha0
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/numthry.h>
12
13namespace Botan {
14
15/**
16* Modular Reducer (using Barrett's technique)
17*/
19 {
20 public:
21 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 { 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 { 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 { 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 { 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 bool initialized() const { return (m_mod_words != 0); }
66
67 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
const BigInt & get_modulus() const
Definition: reducer.h:21
BigInt cube(const BigInt &x) const
Definition: reducer.h:54
BigInt square(const BigInt &x) const
Definition: reducer.h:46
BigInt multiply(const BigInt &x, const BigInt &y) const
Definition: reducer.h:31
bool initialized() const
Definition: reducer.h:65
BigInt multiply(const BigInt &x, const BigInt &y, const BigInt &z) const
Definition: reducer.h:38
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:31
Definition: alg_id.cpp:13
BigInt square(const BigInt &x)
Definition: numthry.cpp:170
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:65