commit 2b9bbc683dbb921a63f83ac0ab3b152f331ea9fe
parent f39afeacb8a83a76fe7b6f02e4f90a6e658f364c
Author: Shivakumar <[email protected]>
Date: Mon, 9 Jun 2025 00:03:52 +0530
add group operations to field (add, sub, mul)
Diffstat:
1 file changed, 24 insertions(+), 0 deletions(-)
diff --git a/ecc/field.py b/ecc/field.py
@@ -7,3 +7,27 @@ class FieldElement:
self.num = num
self.prime = prime
+
+
+ def __add__(self, other):
+
+ if self.prime != other.prime:
+ raise TypeError("Can not add the elements of different Field.")
+
+ return FieldElement((self.num + other.num) % self.prime, self.prime)
+
+
+ def __sub__(self, other):
+
+ if self.prime != other.prime:
+ raise TypeError("Can not subtract the elements of different Field.")
+
+ return FieldElement((self.num - other.num) % self.prime, self.prime)
+
+
+ def __mul__(self, other):
+
+ if self.prime != other.prime:
+ raise TypeError("Can not multiply the elements of different Field.")
+
+ return FieldElement((self.num * other.num) % self.prime, self.prime)