@@ -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
0 commit comments