Refactoring crypto base changes #1806

Merged
g1itch merged 14 commits from crypto-sort into v0.6 2021-08-17 15:07:33 +02:00
5 changed files with 93 additions and 11 deletions
Showing only changes of commit 6360c2773d - Show all commits

View File

View File

@ -0,0 +1,84 @@
"""
Test the arithmetic functions
"""
from binascii import unhexlify
import unittest
try:
from pyelliptic import arithmetic
except ImportError:
from pybitmessage.pyelliptic import arithmetic
# These keys are from addresses test script
sample_pubsigningkey = (
b'044a367f049ec16cb6b6118eb734a9962d10b8db59c890cd08f210c43ff08bdf09d'
b'16f502ca26cd0713f38988a1237f1fc8fa07b15653c996dc4013af6d15505ce')
sample_pubencryptionkey = (
b'044597d59177fc1d89555d38915f581b5ff2286b39d022ca0283d2bdd5c36be5d3c'
b'e7b9b97792327851a562752e4b79475d1f51f5a71352482b241227f45ed36a9')
sample_privsigningkey = \
b'93d0b61371a54b53df143b954035d612f8efa8a3ed1cf842c2186bfd8f876665'
sample_privencryptionkey = \
b'4b0b73a54e19b059dc274ab69df095fe699f43b17397bca26fdf40f4d7400a3a'
sample_factor = \
66858749573256452658262553961707680376751171096153613379801854825275240965733
# G * sample_factor
sample_point = (
33567437183004486938355437500683826356288335339807546987348409590129959362313,
94730058721143827257669456336351159718085716196507891067256111928318063085006
)
class TestArithmetic(unittest.TestCase):
"""Test arithmetic functions"""
def test_base10_multiply(self):
"""Test arithmetic.base10_multiply"""
self.assertEqual(
sample_point,
arithmetic.base10_multiply(arithmetic.G, sample_factor))
def test_decode(self):
"""Decode sample privsigningkey from hex to int and compare to factor"""
self.assertEqual(
arithmetic.decode(sample_privsigningkey, 16), sample_factor)
def test_encode(self):
"""Encode sample factor into hex and compare to privsigningkey"""
self.assertEqual(
arithmetic.encode(sample_factor, 16), sample_privsigningkey)
def test_changebase(self):
"""Check the results of changebase()"""
self.assertEqual(
arithmetic.changebase(sample_privsigningkey, 16, 256, minlen=32),
unhexlify(sample_privsigningkey))
self.assertEqual(
arithmetic.changebase(sample_pubsigningkey, 16, 256, minlen=64),
unhexlify(sample_pubsigningkey))
self.assertEqual(
32, # padding
len(arithmetic.changebase(sample_privsigningkey[:5], 16, 256, 32)))
def test_hex_to_point(self):
"""Check that sample_pubsigningkey is sample_point encoded in hex"""
self.assertEqual(
arithmetic.hex_to_point(sample_pubsigningkey), sample_point)
def test_point_to_hex(self):
"""Check that sample_point is sample_pubsigningkey decoded from hex"""
self.assertEqual(
arithmetic.point_to_hex(sample_point), sample_pubsigningkey)
def test_privtopub(self):
"""Generate public keys and check the result"""
self.assertEqual(
arithmetic.privtopub(sample_privsigningkey),
sample_pubsigningkey
)
self.assertEqual(
arithmetic.privtopub(sample_privencryptionkey),
sample_pubencryptionkey
)

View File

@ -15,6 +15,7 @@ class TestAddresses(unittest.TestCase):
self.assertEqual( self.assertEqual(
addresses.decodeAddress(sample_address), addresses.decodeAddress(sample_address),
('success', 2, 1, unhexlify(sample_ripe))) ('success', 2, 1, unhexlify(sample_ripe)))
status, version, stream, ripe1 = addresses.decodeAddress( status, version, stream, ripe1 = addresses.decodeAddress(
'2cWzSnwjJ7yRP3nLEWUV5LisTZyREWSzUK') '2cWzSnwjJ7yRP3nLEWUV5LisTZyREWSzUK')
self.assertEqual(status, 'success') self.assertEqual(status, 'success')

View File

@ -7,7 +7,7 @@ import unittest
from abc import ABCMeta, abstractmethod from abc import ABCMeta, abstractmethod
from binascii import hexlify from binascii import hexlify
from pybitmessage.pyelliptic import arithmetic from pybitmessage import highlevelcrypto
try: try:
@ -16,7 +16,7 @@ except ImportError:
RIPEMD = None RIPEMD = None
from .samples import ( from .samples import (
sample_factor, sample_point, sample_pubsigningkey, sample_pubencryptionkey, sample_pubsigningkey, sample_pubencryptionkey,
sample_privsigningkey, sample_privencryptionkey, sample_ripe sample_privsigningkey, sample_privencryptionkey, sample_ripe
) )
@ -62,19 +62,13 @@ class TestCrypto(RIPEMD160TestCase, unittest.TestCase):
class TestHighlevelcrypto(unittest.TestCase): class TestHighlevelcrypto(unittest.TestCase):
"""Test highlevelcrypto public functions""" """Test highlevelcrypto public functions"""
def test_base10_multiply(self):
"""Test arithmetic.base10_multiply"""
self.assertEqual(
sample_point,
arithmetic.base10_multiply(arithmetic.G, sample_factor))
def test_privtopub(self): def test_privtopub(self):
"""Generate public keys and check the result""" """Generate public keys and check the result"""
self.assertEqual( self.assertEqual(
arithmetic.privtopub(sample_privsigningkey).encode(), highlevelcrypto.privToPub(sample_privsigningkey),
hexlify(sample_pubsigningkey) hexlify(sample_pubsigningkey)
) )
self.assertEqual( self.assertEqual(
arithmetic.privtopub(sample_privencryptionkey).encode(), highlevelcrypto.privToPub(sample_privencryptionkey),
hexlify(sample_pubencryptionkey) hexlify(sample_pubencryptionkey)
) )

View File

@ -14,7 +14,10 @@ def unittest_discover():
# randomize the order of tests in test cases # randomize the order of tests in test cases
loader.sortTestMethodsUsing = lambda a, b: random.randint(-1, 1) loader.sortTestMethodsUsing = lambda a, b: random.randint(-1, 1)
# pybitmessage symlink may disappear on Windows # pybitmessage symlink may disappear on Windows
return loader.discover('src.tests') testsuite = loader.discover('src.tests')
testsuite.addTests([loader.discover('src.pyelliptic')])
return testsuite
if __name__ == "__main__": if __name__ == "__main__":