Пример #1
0
def test_sign():
	params = setup()

	# user parameters
	m = 10

	# signer 1
	(sk1, vk1) = keygen(params)
	sig1 = sign(params, sk1, m)

	# signer 2
	(sk2, vk2) = keygen(params)
	sig2 = sign(params, sk2, m)

	# affregate signatures
	sig = aggregate_sign(sig1, sig2)

	# randomize signature
	randomize(params, sig)

	# aggregate keys
	vk = aggregate_keys(vk1, vk2)

	# verify signature
	assert verify(params, vk, m, sig)
Пример #2
0
def test_threshold_mix_sign():
	q = 7 # number of messages
	hidden_m = [10] * 2 # hideen message
	clear_m = [3] * 1 # clear messages
	t, n = 2, 3
	params = setup(q)
	(priv, pub) = elgamal_keygen(params) # El Gamal keypair
	
	# generate commitment and encryption for mix signature
	(cm, c, proof_s) = prepare_mix_sign(params, clear_m, hidden_m, pub)

	# generate key
	(sk, vk, vvk) = mix_ttp_th_keygen(params, t, n, q)

	# sign
	enc_sigs = [mix_sign(params, ski, cm, c, pub, proof_s, clear_m) for ski in sk]
	(h, enc_epsilon) = zip(*enc_sigs)
	sigs = [(h[0], elgamal_dec(params, priv, enc)) for enc in enc_epsilon]

	# aggregate signatures
	sig = aggregate_th_sign(params, sigs)

	# randomize signature
	sig = randomize(params, sig)

	# generate kappa and proof of correctness
	(kappa, proof_v) = show_mix_sign(params, vvk, hidden_m)

	# verify signature
	assert mix_verify(params, vvk, kappa, sig, proof_v, clear_m)
Пример #3
0
def test_threshold_blind_sign():
	params = setup()

	# user parameters
	m = 10
	t, n = 2, 4

	# generate key
	(sk, vk, vvk) = ttp_th_keygen(params, t, n)
	(priv, pub) = elgamal_keygen(params) 

	# generate commitment and encryption for blind signature
	(cm, c, proof_s) = prepare_blind_sign(params, m, pub)

	# sign
	blind_sigs = [blind_sign(params, ski, cm, c, pub, proof_s) for ski in sk]
	(h, enc_sig) = zip(*blind_sigs)
	sigs = [(h[0], elgamal_dec(params, priv, enc_sigi)) for enc_sigi in enc_sig]

	# affregate signatures
	sig = aggregate_th_sign(params, sigs)

	# randomize signature
	sig = randomize(params, sig)

	# generate kappa and proof of correctness
	(kappa, proof_v) = show_blind_sign(params, vvk, m)

	# verify signature
	assert blind_verify(params, vvk, kappa, sig, proof_v)
Пример #4
0
def setup_clusters(k, iris_samples):
    '''
    Initialize clusters
        Randomly chooses initial centroids for clusters

    :param k: number of initial clusters
    :param iris_samples: data
    :returns: array of clusters
    '''
    init_centroids = []
    clusters = []
    for i in range(0, k):
        j = lib.randomize(len(iris_samples))
        while j in init_centroids:
            j = lib.randomize(len(iris_samples))
        init_centroids.append(j)
        new_cluster = lib.Cluster(lib.Iris.from_iris(iris_samples[j]))
        clusters.append(new_cluster)

    return clusters
