Skip to content

Commit 41166b1

Browse files
stratosphersipa
andcommitted
Add functions to compute ellswift encoding, x-only ECDH
Co-authored-by: Pieter Wuille <pieter.wuille@gmail.com>
1 parent f058690 commit 41166b1

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

test/functional/test_framework/ellswift.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,19 @@ def xelligatorswift(x):
7070
if t is not None:
7171
return u, t
7272

73+
def ellswift_create():
74+
"""Generate a (privkey, ellswift_pubkey) pair."""
75+
priv = random.randrange(1, GE.ORDER)
76+
u, t = xelligatorswift((priv * G).x)
77+
return priv.to_bytes(32, 'big'), u.to_bytes() + t.to_bytes()
78+
79+
def ellswift_ecdh_xonly(pubkey_theirs, privkey):
80+
"""Compute X coordinate of shared ECDH point between ellswift pubkey and privkey."""
81+
u = FE(int.from_bytes(pubkey_theirs[:32], 'big'))
82+
t = FE(int.from_bytes(pubkey_theirs[32:], 'big'))
83+
d = int.from_bytes(privkey, 'big')
84+
return (d * GE.lift_x(xswiftec(u, t))).x.to_bytes()
85+
7386

7487
class TestFrameworkEllSwift(unittest.TestCase):
7588
def test_elligator_forward(self):
@@ -92,3 +105,11 @@ def test_elligator_roundtrip(self):
92105
u, t = xelligatorswift(x)
93106
x2 = xswiftec(u, t)
94107
self.assertEqual(x2, x)
108+
109+
def test_ellswift_ecdh_xonly(self):
110+
for _ in range(32):
111+
privkey1, encoding1 = ellswift_create()
112+
privkey2, encoding2 = ellswift_create()
113+
shared_secret1 = ellswift_ecdh_xonly(encoding1, privkey2)
114+
shared_secret2 = ellswift_ecdh_xonly(encoding2, privkey1)
115+
assert shared_secret1 == shared_secret2

0 commit comments

Comments
 (0)