def test_random_distinct():
    prime = 11
    num = 7
    random_values = random.get_distinct_positive_random_ints_in_field(num, prime)
    assert len(random_values) == num and \
        len([value for value in random_values if value < prime]) == num and \
        len(random_values) == len(set(random_values))  # check distinct
def test_random_small_multiple():
    prime = 2**3 - 1
    num = 5
    random_values = random.get_distinct_positive_random_ints_in_field(num, prime)
    assert len(random_values) == num and \
        len([value for value in random_values if value < prime]) == 5 and \
        len(random_values) == len(set(random_values))  # check distinct
def test_random_small_single():
    prime = 2**3 - 1
    random_values = random.get_distinct_positive_random_ints_in_field(1, prime)
    assert len(random_values) == 1 and \
        random_values[0] < prime and\
        random_values[0].bit_length > 0 and \
        len(random_values) == len(set(random_values))  # check distinct
Example #4
0
def _share_secret_int(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, an integer to be Shamir secret shared
    Returns:
        a list of tuples of (x, f(x)) values
    Raises:
        ValueError, the input parameters are invalid
    '''
    bitlength = max(num_players.bit_length(), max_secret_length * 8)
    prime = primes.get_prime_by_bitlength(bitlength)

    if not _verify_parameters(num_players, reconstruction_threshold, secret, prime):
        raise ValueError("invalid secret sharing parameters")

    # fix n distinct points, alpha_1,...,alpha_n in Z_ps  (public)
    alphas = [i for i in xrange(1, num_players + 1)]

    # choose at random t points, a_1,...,a_t in Z_ps (private)
    #   we will use the a_i values as our coefficients to define the polynomial f(x) = (a_t x^t) + ... + (a_1 x) + s
    coefficients = [secret] + random.get_distinct_positive_random_ints_in_field(reconstruction_threshold - 1, prime)

    # for values of i from 1 to n, calculate f(alpha_i)
    return polynomials.evaluate(coefficients, alphas, prime)
def test_random_positive_distinct():
    prime = 3
    num = 2
    for i in range(1000):
        random_values = random.get_distinct_positive_random_ints_in_field(num, prime)
        assert 0 not in random_values and \
            len(random_values) == num and \
            len([value for value in random_values if value < prime]) == num and \
            len(random_values) == len(set(random_values))  # check distinct
def get_ids(num_players):
    '''
    Args:
        num_players, the number of players to generate ids for
    Returns:
        a list of random, unique ids
    '''
    ids = []
    for r in random.get_distinct_positive_random_ints_in_field(num_players, PRIME):
        ids.append(str(r))
    return ids
def test_random_too_many():
    prime = 7
    num = 10  # test the case where there are too many requested integers for the prime selected
    with pytest.raises(ValueError):
        random.get_distinct_positive_random_ints_in_field(num, prime)
def test_random_size_of_field():
    prime = 31
    num = prime
    with pytest.raises(ValueError):
        random.get_distinct_positive_random_ints_in_field(num, prime)
def test_random_empty():
    prime = 71
    random_values = random.get_distinct_positive_random_ints_in_field(0, prime)
    assert random_values == []