Skip to content

Commit

Permalink
Add functions to compute ellswift encoding, x-only ECDH
Browse files Browse the repository at this point in the history
Co-authored-by: Pieter Wuille <[email protected]>
  • Loading branch information
stratospher and sipa committed Jun 1, 2023
1 parent f058690 commit 41166b1
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions test/functional/test_framework/ellswift.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ def xelligatorswift(x):
if t is not None:
return u, t

def ellswift_create():
"""Generate a (privkey, ellswift_pubkey) pair."""
priv = random.randrange(1, GE.ORDER)
u, t = xelligatorswift((priv * G).x)
return priv.to_bytes(32, 'big'), u.to_bytes() + t.to_bytes()

def ellswift_ecdh_xonly(pubkey_theirs, privkey):
"""Compute X coordinate of shared ECDH point between ellswift pubkey and privkey."""
u = FE(int.from_bytes(pubkey_theirs[:32], 'big'))
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):
Expand All @@ -92,3 +105,11 @@ def test_elligator_roundtrip(self):
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

0 comments on commit 41166b1

Please sign in to comment.