Skip to content
This repository was archived by the owner on Jan 13, 2023. It is now read-only.

Commit 24b4913

Browse files
committed
Unified Kerl impl in py2k and py3k.
1 parent 9e1154d commit 24b4913

File tree

4 files changed

+17
-177
lines changed

4 files changed

+17
-177
lines changed

iota/crypto/kerl/__init__.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,4 @@
22
from __future__ import absolute_import, division, print_function, \
33
unicode_literals
44

5-
from six import PY3
6-
7-
if PY3:
8-
# noinspection SpellCheckingInspection
9-
from . import pykerl_py3 as pykerl
10-
else:
11-
# noinspection SpellCheckingInspection
12-
from . import pykerl_py2 as pykerl
5+
from .pykerl import *

iota/crypto/kerl/pykerl_py3.py renamed to iota/crypto/kerl/pykerl.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
unicode_literals
44

55
from sha3 import keccak_384
6-
from six import binary_type
6+
from six import PY2
77

88
from iota.crypto.kerl import conv
99
from iota.exceptions import with_context
@@ -17,7 +17,7 @@
1717

1818
class Kerl(object):
1919
def __init__(self):
20-
self._reset()
20+
self.reset()
2121

2222
def absorb(self, trits, offset=0, length=None):
2323
if length is None:
@@ -43,8 +43,7 @@ def absorb(self, trits, offset=0, length=None):
4343

4444
# Convert signed bytes into their equivalent unsigned representation
4545
# In order to use Python's built-in bytes type
46-
unsigned_bytes =\
47-
binary_type([conv.convert_sign(b) for b in signed_nums])
46+
unsigned_bytes = bytearray(conv.convert_sign(b) for b in signed_nums)
4847

4948
self.k.update(unsigned_bytes)
5049

@@ -63,6 +62,9 @@ def squeeze(self, trits, offset=0, length=TRIT_HASH_LENGTH):
6362
while offset < length:
6463
unsigned_hash = self.k.digest()
6564

65+
if PY2:
66+
unsigned_hash = map(ord, unsigned_hash) # type: ignore
67+
6668
signed_hash = [conv.convert_sign(b) for b in unsigned_hash]
6769

6870
trits_from_hash = conv.convertToTrits(signed_hash)
@@ -71,14 +73,13 @@ def squeeze(self, trits, offset=0, length=TRIT_HASH_LENGTH):
7173
stop = min(TRIT_HASH_LENGTH, length)
7274
trits[offset:stop] = trits_from_hash[0:stop]
7375

74-
flipped_bytes =\
75-
binary_type([conv.convert_sign(~b) for b in unsigned_hash])
76+
flipped_bytes = bytearray(conv.convert_sign(~b) for b in unsigned_hash)
7677

7778
# Reset internal state before feeding back in
78-
self._reset()
79+
self.reset()
7980
self.k.update(flipped_bytes)
8081

8182
offset += TRIT_HASH_LENGTH
8283

83-
def _reset(self):
84+
def reset(self):
8485
self.k = keccak_384()

iota/crypto/kerl/pykerl_py2.py

Lines changed: 0 additions & 154 deletions
This file was deleted.

test/crypto/kerl/pykerl_test.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
from sha3 import keccak_384
1111

12+
from iota.crypto.kerl import Kerl
1213
from iota.crypto.kerl.conv import convertToBytes, convertToTrits, \
1314
trits_to_trytes, trytes_to_trits
14-
from iota.crypto.kerl import pykerl
1515

1616

1717
class TestKerl(TestCase):
@@ -35,7 +35,7 @@ def test_correct_first(self):
3535

3636
trits = trytes_to_trits(inp)
3737

38-
kerl = pykerl.Kerl()
38+
kerl = Kerl()
3939
kerl.absorb(trits)
4040
trits_out = []
4141
kerl.squeeze(trits_out)
@@ -59,7 +59,7 @@ def test_output_greater_243(self):
5959

6060
trits = trytes_to_trits(inp)
6161

62-
kerl = pykerl.Kerl()
62+
kerl = Kerl()
6363
kerl.absorb(trits)
6464
trits_out = []
6565
kerl.squeeze(trits_out, length=486)
@@ -85,7 +85,7 @@ def test_input_greater_243(self):
8585

8686
trits = trytes_to_trits(inp)
8787

88-
kerl = pykerl.Kerl()
88+
kerl = Kerl()
8989
kerl.absorb(trits)
9090
trits_out = []
9191
kerl.squeeze(trits_out, length=486)
@@ -133,7 +133,7 @@ def test_generate_trytes_hash(self):
133133

134134
trits = trytes_to_trits(trytes)
135135

136-
kerl = pykerl.Kerl()
136+
kerl = Kerl()
137137
kerl.absorb(trits)
138138
trits_out = []
139139
kerl.squeeze(trits_out)
@@ -167,7 +167,7 @@ def test_generate_multi_trytes_and_hash(self):
167167

168168
trits = trytes_to_trits(trytes)
169169

170-
kerl = pykerl.Kerl()
170+
kerl = Kerl()
171171
kerl.absorb(trits)
172172
trits_out = []
173173
kerl.squeeze(trits_out)
@@ -203,7 +203,7 @@ def test_generate_trytes_and_multi_squeeze(self):
203203

204204
trits = trytes_to_trits(trytes)
205205

206-
kerl = pykerl.Kerl()
206+
kerl = Kerl()
207207
kerl.absorb(trits)
208208

209209
trits_out = []

0 commit comments

Comments
 (0)