commit edd6cd7b252b7d0c60e21e27a256a6b3c2595830
parent cdd47c1437467d3ad19df08bc7c85dea8109764d
Author: Shivakumar <[email protected]>
Date: Thu, 12 Jun 2025 21:29:27 +0530
add tests for Field operations
Diffstat:
4 files changed, 27 insertions(+), 6 deletions(-)
diff --git a/ecc/__init__.py b/ecc/__init__.py
@@ -1,3 +1,2 @@
-
-
+from .field import FieldElement
diff --git a/ecc/field.py b/ecc/field.py
@@ -1,7 +1,7 @@
-# Defination of Field E(Z/pZ) for the elliptic curve : y^2 = x^3 + ax + b
+# Definition of Field E(Z/pZ) for the elliptic curve : y^2 = x^3 + ax + b
#
# The Group operation for the curve are under the Field E(Z/pZ).
-# The operations like : - Addidtion (+)
+# The operations like : - Addition (+)
# - Multiplication (*)
# - Subtraction (-)
#
@@ -55,15 +55,23 @@ class FieldElement:
return FieldElement((self.num * other.num) % self.prime, self.prime)
+
+ def __eq__(self, other):
+
+ if other is None:
+ return False
+
+ return self.num == other.num and self.prime == other.prime
+
def __pow__(self, exponent: int):
exponent = exponent % (self.prime - 1)
- return FieldElement(pow(self.num, self.exponet, self.prime), self.prime)
+ return FieldElement(pow(self.num, exponent, self.prime), self.prime)
def __repr__(self):
- return f"F_({self.prime}({self.num}))"
+ return f"F_{self.prime}({self.num})"
diff --git a/tests/__init__.py b/tests/__init__.py
diff --git a/tests/test_field.py b/tests/test_field.py
@@ -0,0 +1,14 @@
+from ecc import FieldElement
+
+
+def test_addition():
+ a = FieldElement(7, 13)
+ b = FieldElement(12, 13)
+ assert a + b == FieldElement(6, 13)
+
+
+def test_exponentiation():
+ a = FieldElement(3, 13)
+ assert a ** 12 == FieldElement(1, 13)
+
+