diff --git a/pybgl/functions/shamir.py b/pybgl/functions/shamir.py index b61c7a0..7da2d74 100644 --- a/pybgl/functions/shamir.py +++ b/pybgl/functions/shamir.py @@ -89,6 +89,10 @@ def _interpolation(points, x=0): def split_secret(threshold, total, secret): if not isinstance(secret, bytes): raise TypeError("Secret as byte string required") + if threshold < 2: + raise ValueError("threshold >= 2") + if total < threshold: + raise ValueError("total shares >= threshold") if threshold > 255: raise ValueError("threshold <= 255") if total > 255: diff --git a/pybgl/test/shamir.py b/pybgl/test/shamir.py index 1d31fa4..c0ebc36 100644 --- a/pybgl/test/shamir.py +++ b/pybgl/test/shamir.py @@ -58,6 +58,13 @@ def test_secrets(self): self.assertEqual(s, secret) print("Shamir secret sharing OK") + def test_split_secret_rejects_invalid_thresholds(self): + secret = b"wtw5heywrhsrhrtht" + + for threshold, total in ((0, 3), (1, 3), (4, 3), (2, 0)): + with self.assertRaises(ValueError): + shamir.split_secret(threshold, total, secret) + def test_mnemonic_secrets(self): m = entropy_to_mnemonic(generate_entropy()) k = split_mnemonic(m, 3, 5) @@ -76,4 +83,3 @@ def test_mnemonic_secrets(self): -