def test_encode_decode_large(): bytestring = ('123456789012345678901234567890123456789012345678901234567890' '123456789012345678901234567890123456789012345678901234567890' '123456789012345678901234567890123456789012345678901234567890' '123456789012345678901234567890123456789012345678901234567890' '123456789012345678901234567890123456789012345678901234567890' '123456789012345678901234567890123456789012345678901234567890' '123456789012345678901234567890123456789012345678901234567890') int_result = serialization.convert_bytestring_to_int(bytestring) assert serialization.convert_int_to_bytestring(int_result) == bytestring
def get_random_int_in_field(prime): ''' Args: prime, specifies the upper bound (exclusive) for the random values Returns: a cryptographically-secure random integer within the specified field Raises: ValueError, OS does not provide a source of entropy ''' try: bytelength = _get_byte_length(prime) random_int = serialization.convert_bytestring_to_int(os.urandom(bytelength)) return random_int % prime except NotImplementedError: raise ValueError("no found implementation for entropy")
def share_secret(num_players, reconstruction_threshold, max_secret_length, secret): ''' Args: num_players, the number of shares to be distributed reconstruction_threshold, the number of shares needed for reconstruction any collection of fewer shares will reveal no information about the secret max_secret_length, the maximum length of the secret represented as a bytestring (ie, len(secret)) secret, a bytestring to be Shamir secret shared Returns: a list of strings, each representing an integer, that can be passed to reconstruct_secret Raises: ValueError, the input arguments fail validation ''' secret_int = serialization.convert_bytestring_to_int(secret) points = _share_secret_int(num_players, reconstruction_threshold, max_secret_length + 1, secret_int) return [str(pairing.elegant_pair(*tup)) for tup in points]
def share_authenticated_secret(players, reconstruction_threshold, max_secret_length, secret): ''' Args: players, a list of unique string ids for all players reconstruction_threshold, the number of shares needed for reconstruction any collection of fewer shares will reveal no information about the secret max_secret_length, the maximum length of the secret represented as a bytestring (ie, len(secret)) secret, a bytestring to be Shamir secret shared Returns: a dictionary of ids (from the players argument) to robust secret shares, which consist of a share a map of player ids to keys for the shares held by those players a map of player ids to vectors for this share that can be verified by keys held by those players Raises: ValueError, the input parameters fail validation (see share_secret of schemes/sss.py) ''' num_players = len(players) secret_int = serialization.convert_bytestring_to_int(secret) # generate shares of the secret s: ((x_1, s_1), . . . , (x_n, s_n)) int_shares = [pairing.elegant_pair(*share) for share in sss._share_secret_int(num_players, reconstruction_threshold, max_secret_length + 1, # conversion to an integer adds one byte secret_int)] # assign shares to players shares_map = {player: share for (player, share) in zip(players, int_shares)} batch_keys, batch_vectors = defaultdict(dict), defaultdict(dict) for player in players: # generate n MAC keys k_ij and vectors t_ij = MAC(k_ij, s_j) per share s_j keys, vectors = authentication.generate_batch(num_players, shares_map[player], max_secret_length + 1) for player_id, key, vector in zip(players, keys, vectors): batch_keys[player][player_id] = key batch_vectors[player][player_id] = vector return _make_robust_shares(shares_map, batch_keys, batch_vectors)
def test_large_mutation(): bytestring = '\x00\x9c\x9e\x16\xe9' int_result = serialization.convert_bytestring_to_int(bytestring) + 1000000000000000000 # large relative to bytestring length serialization.convert_int_to_bytestring(int_result)
def test_encode_decode_trailing_zeros(): bytestring = 'e\x9c\x9e\x16\xe9\xea\x15+\xbf]\xebx;o\xef\xc9X1c\xaepj\xebj\x12\xe3r\xcd\xeaM\x00\x00' int_result = serialization.convert_bytestring_to_int(bytestring) assert serialization.convert_int_to_bytestring(int_result) == bytestring
def test_encode_decode_leading_star(): bytestring = '**e\x9c\x9e\x16\xe9\xea\x15+\xbf]\xebx;o\xef\xc9X1c\xaepj\xebj\x12\xe3r\xcd\xeaM' int_result = serialization.convert_bytestring_to_int(bytestring) assert serialization.convert_int_to_bytestring(int_result) == bytestring
def test_encode_decode_small(): bytestring = '123' int_result = serialization.convert_bytestring_to_int(bytestring) assert serialization.convert_int_to_bytestring(int_result) == bytestring