<commit_before>from metadata import *

# Returns the relevant portion of the pad
def read_decrypt_pad(sid, uid, decrypt_index, clen):
    """ Read the specified bytes from the pad for decryption.

    Args:
        sid: User ID of the sender.
        uid: User ID of the recipient.
        decrypt_index: The index to start reading.
        clen: Amount of pad to retrieve in BYTES.
    Returns:
        Returns pad data as a string.
    Throws:
        AssertionError if any index goes out of bounds
    """
    metadataFile = get_metadatafile_name(uid, sid)
    metadata = read_metadata(metadataFile)
    assert decrypt_index >= 0 and decrypt_index < metadata["n_bytes"]

    storeFile = metadata["store_filename"]
    with open(storeFile, "rb") as store:
        d = metadata["direction"]
        endIndex = decrypt_index - d * clen #exclusive
        assert endIndex >= 0 and endIndex < metadata["n_bytes"]

        print("Decrypt index skipped: {}\nDecrypt index used: {}\n".
              format(decrypt_index_skipped(sid, uid, decrypt_index),
                     decrypt_index_used(sid, uid, decrypt_index)))
        with open(metadataFile, "w") as mfile:
            updates = {"decrypt_log":
                       "{}{}-{},".format(metadata["decrypt_log"],
                                         decrypt_index,
                                         endIndex),
                       "decrypt_max":
                       max(metadata["decrypt_max"], endIndex)
                       if metadata["direction"] == -1 else
                       min(metadata["decrypt_max"], endIndex)}
            metadata = update_metadata(metadata, updates)
            mfile.write(json.dumps(metadata))
        
        leftInclusive = decrypt_index if d == -1 else endIndex+1
        rightInclusive = endIndex-1 if d == -1 else decrypt_index
        store.seek(leftInclusive)
        return store.read(rightInclusive - leftInclusive + 1)[::-d]

# Returns (pad, index) 
def read_encrypt_pad(uid, rid, mlen):
    """ Read the next mlen bytes from the pad for encryption.

    Advances the pointer in metadata.
    Will return new pad data each call.

    Args:
        uid: User ID of the sender.
        rid: User ID of the recipient.
        mlen: Amount of pad to retrieve in BYTES.
    Returns:
        Returns a tuple of (pad data, index)
        pad data is a string.
        index is the start index of the portion of the pad used.
    Throws:
        AssertionError if any index goes out of bounds
    """
    metadataFile = get_metadatafile_name(uid, rid)
    metadata = read_metadata(metadataFile)

    storeFile = metadata["store_filename"]
    with open(storeFile, "rb") as store:
        e = metadata["encrypt_index"]
        d = metadata["direction"]
        endIndex = e + d * mlen
        assert endIndex >= 0 and endIndex < metadata["n_bytes"]

        with open(metadataFile, "w") as mfile:
            updates = {"encrypt_index": e+d*mlen}
            metadata = update_metadata(metadata, updates)
            mfile.write(json.dumps(metadata))

            leftInclusive = e if d == 1 else endIndex+1
            rightInclusive = endIndex-1 if d == 1 else e
            store.seek(leftInclusive)
            return store.read(rightInclusive - leftInclusive + 1)[::d]

# True if the decrypt index requested has been used before
#  to decrypt 
def decrypt_index_used(sid, uid, decrypt_index):
    metadataFile = get_metadatafile_name(uid, sid)
    metadata = read_metadata(metadataFile)

    # decrypt_log is of the form "1-10,10-15,15-22,"
    # indexes_used stores ["1-10","10-15","15-22"]
    indexes_used = metadata["decrypt_log"].split(",")[:-1]
    
    # If decrypt_index is within the range of any of
    #  the elements in the log, then the index has
    #  been used before
    d = metadata["direction"]
    temp = [s.split("-") for s in indexes_used]<commit_msg>Updated assertions to deal w.r.t. split_index instead of end-extremes<commit_after>from metadata import *