Пример #5
0
def k_means(k, iterations, iris_samples):
    '''
    K-Means Algorithm

    :param k: number of clusters
    :param iterations: max iterations
    :param iris_samples: data
    :returns: ss_total score
    '''
    iris_samples, iris_labels = lib.import_data()

    clusters = setup_clusters(k, iris_samples)
    for i in range(0, iterations):
        # Reset Samples
        for cluster in clusters:
            cluster.clear_samples()

        # Cluster Samples
        for sample in iris_samples:
            closest = -1.0
            cluster_idx = 0
            for j in range(0, len(clusters)):
                cluster_dist = sample.distance(clusters[j].centroid)
                if cluster_dist < closest or closest == -1:
                    # New Closest Cluster
                    closest = cluster_dist
                    cluster_idx = j
                elif cluster_dist == closest:
                    # Randomize Ties
                    cluster_idxs = [cluster_idx, j]
                    cluster_idx = cluster_idxs[lib.randomize(2)]
            clusters[cluster_idx].add_sample(sample)

        # Update Centroids
        to_remove = []
        for cluster in clusters:
            if len(cluster.samples) == 0:
                to_remove.append(cluster)
                continue
            cluster.update_centroid()

        # Vanishing Clusters
        for cluster in to_remove:
            clusters.remove(cluster)

    total_ss_total = 0.0
    for cluster in clusters:
        total_ss_total += cluster.get_ss_total()

    return total_ss_total, clusters
Пример #6
0
def test_threshold_sign():
	params = setup()

	# user parameters
	m = 10
	t, n = 2, 4

	# generate key
	(sk, vk, vvk) = ttp_th_keygen(params, t, n)

	# sign
	sigs = [sign(params, ski, m) for ski in sk]

	# affregate signatures
	sig = aggregate_th_sign(params, sigs)

	# randomize signature
	sig = randomize(params, sig)

	# verify signature
	assert verify(params, vvk, m, sig)
Пример #7
0
def test_mix_sign():
	# user parameters
	q = 7 # number of messages
	hidden_m = [10] * 5 # hideen message
	clear_m = [3] * 2 # clear messages
	params = setup(q)
	(priv, pub) = elgamal_keygen(params) # El Gamal keypair
	
	# generate commitment and encryption for mix signature
	(cm, c, proof_s) = prepare_mix_sign(params, clear_m, hidden_m, pub)

	# signer 1
	(sk1, vk1) = mix_keygen(params, q)
	mix_sig1 = mix_sign(params, sk1, cm, c, pub, proof_s, clear_m)
	(h, enc_sig1) = mix_sig1
	sig1 = (h, elgamal_dec(params, priv, enc_sig1))

	# signer 1
	(sk2, vk2) = mix_keygen(params, q)
	mix_sig2 = mix_sign(params, sk2, cm, c, pub, proof_s, clear_m)
	(h, enc_sig2) = mix_sig2
	sig2 = (h, elgamal_dec(params, priv, enc_sig2))
	
	# aggregate signatures
	sig = aggregate_sign(sig1, sig2)

	# randomize signature
	sig = randomize(params, sig)

	# aggregate keys
	vk = mix_aggregate_keys([vk1, vk2])
	
	# generate kappa and proof of correctness
	(kappa, proof_v) = show_mix_sign(params, vk, hidden_m)

	# verify signature
	assert mix_verify(params, vk, kappa, sig, proof_v, clear_m)
Пример #8
0
def test_blind_sign():
	params = setup()

	# user parameters
	m = 5 # message
	(priv, pub) = elgamal_keygen(params) # El Gamal keypair
	
	# generate commitment and encryption for blind signature
	(cm, c, proof_s) = prepare_blind_sign(params, m, pub)

	# signer 1
	(sk1, vk1) = keygen(params)
	blind_sig1 = blind_sign(params, sk1, cm, c, pub, proof_s)
	(h, enc_sig1) = blind_sig1
	sig1 = (h, elgamal_dec(params, priv, enc_sig1))

	# signer 2
	(sk2, vk2) = keygen(params)
	blind_sig2 = blind_sign(params, sk2, cm, c, pub, proof_s)
	(h, enc_sig2) = blind_sig2
	sig2 = (h, elgamal_dec(params, priv, enc_sig2))

	# aggregate signatures
	sig = aggregate_sign(sig1, sig2)

	# randomize signature
	sig = randomize(params, sig)

	# aggregate keys
	vk = aggregate_keys(vk1, vk2)

	# generate kappa and proof of correctness
	(kappa, proof_v) = show_blind_sign(params, vk, m)

	# verify signature
	assert blind_verify(params, vk, kappa, sig, proof_v)