def test_raises_value_error_on_message_not_less_than_p(self): m = 3 k = 3 n = 3 p = 3 with self.assertRaises(ValueError): shamir.encode(message=m, k=k, n=n, p=p)
def test_raises_value_error_on_n_greater_or_equal_p(self): m = 6 k = 4 p = 7 with self.assertRaises(ValueError): shamir.encode(message=m, k=k, n=p, p=p) with self.assertRaises(ValueError): shamir.encode(message=m, k=k, n=p + 1, p=p)
def test_decoded_equals_original_message(self): message = 10 k = 10 n = 11 p = 13 shares = shamir.encode(message=message, k=k, n=n, p=p) decoded = shamir.decode(shares=shares, k=k, p=p) self.assertEqual(message, decoded)
def test_encoding_large_strings(self): k = 3 n = 5 p = 2 ** 521 - 1 # 13th Mersenne prime string = 'This is a pretty long secret message. Big field order is needed.' integer = shamir.str_to_int(string) shares = shamir.encode(integer, k=k, n=n, p=p) recovered_integer = shamir.decode(shares, k=k, p=p) recovered_string = shamir.int_to_str(recovered_integer) self.assertEqual(string, recovered_string)
def test_decoded_str_equals_original(self): k = 3 n = 5 p = 2 ** 61 - 1 # 9th Mersenne prime string = 'message' integer = shamir.str_to_int(string) shares = shamir.encode(integer, k=k, n=n, p=p) recovered_integer = shamir.decode(shares, k=k, p=p) recovered_string = shamir.int_to_str(recovered_integer) self.assertEqual(string, recovered_string)
def test_different_permutations_of_shares_give_the_same_result(self): message = 10 k = 3 n = 4 p = 13 shares = shamir.encode(message=message, k=k, n=n, p=p) for permutation in permutations(shares): decoded = shamir.decode(shares=permutation, k=k, p=p) self.assertEqual(message, decoded)
def calc_shares(self): try: secret = self.edit_secret.text() n = int(self.edit_n.text()) k = int(self.edit_k.text()) if k > n: error_message = QErrorMessage(self) error_message.showMessage('minimalna liczba fragmentów nie może być większa niż ' 'całkowita liczba fragmentów') return int_secret = shamir.str_to_int(secret) if int_secret >= self.field_order: error_message = QErrorMessage(self) error_message.showMessage('podany sekret jest zbyt długi') return shares = shamir.encode(int_secret, k=k, n=n, p=self.field_order) except ValueError: print('wrong values') return shares_string = self.shares_to_string(shares) self.edit_shares.setText(shares_string)
def test_raises_value_error_on_k_greater_than_n(self): m = 2 n = 5 p = 7 with self.assertRaises(ValueError): shamir.encode(message=m, k=n + 1, n=n, p=p)