aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/math/numbertheory/reducer.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/math/numbertheory/reducer.h')
-rw-r--r--src/lib/math/numbertheory/reducer.h61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/lib/math/numbertheory/reducer.h b/src/lib/math/numbertheory/reducer.h
new file mode 100644
index 000000000..76712074c
--- /dev/null
+++ b/src/lib/math/numbertheory/reducer.h
@@ -0,0 +1,61 @@
+/*
+* Modular Reducer
+* (C) 1999-2010 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#ifndef BOTAN_MODULAR_REDUCER_H__
+#define BOTAN_MODULAR_REDUCER_H__
+
+#include <botan/numthry.h>
+
+namespace Botan {
+
+/**
+* Modular Reducer (using Barrett's technique)
+*/
+class BOTAN_DLL Modular_Reducer
+ {
+ public:
+ const BigInt& get_modulus() const { return modulus; }
+
+ BigInt reduce(const BigInt& x) const;
+
+ /**
+ * Multiply mod p
+ * @param x
+ * @param y
+ * @return (x * y) % p
+ */
+ BigInt multiply(const BigInt& x, const BigInt& y) const
+ { return reduce(x * y); }
+
+ /**
+ * Square mod p
+ * @param x
+ * @return (x * x) % p
+ */
+ BigInt square(const BigInt& x) const
+ { return reduce(Botan::square(x)); }
+
+ /**
+ * Cube mod p
+ * @param x
+ * @return (x * x * x) % p
+ */
+ BigInt cube(const BigInt& x) const
+ { return multiply(x, this->square(x)); }
+
+ bool initialized() const { return (mod_words != 0); }
+
+ Modular_Reducer() { mod_words = 0; }
+ Modular_Reducer(const BigInt& mod);
+ private:
+ BigInt modulus, modulus_2, mu;
+ size_t mod_words;
+ };
+
+}
+
+#endif