11# coding=utf-8
22from __future__ import absolute_import , division , print_function , \
3- unicode_literals
4-
3+ unicode_literals
54
65BYTE_HASH_LENGTH = 48
76TRIT_HASH_LENGTH = 243
87
98tryte_table = {
10- '9' : [ 0 , 0 , 0 ], # 0
11- 'A' : [ 1 , 0 , 0 ], # 1
12- 'B' : [- 1 , 1 , 0 ], # 2
13- 'C' : [ 0 , 1 , 0 ], # 3
14- 'D' : [ 1 , 1 , 0 ], # 4
15- 'E' : [- 1 , - 1 , 1 ], # 5
16- 'F' : [ 0 , - 1 , 1 ], # 6
17- 'G' : [ 1 , - 1 , 1 ], # 7
18- 'H' : [- 1 , 0 , 1 ], # 8
19- 'I' : [ 0 , 0 , 1 ], # 9
20- 'J' : [ 1 , 0 , 1 ], # 10
21- 'K' : [- 1 , 1 , 1 ], # 11
22- 'L' : [ 0 , 1 , 1 ], # 12
23- 'M' : [ 1 , 1 , 1 ], # 13
24- 'N' : [- 1 , - 1 , - 1 ], # -13
25- 'O' : [ 0 , - 1 , - 1 ], # -12
26- 'P' : [ 1 , - 1 , - 1 ], # -11
27- 'Q' : [- 1 , 0 , - 1 ], # -10
28- 'R' : [ 0 , 0 , - 1 ], # -9
29- 'S' : [ 1 , 0 , - 1 ], # -8
30- 'T' : [- 1 , 1 , - 1 ], # -7
31- 'U' : [ 0 , 1 , - 1 ], # -6
32- 'V' : [ 1 , 1 , - 1 ], # -5
33- 'W' : [- 1 , - 1 , 0 ], # -4
34- 'X' : [ 0 , - 1 , 0 ], # -3
35- 'Y' : [ 1 , - 1 , 0 ], # -2
36- 'Z' : [- 1 , 0 , 0 ], # -1
37- }
9+ '9' : [0 , 0 , 0 ], # 0
10+ 'A' : [1 , 0 , 0 ], # 1
11+ 'B' : [- 1 , 1 , 0 ], # 2
12+ 'C' : [0 , 1 , 0 ], # 3
13+ 'D' : [1 , 1 , 0 ], # 4
14+ 'E' : [- 1 , - 1 , 1 ], # 5
15+ 'F' : [0 , - 1 , 1 ], # 6
16+ 'G' : [1 , - 1 , 1 ], # 7
17+ 'H' : [- 1 , 0 , 1 ], # 8
18+ 'I' : [0 , 0 , 1 ], # 9
19+ 'J' : [1 , 0 , 1 ], # 10
20+ 'K' : [- 1 , 1 , 1 ], # 11
21+ 'L' : [0 , 1 , 1 ], # 12
22+ 'M' : [1 , 1 , 1 ], # 13
23+ 'N' : [- 1 , - 1 , - 1 ], # -13
24+ 'O' : [0 , - 1 , - 1 ], # -12
25+ 'P' : [1 , - 1 , - 1 ], # -11
26+ 'Q' : [- 1 , 0 , - 1 ], # -10
27+ 'R' : [0 , 0 , - 1 ], # -9
28+ 'S' : [1 , 0 , - 1 ], # -8
29+ 'T' : [- 1 , 1 , - 1 ], # -7
30+ 'U' : [0 , 1 , - 1 ], # -6
31+ 'V' : [1 , 1 , - 1 ], # -5
32+ 'W' : [- 1 , - 1 , 0 ], # -4
33+ 'X' : [0 , - 1 , 0 ], # -3
34+ 'Y' : [1 , - 1 , 0 ], # -2
35+ 'Z' : [- 1 , 0 , 0 ], # -1
36+ }
3837
3938# Invert for trit -> tryte lookup
4039trit_table = {tuple (v ): k for k , v in tryte_table .items ()}
4140
41+
4242def trytes_to_trits (trytes ):
4343 trits = []
4444 for tryte in trytes :
4545 trits .extend (tryte_table [tryte ])
4646
4747 return trits
4848
49+
4950def trits_to_trytes (trits ):
5051 trytes = []
5152 trits_chunks = [trits [i :i + 3 ] for i in range (0 , len (trits ), 3 )]
@@ -55,16 +56,19 @@ def trits_to_trytes(trits):
5556
5657 return '' .join (trytes )
5758
59+
5860def convertToTrits (bytes_k ):
5961 bigInt = convertBytesToBigInt (bytes_k )
6062 trits = convertBigintToBase (bigInt , 3 , TRIT_HASH_LENGTH )
6163 return trits
6264
65+
6366def convertToBytes (trits ):
6467 bigInt = convertBaseToBigint (trits , 3 )
6568 bytes_k = convertBigintToBytes (bigInt )
6669 return bytes_k
6770
71+
6872def convertBytesToBigInt (ba ):
6973 # copy of array
7074 bytesArray = list (map (lambda x : x , ba ))
@@ -93,8 +97,10 @@ def convertBigintToBytes(big):
9397 range (48 )]
9498
9599 # big endian and balanced
96- bytesArray = list (map (lambda x : (x if x <= 0x7F else x - 0x100 ),
97- reversed (bytesArrayTemp )))
100+ bytesArray = list (map (
101+ lambda x : (x if x <= 0x7F else x - 0x100 ),
102+ reversed (bytesArrayTemp )
103+ ))
98104
99105 if big < 0 :
100106 # 1-compliment
@@ -109,6 +115,7 @@ def convertBigintToBytes(big):
109115
110116 return bytesArray
111117
118+
112119def convertBaseToBigint (array , base ):
113120 bigint = 0
114121
@@ -117,13 +124,14 @@ def convertBaseToBigint(array, base):
117124
118125 return bigint
119126
127+
120128def convertBigintToBase (bigInt , base , length ):
121129 result = []
122130
123131 is_negative = bigInt < 0
124132 quotient = abs (bigInt )
125133
126- MAX = (base - 1 ) // 2
134+ MAX = (base - 1 ) // 2
127135 if is_negative :
128136 MAX = base // 2
129137
@@ -142,9 +150,10 @@ def convertBigintToBase(bigInt, base, length):
142150
143151 return result
144152
153+
145154def convert_sign (byte ):
146155 """
147- Convert between signed and unsigned bytes
156+ Convert between signed and unsigned bytes.
148157 """
149158 if byte < 0 :
150159 return 256 + byte
0 commit comments