Пример #1
0
	# We need a ThresholdPublicKey
	tesu = ThresholdEncryptionSetUp(cryptosystem, len(trustees), trusteeThreshold)
	for trustee in xrange(0, len(trustees)):
		commitment = trustees[trustee]['commitment']
		
		def to_ciphertext(idx):
			ciphertext = Ciphertext(nbits, trustees[idx]['public_key_hash'])
			
			for i in xrange(0, len(commitment['encrypted_partial_private_keys'][idx])):
				ciphertext.append(long(commitment['encrypted_partial_private_keys'][idx][i]['alpha']), long(commitment['encrypted_partial_private_keys'][idx][i]['beta']))
			
			return ciphertext
		
		tesu.add_trustee_commitment(trustee, ThresholdEncryptionCommitment(
			cryptosystem, len(trustees), trusteeThreshold,
			[long(x) for x in commitment['public_coefficients']],
			[to_ciphertext(x) for x in xrange(0, len(commitment['encrypted_partial_private_keys']))]
		))
	tpk = tesu.generate_public_key()
	
	for q in xrange(0, numQuestions):
		ballots = mixnets[0][0][q]["answers"]
		for i in xrange(0, len(ballots)):
			print("Verifying decryptions for question " + str(q) + " ballot " + str(i))
			ballot = ballots[i]
			result = long(results[q][i])
			
			ciphertext = Ciphertext(cryptosystem.get_nbits(), tpk.get_fingerprint())
			ciphertext.append(long(ballot["choice"]["alpha"]), long(ballot["choice"]["beta"]))
			combinator = ThresholdDecryptionCombinator(tpk, ciphertext, len(trustees), trusteeThreshold)
			
        for i in xrange(
                0,
                len(trustees[trustee]["commitment"]
                    ["encrypted_partial_private_keys"][idx])):
            ciphertext.append(
                long(trustees[trustee]["commitment"]
                     ["encrypted_partial_private_keys"][idx][i]["alpha"]),
                long(trustees[trustee]["commitment"]
                     ["encrypted_partial_private_keys"][idx][i]["beta"]))

        return ciphertext

    setup.add_trustee_commitment(
        trustee,
        ThresholdEncryptionCommitment(
            cryptosystem, len(trustees), election["trustee_threshold"], [
                long(x)
                for x in trustees[trustee]["commitment"]["public_coefficients"]
            ], [to_ciphertext(idx) for idx in range(0, len(trustees))]))

threshold_key = setup.generate_private_key(number, secret)

print(
    json.dumps({
        "public_key": {
            "g": str(threshold_key.cryptosystem.get_generator()),
            "p": str(threshold_key.cryptosystem.get_prime()),
            "q": str((threshold_key.cryptosystem.get_prime() - 1) / 2),
            "y": str(threshold_key.public_key._key)
        },
        "x": str(threshold_key._key)
setup = ThresholdEncryptionSetUp(cryptosystem, len(trustees), election["trustee_threshold"])

# Add trustee commitments
for trustee in xrange(0, len(trustees)):
	def to_ciphertext(idx):
		ciphertext = Ciphertext(nbits, PublicKey(cryptosystem, long(trustees[idx]["public_key"]["y"])).get_fingerprint())
		
		for i in xrange(0, len(trustees[trustee]["commitment"]["encrypted_partial_private_keys"][idx])):
			ciphertext.append(long(trustees[trustee]["commitment"]["encrypted_partial_private_keys"][idx][i]["alpha"]), long(trustees[trustee]["commitment"]["encrypted_partial_private_keys"][idx][i]["beta"]))
		
		return ciphertext
	
	setup.add_trustee_commitment(trustee, ThresholdEncryptionCommitment(
		cryptosystem, len(trustees), election["trustee_threshold"],
		[long(x) for x in trustees[trustee]["commitment"]["public_coefficients"]],
		[to_ciphertext(idx) for idx in range(0, len(trustees))]
	))

threshold_key = setup.generate_private_key(number, secret)

print(json.dumps({
	"public_key": {
		"g": str(threshold_key.cryptosystem.get_generator()),
		"p": str(threshold_key.cryptosystem.get_prime()),
		"q": str((threshold_key.cryptosystem.get_prime() - 1) / 2),
		"y": str(threshold_key.public_key._key)
	},
	"x": str(threshold_key._key)
}))
Пример #4
0
		# We need a ThresholdPublicKey
		tesu = ThresholdEncryptionSetUp(cryptosystem, len(trustees), trusteeThreshold)
		for trustee in xrange(0, len(trustees)):
			commitment = trustees[trustee]['commitment']
			
			def to_ciphertext(idx):
				ciphertext = Ciphertext(nbits, trustees[idx]['public_key_hash'])
				
				for i in xrange(0, len(commitment['encrypted_partial_private_keys'][idx])):
					ciphertext.append(long(commitment['encrypted_partial_private_keys'][idx][i]['alpha']), long(commitment['encrypted_partial_private_keys'][idx][i]['beta']))
				
				return ciphertext
			
			tesu.add_trustee_commitment(trustee, ThresholdEncryptionCommitment(
				cryptosystem, len(trustees), trusteeThreshold,
				[long(x) for x in commitment['public_coefficients']],
				[to_ciphertext(x) for x in xrange(0, len(commitment['encrypted_partial_private_keys']))]
			))
		tpk = tesu.generate_public_key()
		
		for q in xrange(0, numQuestions):
			with statusCheck("Verifying decryptions for question " + str(q)) as sc:
				ballots = mixnets[0][0][q]["answers"]
				for i in xrange(0, len(ballots)):
					with statusCheck("Verifying decryptions for question " + str(q) + " ballot " + str(i), sc) as sc2:
						ballot = ballots[i]
						result = long(results[q][i])
						
						ciphertext = Ciphertext(cryptosystem.get_nbits(), tpk.get_fingerprint())
						ciphertext.append(long(ballot["choice"]["alpha"]), long(ballot["choice"]["beta"]))
						combinator = ThresholdDecryptionCombinator(tpk, ciphertext, len(trustees), trusteeThreshold)