<commit_before>import unittest
from binascii import hexlify, unhexlify
from hashlib import sha256
from hkdf import Hkdf
from .myhkdf import HKDF as myHKDF
from spake2 import groups, ed25519_group
from spake2.spake2 import (SPAKE2_A, SPAKE2_B, SPAKE2_Symmetric,
                           finalize_SPAKE2, finalize_SPAKE2_symmetric)
from .common import PRG

class TestPRG(unittest.TestCase):
    def test_basic(self):
        PRGA = PRG(b"A")
        dataA = PRGA(16)
        self.assertEqual(hexlify(dataA), b"c1d59d78903e9d7874d9064e12d36c58")
        PRGB = PRG(b"B")
        dataB = PRGB(16)
        self.assertEqual(hexlify(dataB), b"2af6d4b843a9e6cd1d185eb5de870f77")

class SPAKE2(unittest.TestCase):
    """Make sure we know when an incompatible change has landed"""
    def test_asymmetric(self):
        PRGA = PRG(b"A")
        PRGB = PRG(b"B")
        pw = b"password"
        sA,sB = SPAKE2_A(pw, entropy_f=PRGA), SPAKE2_B(pw, entropy_f=PRGB)
        m1A,m1B = sA.start(), sB.start()
        self.assertEqual(hexlify(m1A), b"416fc960df73c9cf8ed7198b0c9534e2e96a5984bfc5edc023fd24dacf371f2af9")
        self.assertEqual(hexlify(m1B), b"42354e97b88406922b1df4bea1d7870f17aed3dba7c720b313edae315b00959309")
        # peek at the scalars, since it ought to be stable, and other
        # implementations that want to use this as a test vector might start
        # with the scalar, rather than duplicating our deterministic RNG
        self.assertEqual(sA.pw_scalar,
                         3515301705789368674385125653994241092664323519848410154015274772661223168839)
        self.assertEqual(sB.pw_scalar,
                         3515301705789368674385125653994241092664323519848410154015274772661223168839)
        self.assertEqual(sA.xy_scalar,
                         2611694063369306139794446498317402240796898290761098242657700742213257926693L)
        self.assertEqual(sB.xy_scalar,
                         7002393159576182977806091886122272758628412261510164356026361256515836884383)

        kA,kB = sA.finish(m1B), sB.finish(m1A)
        self.assertEqual(hexlify(kA),
                         b"a480bca13fa04464bb644f10e340125e96c9494f7399fef7c2bda67eb0fdf06d")
        self.assertEqual(hexlify(kA), hexlify(kB))
        self.assertEqual(len(kA), len(sha256().digest()))

    def test_symmetric(self):
        PRG1 = PRG(b"1")
        PRG2 = PRG(b"2")<commit_msg>fix py2-ism in test vector<commit_after>import unittest
from binascii import hexlify, unhexlify
from hashlib import sha256
from hkdf import Hkdf
from .myhkdf import HKDF as myHKDF
from spake2 import groups, ed25519_group
from spake2.spake2 import (SPAKE2_A, SPAKE2_B, SPAKE2_Symmetric,
                           finalize_SPAKE2, finalize_SPAKE2_symmetric)
from .common import PRG

class TestPRG(unittest.TestCase):
    def test_basic(self):
        PRGA = PRG(b"A")
        dataA = PRGA(16)
        self.assertEqual(hexlify(dataA), b"c1d59d78903e9d7874d9064e12d36c58")
        PRGB = PRG(b"B")
        dataB = PRGB(16)
        self.assertEqual(hexlify(dataB), b"2af6d4b843a9e6cd1d185eb5de870f77")

class SPAKE2(unittest.TestCase):
    """Make sure we know when an incompatible change has landed"""
    def test_asymmetric(self):
        PRGA = PRG(b"A")
        PRGB = PRG(b"B")
        pw = b"password"
        sA,sB = SPAKE2_A(pw, entropy_f=PRGA), SPAKE2_B(pw, entropy_f=PRGB)
        m1A,m1B = sA.start(), sB.start()
        self.assertEqual(hexlify(m1A), b"416fc960df73c9cf8ed7198b0c9534e2e96a5984bfc5edc023fd24dacf371f2af9")
        self.assertEqual(hexlify(m1B), b"42354e97b88406922b1df4bea1d7870f17aed3dba7c720b313edae315b00959309")
        # peek at the scalars, since it ought to be stable, and other
        # implementations that want to use this as a test vector might start
        # with the scalar, rather than duplicating our deterministic RNG
        self.assertEqual(sA.pw_scalar,
                         3515301705789368674385125653994241092664323519848410154015274772661223168839)
        self.assertEqual(sB.pw_scalar,
                         3515301705789368674385125653994241092664323519848410154015274772661223168839)
        self.assertEqual(sA.xy_scalar,
                         2611694063369306139794446498317402240796898290761098242657700742213257926693)
        self.assertEqual(sB.xy_scalar,
                         7002393159576182977806091886122272758628412261510164356026361256515836884383)

        kA,kB = sA.finish(m1B), sB.finish(m1A)
        self.assertEqual(hexlify(kA),
                         b"a480bca13fa04464bb644f10e340125e96c9494f7399fef7c2bda67eb0fdf06d")
        self.assertEqual(hexlify(kA), hexlify(kB))
        self.assertEqual(len(kA), len(sha256().digest()))

    def test_symmetric(self):
        PRG1 = PRG(b"1")
        PRG2 = PRG(b"2")
{'fraction_non_alphanumeric': 0.08047658862876254, 'fraction_numerical': 0.20882107023411373, 'mean_word_length': 3.242021276595745, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7224564', 'n_tokens_mistral': 2377, 'n_tokens_neox': 1806, 'n_words': 287}