From 49e7b8e9b883dcc215cb56653395d0bf84612687 Mon Sep 17 00:00:00 2001 From: Phoenix Zerin Date: Thu, 14 Jun 2018 08:42:24 +1200 Subject: [PATCH] [#145] Reformat kerl package for PEP-8. --- iota/crypto/kerl/__init__.py | 2 +- iota/crypto/kerl/conv.py | 77 ++++++++------ iota/crypto/kerl/pykerl.py | 195 ++++++++++++++++++----------------- 3 files changed, 145 insertions(+), 129 deletions(-) diff --git a/iota/crypto/kerl/__init__.py b/iota/crypto/kerl/__init__.py index 489aceb..094b795 100644 --- a/iota/crypto/kerl/__init__.py +++ b/iota/crypto/kerl/__init__.py @@ -1,5 +1,5 @@ # coding=utf-8 from __future__ import absolute_import, division, print_function, \ - unicode_literals + unicode_literals from .pykerl import * diff --git a/iota/crypto/kerl/conv.py b/iota/crypto/kerl/conv.py index 8d54888..5541fa1 100644 --- a/iota/crypto/kerl/conv.py +++ b/iota/crypto/kerl/conv.py @@ -1,44 +1,44 @@ # coding=utf-8 from __future__ import absolute_import, division, print_function, \ - unicode_literals - + unicode_literals BYTE_HASH_LENGTH = 48 TRIT_HASH_LENGTH = 243 tryte_table = { - '9': [ 0, 0, 0], # 0 - 'A': [ 1, 0, 0], # 1 - 'B': [-1, 1, 0], # 2 - 'C': [ 0, 1, 0], # 3 - 'D': [ 1, 1, 0], # 4 - 'E': [-1, -1, 1], # 5 - 'F': [ 0, -1, 1], # 6 - 'G': [ 1, -1, 1], # 7 - 'H': [-1, 0, 1], # 8 - 'I': [ 0, 0, 1], # 9 - 'J': [ 1, 0, 1], # 10 - 'K': [-1, 1, 1], # 11 - 'L': [ 0, 1, 1], # 12 - 'M': [ 1, 1, 1], # 13 - 'N': [-1, -1, -1], # -13 - 'O': [ 0, -1, -1], # -12 - 'P': [ 1, -1, -1], # -11 - 'Q': [-1, 0, -1], # -10 - 'R': [ 0, 0, -1], # -9 - 'S': [ 1, 0, -1], # -8 - 'T': [-1, 1, -1], # -7 - 'U': [ 0, 1, -1], # -6 - 'V': [ 1, 1, -1], # -5 - 'W': [-1, -1, 0], # -4 - 'X': [ 0, -1, 0], # -3 - 'Y': [ 1, -1, 0], # -2 - 'Z': [-1, 0, 0], # -1 - } + '9': [0, 0, 0], # 0 + 'A': [1, 0, 0], # 1 + 'B': [-1, 1, 0], # 2 + 'C': [0, 1, 0], # 3 + 'D': [1, 1, 0], # 4 + 'E': [-1, -1, 1], # 5 + 'F': [0, -1, 1], # 6 + 'G': [1, -1, 1], # 7 + 'H': [-1, 0, 1], # 8 + 'I': [0, 0, 1], # 9 + 'J': [1, 0, 1], # 10 + 'K': [-1, 1, 1], # 11 + 'L': [0, 1, 1], # 12 + 'M': [1, 1, 1], # 13 + 'N': [-1, -1, -1], # -13 + 'O': [0, -1, -1], # -12 + 'P': [1, -1, -1], # -11 + 'Q': [-1, 0, -1], # -10 + 'R': [0, 0, -1], # -9 + 'S': [1, 0, -1], # -8 + 'T': [-1, 1, -1], # -7 + 'U': [0, 1, -1], # -6 + 'V': [1, 1, -1], # -5 + 'W': [-1, -1, 0], # -4 + 'X': [0, -1, 0], # -3 + 'Y': [1, -1, 0], # -2 + 'Z': [-1, 0, 0], # -1 +} # Invert for trit -> tryte lookup trit_table = {tuple(v): k for k, v in tryte_table.items()} + def trytes_to_trits(trytes): trits = [] for tryte in trytes: @@ -46,6 +46,7 @@ def trytes_to_trits(trytes): return trits + def trits_to_trytes(trits): trytes = [] trits_chunks = [trits[i:i + 3] for i in range(0, len(trits), 3)] @@ -55,16 +56,19 @@ def trits_to_trytes(trits): return ''.join(trytes) + def convertToTrits(bytes_k): bigInt = convertBytesToBigInt(bytes_k) trits = convertBigintToBase(bigInt, 3, TRIT_HASH_LENGTH) return trits + def convertToBytes(trits): bigInt = convertBaseToBigint(trits, 3) bytes_k = convertBigintToBytes(bigInt) return bytes_k + def convertBytesToBigInt(ba): # copy of array bytesArray = list(map(lambda x: x, ba)) @@ -93,8 +97,10 @@ def convertBigintToBytes(big): range(48)] # big endian and balanced - bytesArray = list(map(lambda x: (x if x <= 0x7F else x - 0x100), - reversed(bytesArrayTemp))) + bytesArray = list(map( + lambda x: (x if x <= 0x7F else x - 0x100), + reversed(bytesArrayTemp) + )) if big < 0: # 1-compliment @@ -109,6 +115,7 @@ def convertBigintToBytes(big): return bytesArray + def convertBaseToBigint(array, base): bigint = 0 @@ -117,13 +124,14 @@ def convertBaseToBigint(array, base): return bigint + def convertBigintToBase(bigInt, base, length): result = [] is_negative = bigInt < 0 quotient = abs(bigInt) - MAX = (base-1) // 2 + MAX = (base - 1) // 2 if is_negative: MAX = base // 2 @@ -142,9 +150,10 @@ def convertBigintToBase(bigInt, base, length): return result + def convert_sign(byte): """ - Convert between signed and unsigned bytes + Convert between signed and unsigned bytes. """ if byte < 0: return 256 + byte diff --git a/iota/crypto/kerl/pykerl.py b/iota/crypto/kerl/pykerl.py index ea4187e..86edcb5 100644 --- a/iota/crypto/kerl/pykerl.py +++ b/iota/crypto/kerl/pykerl.py @@ -1,140 +1,147 @@ # coding=utf-8 from __future__ import absolute_import, division, print_function, \ - unicode_literals + unicode_literals + +from typing import MutableSequence, Optional from sha3 import keccak_384 from six import PY2 -from typing import MutableSequence, Optional from iota.crypto.kerl import conv from iota.exceptions import with_context __all__ = [ - 'Kerl', + 'Kerl', ] BYTE_HASH_LENGTH = 48 TRIT_HASH_LENGTH = 243 -class Kerl(object): - k = None # type: keccak_384 - - def __init__(self): - self.reset() - def absorb(self, trits, offset=0, length=None): - # type: (MutableSequence[int], int, Optional[int]) -> None - """ - Absorb trits into the sponge from a buffer. +class Kerl(object): + k = None # type: keccak_384 + + def __init__(self): + self.reset() + + def absorb(self, trits, offset=0, length=None): + # type: (MutableSequence[int], int, Optional[int]) -> None + """ + Absorb trits into the sponge from a buffer. - :param trits: - Buffer that contains the trits to absorb. + :param trits: + Buffer that contains the trits to absorb. - :param offset: - Starting offset in ``trits``. + :param offset: + Starting offset in ``trits``. - :param length: - Number of trits to absorb. Defaults to ``len(trits)``. - """ - # Pad input if necessary, so that it can be divided evenly into - # hashes. - # Note that this operation creates a COPY of ``trits``; the - # incoming buffer is not modified! - pad = ((len(trits) % TRIT_HASH_LENGTH) or TRIT_HASH_LENGTH) - trits += [0] * (TRIT_HASH_LENGTH - pad) + :param length: + Number of trits to absorb. Defaults to ``len(trits)``. + """ + # Pad input if necessary, so that it can be divided evenly into + # hashes. + # Note that this operation creates a COPY of ``trits``; the + # incoming buffer is not modified! + pad = ((len(trits) % TRIT_HASH_LENGTH) or TRIT_HASH_LENGTH) + trits += [0] * (TRIT_HASH_LENGTH - pad) - if length is None: - length = len(trits) + if length is None: + length = len(trits) - if length < 1: - raise with_context( - exc = ValueError('Invalid length passed to ``absorb``.'), + if length < 1: + raise with_context( + exc=ValueError('Invalid length passed to ``absorb``.'), - context = { - 'trits': trits, - 'offset': offset, - 'length': length, - }, - ) + context={ + 'trits': trits, + 'offset': offset, + 'length': length, + }, + ) - while offset < length: - stop = min(offset + TRIT_HASH_LENGTH, length) + while offset < length: + stop = min(offset + TRIT_HASH_LENGTH, length) - # If we're copying over a full chunk, zero last trit - if stop - offset == TRIT_HASH_LENGTH: - trits[stop - 1] = 0 + # If we're copying over a full chunk, zero last trit. + if stop - offset == TRIT_HASH_LENGTH: + trits[stop - 1] = 0 - signed_nums = conv.convertToBytes(trits[offset:stop]) + signed_nums = conv.convertToBytes(trits[offset:stop]) - # Convert signed bytes into their equivalent unsigned representation - # In order to use Python's built-in bytes type - unsigned_bytes = bytearray(conv.convert_sign(b) for b in signed_nums) + # Convert signed bytes into their equivalent unsigned + # representation, in order to use Python's built-in bytes + # type. + unsigned_bytes = bytearray( + conv.convert_sign(b) for b in signed_nums + ) - self.k.update(unsigned_bytes) + self.k.update(unsigned_bytes) - offset += TRIT_HASH_LENGTH + offset += TRIT_HASH_LENGTH - def squeeze(self, trits, offset=0, length=None): - # type: (MutableSequence[int], int, Optional[int]) -> None - """ - Squeeze trits from the sponge into a buffer. + def squeeze(self, trits, offset=0, length=None): + # type: (MutableSequence[int], int, Optional[int]) -> None + """ + Squeeze trits from the sponge into a buffer. - :param trits: - Buffer that will hold the squeezed trits. + :param trits: + Buffer that will hold the squeezed trits. - IMPORTANT: If ``trits`` is too small, it will be extended! + IMPORTANT: If ``trits`` is too small, it will be extended! - :param offset: - Starting offset in ``trits``. + :param offset: + Starting offset in ``trits``. - :param length: - Number of trits to squeeze from the sponge. + :param length: + Number of trits to squeeze from the sponge. - If not specified, defaults to :py:data:`TRIT_HASH_LENGTH` (i.e., - by default, we will try to squeeze exactly 1 hash). - """ - # Pad input if necessary, so that it can be divided evenly into - # hashes. - pad = ((len(trits) % TRIT_HASH_LENGTH) or TRIT_HASH_LENGTH) - trits += [0] * (TRIT_HASH_LENGTH - pad) + If not specified, defaults to :py:data:`TRIT_HASH_LENGTH` + (i.e., by default, we will try to squeeze exactly 1 hash). + """ + # Pad input if necessary, so that it can be divided evenly into + # hashes. + pad = ((len(trits) % TRIT_HASH_LENGTH) or TRIT_HASH_LENGTH) + trits += [0] * (TRIT_HASH_LENGTH - pad) - if length is None: - # By default, we will try to squeeze one hash. - # Note that this is different than ``absorb``. - length = len(trits) or TRIT_HASH_LENGTH + if length is None: + # By default, we will try to squeeze one hash. + # Note that this is different than ``absorb``. + length = len(trits) or TRIT_HASH_LENGTH - if length < 1: - raise with_context( - exc = ValueError('Invalid length passed to ``squeeze``.'), + if length < 1: + raise with_context( + exc=ValueError('Invalid length passed to ``squeeze``.'), - context = { - 'trits': trits, - 'offset': offset, - 'length': length, - }, - ) + context={ + 'trits': trits, + 'offset': offset, + 'length': length, + }, + ) - while offset < length: - unsigned_hash = self.k.digest() + while offset < length: + unsigned_hash = self.k.digest() - if PY2: - unsigned_hash = map(ord, unsigned_hash) # type: ignore + if PY2: + unsigned_hash = map(ord, unsigned_hash) # type: ignore - signed_hash = [conv.convert_sign(b) for b in unsigned_hash] + signed_hash = [conv.convert_sign(b) for b in unsigned_hash] - trits_from_hash = conv.convertToTrits(signed_hash) - trits_from_hash[TRIT_HASH_LENGTH - 1] = 0 + trits_from_hash = conv.convertToTrits(signed_hash) + trits_from_hash[TRIT_HASH_LENGTH - 1] = 0 - stop = min(TRIT_HASH_LENGTH, length-offset) - trits[offset:offset+stop] = trits_from_hash[0:stop] + stop = min(TRIT_HASH_LENGTH, length - offset) + trits[offset:offset + stop] = trits_from_hash[0:stop] - flipped_bytes = bytearray(conv.convert_sign(~b) for b in unsigned_hash) + flipped_bytes = bytearray( + conv.convert_sign(~b) for b in unsigned_hash + ) - # Reset internal state before feeding back in - self.reset() - self.k.update(flipped_bytes) + # Reset internal state before feeding back in. + self.reset() + self.k.update(flipped_bytes) - offset += TRIT_HASH_LENGTH + offset += TRIT_HASH_LENGTH - def reset(self): - self.k = keccak_384() + def reset(self): + self.k = keccak_384()