Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/muhash.py
Show First 20 Lines • Show All 83 Lines • ▼ Show 20 Lines | class MuHash3072: | ||||
def __init__(self): | def __init__(self): | ||||
"""Initialize for an empty set.""" | """Initialize for an empty set.""" | ||||
self.numerator = 1 | self.numerator = 1 | ||||
self.denominator = 1 | self.denominator = 1 | ||||
def insert(self, data): | def insert(self, data): | ||||
"""Insert a byte array data in the set.""" | """Insert a byte array data in the set.""" | ||||
data_hash = hashlib.sha256(data).digest() | |||||
self.numerator = ( | self.numerator = ( | ||||
self.numerator * data_to_num3072(data)) % self.MODULUS | self.numerator * data_to_num3072(data_hash)) % self.MODULUS | ||||
def remove(self, data): | def remove(self, data): | ||||
"""Remove a byte array from the set.""" | """Remove a byte array from the set.""" | ||||
data_hash = hashlib.sha256(data).digest() | |||||
self.denominator = ( | self.denominator = ( | ||||
self.denominator * data_to_num3072(data)) % self.MODULUS | self.denominator * data_to_num3072(data_hash)) % self.MODULUS | ||||
def digest(self): | def digest(self): | ||||
"""Extract the final hash. Does not modify this object.""" | """Extract the final hash. Does not modify this object.""" | ||||
val = (self.numerator * | val = (self.numerator * | ||||
modinv(self.denominator, self.MODULUS)) % self.MODULUS | modinv(self.denominator, self.MODULUS)) % self.MODULUS | ||||
bytes384 = val.to_bytes(384, 'little') | bytes384 = val.to_bytes(384, 'little') | ||||
return hashlib.sha256(bytes384).digest() | return hashlib.sha256(bytes384).digest() | ||||
class TestFrameworkMuhash(unittest.TestCase): | class TestFrameworkMuhash(unittest.TestCase): | ||||
def test_muhash(self): | def test_muhash(self): | ||||
muhash = MuHash3072() | muhash = MuHash3072() | ||||
muhash.insert([0] * 32) | muhash.insert(b'\x00' * 32) | ||||
muhash.insert([1] + [0] * 31) | muhash.insert((b'\x01' + b'\x00' * 31)) | ||||
muhash.remove([2] + [0] * 31) | muhash.remove((b'\x02' + b'\x00' * 31)) | ||||
finalized = muhash.digest() | finalized = muhash.digest() | ||||
# This mirrors the result in the C++ MuHash3072 unit test | # This mirrors the result in the C++ MuHash3072 unit test | ||||
self.assertEqual( | self.assertEqual( | ||||
finalized[::-1].hex(), | finalized[::-1].hex(), | ||||
"a44e16d5e34d259b349af21c06e65d653915d2e208e4e03f389af750dc0bfdc3") | "10d312b100cbd32ada024a6646e40d3482fcff103668d2625f10002a607d5863" | ||||
) | |||||
def test_chacha20(self): | def test_chacha20(self): | ||||
def chacha_check(key, result): | def chacha_check(key, result): | ||||
self.assertEqual(chacha20_32_to_384(key)[:64].hex(), result) | self.assertEqual(chacha20_32_to_384(key)[:64].hex(), result) | ||||
# Test vectors from https://tools.ietf.org/html/draft-agl-tls-chacha20poly1305-04#section-7 | # Test vectors from https://tools.ietf.org/html/draft-agl-tls-chacha20poly1305-04#section-7 | ||||
# Since the nonce is hardcoded to 0 in our function we only use those | # Since the nonce is hardcoded to 0 in our function we only use those | ||||
# vectors. | # vectors. | ||||
chacha_check( | chacha_check( | ||||
[0] * 32, | [0] * 32, | ||||
"76b8e0ada0f13d90405d6ae55386bd28bdd219b8a08ded1aa836efcc8b770dc7da41597c5157488d7724e03fb8d84a376a43b8f41518a11cc387b669b2ee6586") | "76b8e0ada0f13d90405d6ae55386bd28bdd219b8a08ded1aa836efcc8b770dc7da41597c5157488d7724e03fb8d84a376a43b8f41518a11cc387b669b2ee6586") | ||||
chacha_check( | chacha_check( | ||||
[0] * 31 + [1], | [0] * 31 + [1], | ||||
"4540f05a9f1fb296d7736e7b208e3c96eb4fe1834688d2604f450952ed432d41bbe2a0b6ea7566d2a5d1e7e20d42af2c53d792b1c43fea817e9ad275ae546963") | "4540f05a9f1fb296d7736e7b208e3c96eb4fe1834688d2604f450952ed432d41bbe2a0b6ea7566d2a5d1e7e20d42af2c53d792b1c43fea817e9ad275ae546963") |