zmodn

A simple C++ library for integers modulo N
git clone https://git.tronto.net/zmodn
Download | Log | Files | Refs | README

commit 8ca27427af98bd3a82e8c5f4a199513295747930
parent 771667c2bb52235d6dacf67eab933207b59e167b
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date:   Sat, 21 Dec 2024 18:13:27 +0100

Revert "Fix division"
Actually you cannot do division like that, the result is not unique.

This reverts commit 771667c2bb52235d6dacf67eab933207b59e167b.

Diffstat:
Mtest | 10----------
Mzmodn.h | 8+++-----
2 files changed, 3 insertions(+), 15 deletions(-)

diff --git a/test b/test @@ -141,16 +141,6 @@ public: assert_equal(p, 92); } }, -{ - .name = "4 / 2 mod 6", - .f = []() { - Zmod<6> four = 4; - Zmod<6> two = 2; - auto d = four / two; - assert_equal(d.has_value(), true); - assert_equal(d.value(), 2); - } -}, }; int main() { diff --git a/zmodn.h b/zmodn.h @@ -40,15 +40,13 @@ public: bool operator!=(const Zmod& z) const { return value != z.value; } std::optional<Zmod> inverse() const { - auto [g, a, b] = extended_gcd(value, N); + auto [g, a, _] = extended_gcd(value, N); return g == 1 ? Zmod(a) : std::optional<Zmod>{}; } std::optional<Zmod> operator/(const Zmod& d) const { - auto [g, x, y] = extended_gcd(value, d.toint()); - auto [a, b] = std::pair<Zmod, Zmod>{value / g, d.toint() / g}; - auto i = b.inverse(); - return i ? a * i.value() : i; + auto i = d.inverse(); + return i ? (*this) * i.value() : i; } std::optional<Zmod> operator/=(const Zmod& d) {