示例#1
0
def gen_triples(t, n, n_triples):
    triples = [[] for _ in range(n)]
    for i in range(n_triples):
        a = randelement()
        b = randelement()
        c = a * b
        a_shares = Shamir(t, n).share_secret(a)
        b_shares = Shamir(t, n).share_secret(b)
        c_shares = Shamir(t, n).share_secret(c)
        for i in range(n):
            triples[i].append(
                TripleShare(a_shares[i], b_shares[i], c_shares[i]))
    return triples
示例#2
0
def test_secure_addition():
	t = 2
	n = 2*t+1
	shamir = Shamir(t, n)
	s1 = randelement()
	s2 = randelement()
	answer = s1+s2
	shares1 = shamir.share_secret(s1)
	shares2 = shamir.share_secret(s2)
	# Each party locally adds their share of secret 1 and 2
	final_shares = [shares1[i]+shares2[i] for i in range(n)]
	# Check
	reconstructed = shamir.reconstruct_secret(final_shares[0:t+1])
	assert answer == reconstructed, "reconstructed the wrong value"
示例#3
0
def test_reconstruction():
	t = 2
	n = 2*t+1
	shamir = Shamir(t, n)
	secret = randelement()
	shares = shamir.share_secret(secret)
	reconstructed = shamir.reconstruct_secret(shares[:t+1])
	assert secret == reconstructed, "reconstructed the wrong value"
示例#4
0
def test_secure_multiplication():
    t = 2
    n = 2 * t + 1
    n_muls = 5
    shamir = Shamir(t, n)
    xs = [randelement() for _ in range(n_muls)]
    ys = [randelement() for _ in range(n_muls)]
    answers = [xs[i] * ys[i] for i in range(n_muls)]
    x_sharings = [shamir.share_secret(xs[i]) for i in range(n_muls)]
    y_sharings = [shamir.share_secret(ys[i]) for i in range(n_muls)]
    triples = test_triple_creation(t, n, n_muls)

    ##
    ## ROUND 1
    ##

    broadcasts = []
    # Each party
    for i in range(n):
        # computes intermediate values (er_list) for shares to be multiplied
        er_list = shamir.mul_gates_round_1([x[i] for x in x_sharings],
                                           [y[i] for y in y_sharings],
                                           triples[i])
        # broadcasts the intermediate values to all other parties
        broadcasts.append(er_list)

    ##
    ## ROUND 2
    ##

    player_result_shares = []
    # Each party
    for i in range(n):
        # Collects broadcasted messages and uses these intermediate values to compute result shares
        result_shares = shamir.mul_gates_round_2([x[i] for x in x_sharings],
                                                 [y[i] for y in y_sharings],
                                                 broadcasts,
                                                 [t.c for t in triples[i]])
        player_result_shares.append(result_shares)

    # Check values
    for i in range(len(answers)):
        reconstructed = shamir.reconstruct_secret(
            [p[i] for p in player_result_shares])
        assert reconstructed == answers[i], "reconstructed the wrong value"