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 decode_secret(self): k = int(self.edit_k.text()) try: shares = self.string_to_shares(self.edit_shares.toPlainText()) supplied_shares = len(shares) if supplied_shares < k: error_message = QErrorMessage(self) error_message.showMessage( 'zbyt mało fragmentów<br>' 'podano: {}<br>' 'wymagane: co najmniej {}'.format(supplied_shares, k)) return secret = shamir.decode(shares, k=k, p=self.field_order) self.edit_secret.setText(shamir.int_to_str(secret)) except Exception as e: print('exception:') print(e)
def test_decode_raises_value_error_on_empty_shares(self): k = 3 p = 13 with self.assertRaises(ValueError): shamir.decode([], k=k, p=p)