Skip to content

Commit a312877

Browse files
stratosphertheStack
andcommitted
test: Add ellswift unit tests
remove util also since unit tests there were removed in #27538 Co-authored-by: theStack <[email protected]>
1 parent 714fb2c commit a312877

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

test/functional/test_framework/ellswift.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
Do not use for anything but tests."""
99

1010
import random
11+
import unittest
1112

1213
from test_framework.secp256k1 import FE, G, GE
1314

@@ -83,3 +84,47 @@ def ellswift_ecdh_xonly(pubkey_theirs, privkey):
8384
t = FE(int.from_bytes(pubkey_theirs[32:], 'big'))
8485
d = int.from_bytes(privkey, 'big')
8586
return (d * GE.lift_x(xswiftec(u, t))).x.to_bytes()
87+
88+
89+
class TestFrameworkEllSwift(unittest.TestCase):
90+
def test_xswiftec(self):
91+
"""Verify that xswiftec maps all inputs to the curve."""
92+
for _ in range(32):
93+
u = FE(random.randrange(0, FE.SIZE))
94+
t = FE(random.randrange(0, FE.SIZE))
95+
x = xswiftec(u, t)
96+
self.assertTrue(GE.is_valid_x(x))
97+
98+
# Check that inputs which are considered undefined in the original
99+
# SwiftEC paper can also be decoded successfully (by remapping)
100+
undefined_inputs = [
101+
(FE(0), FE(23)), # u = 0
102+
(FE(42), FE(0)), # t = 0
103+
(FE(5), FE(-132).sqrt()), # u^3 + t^2 + 7 = 0
104+
]
105+
assert undefined_inputs[-1][0]**3 + undefined_inputs[-1][1]**2 + 7 == 0
106+
for u, t in undefined_inputs:
107+
x = xswiftec(u, t)
108+
self.assertTrue(GE.is_valid_x(x))
109+
110+
def test_elligator_roundtrip(self):
111+
"""Verify that encoding using xelligatorswift decodes back using xswiftec."""
112+
for _ in range(32):
113+
while True:
114+
# Loop until we find a valid X coordinate on the curve.
115+
x = FE(random.randrange(1, FE.SIZE))
116+
if GE.is_valid_x(x):
117+
break
118+
# Encoding it to (u, t), decode it back, and compare.
119+
u, t = xelligatorswift(x)
120+
x2 = xswiftec(u, t)
121+
self.assertEqual(x2, x)
122+
123+
def test_ellswift_ecdh_xonly(self):
124+
"""Verify that shared secret computed by ellswift_ecdh_xonly match."""
125+
for _ in range(32):
126+
privkey1, encoding1 = ellswift_create()
127+
privkey2, encoding2 = ellswift_create()
128+
shared_secret1 = ellswift_ecdh_xonly(encoding1, privkey2)
129+
shared_secret2 = ellswift_ecdh_xonly(encoding2, privkey1)
130+
self.assertEqual(shared_secret1, shared_secret2)

test/functional/test_runner.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@
7474
TEST_FRAMEWORK_MODULES = [
7575
"address",
7676
"blocktools",
77-
"muhash",
77+
"ellswift",
7878
"key",
79+
"muhash",
7980
"ripemd160",
8081
"script",
8182
"segwit_addr",
82-
"util",
8383
]
8484

8585
EXTENDED_SCRIPTS = [

0 commit comments

Comments
 (0)