aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2024-12-21 16:19:39 +0100
committerSebastiano Tronto <sebastiano@tronto.net>2024-12-21 16:19:39 +0100
commitb0d8a2f5f5718ba3fea628cad7bcbbc76c6a140c (patch)
tree14fa46de9219f95858c1d7de04d1f2cb66db3e0b
parent4d9b0b7cb5c749cd1db3828ab9a3475e6c9811b6 (diff)
downloadzmodn-b0d8a2f5f5718ba3fea628cad7bcbbc76c6a140c.tar.gz
zmodn-b0d8a2f5f5718ba3fea628cad7bcbbc76c6a140c.zip
Fix possible overflow with assignment operations
-rwxr-xr-xtest18
-rw-r--r--zmodn.h6
2 files changed, 21 insertions, 3 deletions
diff --git a/test b/test
index 2930e89..3256a12 100755
--- a/test
+++ b/test
@@ -115,6 +115,24 @@ public:
115 assert_equal(n, Zmod<12>(4)); 115 assert_equal(n, Zmod<12>(4));
116 } 116 }
117}, 117},
118{
119 .name = "Multiplication overflow",
120 .f = []() {
121 Zmod<10> n = 8;
122 Zmod<10> m = 9;
123 auto prod = m * n;
124 assert_equal(prod.toint64(), 2);
125 }
126},
127{
128 .name = "Multiplication and assignment overflow",
129 .f = []() {
130 Zmod<10> n = 8;
131 Zmod<10> m = 9;
132 n *= m;
133 assert_equal(n.toint64(), 2);
134 }
135},
118}; 136};
119 137
120int main() { 138int main() {
diff --git a/zmodn.h b/zmodn.h
index 562aa22..f161ad1 100644
--- a/zmodn.h
+++ b/zmodn.h
@@ -17,9 +17,9 @@ public:
17 Zmod operator+(const Zmod& z) const { return int64 + z.int64; } 17 Zmod operator+(const Zmod& z) const { return int64 + z.int64; }
18 Zmod operator-(const Zmod& z) const { return int64 - z.int64; } 18 Zmod operator-(const Zmod& z) const { return int64 - z.int64; }
19 Zmod operator*(const Zmod& z) const { return int64 * z.int64; } 19 Zmod operator*(const Zmod& z) const { return int64 * z.int64; }
20 Zmod operator+=(const Zmod& z) { return int64 += z.int64; } 20 Zmod operator+=(const Zmod& z) { return (*this) = int64 + z.int64; }
21 Zmod operator-=(const Zmod& z) { return int64 -= z.int64; } 21 Zmod operator-=(const Zmod& z) { return (*this) = int64 - z.int64; }
22 Zmod operator*=(const Zmod& z) { return int64 *= z.int64; } 22 Zmod operator*=(const Zmod& z) { return (*this) = int64 * z.int64; }
23 23
24 bool operator==(const Zmod& z) const { return int64 == z.int64; } 24 bool operator==(const Zmod& z) const { return int64 == z.int64; }
25 bool operator!=(const Zmod& z) const { return int64 != z.int64; } 25 bool operator!=(const Zmod& z) const { return int64 != z.int64; }