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

Commit fb98b22

Browse files
committed
Add squeeze length and test case
1 parent 358d4b0 commit fb98b22

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-7
lines changed

iota/crypto/pycurl.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ def absorb(self, trits, offset=0, length=None):
113113
# Move on to the next hash.
114114
offset += HASH_LENGTH
115115

116-
def squeeze(self, trits, offset=0):
117-
# type: (MutableSequence[int]) -> None
116+
def squeeze(self, trits, offset=0, length=HASH_LENGTH):
117+
# type: (MutableSequence[int], Optional[int], Optional[int]) -> None
118118
"""
119119
Squeeze trits from the sponge.
120120
@@ -124,6 +124,9 @@ def squeeze(self, trits, offset=0):
124124
125125
:param offset:
126126
Starting offset in ``trits``.
127+
128+
:param length:
129+
Number of trits to squeeze, default to ``HASH_LENGTH``
127130
"""
128131
#
129132
# Squeeze is kind of like the opposite of absorb; it copies trits
@@ -134,8 +137,18 @@ def squeeze(self, trits, offset=0):
134137
# can simplify the implementation somewhat.
135138
#
136139

140+
# Ensure length can be mod by HASH_LENGTH
141+
if length % HASH_LENGTH != 0:
142+
raise with_context(
143+
exc = ValueError('Invalid length passed to ``sequeeze`.'),
144+
context = {
145+
'trits': trits,
146+
'offset': offset,
147+
'length': length,
148+
})
149+
137150
# Ensure that ``trits`` can hold at least one hash worth of trits.
138-
trits.extend([0] * max(0, HASH_LENGTH - len(trits)))
151+
trits.extend([0] * max(0, length - len(trits)))
139152

140153
# Check trits with offset can handle hash length
141154
if len(trits) - offset < HASH_LENGTH:
@@ -144,14 +157,19 @@ def squeeze(self, trits, offset=0):
144157
context = {
145158
'trits': trits,
146159
'offset': offset,
160+
'length': length
147161
},
148162
)
149163

150-
# Copy exactly one hash.
151-
trits[offset:offset + HASH_LENGTH] = self._state[0:HASH_LENGTH]
164+
while length >= HASH_LENGTH:
165+
# Copy exactly one hash.
166+
trits[offset:offset + HASH_LENGTH] = self._state[0:HASH_LENGTH]
152167

153-
# One hash worth of trits copied; now transform.
154-
self._transform()
168+
# One hash worth of trits copied; now transform.
169+
self._transform()
170+
171+
offset += HASH_LENGTH
172+
length -= HASH_LENGTH
155173

156174
def _transform(self):
157175
# type: () -> None

test/crypto/pycurl_test.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,29 @@ def test_squeeze_with_offset(self):
137137
trits_out,
138138
'TAWDGNSEAD9ZRGBBVRVEKQYYVDOKHYQ9KEIYJKFT'
139139
'BQEYZDWZVMRFJQQGTMPHBZOGPIJCCVWLZVDKLAQVI')
140+
141+
def test_squeeze_with_486_length_should_work(self):
142+
"""
143+
Test squeeze with 486 length should work as well, no one use this
144+
in real situation
145+
"""
146+
inp = (
147+
'EMIDYNHBWMBCXVDEFOFWINXTERALUKYYPPHKP9JJ'
148+
'FGJEIUY9MUDVNFZHMMWZUYUSWAIOWEVTHNWMHANBH'
149+
)
150+
151+
trits = TryteString(inp).as_trits()
152+
153+
curl = Curl()
154+
curl.absorb(trits)
155+
trits_out = []
156+
curl.squeeze(trits_out, length=486)
157+
158+
trits_out = TryteString.from_trits(trits_out)
159+
160+
self.assertEqual(
161+
trits_out,
162+
'AQBOPUMJMGVHFOXSMUAGZNACKUTISDPBSILMRAGIG'
163+
'RXXS9JJTLIKZUW9BCJWKSTFBDSBLNVEEGVGAMSSMQ'
164+
'GSJWCCFQRHWKTSMVPWWCEGOMCNWFYWDZBEDBLXIFB'
165+
'HOTCKUMCANLSXXTNKSYNBMOSDDEYFTDOYIKDRJM')

0 commit comments

Comments
 (0)