iota/crypto/signing.py
def normalize(hash_):
    # type: (Hash) -> List[List[int]]
    """
    "Normalizes" a hash, converting it into a sequence of integers
    (not trits!) suitable for use in signature generation/validation.

    The hash is divided up into 3 parts, each of which is "balanced"
    (sum of all the values is equal to zero).
    """
    normalized = []
    source = hash_.as_integers()

    chunk_size = 27

    for i in range(Hash.LEN // chunk_size):
        start = i * chunk_size
        stop = start + chunk_size

        chunk = source[start:stop]
        chunk_sum = sum(chunk)

        while chunk_sum > 0:
            chunk_sum -= 1
            for j in range(chunk_size):
                if chunk[j] > -13:
                    chunk[j] -= 1
                    break

        while chunk_sum < 0:
            chunk_sum += 1
            for j in range(chunk_size):
                if chunk[j] < 13:
                    chunk[j] += 1
                    break

        normalized.append(chunk)

    return normalized
def normalize(hash_):
    # type: (Hash) -> List[List[int]]
    """
    "Normalizes" a hash, converting it into a sequence of integers
    (not trits!) suitable for use in signature generation/validation.

    The hash is divided up into 3 parts, each of which is "balanced"
    (sum of all the values is equal to zero).
    """
    normalized = []
    source = hash_.as_integers()

    chunk_size = 27

    for i in range(Hash.LEN // chunk_size):
        start = i * chunk_size
        stop = start + chunk_size

        chunk = source[start:stop]
        chunk_sum = sum(chunk)

        while chunk_sum > 0:
            chunk_sum -= 1
            for j in range(chunk_size):
                if chunk[j] > -13:
                    chunk[j] -= 1
                    break

        while chunk_sum < 0:
            chunk_sum += 1
            for j in range(chunk_size):
                if chunk[j] < 13:
                    chunk[j] += 1
                    break

        normalized.append(chunk)

    return normalized
"Normalizes" a hash, converting it into a sequence of integers
    (not trits!) suitable for use in signature generation/validation.

    The hash is divided up into 3 parts, each of which is "balanced"
    (sum of all the values is equal to zero).