import unittest
import os
import sys
import secrets
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent))
import bitcoinpqc
from bitcoinpqc import Algorithm
class TestBitcoinPQC(unittest.TestCase):
def test_key_sizes(self):
for algo in [
Algorithm.FN_DSA_512,
Algorithm.ML_DSA_44,
Algorithm.SLH_DSA_SHAKE_128S
]:
self.assertGreater(bitcoinpqc.public_key_size(algo), 0)
self.assertGreater(bitcoinpqc.secret_key_size(algo), 0)
self.assertGreater(bitcoinpqc.signature_size(algo), 0)
def _test_algorithm(self, algorithm):
random_data = secrets.token_bytes(128)
keypair = bitcoinpqc.keygen(algorithm, random_data)
self.assertEqual(len(keypair.public_key), bitcoinpqc.public_key_size(algorithm))
self.assertEqual(len(keypair.secret_key), bitcoinpqc.secret_key_size(algorithm))
message = b"Hello, Bitcoin PQC!"
signature = bitcoinpqc.sign(algorithm, keypair.secret_key, message)
self.assertEqual(len(signature.signature), bitcoinpqc.signature_size(algorithm))
self.assertTrue(bitcoinpqc.verify(
algorithm, keypair.public_key, message, signature
))
self.assertTrue(bitcoinpqc.verify(
algorithm, keypair.public_key, message, signature.signature
))
bad_message = b"Bad message!"
self.assertFalse(bitcoinpqc.verify(
algorithm, keypair.public_key, bad_message, signature
))
def test_fn_dsa(self):
self._test_algorithm(Algorithm.FN_DSA_512)
def test_ml_dsa(self):
self._test_algorithm(Algorithm.ML_DSA_44)
def test_slh_dsa(self):
self._test_algorithm(Algorithm.SLH_DSA_SHAKE_128S)
if __name__ == "__main__":
unittest.main()