<commit_before>import time
import base64

from pyoram.crypto.aes import AES

def runtest(label, enc_func, dec_func):
    print("")
    print("$"*20)
    print("{0:^20}".format(label))
    print("$"*20)
    for keysize in AES.key_sizes:
        print("")
        print("@@@@@@@@@@@@@@@@@@@@")
        print(" Key Size: %s bytes" % (keysize))
        print("@@@@@@@@@@@@@@@@@@@@")
        print("\nTest Bulk")
        #
        # generate a key
        #
        key = AES.KeyGen(keysize)
        print("Key: %s" % (base64.b64encode(key)))

        #
        # generate some plaintext
        #
        nblocks = 10000000
        plaintext_numbytes = AES.block_size * nblocks
        print("Plaintext Size: %s MB"
              % (plaintext_numbytes * 1.0e-6))
        # all zeros
        plaintext = bytes(bytearray(plaintext_numbytes))

        #
        # time encryption
        #
        start_time = time.time()
        ciphertext = enc_func(key, plaintext)
        stop_time = time.time()
        print("Encryption Time: %.3fs (%.3f MB/s)"
              % (stop_time-start_time,
                 (plaintext_numbytes * 1.0e-6) / (stop_time-start_time)))

        #
        # time decryption
        #
        start_time = time.time()
        plaintext_decrypted = dec_func(key, ciphertext)
        stop_time = time.time()
        print("Decryption Time: %.3fs (%.3f MB/s)"
              % (stop_time-start_time,
                 (plaintext_numbytes * 1.0e-6) / (stop_time-start_time)))

        assert plaintext_decrypted == plaintext
        assert ciphertext != plaintext
        # IND-CPA
        assert enc_func(key, plaintext) != ciphertext
        # make sure the only difference is not in the IV
        assert enc_func(key, plaintext)[AES.block_size:] \
            != ciphertext[AES.block_size:]
        if enc_func is AES.CTREnc:
            assert len(plaintext) == \
                len(ciphertext) - AES.block_size
        else:
            assert enc_func is AES.GCMEnc
            assert len(plaintext) == \
                len(ciphertext) - 2*AES.block_size

        del plaintext
        del plaintext_decrypted
        del ciphertext

        print("\nTest Chunks")
        #
        # generate a key
        #
        key = AES.KeyGen(keysize)
        print("Key: %s" % (base64.b64encode(key)))

        #
        # generate some plaintext
        #
        nblocks = 10000
        blocksize = 16000
        total_bytes = blocksize * nblocks
        print("Block Size: %s KB" % (blocksize * 1.0e-3))
        print("Block Count: %s" % (nblocks))
        print("Total: %s MB" % (total_bytes * 1.0e-6))
        plaintext_blocks = [bytes(bytearray(blocksize))
                            for i in range(nblocks)]

        #<commit_msg>reducing memory used by example so tests can be run on 32bit systems<commit_after>import time
import base64

from pyoram.crypto.aes import AES

def runtest(label, enc_func, dec_func):
    print("")
    print("$"*20)
    print("{0:^20}".format(label))
    print("$"*20)
    for keysize in AES.key_sizes:
        print("")
        print("@@@@@@@@@@@@@@@@@@@@")
        print(" Key Size: %s bytes" % (keysize))
        print("@@@@@@@@@@@@@@@@@@@@")
        print("\nTest Bulk")
        #
        # generate a key
        #
        key = AES.KeyGen(keysize)
        print("Key: %s" % (base64.b64encode(key)))

        #
        # generate some plaintext
        #
        nblocks = 1000000
        plaintext_numbytes = AES.block_size * nblocks
        print("Plaintext Size: %s MB"
              % (plaintext_numbytes * 1.0e-6))
        # all zeros
        plaintext = bytes(bytearray(plaintext_numbytes))

        #
        # time encryption
        #
        start_time = time.time()
        ciphertext = enc_func(key, plaintext)
        stop_time = time.time()
        print("Encryption Time: %.3fs (%.3f MB/s)"
              % (stop_time-start_time,
                 (plaintext_numbytes * 1.0e-6) / (stop_time-start_time)))

        #
        # time decryption
        #
        start_time = time.time()
        plaintext_decrypted = dec_func(key, ciphertext)
        stop_time = time.time()
        print("Decryption Time: %.3fs (%.3f MB/s)"
              % (stop_time-start_time,
                 (plaintext_numbytes * 1.0e-6) / (stop_time-start_time)))

        assert plaintext_decrypted == plaintext
        assert ciphertext != plaintext
        # IND-CPA
        assert enc_func(key, plaintext) != ciphertext
        # make sure the only difference is not in the IV
        assert enc_func(key, plaintext)[AES.block_size:] \
            != ciphertext[AES.block_size:]
        if enc_func is AES.CTREnc:
            assert len(plaintext) == \
                len(ciphertext) - AES.block_size
        else:
            assert enc_func is AES.GCMEnc
            assert len(plaintext) == \
                len(ciphertext) - 2*AES.block_size

        del plaintext
        del plaintext_decrypted
        del ciphertext

        print("\nTest Chunks")
        #
        # generate a key
        #
        key = AES.KeyGen(keysize)
        print("Key: %s" % (base64.b64encode(key)))

        #
        # generate some plaintext
        #
        nblocks = 1000
        blocksize = 16000
        total_bytes = blocksize * nblocks
        print("Block Size: %s KB" % (blocksize * 1.0e-3))
        print("Block Count: %s" % (nblocks))
        print("Total: %s MB" % (total_bytes * 1.0e-6))
        plaintext_blocks = [bytes(bytearray(blocksize))
                            for i in range(nblocks)]

        #
{'fraction_non_alphanumeric': 0.12731523107354792, 'fraction_numerical': 0.01978061499730264, 'mean_word_length': 1.7907676869041647, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, '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': '2950510', 'n_tokens_mistral': 1853, 'n_tokens_neox': 1719, 'n_words': 376}