<reponame>Kayuii/trezor-crypto<filename>core/tests/test_trezor.crypto.hashlib.blake2s.py
from common import *

from trezor.crypto import hashlib


class TestCryptoBlake2s(unittest.TestCase):

    # vectors from https://raw.githubusercontent.com/BLAKE2/BLAKE2/master/testvectors/blake2s-kat.txt

    vectors = [
        ('', '<KEY>'),
        ('00', '40d15fee7c328830166ac3f918650f807e7e01e177258cdc0a39b11f598066f1'),
        ('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e', 'b6156f72d380ee9ea6acd190464f2307a5c179ef01fd71f99f2d0f7a57360aea'),
        ('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20', '<KEY>'),
        ('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f4041', '2ef73f3c26f12d93889f3c78b6a66c1d52b649dc9e856e2c172ea7c58ac2b5e3'),
        ('<KEY>', '288c4ad9b9409762ea07c24a41f04f69a7d74bee2d95435374bde946d7241c7b'),
    ]

    def test_digest(self):
        key = unhexlify('<KEY>')
        for d, h in self.vectors:
            self.assertEqual(hashlib.blake2s(unhexlify(d), key=key).digest(), unhexlify(h))

    def test_update(self):
        key = unhexlify('<KEY>')
        x = hashlib.blake2s(b'', key=key)
        x.update(bytes(range(10)))
        self.assertEqual(x.digest(), unhexlify('f5c4b2ba1a00781b13aba0425242c69cb1552f3f71a9a3bb22b4a6b4277b46dd'))
        x.update(bytes(range(10, 30)))
        self.assertEqual(x.digest(), unhexlify('3ca989de10cfe609909472c8d35610805b2f977734cf652cc64b3bfc882d5d89'))
        x.update(bytes(range(30, 80)))
        self.assertEqual(x.digest(), unhexlify('30f3548370cfdceda5c37b569b6175e799eef1a62aaa943245ae7669c227a7b5'))
        x.update(bytes(range(80, 111)))
        self.assertEqual(x.digest(), unhexlify('9fe03bbe69ab1834f5219b0da88a08b30a66c5913f0151963c360560db0387b3'))
        x.update(bytes(range(111, 127)))
        self.assertEqual(x.digest(), unhexlify('ddbfea75cc467882eb3483ce5e2e756a4f4701b76b445519e89f22d60fa86e06'))
        x.update(bytes(range(127, 255)))
        self.assertEqual(x.digest(), unhexlify('3fb735061abc519dfe979e54c1ee5bfad0a9d858b3315bad34bde999efd724dd'))

    def test_digest_multi(self):
        x = hashlib.blake2s()
        d0 = x.digest()
        d1 = x.digest()
        d2 = x.digest()
        self.assertEqual(d0, d1)
        self.assertEqual(d0, d2)


if __name__ == '__main__':
    unittest.main()

from common import *

from trezor.crypto import hashlib


class TestCryptoBlake2s(unittest.TestCase):

    # vectors from https://raw.githubusercontent.com/BLAKE2/BLAKE2/master/testvectors/blake2s-kat.txt

    vectors = [
        ('', '<KEY>'),
        ('00', '40d15fee7c328830166ac3f918650f807e7e01e177258cdc0a39b11f598066f1'),
        ('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e', 'b6156f72d380ee9ea6acd190464f2307a5c179ef01fd71f99f2d0f7a57360aea'),
        ('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20', '<KEY>'),
        ('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f4041', '2ef73f3c26f12d93889f3c78b6a66c1d52b649dc9e856e2c172ea7c58ac2b5e3'),
        ('<KEY>', '288c4ad9b9409762ea07c24a41f04f69a7d74bee2d95435374bde946d7241c7b'),
    ]

    def test_digest(self):
        key = unhexlify('<KEY>')
        for d, h in self.vectors:
            self.assertEqual(hashlib.blake2s(unhexlify(d), key=key).digest(), unhexlify(h))

    def test_update(self):
        key = unhexlify('<KEY>')
        x = hashlib.blake2s(b'', key=key)
        x.update(bytes(range(10)))
        self.assertEqual(x.digest(), unhexlify('f5c4b2ba1a00781b13aba0425242c69cb1552f3f71a9a3bb22b4a6b4277b46dd'))
        x.update(bytes(range(10, 30)))
        self.assertEqual(x.digest(), unhexlify('3ca989de10cfe609909472c8d35610805b2f977734cf652cc64b3bfc882d5d89'))
        x.update(bytes(range(30, 80)))
        self.assertEqual(x.digest(), unhexlify('30f3548370cfdceda5c37b569b6175e799eef1a62aaa943245ae7669c227a7b5'))
        x.update(bytes(range(80, 111)))
        self.assertEqual(x.digest(), unhexlify('9fe03bbe69ab1834f5219b0da88a08b30a66c5913f0151963c360560db0387b3'))
        x.update(bytes(range(111, 127)))
        self.assertEqual(x.digest(), unhexlify('ddbfea75cc467882eb3483ce5e2e756a4f4701b76b445519e89f22d60fa86e06'))
        x.update(bytes(range(127, 255)))
        self.assertEqual(x.digest(), unhexlify('3fb735061abc519dfe979e54c1ee5bfad0a9d858b3315bad34bde999efd724dd'))

    def test_digest_multi(self):
        x = hashlib.blake2s()
        d0 = x.digest()
        d1 = x.digest()
        d2 = x.digest()
        self.assertEqual(d0, d1)
        self.assertEqual(d0, d2)


if __name__ == '__main__':
    unittest.main()
# vectors from https://raw.githubusercontent.com/BLAKE2/BLAKE2/master/testvectors/blake2s-kat.txt