22
This commit is contained in:
3
zmod/CMakeLists.txt
Normal file
3
zmod/CMakeLists.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
add_library(zmod INTERFACE)
|
||||
target_link_libraries(zmod PkgConfig::GMP)
|
||||
target_include_directories(zmod PUBLIC include)
|
50
zmod/include/zmod.hpp
Normal file
50
zmod/include/zmod.hpp
Normal file
@@ -0,0 +1,50 @@
|
||||
#ifndef ZMOD_HPP_
|
||||
#define ZMOD_HPP_
|
||||
|
||||
#include <iostream>
|
||||
#include <gmpxx.h>
|
||||
|
||||
namespace zmod {
|
||||
|
||||
template <unsigned long Mod>
|
||||
class ZMod {
|
||||
mpz_class value;
|
||||
|
||||
public:
|
||||
ZMod() : value {} {}
|
||||
ZMod(long value) : ZMod{mpz_class{value}} {}
|
||||
ZMod(mpz_class value) {
|
||||
mpz_mod_ui(this->value.get_mpz_t(), value.get_mpz_t(), Mod);
|
||||
}
|
||||
|
||||
auto operator+(ZMod const& rhs) const -> ZMod {
|
||||
return {value + rhs.value};
|
||||
}
|
||||
|
||||
auto operator-() const -> ZMod {
|
||||
return {-value};
|
||||
}
|
||||
|
||||
auto operator-(ZMod const& rhs) const -> ZMod {
|
||||
return {value - rhs.value};
|
||||
}
|
||||
|
||||
auto operator*(ZMod const& rhs) const -> ZMod {
|
||||
return {value * rhs.value};
|
||||
}
|
||||
|
||||
auto inverse() const -> ZMod {
|
||||
mpz_class m{Mod};
|
||||
ZMod result;
|
||||
mpz_invert(result.value.get_mpz_t(), value.get_mpz_t(), m.get_mpz_t());
|
||||
return result;
|
||||
}
|
||||
|
||||
auto friend operator<<(std::ostream & out, ZMod<Mod> const& x) -> std::ostream & {
|
||||
return out << x.value.get_ui();
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
1
zmod/src/zmod.cpp
Normal file
1
zmod/src/zmod.cpp
Normal file
@@ -0,0 +1 @@
|
||||
#include <zmod.hpp>
|
Reference in New Issue
Block a user