def test_chacha_block(self):
        # RFC 7539 in text test vector
        key = [
            0x00010203, 0x04050607, 0x08090a0b, 0x0c0d0e0f, 0x10111213,
            0x14151617, 0x18191a1b, 0x1c1d1e1f
        ]
        for i, item in enumerate(key):
            key[i] = self.betole32(item)
        nonce = [0x00000009, 0x0000004a, 0x00000000]
        for i, item in enumerate(nonce):
            nonce[i] = self.betole32(item)
        counter = 1

        x = ChaCha.chacha_block(key, counter, nonce, rounds=20)

        self.assertEqual(x, [
            0xe4e7f110, 0x15593bd1, 0x1fdd0f50, 0xc47120a3, 0xc7f4d1c7,
            0x0368c033, 0x9aaa2204, 0x4e6cd4c3, 0x466482d2, 0x09aa9f07,
            0x05d7c214, 0xa2028bd9, 0xd19c12b5, 0xb94e16de, 0xe883d0cb,
            0x4e3c50a2
        ])

        self.assertEqual(
            ChaCha.word_to_bytearray(x),
            bytearray(
                b'\x10\xf1\xe7\xe4\xd1\x3b\x59\x15\x50\x0f\xdd\x1f\xa3\x20\x71\xc4'
                b'\xc7\xd1\xf4\xc7\x33\xc0\x68\x03\x04\x22\xaa\x9a\xc3\xd4\x6c\x4e'
                b'\xd2\x82\x64\x46\x07\x9f\xaa\x09\x14\xc2\xd7\x05\xd9\x8b\x02\xa2'
                b'\xb5\x12\x9c\xd1\xde\x16\x4e\xb9\xcb\xd0\x83\xe8\xa2\x50\x3c\x4e'
            ))
    def test_chacha_block_vector4(self):
        # RFC 7539 Appendix A.1 test vector #4
        key = bytearray(b'\x00' + b'\xff' + b'\x00' * 30)
        nonce = bytearray(12)
        counter = 2

        chacha = ChaCha(key, nonce, counter=counter)

        x = ChaCha.chacha_block(chacha.key,
                                chacha.counter,
                                chacha.nonce,
                                rounds=20)

        self.assertEqual(x, [
            0xfb4dd572, 0x4bc42ef1, 0xdf922636, 0x327f1394, 0xa78dea8f,
            0x5e269039, 0xa1bebbc1, 0xcaf09aae, 0xa25ab213, 0x48a6b46c,
            0x1b9d9bcb, 0x092c5be6, 0x546ca624, 0x1bec45d5, 0x87f47473,
            0x96f0992e
        ])

        ciphertext = ChaCha.word_to_bytearray(x)

        self.assertEqual(
            ciphertext,
            bytearray(
                b'\x72\xd5\x4d\xfb\xf1\x2e\xc4\x4b\x36\x26\x92\xdf\x94\x13\x7f\x32'
                b'\x8f\xea\x8d\xa7\x39\x90\x26\x5e\xc1\xbb\xbe\xa1\xae\x9a\xf0\xca'
                b'\x13\xb2\x5a\xa2\x6c\xb4\xa6\x48\xcb\x9b\x9d\x1b\xe6\x5b\x2c\x09'
                b'\x24\xa6\x6c\x54\xd5\x45\xec\x1b\x73\x74\xf4\x87\x2e\x99\xf0\x96'
            ))
    def test_chacha_block_vector5(self):
        # RFC 7539 Appendix A.1 test vector #5
        key = bytearray(32)
        nonce = bytearray(b'\x00' * 11 + b'\x02')
        counter = 0

        chacha = ChaCha(key, nonce, counter=counter)

        x = ChaCha.chacha_block(chacha.key,
                                chacha.counter,
                                chacha.nonce,
                                rounds=20)

        self.assertEqual(x, [
            0x374dc6c2, 0x3736d58c, 0xb904e24a, 0xcd3f93ef, 0x88228b1a,
            0x96a4dfb3, 0x5b76ab72, 0xc727ee54, 0x0e0e978a, 0xf3145c95,
            0x1b748ea8, 0xf786c297, 0x99c28f5f, 0x628314e8, 0x398a19fa,
            0x6ded1b53
        ])

        ciphertext = ChaCha.word_to_bytearray(x)

        self.assertEqual(
            ciphertext,
            bytearray(
                b'\xc2\xc6\x4d\x37\x8c\xd5\x36\x37\x4a\xe2\x04\xb9\xef\x93\x3f\xcd'
                b'\x1a\x8b\x22\x88\xb3\xdf\xa4\x96\x72\xab\x76\x5b\x54\xee\x27\xc7'
                b'\x8a\x97\x0e\x0e\x95\x5c\x14\xf3\xa8\x8e\x74\x1b\x97\xc2\x86\xf7'
                b'\x5f\x8f\xc2\x99\xe8\x14\x83\x62\xfa\x19\x8a\x39\x53\x1b\xed\x6d'
            ))
    def test_chacha_block_vector2(self):
        # RFC 7539 Appendix A.1 test vector #2
        key = [0] * 8
        nonce = [0] * 3
        counter = 1

        x = ChaCha.chacha_block(key, counter, nonce, rounds=20)

        self.assertEqual(x, [
            0xbee7079f, 0x7a385155, 0x7c97ba98, 0x0d082d73, 0xa0290fcb,
            0x6965e348, 0x3e53c612, 0xed7aee32, 0x7621b729, 0x434ee69c,
            0xb03371d5, 0xd539d874, 0x281fed31, 0x45fb0a51, 0x1f0ae1ac,
            0x6f4d794b
        ])

        ciphertext = ChaCha.word_to_bytearray(x)

        self.assertEqual(
            ciphertext,
            bytearray(
                b'\x9f\x07\xe7\xbe\x55\x51\x38\x7a\x98\xba\x97\x7c\x73\x2d\x08\x0d'
                b'\xcb\x0f\x29\xa0\x48\xe3\x65\x69\x12\xc6\x53\x3e\x32\xee\x7a\xed'
                b'\x29\xb7\x21\x76\x9c\xe6\x4e\x43\xd5\x71\x33\xb0\x74\xd8\x39\xd5'
                b'\x31\xed\x1f\x28\x51\x0a\xfb\x45\xac\xe1\x0a\x1f\x4b\x79\x4d\x6f'
            ))
    def test_chacha_block_vector3(self):
        # RFC 7539 Appendix A.1 test vector #3
        key = bytearray(b'\x00' * 31 + b'\x01')
        nonce = bytearray(12)
        counter = 1

        chacha = ChaCha(key, nonce, counter=counter)

        x = ChaCha.chacha_block(chacha.key,
                                chacha.counter,
                                chacha.nonce,
                                rounds=20)

        self.assertEqual(x, [
            0x2452eb3a, 0x9249f8ec, 0x8d829d9b, 0xddd4ceb1, 0xe8252083,
            0x60818b01, 0xf38422b8, 0x5aaa49c9, 0xbb00ca8e, 0xda3ba7b4,
            0xc4b592d1, 0xfdf2732f, 0x4436274e, 0x2561b3c8, 0xebdd4aa6,
            0xa0136c00
        ])

        ciphertext = ChaCha.word_to_bytearray(x)

        self.assertEqual(
            ciphertext,
            bytearray(
                b'\x3a\xeb\x52\x24\xec\xf8\x49\x92\x9b\x9d\x82\x8d\xb1\xce\xd4\xdd'
                b'\x83\x20\x25\xe8\x01\x8b\x81\x60\xb8\x22\x84\xf3\xc9\x49\xaa\x5a'
                b'\x8e\xca\x00\xbb\xb4\xa7\x3b\xda\xd1\x92\xb5\xc4\x2f\x73\xf2\xfd'
                b'\x4e\x27\x36\x44\xc8\xb3\x61\x25\xa6\x4a\xdd\xeb\x00\x6c\x13\xa0'
            ))
    def test_chacha_block(self):
        # RFC 7539 in text test vector
        key = [0x00010203, 0x04050607, 0x08090a0b, 0x0c0d0e0f,
               0x10111213, 0x14151617, 0x18191a1b, 0x1c1d1e1f]
        for i, item in enumerate(key):
            key[i] = self.betole32(item)
        nonce = [0x00000009, 0x0000004a, 0x00000000]
        for i, item in enumerate(nonce):
            nonce[i] = self.betole32(item)
        counter = 1

        x = ChaCha.chacha_block(key, counter, nonce, rounds=20)

        self.assertEqual(x,
                         [0xe4e7f110,  0x15593bd1,  0x1fdd0f50,  0xc47120a3,
                          0xc7f4d1c7,  0x0368c033,  0x9aaa2204,  0x4e6cd4c3,
                          0x466482d2,  0x09aa9f07,  0x05d7c214,  0xa2028bd9,
                          0xd19c12b5,  0xb94e16de,  0xe883d0cb,  0x4e3c50a2])

        self.assertEqual(ChaCha.word_to_bytearray(x), bytearray(
            b'\x10\xf1\xe7\xe4\xd1\x3b\x59\x15\x50\x0f\xdd\x1f\xa3\x20\x71\xc4'
            b'\xc7\xd1\xf4\xc7\x33\xc0\x68\x03\x04\x22\xaa\x9a\xc3\xd4\x6c\x4e'
            b'\xd2\x82\x64\x46\x07\x9f\xaa\x09\x14\xc2\xd7\x05\xd9\x8b\x02\xa2'
            b'\xb5\x12\x9c\xd1\xde\x16\x4e\xb9\xcb\xd0\x83\xe8\xa2\x50\x3c\x4e'
            ))
    def test_chacha_block_vector1(self):
        # RFC 7539 Appendix A.1 test vector #1
        key = [0] * 8
        nonce = [0] * 3
        counter = 0

        x = ChaCha.chacha_block(key, counter, nonce, rounds=20)

        self.assertEqual(x, [
            0xade0b876, 0x903df1a0, 0xe56a5d40, 0x28bd8653, 0xb819d2bd,
            0x1aed8da0, 0xccef36a8, 0xc70d778b, 0x7c5941da, 0x8d485751,
            0x3fe02477, 0x374ad8b8, 0xf4b8436a, 0x1ca11815, 0x69b687c3,
            0x8665eeb2
        ])

        ciphertext = ChaCha.word_to_bytearray(x)

        self.assertEqual(
            ciphertext,
            bytearray(
                b'\x76\xb8\xe0\xad\xa0\xf1\x3d\x90\x40\x5d\x6a\xe5\x53\x86\xbd\x28'
                b'\xbd\xd2\x19\xb8\xa0\x8d\xed\x1a\xa8\x36\xef\xcc\x8b\x77\x0d\xc7'
                b'\xda\x41\x59\x7c\x51\x57\x48\x8d\x77\x24\xe0\x3f\xb8\xd8\x4a\x37'
                b'\x6a\x43\xb8\xf4\x15\x18\xa1\x1c\xc3\x87\xb6\x69\xb2\xee\x65\x86'
            ))
    def test_chacha_block_vector5(self):
        # RFC 7539 Appendix A.1 test vector #5
        key = bytearray(32)
        nonce = bytearray(b'\x00'*11 + b'\x02')
        counter = 0

        chacha = ChaCha(key, nonce, counter=counter)

        x = ChaCha.chacha_block(chacha.key,
                                chacha.counter,
                                chacha.nonce,
                                rounds=20)

        self.assertEqual(x,
                         [0x374dc6c2,  0x3736d58c,  0xb904e24a,  0xcd3f93ef,
                          0x88228b1a,  0x96a4dfb3,  0x5b76ab72,  0xc727ee54,
                          0x0e0e978a,  0xf3145c95,  0x1b748ea8,  0xf786c297,
                          0x99c28f5f,  0x628314e8,  0x398a19fa,  0x6ded1b53])

        ciphertext = ChaCha.word_to_bytearray(x)

        self.assertEqual(ciphertext, bytearray(
            b'\xc2\xc6\x4d\x37\x8c\xd5\x36\x37\x4a\xe2\x04\xb9\xef\x93\x3f\xcd'
            b'\x1a\x8b\x22\x88\xb3\xdf\xa4\x96\x72\xab\x76\x5b\x54\xee\x27\xc7'
            b'\x8a\x97\x0e\x0e\x95\x5c\x14\xf3\xa8\x8e\x74\x1b\x97\xc2\x86\xf7'
            b'\x5f\x8f\xc2\x99\xe8\x14\x83\x62\xfa\x19\x8a\x39\x53\x1b\xed\x6d'
            ))
    def test_chacha_block_vector4(self):
        # RFC 7539 Appendix A.1 test vector #4
        key = bytearray(b'\x00' + b'\xff' + b'\x00'*30)
        nonce = bytearray(12)
        counter = 2

        chacha = ChaCha(key, nonce, counter=counter)

        x = ChaCha.chacha_block(chacha.key,
                                chacha.counter,
                                chacha.nonce,
                                rounds=20)

        self.assertEqual(x,
                         [0xfb4dd572,  0x4bc42ef1,  0xdf922636,  0x327f1394,
                          0xa78dea8f,  0x5e269039,  0xa1bebbc1,  0xcaf09aae,
                          0xa25ab213,  0x48a6b46c,  0x1b9d9bcb,  0x092c5be6,
                          0x546ca624,  0x1bec45d5,  0x87f47473,  0x96f0992e])

        ciphertext = ChaCha.word_to_bytearray(x)

        self.assertEqual(ciphertext, bytearray(
            b'\x72\xd5\x4d\xfb\xf1\x2e\xc4\x4b\x36\x26\x92\xdf\x94\x13\x7f\x32'
            b'\x8f\xea\x8d\xa7\x39\x90\x26\x5e\xc1\xbb\xbe\xa1\xae\x9a\xf0\xca'
            b'\x13\xb2\x5a\xa2\x6c\xb4\xa6\x48\xcb\x9b\x9d\x1b\xe6\x5b\x2c\x09'
            b'\x24\xa6\x6c\x54\xd5\x45\xec\x1b\x73\x74\xf4\x87\x2e\x99\xf0\x96'
            ))
    def test_chacha_block_vector3(self):
        # RFC 7539 Appendix A.1 test vector #3
        key = bytearray(b'\x00'*31 + b'\x01')
        nonce = bytearray(12)
        counter = 1

        chacha = ChaCha(key, nonce, counter=counter)

        x = ChaCha.chacha_block(chacha.key,
                                chacha.counter,
                                chacha.nonce,
                                rounds=20)

        self.assertEqual(x,
                         [0x2452eb3a,  0x9249f8ec,  0x8d829d9b,  0xddd4ceb1,
                          0xe8252083,  0x60818b01,  0xf38422b8,  0x5aaa49c9,
                          0xbb00ca8e,  0xda3ba7b4,  0xc4b592d1,  0xfdf2732f,
                          0x4436274e,  0x2561b3c8,  0xebdd4aa6,  0xa0136c00])

        ciphertext = ChaCha.word_to_bytearray(x)

        self.assertEqual(ciphertext, bytearray(
            b'\x3a\xeb\x52\x24\xec\xf8\x49\x92\x9b\x9d\x82\x8d\xb1\xce\xd4\xdd'
            b'\x83\x20\x25\xe8\x01\x8b\x81\x60\xb8\x22\x84\xf3\xc9\x49\xaa\x5a'
            b'\x8e\xca\x00\xbb\xb4\xa7\x3b\xda\xd1\x92\xb5\xc4\x2f\x73\xf2\xfd'
            b'\x4e\x27\x36\x44\xc8\xb3\x61\x25\xa6\x4a\xdd\xeb\x00\x6c\x13\xa0'
            ))
    def test_chacha_block_vector2(self):
        # RFC 7539 Appendix A.1 test vector #2
        key = [0]*8
        nonce = [0]*3
        counter = 1

        x = ChaCha.chacha_block(key, counter, nonce, rounds=20)

        self.assertEqual(x,
                         [0xbee7079f,  0x7a385155,  0x7c97ba98,  0x0d082d73,
                          0xa0290fcb,  0x6965e348,  0x3e53c612,  0xed7aee32,
                          0x7621b729,  0x434ee69c,  0xb03371d5,  0xd539d874,
                          0x281fed31,  0x45fb0a51,  0x1f0ae1ac,  0x6f4d794b])

        ciphertext = ChaCha.word_to_bytearray(x)

        self.assertEqual(ciphertext, bytearray(
            b'\x9f\x07\xe7\xbe\x55\x51\x38\x7a\x98\xba\x97\x7c\x73\x2d\x08\x0d'
            b'\xcb\x0f\x29\xa0\x48\xe3\x65\x69\x12\xc6\x53\x3e\x32\xee\x7a\xed'
            b'\x29\xb7\x21\x76\x9c\xe6\x4e\x43\xd5\x71\x33\xb0\x74\xd8\x39\xd5'
            b'\x31\xed\x1f\x28\x51\x0a\xfb\x45\xac\xe1\x0a\x1f\x4b\x79\x4d\x6f'
            ))
    def test_chacha_block_vector1(self):
        # RFC 7539 Appendix A.1 test vector #1
        key = [0]*8
        nonce = [0]*3
        counter = 0

        x = ChaCha.chacha_block(key, counter, nonce, rounds=20)

        self.assertEqual(x,
                         [0xade0b876,  0x903df1a0,  0xe56a5d40,  0x28bd8653,
                          0xb819d2bd,  0x1aed8da0,  0xccef36a8,  0xc70d778b,
                          0x7c5941da,  0x8d485751,  0x3fe02477,  0x374ad8b8,
                          0xf4b8436a,  0x1ca11815,  0x69b687c3,  0x8665eeb2])

        ciphertext = ChaCha.word_to_bytearray(x)

        self.assertEqual(ciphertext, bytearray(
            b'\x76\xb8\xe0\xad\xa0\xf1\x3d\x90\x40\x5d\x6a\xe5\x53\x86\xbd\x28'
            b'\xbd\xd2\x19\xb8\xa0\x8d\xed\x1a\xa8\x36\xef\xcc\x8b\x77\x0d\xc7'
            b'\xda\x41\x59\x7c\x51\x57\x48\x8d\x77\x24\xe0\x3f\xb8\xd8\x4a\x37'
            b'\x6a\x43\xb8\xf4\x15\x18\xa1\x1c\xc3\x87\xb6\x69\xb2\xee\x65\x86'
            ))