Skip to content

Commit

Permalink
test: Add ellswift unit tests
Browse files Browse the repository at this point in the history
remove util also since unit tests there were removed in bitcoin#27538

Co-authored-by: theStack <sebastian.falbesoner@gmail.com>
  • Loading branch information
stratospher and theStack committed Jun 28, 2023
1 parent d9f393a commit eeb44ff
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
44 changes: 44 additions & 0 deletions test/functional/test_framework/ellswift.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
Do not use for anything but tests."""

import random
import unittest

from test_framework.secp256k1 import FE, G, GE

Expand Down Expand Up @@ -81,3 +82,46 @@ def ellswift_ecdh_xonly(pubkey_theirs, privkey):
t = FE(int.from_bytes(pubkey_theirs[32:], 'big'))
d = int.from_bytes(privkey, 'big')
return (d * GE.lift_x(xswiftec(u, t))).x.to_bytes()


class TestFrameworkEllSwift(unittest.TestCase):
def test_elligator_forward(self):
"""Verify that xswiftec maps all inputs to the curve."""
for _ in range(32):
u = FE(random.randrange(0, FE.SIZE))
t = FE(random.randrange(0, FE.SIZE))
x = xswiftec(u, t)
self.assertTrue(GE.is_valid_x(x))

# Check that inputs which are considered undefined in the original
# SwiftEC paper can also be decoded successfully (by remapping)
undefined_inputs = [
(FE(0), FE(23)), # u = 0
(FE(42), FE(0)), # t = 0
(FE(5), FE(-132).sqrt()), # u^3 + t^2 + 7 = 0
]
assert undefined_inputs[-1][0]**3 + undefined_inputs[-1][1]**2 + 7 == 0
for u, t in undefined_inputs:
x = xswiftec(u, t)
self.assertTrue(GE.is_valid_x(x))

def test_elligator_roundtrip(self):
"""Verify that encoding using xelligatorswift decodes back using xswiftec."""
for _ in range(32):
while True:
# Loop until we find a valid X coordinate on the curve.
x = FE(random.randrange(1, FE.SIZE))
if GE.is_valid_x(x):
break
# Encoding it to (u, t), decode it back, and compare.
u, t = xelligatorswift(x)
x2 = xswiftec(u, t)
self.assertEqual(x2, x)

def test_ellswift_ecdh_xonly(self):
for _ in range(32):
privkey1, encoding1 = ellswift_create()
privkey2, encoding2 = ellswift_create()
shared_secret1 = ellswift_ecdh_xonly(encoding1, privkey2)
shared_secret2 = ellswift_ecdh_xonly(encoding2, privkey1)
assert shared_secret1 == shared_secret2
4 changes: 2 additions & 2 deletions test/functional/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@
TEST_FRAMEWORK_MODULES = [
"address",
"blocktools",
"muhash",
"ellswift",
"key",
"muhash",
"ripemd160",
"script",
"segwit_addr",
"util",
]

EXTENDED_SCRIPTS = [
Expand Down

0 comments on commit eeb44ff

Please sign in to comment.