예제 #1
0
파일: pykerl_test.py 프로젝트: lzpap/pyota
    def test_all_bytes(self):
        for i in range(-128, 128):
            in_bytes = [i] * 48
            trits = convertToTrits(in_bytes)
            out_bytes = convertToBytes(trits)

            self.assertEqual(in_bytes, out_bytes)
예제 #2
0
파일: pykerl_test.py 프로젝트: lzpap/pyota
    def test_random_trits(self):
        in_trits = [randrange(-1, 2) for _ in range(243)]
        in_trits[242] = 0
        in_bytes = convertToBytes(in_trits)
        out_trits = convertToTrits(in_bytes)

        self.assertEqual(in_trits, out_trits)
예제 #3
0
    def test_random_trits(self):
        in_trits = [randrange(-1,2) for _ in range(243)]
        in_trits[242] = 0
        in_bytes = convertToBytes(in_trits)
        out_trits = convertToTrits(in_bytes)

        self.assertEqual(in_trits, out_trits)
예제 #4
0
    def test_all_bytes(self):
        for i in range(-128, 128):
            in_bytes = [i] * 48
            trits = convertToTrits(in_bytes)
            out_bytes = convertToBytes(trits)

            self.assertEqual(in_bytes, out_bytes)
예제 #5
0
파일: pykerl.py 프로젝트: watertim/iota.py
    def absorb(self,
               trits: MutableSequence[int],
               offset: int = 0,
               length: Optional[int] = None) -> None:
        """
        Absorb trits into the sponge from a buffer.

        :param trits:
            Buffer that contains the trits to absorb.

        :param offset:
            Starting offset in ``trits``.

        :param length:
            Number of trits to absorb.  Defaults to ``len(trits)``.
        """
        # Pad input if necessary, so that it can be divided evenly into
        # hashes.
        # Note that this operation creates a COPY of ``trits``; the
        # incoming buffer is not modified!
        pad = ((len(trits) % TRIT_HASH_LENGTH) or TRIT_HASH_LENGTH)
        trits += [0] * (TRIT_HASH_LENGTH - pad)

        if length is None:
            length = len(trits)

        if length < 1:
            raise with_context(
                exc=ValueError('Invalid length passed to ``absorb``.'),
                context={
                    'trits': trits,
                    'offset': offset,
                    'length': length,
                },
            )

        while offset < length:
            stop = min(offset + TRIT_HASH_LENGTH, length)

            # If we're copying over a full chunk, zero last trit.
            if stop - offset == TRIT_HASH_LENGTH:
                trits[stop - 1] = 0

            signed_nums = conv.convertToBytes(trits[offset:stop])

            # Convert signed bytes into their equivalent unsigned
            # representation, in order to use Python's built-in bytes
            # type.
            unsigned_bytes = bytearray(
                conv.convert_sign(b) for b in signed_nums)

            self.k.update(unsigned_bytes)

            offset += TRIT_HASH_LENGTH
예제 #6
0
  def absorb(self, trits, offset=0, length=None):
    # type: (MutableSequence[int], int, Optional[int]) -> None
    """
    Absorb trits into the sponge from a buffer.

    :param trits:
      Buffer that contains the trits to absorb.

    :param offset:
      Starting offset in ``trits``.

    :param length:
      Number of trits to absorb.  Defaults to ``len(trits)``.
    """
    # Pad input if necessary, so that it can be divided evenly into
    # hashes.
    # Note that this operation creates a COPY of ``trits``; the
    # incoming buffer is not modified!
    pad = ((len(trits) % TRIT_HASH_LENGTH) or TRIT_HASH_LENGTH)
    trits += [0] * (TRIT_HASH_LENGTH - pad)

    if length is None:
      length = len(trits)

    if length < 1:
      raise with_context(
        exc = ValueError('Invalid length passed to ``absorb``.'),

        context = {
          'trits': trits,
          'offset': offset,
          'length': length,
        },
      )

    while offset < length:
      stop = min(offset + TRIT_HASH_LENGTH, length)

      # If we're copying over a full chunk, zero last trit
      if stop - offset == TRIT_HASH_LENGTH:
        trits[stop - 1] = 0

      signed_nums = conv.convertToBytes(trits[offset:stop])

      # Convert signed bytes into their equivalent unsigned representation
      # In order to use Python's built-in bytes type
      unsigned_bytes = bytearray(conv.convert_sign(b) for b in signed_nums)

      self.k.update(unsigned_bytes)

      offset += TRIT_HASH_LENGTH