def test_secretshare_combine(self): shamir = SecretShare(threshold=2, share_count=3) # len(shares) < threshold shamir.shares = [] self.assertRaises(ValueError, shamir.combine) # len(shares) > share_count shamir.shares = [Share() for _ in range(4)] self.assertRaises(ValueError, shamir.combine)
def test_secretshare_split(self): shamir = SecretShare() # share_count > max max_count = Primes.max_bits() - 1 shamir.share_count = max_count + 1 self.assertRaises(ValueError, shamir.split) # threshold > share_count shamir.threshold = 3 shamir.share_count = 2 self.assertRaises(ValueError, shamir.split)
def test_secretshare_split(self): secret = Secret(1633902946) shamir = SecretShare(2, 3, secret=secret) shares = shamir.split() self.assertEqual(shares, shamir.shares) self.assertEqual(len(shares), 3) point = 1 for share in shares: self.assertIsInstance(share, Share) self.assertEqual(share.point, point) point += 1
def test_secretshare_init(self): secret = Secret(1633902946) threshold = 2 share_count = 3 shares = Share(1), Share(2) shamir = SecretShare(threshold, share_count, secret=secret, shares=shares) self.assertEqual(shamir.threshold, threshold) self.assertEqual(shamir.share_count, share_count) self.assertEqual(shamir.secret, secret) self.assertEqual(shamir.shares, list(shares))
def test_secretshare_setters_getters(self): shamir = SecretShare() for wrongtype in WRONGTYPES_INT: with self.assertRaises(TypeError): shamir.threshold = wrongtype with self.assertRaises(TypeError): shamir.share_count = wrongtype for wrongtype in WRONGTYPES: with self.assertRaises(TypeError): shamir.secret = wrongtype with self.assertRaises(TypeError): shamir.shares = wrongtype with self.assertRaises(ValueError): shamir.threshold = 0 with self.assertRaises(ValueError): shamir.share_count = 1
def test_secretshare_split_combine_all_primes_sizes(self): for bits in Primes._PRIMES.keys(): if bits == 512: continue secret = Secret() secret.random(bits) shamir = SecretShare(3, 5, secret=secret) shamir.split() shamir.shares = shamir.shares[1:4] secret_combined = shamir.combine() self.assertEqual(secret_combined.value, secret.value, f'Secrets differ for {bits} bits')
def test_secretshare_setters_getters(self): shamir = SecretShare() shamir.threshold = 5 self.assertEqual(shamir.threshold, 5) shamir.share_count = 7 self.assertEqual(shamir.share_count, 7) shamir.secret = Secret(1633902946) self.assertEqual(shamir.secret.value, 1633902946) s1 = Share(1, 5) s2 = Share(2, 5) shamir.shares = s1, s2, self.assertEqual(shamir.shares[0], s1) self.assertEqual(shamir.shares[1], s2) self.assertEqual(shamir.max_share_count, 30)
def test_secretshare_combine(self): shares_int = ( (1, 50250691263452338915556183402696678272), (2, 100501382526904677831112366803759453598), (3, 150752073790357016746668550204822228924), ) secret_expected = Secret(1633902946) shamir = SecretShare(2, 3) shamir.shares = [] for share_int in shares_int[:2]: share = Share(*share_int) shamir.shares.append(share) secret = shamir.combine() self.assertEqual(shamir.secret, secret) self.assertEqual(secret.value, secret_expected.value) shamir.shares = [] for share_int in shares_int[1:]: share = Share(*share_int) shamir.shares.append(share) shamir.combine() self.assertEqual(shamir.secret.value, secret_expected.value)
def test_secretshare_split_combine(self): secret_int = 141674243754083726050570831578464295953 shares_int = ( (1, 42125844484391047748301228917955063925), (2, 305662287504277561771218479172038047953), (3, 251718838971866341192573367477176825023), (4, 220577865808095849475740501265139606642), (5, 212239368012966086620719880535926392810), (6, 226703345586477052627511505289537183527), ) threshold, share_count = 3, 6 shamir = SecretShare(threshold, share_count) for index in (0, 2, 4): share = Share(*shares_int[index]) shamir.shares.append(share) secret_recovered = shamir.combine() self.assertEqual(int(secret_recovered), secret_int) shamir = SecretShare(threshold, share_count) for index in (1, 3, 5): share = Share(*shares_int[index]) shamir.shares.append(share) secret_recovered = shamir.combine() self.assertEqual(int(secret_recovered), secret_int)