# Path: pycoind/util/base58.py
# def decode_check(payload):
#     'Returns the base58 decoded value, verifying the checksum.'
#     payload = b58decode(payload, None)
#     if payload and sha256d(payload[:-4])[:4] == payload[-4:]:
#         return payload[:-4]
#     return None
# 
# def encode_check(payload):
#     'Returns the base58 encoding with a 4-byte checksum.'
# 
#     checksum = sha256d(payload)[:4]
#     return b58encode(payload + checksum)
# 
# Path: pycoind/util/hash.py
# def sha256d(data):
#     return sha256(sha256(data))
# 
# Path: pycoind/util/key.py
# def privkey_from_wif(privkey, prefix = chr(0x80)):
#     key = base58.decode_check(privkey)
#     if prefix != key[0]:
#         raise ValueError('wif private key has does not match prefix')
#     if len(key) == 33:
#         if privkey[0] != '5':
#             raise ValueError('uncompressed wif private key does not begin with 5')
#         return key[1:]
#     elif len(key) == 34:
#         if key[-1] != chr(0x01):
#             raise ValueError('compressed wif private key missing compression bit')
#         if privkey[0] not in ('L', 'K'):
#             raise ValueError('uncompressed wif private key does not begin with 5')
#         return key[1:-1]
#     raise ValueError('invalid wif private key')
# 
# def privkey_to_wif(privkey, prefix = chr(0x80)):
#     return base58.encode_check(prefix + privkey)
# 
# def publickey_to_address(publickey, version = chr(0)):
#     return pubkeyhash_to_address(hash160(publickey), version)


import base64
from .base58 import decode_check, encode_check
from .hash import sha256d
from .ecdsa import SECP256k1 as curve
from .ecdsa import ellipticcurve
from .ecdsa.util import randrange, string_to_number, number_to_string
from .key import privkey_from_wif, privkey_to_wif, publickey_to_address
#    recover the key-sets before the robber.
#
#    If two of the banks catches fire, floods or becomes bankrupt and closes,
#    Alice may visit the remaining 2 banks to recover her key.
#
# Use case - Easter Egg Hunt
#    Bob wishes to host a bitcoin Easter Egg hunt. So he creates a partial
#    key-set, requiring 12 of 250 key-sets. Printing the 250 key-sets on small
#    sheets (each with 12 QR codes) he hides the sheets around a park.
#
#    Contestants may then hunt to find them, the first to acquire 12 of the
#    sheets wins, and is able to claim the funds.

# Future consideration - Larger m range
#
# Currently, m is restricted to a maximum of 255. Future versions could use
# a different prefix (eg. 0x1002) that would maintain the 6C prefix, but would
# indicate to use 2-bytes for the required count, stealing a byte from the
# checksum, to maintain the length.
#
# Future Consideration - Compressed key-sets
#
# For the above Easter Egg Hunt example, a new format could be used to more
# efficiently encode a key-set into a QR code, since the checksum and required
# are identical for all keys within it. This could be done at the application
# level.

# Valid prefix is in the range [0x0ff7, 0x1002] for prefix 6C
# Valid prefix is in the range [0x113c, 0x1148] for prefix 6c


# Path: pycoind/util/base58.py
# def decode_check(payload):
#     'Returns the base58 decoded value, verifying the checksum.'
#     payload = b58decode(payload, None)
#     if payload and sha256d(payload[:-4])[:4] == payload[-4:]:
#         return payload[:-4]
#     return None
# 
# def encode_check(payload):
#     'Returns the base58 encoding with a 4-byte checksum.'
# 
#     checksum = sha256d(payload)[:4]
#     return b58encode(payload + checksum)
# 
# Path: pycoind/util/hash.py
# def sha256d(data):
#     return sha256(sha256(data))
# 
# Path: pycoind/util/key.py
# def privkey_from_wif(privkey, prefix = chr(0x80)):
#     key = base58.decode_check(privkey)
#     if prefix != key[0]:
#         raise ValueError('wif private key has does not match prefix')
#     if len(key) == 33:
#         if privkey[0] != '5':
#             raise ValueError('uncompressed wif private key does not begin with 5')
#         return key[1:]
#     elif len(key) == 34:
#         if key[-1] != chr(0x01):
#             raise ValueError('compressed wif private key missing compression bit')
#         if privkey[0] not in ('L', 'K'):
#             raise ValueError('uncompressed wif private key does not begin with 5')
#         return key[1:-1]
#     raise ValueError('invalid wif private key')
# 
# def privkey_to_wif(privkey, prefix = chr(0x80)):
#     return base58.encode_check(prefix + privkey)
# 
# def publickey_to_address(publickey, version = chr(0)):
#     return pubkeyhash_to_address(hash160(publickey), version)


# Path: pycoind/util/piecewise.py
import base64
from .base58 import decode_check, encode_check
from .hash import sha256d
from .ecdsa import SECP256k1 as curve
from .ecdsa import ellipticcurve
from .ecdsa.util import randrange, string_to_number, number_to_string
from .key import privkey_from_wif, privkey_to_wif, publickey_to_address

#    recover the key-sets before the robber.
#
#    If two of the banks catches fire, floods or becomes bankrupt and closes,
#    Alice may visit the remaining 2 banks to recover her key.
#
# Use case - Easter Egg Hunt
#    Bob wishes to host a bitcoin Easter Egg hunt. So he creates a partial
#    key-set, requiring 12 of 250 key-sets. Printing the 250 key-sets on small
#    sheets (each with 12 QR codes) he hides the sheets around a park.
#
#    Contestants may then hunt to find them, the first to acquire 12 of the
#    sheets wins, and is able to claim the funds.

# Future consideration - Larger m range
#
# Currently, m is restricted to a maximum of 255. Future versions could use
# a different prefix (eg. 0x1002) that would maintain the 6C prefix, but would
# indicate to use 2-bytes for the required count, stealing a byte from the
# checksum, to maintain the length.
#
# Future Consideration - Compressed key-sets
#
# For the above Easter Egg Hunt example, a new format could be used to more
# efficiently encode a key-set into a QR code, since the checksum and required
# are identical for all keys within it. This could be done at the application
# level.

# Valid prefix is in the range [0x0ff7, 0x1002] for prefix 6C
# Valid prefix is in the range [0x113c, 0x1148] for prefix 6c


def partial_split_private_key(private_key, required, total):