aboutsummaryrefslogtreecommitdiff
path: root/test/functional/test_framework/ellswift.py
diff options
context:
space:
mode:
authorstratospher <44024636+stratospher@users.noreply.github.com>2023-06-29 23:09:45 +0530
committerstratospher <44024636+stratospher@users.noreply.github.com>2023-06-29 23:32:56 +0530
commit4f4d039a98370a91e3cd5977352a9a4b260aa06b (patch)
tree6fed5bd6e41939cbb85dace31d2c5e16c87e91de /test/functional/test_framework/ellswift.py
parenta31287718aebad847b232eff59adc16c166c99e8 (diff)
test: add ellswift test vectors from BIP324
The test vector input file is taken from: 1. https://github.com/bitcoin/bips/blob/master/bip-0324/xswiftec_inv_test_vectors.csv 2. https://github.com/bitcoin/bips/blob/master/bip-0324/ellswift_decode_test_vectors.csv Co-authored-by: theStack <sebastian.falbesoner@gmail.com>
Diffstat (limited to 'test/functional/test_framework/ellswift.py')
-rw-r--r--test/functional/test_framework/ellswift.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/test/functional/test_framework/ellswift.py b/test/functional/test_framework/ellswift.py
index 9ea6544ac2..97b10118e6 100644
--- a/test/functional/test_framework/ellswift.py
+++ b/test/functional/test_framework/ellswift.py
@@ -7,6 +7,8 @@
WARNING: This code is slow and uses bad randomness.
Do not use for anything but tests."""
+import csv
+import os
import random
import unittest
@@ -128,3 +130,34 @@ class TestFrameworkEllSwift(unittest.TestCase):
shared_secret1 = ellswift_ecdh_xonly(encoding1, privkey2)
shared_secret2 = ellswift_ecdh_xonly(encoding2, privkey1)
self.assertEqual(shared_secret1, shared_secret2)
+
+ def test_elligator_encode_testvectors(self):
+ """Implement the BIP324 test vectors for ellswift encoding (read from xswiftec_inv_test_vectors.csv)."""
+ vectors_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'xswiftec_inv_test_vectors.csv')
+ with open(vectors_file, newline='', encoding='utf8') as csvfile:
+ reader = csv.DictReader(csvfile)
+ for row in reader:
+ u = FE.from_bytes(bytes.fromhex(row['u']))
+ x = FE.from_bytes(bytes.fromhex(row['x']))
+ for case in range(8):
+ ret = xswiftec_inv(x, u, case)
+ if ret is None:
+ self.assertEqual(row[f"case{case}_t"], "")
+ else:
+ self.assertEqual(row[f"case{case}_t"], ret.to_bytes().hex())
+ self.assertEqual(xswiftec(u, ret), x)
+
+ def test_elligator_decode_testvectors(self):
+ """Implement the BIP324 test vectors for ellswift decoding (read from ellswift_decode_test_vectors.csv)."""
+ vectors_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'ellswift_decode_test_vectors.csv')
+ with open(vectors_file, newline='', encoding='utf8') as csvfile:
+ reader = csv.DictReader(csvfile)
+ for row in reader:
+ encoding = bytes.fromhex(row['ellswift'])
+ assert len(encoding) == 64
+ expected_x = FE(int(row['x'], 16))
+ u = FE(int.from_bytes(encoding[:32], 'big'))
+ t = FE(int.from_bytes(encoding[32:], 'big'))
+ x = xswiftec(u, t)
+ self.assertEqual(x, expected_x)
+ self.assertTrue(GE.is_valid_x(x))