# Returns the relevant portion of the pad
def read_decrypt_pad(sid, uid, decrypt_index, clen):
    """ Read the specified bytes from the pad for decryption.

    Args:
        sid: User ID of the sender.
        uid: User ID of the recipient.
        decrypt_index: The index to start reading.
        clen: Amount of pad to retrieve in BYTES.
    Returns:
        Returns pad data as a string.
    Throws:
        AssertionError if any index goes out of bounds
    """
    metadataFile = get_metadatafile_name(uid, sid)
    metadata = read_metadata(metadataFile)
    assert decrypt_index >= 0 and decrypt_index < metadata["n_bytes"]
    d = metadata["direction"]
    if d == 1:
        assert decrypt_index >= metadata["split_index"]
    if d == -1:
        assert decrypt_index < metadata["split_index"]

    storeFile = metadata["store_filename"]
    with open(storeFile, "rb") as store:
        endIndex = decrypt_index - d * clen #exclusive
        assert endIndex >= 0 and endIndex < metadata["n_bytes"]
        if d == 1:
            assert endIndex >= metadata["split_index"]
        if d == -1:
            assert endIndex < metadata["split_index"]
        

        print("Decrypt index skipped: {}\nDecrypt index used: {}\n".
              format(decrypt_index_skipped(sid, uid, decrypt_index),
                     decrypt_index_used(sid, uid, decrypt_index)))
        with open(metadataFile, "w") as mfile:
            updates = {"decrypt_log":
                       "{}{}-{},".format(metadata["decrypt_log"],
                                         decrypt_index,
                                         endIndex),
                       "decrypt_max":
                       max(metadata["decrypt_max"], endIndex)
                       if metadata["direction"] == -1 else
                       min(metadata["decrypt_max"], endIndex)}
            metadata = update_metadata(metadata, updates)
            mfile.write(json.dumps(metadata))
        
        leftInclusive = decrypt_index if d == -1 else endIndex+1
        rightInclusive = endIndex-1 if d == -1 else decrypt_index
        store.seek(leftInclusive)
        return store.read(rightInclusive - leftInclusive + 1)[::-d]

# Returns (pad, index) 
def read_encrypt_pad(uid, rid, mlen):
    """ Read the next mlen bytes from the pad for encryption.

    Advances the pointer in metadata.
    Will return new pad data each call.

    Args:
        uid: User ID of the sender.
        rid: User ID of the recipient.
        mlen: Amount of pad to retrieve in BYTES.
    Returns:
        Returns a tuple of (pad data, index)
        pad data is a string.
        index is the start index of the portion of the pad used.
    Throws:
        AssertionError if any index goes out of bounds
    """
    metadataFile = get_metadatafile_name(uid, rid)
    metadata = read_metadata(metadataFile)
    d = metadata["direction"]

    storeFile = metadata["store_filename"]
    with open(storeFile, "rb") as store:
        e = metadata["encrypt_index"]
        endIndex = e + d * mlen
        assert endIndex >= 0 and endIndex < metadata["n_bytes"]
        if d == 1:
            assert endIndex < metadata["split_index"]
        if d == -1:
            assert endIndex >= metadata["split_index"]
        
        with open(metadataFile, "w") as mfile:
            updates = {"encrypt_index": e+d*mlen}
            metadata = update_metadata(metadata, updates)
            mfile.write(json.dumps(metadata))

            leftInclusive = e if d == 1 else endIndex+1
            rightInclusive = endIndex-1 if d == 1 else e
            store.seek(leftInclusive)
            return store.read(rightInclusive - leftInclusive + 1)[::d]

# True if the decrypt index requested has been used before
#  to decrypt 
def decrypt_index_used(sid, uid, decrypt_index):
    metadataFile = get_metadatafile_name(uid, sid)
    metadata = read_metadata(metadataFile)

    # decrypt_log is of the form "1-10,10-15,15-22,"
    # indexes_used stores ["1-10","10-15","15-22"]
    indexes_used = metadata["decrypt_log"].split(",")[:-1]
    
    # If decrypt_index is within the range of any of
    #  the elements in the log, then the index has
    #  been used before
    d = metadata["direction"]
    temp = [s.split("-") for s in indexes_used]
{'fraction_non_alphanumeric': 0.09590530257277449, 'fraction_numerical': 0.009663002778113298, 'mean_word_length': 2.2636972802522664, 'pattern_counts': {'":': 6, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4574885', 'n_tokens_mistral': 2389, 'n_tokens_neox': 2321, 'n_words': 753}