Skip to content

Commit

Permalink
[iotaledger#145] Reformat kerl package for PEP-8.
Browse files Browse the repository at this point in the history
  • Loading branch information
todofixthis committed Jun 13, 2018
1 parent f3073a5 commit 8e91f8a
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 129 deletions.
2 changes: 1 addition & 1 deletion iota/crypto/kerl/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# coding=utf-8
from __future__ import absolute_import, division, print_function, \
unicode_literals
unicode_literals

from .pykerl import *
77 changes: 43 additions & 34 deletions iota/crypto/kerl/conv.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,52 @@
# 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:
trits.extend(tryte_table[tryte])

return trits


def trits_to_trytes(trits):
trytes = []
trits_chunks = [trits[i:i + 3] for i in range(0, len(trits), 3)]
Expand All @@ -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))
Expand Down Expand Up @@ -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
Expand All @@ -109,6 +115,7 @@ def convertBigintToBytes(big):

return bytesArray


def convertBaseToBigint(array, base):
bigint = 0

Expand All @@ -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

Expand All @@ -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
Expand Down
Loading

0 comments on commit 8e91f8a

Please sign in to comment.