Esempio n. 1
0
 def test_shamir_recombine_threshold3_shares3(self):
     field = ZpField(37)
     V = field.value_type
     recombiner = SecretRecombiner(field)
     # 3 shares NOK: random result
     secret = recombiner.recombine([(V(2), V(35)), (V(4), V(34)), (V(5), V(20))], x_recomb=V(0))
     self.assertEquals(secret, V(0))
Esempio n. 2
0
 def test_shamir_recombine_threshold3_shares4(self):
     field = ZpField(37)
     V = field.value_type
     recombiner = SecretRecombiner(field)
     # 4 shares OK: secret is recovered
     secret = recombiner.recombine([(V(2), V(35)), (V(7), V(30)), (V(5), V(20)), (V(4), V(34))], x_recomb=V(0))
     self.assertEquals(secret, V(12))
Esempio n. 3
0
 def test_shamir_recombine_threshold3_shares4_polynomial(self):
     field = ZpField(37)
     V = field.value_type
     recombiner = SecretRecombiner(field)
     # 4 shares OK: secret is recovered
     polynom = recombiner.recombine_polynomial([(V(2), V(35)), (V(7), V(30)), (V(5), V(20)), (V(4), V(34))])
     self.assertEquals(polynom, Polynomial([V(33), V(21), V(4), V(12)]))
    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