<commit_before>#!/usr/bin/env python3

# Copyright (C) 2017-2019 The btclib developers
#
# This file is part of btclib. It is subject to the license terms in the
# LICENSE file found in the top-level directory of this distribution.
#
# No part of btclib including this file, may be copied, modified, propagated,
# or distributed except according to the terms contained in the LICENSE file.

import unittest
from hashlib import sha1

from btclib.ec import pointMult
from btclib.curves import secp160r1
from btclib.utils import octets2point, point2octets, int2octets
from btclib import dh

class TestEcdh(unittest.TestCase):
    def test_ecdh(self):
        ec = secp160r1
        hf = sha1
        keydatasize = 20

        dU = 0x1
        QU = pointMult(ec, dU, ec.G)
        dV = 0x2
        QV = pointMult(ec, dV, ec.G)

        keyingdataU = dh.key_agreement, dh.kdf(dU, QV, keydatasize, ec, hf)
        keyingdataV = dh.key_agreement, dh.kdf(dV, QU, keydatasize, ec, hf)
        self.assertEqual(keyingdataU, keyingdataV)

    def test_key_deployment(self):
        """ GEC 2: Test Vectors for SEC 1, section 4.1

            http://read.pudn.com/downloads168/doc/772358/TestVectorsforSEC%201-gec2.pdf
        """

        # 4.1.1
        ec = secp160r1
        hf = sha1

        # 4.1.2
        dU = 971761939728640320549601132085879836204587084162
        self.assertEqual(format(dU, str(ec.psize)+'x'),
                         'aa374ffc3ce144e6b073307972cb6d57b2a4e982')
        QU = pointMult(ec, dU, ec.G)
        self.assertEqual(QU, (466448783855397898016055842232266600516272889280, 1110706324081757720403272427311003102474457754220))
        self.assertEqual(point2octets(ec, QU, True).hex(),
                         '0251b4496fecc406ed0e75a24a3c03206251419dc0')

        # 4.1.3
        dV = 399525573676508631577122671218044116107572676710
        self.assertEqual(format(dV, str(ec.psize)+'x'),
                         '45fb58a92a17ad4b15101c66e74f277e2b460866')
        QV = pointMult(ec, dV, ec.G)
        self.assertEqual(QV, (420773078745784176406965940076771545932416607676, 221937774842090227911893783570676792435918278531))
        self.assertEqual(point2octets(ec, QV, True).hex(),
                         '0349b41e0e9c0369c2328739d90f63d56707c6e5bc')

        # expected results
        z_exp = 1155982782519895915997745984453282631351432623114
        zstr = 'ca7c0f8c3ffa87a96e1b74ac8e6af594347bb40a'
        keydatasize = 20
        keying_data_exp = '<KEY>'

        # 4.1.4
        z, _ = pointMult(ec, dU, QV)
        self.assertEqual(z, z_exp)
        self.assertEqual(format(z, str(ec.psize)+'x'), zstr)
        keyingdata = dh.kdf(int2octets(z, ec.psize), keydatasize, ec, hf)
        self.assertEqual(keyingdata.hex(), keying_data_exp)

        # 4.1.5
        z, _ = pointMult(ec, dV, QU)
        self.assertEqual(z, z_exp)
        self.assertEqual(format(z, str(ec.psize)+'x'), zstr)
        keyingdata = dh.kdf(int2octets(z, ec.psize), keydatasize, ec, hf)
        self.assertEqual(keyingdata.hex(), keying_data_exp)


if __name__ == "__main__":
    # execute only if run as a script
    unittest.main()
<commit_msg>changed import style<commit_after>#!/usr/bin/env python3

# Copyright (C) 2017-2019 The btclib developers
#
# This file is part of btclib. It is subject to the license terms in the
# LICENSE file found in the top-level directory of this distribution.
#
# No part of btclib including this file, may be copied, modified, propagated,
# or distributed except according to the terms contained in the LICENSE file.

import unittest
from hashlib import sha1

from btclib.ec import pointMult
from btclib.curves import secp160r1
from btclib.utils import octets2point, point2octets, int2octets
from btclib import dh

class TestEcdh(unittest.TestCase):
    def test_ecdh(self):
        ec = secp160r1
        hf = sha1
        keydatasize = 20

        dU = 0x1
        QU = pointMult(ec, dU, ec.G)
        dV = 0x2
        QV = pointMult(ec, dV, ec.G)

        keyingdataU = dh.key_agreement(dU, QV, keydatasize, ec, hf)
        keyingdataV = dh.key_agreement(dV, QU, keydatasize, ec, hf)
        self.assertEqual(keyingdataU, keyingdataV)

    def test_key_deployment(self):
        """ GEC 2: Test Vectors for SEC 1, section 4.1

            http://read.pudn.com/downloads168/doc/772358/TestVectorsforSEC%201-gec2.pdf
        """

        # 4.1.1
        ec = secp160r1
        hf = sha1

        # 4.1.2
        dU = 971761939728640320549601132085879836204587084162
        self.assertEqual(format(dU, str(ec.psize)+'x'),
                         'aa374ffc3ce144e6b073307972cb6d57b2a4e982')
        QU = pointMult(ec, dU, ec.G)
        self.assertEqual(QU, (466448783855397898016055842232266600516272889280, 1110706324081757720403272427311003102474457754220))
        self.assertEqual(point2octets(ec, QU, True).hex(),
                         '0251b4496fecc406ed0e75a24a3c03206251419dc0')

        # 4.1.3
        dV = 399525573676508631577122671218044116107572676710
        self.assertEqual(format(dV, str(ec.psize)+'x'),
                         '45fb58a92a17ad4b15101c66e74f277e2b460866')
        QV = pointMult(ec, dV, ec.G)
        self.assertEqual(QV, (420773078745784176406965940076771545932416607676, 221937774842090227911893783570676792435918278531))
        self.assertEqual(point2octets(ec, QV, True).hex(),
                         '0349b41e0e9c0369c2328739d90f63d56707c6e5bc')

        # expected results
        z_exp = 1155982782519895915997745984453282631351432623114
        zstr = 'ca7c0f8c3ffa87a96e1b74ac8e6af594347bb40a'
        keydatasize = 20
        keying_data_exp = '<KEY>'

        # 4.1.4
        z, _ = pointMult(ec, dU, QV)
        self.assertEqual(z, z_exp)
        self.assertEqual(format(z, str(ec.psize)+'x'), zstr)
        keyingdata = dh.kdf(int2octets(z, ec.psize), keydatasize, ec, hf)
        self.assertEqual(keyingdata.hex(), keying_data_exp)

        # 4.1.5
        z, _ = pointMult(ec, dV, QU)
        self.assertEqual(z, z_exp)
        self.assertEqual(format(z, str(ec.psize)+'x'), zstr)
        keyingdata = dh.kdf(int2octets(z, ec.psize), keydatasize, ec, hf)
        self.assertEqual(keyingdata.hex(), keying_data_exp)


if __name__ == "__main__":
    # execute only if run as a script
    unittest.main()

{'fraction_non_alphanumeric': 0.09633972429092061, 'fraction_numerical': 0.17176358738710187, 'mean_word_length': 2.7955502104630185, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '460380', 'n_tokens_mistral': 2999, 'n_tokens_neox': 2344, 'n_words': 454}