示例#1
0
 def serialize_pushdata(self, instr):
     if (OP_PUSHDATA1_75_MIN <= instr.opcode <= OP_PUSHDATA1_75_MAX):
         return (chr(instr.opcode) + instr.data)
     if instr.opcode == OP_PUSHDATA1: 
         return (chr(instr.opcode) + base256encode(len(instr.data), pad=1) + instr.data)
     if instr.opcode == OP_PUSHDATA2:
         return (chr(instr.opcode) + base256encode(len(instr.data), pad=2)[::-1] + instr.data)
     if instr.opcode == OP_PUSHDATA4:
         return (chr(instr.opcode) + base256encode(len(instr.data), pad=4)[::-1] + instr.data)
     raise Exception("Unknown PUSHDATA opcode")
示例#2
0
 def set_privkey_bignum(self, priv_key, compressed=True):
     b256privkey = base256encode(priv_key)
     bn = ssl.BN_bin2bn(b256privkey, 32, 0)
     if bn == 0:
         raise Exception("set_secret: BN_bin2bn failed")
     EC_KEY_regenerate_key(self.k, bn)
     if compressed:
         self.set_compressed()
示例#3
0
文件: ecdsa.py 项目: sirk390/coinpy
 def set_privkey_bignum(self, priv_key, compressed=True):
     b256privkey = base256encode(priv_key)
     bn = ssl.BN_bin2bn(b256privkey, 32, 0)
     if bn == 0:
         raise Exception("set_secret: BN_bin2bn failed")
     EC_KEY_regenerate_key(self.k, bn)
     if compressed:
         self.set_compressed()
    def test_shamir_share_private_key(self):
        ssl_add_system_seeds()
        k = KEY()
        k.generate()
        pkey_bignum = k.get_privkey_bignum()
        pubkey = k.get_pubkey()
        numshares = 600
        threshold = 100
        sharenum_bytes = 2
        print "private_key_bignum:", pkey_bignum
        print "public_key:", hexstr(pubkey)
        print "address:", BitcoinAddress.from_publickey(pubkey, MAIN)
        
        field = ZpField()
        V = field.value_type
        ZpPkey = V(pkey_bignum)

        sharer = SecretSharer(field, ZpRandom(field))
        shares = sharer.share(ZpPkey, threshold, [V(i+1) for i in range(numshares)])
        # print shares
        print "Shamir Shares: (%d/%d):" % (threshold, numshares)
        shares_hex = [hexstr(base256encode(int(pt), sharenum_bytes) + base256encode(int(value), 32)) for pt, value in shares]
        
        for share in shares_hex:
            print share
        # Try to reconstruct the private key using the hex encoded shares.
        recombiner = SecretRecombiner(field)
        for i in range(10):
            random4_hex = random.sample(shares_hex, threshold)
            random4_decoded = [decodehexstr(h) for h in random4_hex]
            random4 = [(V(base256decode(data[:sharenum_bytes])), V(base256decode(data[sharenum_bytes:]))) for data in random4_decoded]
            recombined_pkey_bignum = recombiner.recombine(random4, V(0))
            assert recombined_pkey_bignum == ZpPkey
            k2 = KEY()
            k2.set_privkey_bignum(int(recombined_pkey_bignum))
            assert k2.get_pubkey() == pubkey
            print i
        # With threshold-1 shares this fails
        for i in range(10):
            random4_hex = random.sample(shares_hex, threshold-1)
            random4_decoded = [decodehexstr(h) for h in random4_hex]
            random4 = [(V(base256decode(data[:sharenum_bytes])), V(base256decode(data[sharenum_bytes:]))) for data in random4_decoded]
            recombined_pkey_bignum = recombiner.recombine(random4, V(0))
            assert recombined_pkey_bignum != ZpPkey
示例#5
0
def decode_base58check(data, preserve_leading_zeros=True):
    raw = preserve_leading_zeros and (count_leading_base58_zeros(data) * "\0") or ""
    raw += base256encode(base58decode(data))
    if len(raw) < 4:
        raise Exception("base58check: format error")
    content, check = raw[:-4], raw[-4:]
    digest2 = doublesha256(content)
    if digest2[:4] != check:
        raise Exception("base58check: checksum error %s != %s" % (hexstr(digest2[:4]), hexstr(check)))
    return content
示例#6
0
def decode_base58check(data, preserve_leading_zeros=True):
    raw = preserve_leading_zeros and (count_leading_base58_zeros(data) *
                                      "\0") or ""
    raw += base256encode(base58decode(data))
    if len(raw) < 4:
        raise Exception("base58check: format error")
    content, check = raw[:-4], raw[-4:]
    digest2 = doublesha256(content)
    if (digest2[:4] != check):
        raise Exception("base58check: checksum error %s != %s" %
                        (hexstr(digest2[:4]), hexstr(check)))
    return (content)
示例#7
0
 def from_bignum(value):
     return (Uint256(base256encode(value, 32)[::-1]))