|
| 1 | +# coding=utf-8 |
| 2 | +from __future__ import absolute_import, division, print_function, \ |
| 3 | + unicode_literals |
| 4 | + |
| 5 | +from sha3 import keccak_384 |
| 6 | +from six import PY2 |
| 7 | +from typing import MutableSequence, Optional |
| 8 | + |
| 9 | +from iota.crypto.kerl import conv |
| 10 | +from iota.exceptions import with_context |
| 11 | + |
| 12 | +__all__ = [ |
| 13 | + 'Kerl', |
| 14 | +] |
| 15 | + |
| 16 | +BYTE_HASH_LENGTH = 48 |
| 17 | +TRIT_HASH_LENGTH = 243 |
| 18 | + |
| 19 | +class Kerl(object): |
| 20 | + k = None # type: keccak_384 |
| 21 | + |
| 22 | + def __init__(self): |
| 23 | + self.reset() |
| 24 | + |
| 25 | + def absorb(self, trits, offset=0, length=None): |
| 26 | + # type: (MutableSequence[int], int, Optional[int]) -> None |
| 27 | + """ |
| 28 | + Absorb trits into the sponge from a buffer. |
| 29 | +
|
| 30 | + :param trits: |
| 31 | + Buffer that contains the trits to absorb. |
| 32 | +
|
| 33 | + :param offset: |
| 34 | + Starting offset in ``trits``. |
| 35 | +
|
| 36 | + :param length: |
| 37 | + Number of trits to absorb. Defaults to ``len(trits)``. |
| 38 | + """ |
| 39 | + # Pad input if necessary, so that it can be divided evenly into |
| 40 | + # hashes. |
| 41 | + # Note that this operation creates a COPY of ``trits``; the |
| 42 | + # incoming buffer is not modified! |
| 43 | + pad = ((len(trits) % TRIT_HASH_LENGTH) or TRIT_HASH_LENGTH) |
| 44 | + trits += [0] * (TRIT_HASH_LENGTH - pad) |
| 45 | + |
| 46 | + if length is None: |
| 47 | + length = len(trits) |
| 48 | + |
| 49 | + if length < 1: |
| 50 | + raise with_context( |
| 51 | + exc = ValueError('Invalid length passed to ``absorb``.'), |
| 52 | + |
| 53 | + context = { |
| 54 | + 'trits': trits, |
| 55 | + 'offset': offset, |
| 56 | + 'length': length, |
| 57 | + }, |
| 58 | + ) |
| 59 | + |
| 60 | + while offset < length: |
| 61 | + stop = min(offset + TRIT_HASH_LENGTH, length) |
| 62 | + |
| 63 | + # If we're copying over a full chunk, zero last trit |
| 64 | + if stop - offset == TRIT_HASH_LENGTH: |
| 65 | + trits[stop - 1] = 0 |
| 66 | + |
| 67 | + signed_nums = conv.convertToBytes(trits[offset:stop]) |
| 68 | + |
| 69 | + # Convert signed bytes into their equivalent unsigned representation |
| 70 | + # In order to use Python's built-in bytes type |
| 71 | + unsigned_bytes = bytearray(conv.convert_sign(b) for b in signed_nums) |
| 72 | + |
| 73 | + self.k.update(unsigned_bytes) |
| 74 | + |
| 75 | + offset += TRIT_HASH_LENGTH |
| 76 | + |
| 77 | + def squeeze(self, trits, offset=0, length=None): |
| 78 | + # type: (MutableSequence[int], int, Optional[int]) -> None |
| 79 | + """ |
| 80 | + Squeeze trits from the sponge into a buffer. |
| 81 | +
|
| 82 | + :param trits: |
| 83 | + Buffer that will hold the squeezed trits. |
| 84 | +
|
| 85 | + IMPORTANT: If ``trits`` is too small, it will be extended! |
| 86 | +
|
| 87 | + :param offset: |
| 88 | + Starting offset in ``trits``. |
| 89 | +
|
| 90 | + :param length: |
| 91 | + Number of trits to squeeze from the sponge. |
| 92 | +
|
| 93 | + If not specified, defaults to :py:data:`TRIT_HASH_LENGTH` (i.e., |
| 94 | + by default, we will try to squeeze exactly 1 hash). |
| 95 | + """ |
| 96 | + # Pad input if necessary, so that it can be divided evenly into |
| 97 | + # hashes. |
| 98 | + pad = ((len(trits) % TRIT_HASH_LENGTH) or TRIT_HASH_LENGTH) |
| 99 | + trits += [0] * (TRIT_HASH_LENGTH - pad) |
| 100 | + |
| 101 | + if length is None: |
| 102 | + # By default, we will try to squeeze one hash. |
| 103 | + # Note that this is different than ``absorb``. |
| 104 | + length = len(trits) or TRIT_HASH_LENGTH |
| 105 | + |
| 106 | + if length < 1: |
| 107 | + raise with_context( |
| 108 | + exc = ValueError('Invalid length passed to ``squeeze``.'), |
| 109 | + |
| 110 | + context = { |
| 111 | + 'trits': trits, |
| 112 | + 'offset': offset, |
| 113 | + 'length': length, |
| 114 | + }, |
| 115 | + ) |
| 116 | + |
| 117 | + while offset < length: |
| 118 | + unsigned_hash = self.k.digest() |
| 119 | + |
| 120 | + if PY2: |
| 121 | + unsigned_hash = map(ord, unsigned_hash) # type: ignore |
| 122 | + |
| 123 | + signed_hash = [conv.convert_sign(b) for b in unsigned_hash] |
| 124 | + |
| 125 | + trits_from_hash = conv.convertToTrits(signed_hash) |
| 126 | + trits_from_hash[TRIT_HASH_LENGTH - 1] = 0 |
| 127 | + |
| 128 | + stop = min(TRIT_HASH_LENGTH, length-offset) |
| 129 | + trits[offset:offset+stop] = trits_from_hash[0:stop] |
| 130 | + |
| 131 | + flipped_bytes = bytearray(conv.convert_sign(~b) for b in unsigned_hash) |
| 132 | + |
| 133 | + # Reset internal state before feeding back in |
| 134 | + self.reset() |
| 135 | + self.k.update(flipped_bytes) |
| 136 | + |
| 137 | + offset += TRIT_HASH_LENGTH |
| 138 | + |
| 139 | + def reset(self): |
| 140 | + self.k = keccak_384() |
0 commit comments