Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test curves added #44

Merged
merged 3 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lightphe/elliptic_curve_forms/koblitz.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ def is_on_curve(self, P: Tuple[int, int]) -> bool:
Returns:
result (bool): True if the point is on the curve, False otherwise
"""
if P == self.O:
return True

x, y = P

return bin_ops.mod(
Expand Down
3 changes: 3 additions & 0 deletions lightphe/elliptic_curve_forms/weierstrass.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ def is_on_curve(self, P: Tuple[int, int]):
Returns
is_on_curve (boolean): returns True if point is on the curve
"""
if P == self.O:
return True

x, y = P
return (y * y) % self.modulo == (
pow(x, 3, self.modulo) + self.a * x + self.b
Expand Down
17 changes: 17 additions & 0 deletions lightphe/standard_curves/edwards.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

# project dependencies
from lightphe.models.Curve import TwistedEdwardsInterface
from lightphe.commons.logger import Logger

logger = Logger(module="lightphe/standard_curves/edwards.py")

DEFAULT_CURVE = "ed25519"

Expand Down Expand Up @@ -125,3 +128,17 @@ class Id_tc26_gost_3410_2012_512_paramSetC(TwistedEdwardsInterface):
0x469AF79D1FB1F5E16B99592B77A01E2A0FDFB0D01794368D9A56117F7B38669522DD4B650CF789EEBF068C5D139732F0905622C04B2BAAE7600303EE73001A3D,
)
n = 0x3FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC98CDBA46506AB004C33A9FF5147502CC8EDA9E7A769A12694623CEF47F023ED


class Test_Curve(TwistedEdwardsInterface):
p = 13
a = 1
d = 2
G = (9, 4)
n = 8

def __init__(self):
logger.warn(
"edwards test-curve is for development and educational purposes only"
" and should not be used in production."
)
17 changes: 17 additions & 0 deletions lightphe/standard_curves/koblitz.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

# project dependencies
from lightphe.models.Curve import KoblitzInterface
from lightphe.commons.logger import Logger

logger = Logger(module="lightphe/standard_curves/koblitz.py")

DEFAULT_CURVE = "k163"

Expand Down Expand Up @@ -414,3 +416,18 @@ class Wap_wsg_idm_ecid_wtls1(KoblitzInterface):
b = 1
G = (0x01667979A40BA497E5D5C270780617, 0x00F44B4AF1ECC2630E08785CEBCC15)
n = 0x00FFFFFFFFFFFFFFFDBF91AF6DEA73


class Test_Curve(KoblitzInterface):
m = 5
coefficients = [5, 2, 0]
a = 13
b = 4
G = (22, 19)
n = 16

def __init__(self):
logger.warn(
"koblitz test-curve is for development and educational purposes only"
" and should not be used in production."
)
12 changes: 11 additions & 1 deletion lightphe/standard_curves/weierstrass.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

# project dependencies
from lightphe.models.Curve import WeierstrassInterface
from lightphe.commons.logger import Logger

logger = Logger(module="lightphe/standard_curves/weierstrass.py")


DEFAULT_CURVE = "secp256k1"

Expand Down Expand Up @@ -914,7 +918,7 @@ class Secp224k1(WeierstrassInterface):
n = 0x10000000000000000000000000001DCE8D2EC6184CAF0A971769FB1F7


class Test_Curve_PF_23(WeierstrassInterface):
class Test_Curve(WeierstrassInterface):
"""
http://koclab.cs.ucsb.edu/teaching/ccs130h/2018/04ecc.pdf
"""
Expand All @@ -924,3 +928,9 @@ class Test_Curve_PF_23(WeierstrassInterface):
p = 23
G = (0, 1)
n = 28

def __init__(self):
logger.warn(
"weierstrass test-curve is for development and educational purposes only"
" and should not be used in production."
)
62 changes: 51 additions & 11 deletions tests/test_ellipticcurveelgamal.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ def __test_elliptic_curve_elgamal():
curves = inventory.list_curves(form)
for curve in curves:

if curve in ["test-curve"]:
continue

logger.debug(
f"ℹ️ Elliptic Curve ElGamal test is running for EC form {form}&{curve}"
)
Expand Down Expand Up @@ -158,22 +161,27 @@ def test_double_and_add_with_negative_input():


def test_elliptic_curve_cyclic_group_on_test_curve():
cs = EllipticCurveElGamal(form="weierstrass", curve="test-curve-pf-23")

for k in range(0, 5 * cs.curve.n):
P = cs.curve.double_and_add(cs.curve.G, k)
logger.debug(f"{k} x G = {P}")
curves = ["weierstrass", "koblitz", "edwards"]

for form in curves:
logger.debug(f"ℹ️ Testing elliptic curve cyclic group on {form} test curve")
cs = EllipticCurveElGamal(form=form, curve="test-curve")

for k in range(0, 2 * cs.curve.n + 1):
P = cs.curve.double_and_add(cs.curve.G, k)
logger.debug(f"{k} x G = {P}")

if k in [0, cs.curve.n]:
assert P == cs.curve.O
if k in [0, cs.curve.n]:
assert P == cs.curve.O

logger.info("✅ Test elliptic curve cyclic group on test curve done.")
logger.info(f"✅ Test elliptic curve cyclic group on test {form} curve done.")


def test_point_addition_returning_point_at_infinity():
cs = EllipticCurveElGamal(form="weierstrass", curve="test-curve-pf-23")
def test_weierstrass_point_addition_returning_point_at_infinity():
cs = EllipticCurveElGamal(form="weierstrass", curve="test-curve")

# we know that 20G + 8 G = 28G = point at infinity
# we know that 20G + 8G = 28G = point at infinity
P = cs.curve.add_points(
cs.curve.double_and_add(cs.curve.G, 20), cs.curve.double_and_add(cs.curve.G, 8)
)
Expand All @@ -183,7 +191,39 @@ def test_point_addition_returning_point_at_infinity():
Q = cs.curve.add_points(_14G, _14G)
assert Q == cs.curve.O

logger.info("✅ Test elliptic curve cyclic group on test curve done.")
logger.info("✅ Test weierstras point addition returning point at infinity done.")


def test_koblitz_point_addition_returning_point_at_infinity():
cs = EllipticCurveElGamal(form="koblitz", curve="test-curve")

# we know that 12G + 4G = 16G = point at infinity
P = cs.curve.add_points(
cs.curve.double_and_add(cs.curve.G, 12), cs.curve.double_and_add(cs.curve.G, 4)
)
assert P == cs.curve.O

_8G = cs.curve.double_and_add(cs.curve.G, 8)
Q = cs.curve.add_points(_8G, _8G)
assert Q == cs.curve.O

logger.info("✅ Test koblitz point addition returning point at infinity done.")


def test_edwards_point_addition_returning_point_at_infinity():
cs = EllipticCurveElGamal(form="edwards", curve="test-curve")

# we know that 6G + 2G = 8G = point at infinity
P = cs.curve.add_points(
cs.curve.double_and_add(cs.curve.G, 6), cs.curve.double_and_add(cs.curve.G, 2)
)
assert P == cs.curve.O

_4G = cs.curve.double_and_add(cs.curve.G, 4)
Q = cs.curve.add_points(_4G, _4G)
assert Q == cs.curve.O

logger.info("✅ Test edwards point addition returning point at infinity done.")


def test_double_and_add_for_k_close_to_n():
Expand Down
Loading