Ejemplo n.º 1
0
def pad_and_checksum(s, compact, checksum):
    """Apply length padding and checksum to a string"""
    assert isinstance(s, bytes)
    if checksum:
        k = Keccak()
        k.absorb(s)
        checksum_length = max(1, (len(s)-1).bit_length())
        checksum = k.squeeze(checksum_length)

        length = chr(checksum_length) if compact else encode_varint(len(s), endian='little')
        return s + checksum + length
    else:
        length = '\x01' if compact else encode_varint(len(s), endian='little')
        return s + length
Ejemplo n.º 2
0
def pad_and_checksum(s, compact, checksum):
    """Apply length padding and checksum to a string"""
    assert isinstance(s, bytes)
    if checksum:
        k = Keccak()
        k.absorb(s)
        checksum_length = max(1, (len(s) - 1).bit_length())
        checksum = k.squeeze(checksum_length)

        length = chr(checksum_length) if compact else encode_varint(
            len(s), endian='little')
        return s + checksum + length
    else:
        length = '\x01' if compact else encode_varint(len(s), endian='little')
        return s + length
Ejemplo n.º 3
0
def encode(s, compact=False):
    """From a byte string, produce a list of words that durably encodes the string.

    s: the byte string to be encoded
    compact: instead of using the length encoding scheme, pad by prepending a 1 bit

    The words in the encoding dictionary were chosen to be common and unambiguous.
    The encoding also includes a checksum. The encoding is constructed so that
    common errors are extremely unlikely to produce a valid encoding.
    """
    if not isinstance(s, bytes):
        raise TypeError("mnemonic.encode can only encode byte strings")

    k = Keccak()
    k.absorb(s)
    checksum_length = max(1, (len(s)-1).bit_length())
    checksum = k.squeeze(checksum_length)

    length = chr(checksum_length) if compact else encode_varint(len(s), endian='little')

    s += checksum
    s += length

    word_index = 0
    i = bytes2int(s)
    retval = [None] * int(floor(log(i, len(words)) + 1))
    for j in xrange(len(retval)):
        assert i > 0
        word_index += i % len(words)
        word_index %= len(words)
        retval[j] = words[word_index]
        i //= len(words)
    assert i == 0
    return tuple(retval)
Ejemplo n.º 4
0
def encode(s, compact=False):
    """From a byte string, produce a list of words that durably encodes the string.

    s: the byte string to be encoded
    compact: instead of using the length encoding scheme, pad by prepending a 1 bit

    The words in the encoding dictionary were chosen to be common and unambiguous.
    The encoding also includes a checksum. The encoding is constructed so that
    common errors are extremely unlikely to produce a valid encoding.
    """
    if not isinstance(s, bytes):
        raise TypeError("mnemonic.encode can only encode byte strings")

    k = Keccak()
    k.absorb(s)
    checksum_length = max(1, (len(s) - 1).bit_length())
    checksum = k.squeeze(checksum_length)

    length = chr(checksum_length) if compact else encode_varint(
        len(s), endian='little')

    s += checksum
    s += length

    word_index = 0
    i = bytes2int(s)
    retval = [None] * int(floor(log(i, len(words)) + 1))
    for j in xrange(len(retval)):
        assert i > 0
        word_index += i % len(words)
        word_index %= len(words)
        retval[j] = words[word_index]
        i //= len(words)
    assert i == 0
    return tuple(retval)
Ejemplo n.º 5
0
    def from_seed_index(cls, seed, depth, index):
        if not isinstance(seed, bytes):
            raise TypeError('seed must be a bytes')
        if not isinstance(depth, Integral):
            raise TypeError('depth must be an Integral')
        if not isinstance(index, Integral):
            raise TypeError('index must be an Integral')

        k = keccak.Keccak(**_keccak_args)
        seed = k.pad10star1(seed, k.r)
        k.update(seed)
        depth = encode_varint(depth,endian='big')
        depth = k.pad10star1(depth, k.r)
        k.update(depth)
        index = encode_varint(index,endian='big')
        # k will automatically pad index upon squeezing
        k.update(index)
        raw_key_material = k.squeeze(key_size * num_subkey_pairs * 2)
        subkey_type = globals()[cls.subkey_type]
        temp = ( subkey_type(''.join(subkey))
                 for subkey in _grouper(raw_key_material,
                                        key_size) )
        temp = tuple(temp)
        return cls(temp)
Ejemplo n.º 6
0
def string_to_element(s):
    if not isinstance(s, bytes):
        s = int2bytes(s, endian='little')
    if len(s) > 30:
        raise ValueError("Argument too large to fit into an element")

    encoded_length = encode_varint(len(s), endian='little')
    null_padding = '\x00' * (31 - len(encoded_length) - len(s))
    s += null_padding
    s += encoded_length
    s = bytes2int(s, endian='little')

    if s >= p:
        raise ValueError("Argument too large to fit into an element")
    s = Element(s)
    return s
Ejemplo n.º 7
0
 def serialize(self):
     retval = ''.join(imap(methodcaller('serialize'), self))
     return self.tag_byte+encode_varint(len(self),endian='big')+retval
Ejemplo n.º 8
0
 def serialize(self):
     return self.tag_byte+encode_varint(len(self),endian='big')+self