예제 #1
0
						BFC = (long(proof["commitment"]["B"]) * pow(factor, C, P)) % P

						if AT != BFC:
							raise VerificationException("alpha^t != B(factor)^c (mod p)")
						
						decryption_factor_combination *= factor
				
				# Check the claimed decryption
				decryption_factor_combination *= result[block]
				
				if (decryption_factor_combination % P) != (long(ballot["choices"][block]["beta"]) % P):
					print("FAIL")
					raise VerificationException("Claimed plaintext doesn't match decryption factors")
else:
	# 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']))]
trusteesIn = sys.argv[2]

with open(electionIn, 'r') as electionFile:
    election = json.load(electionFile)

with open(trusteesIn, 'r') as trusteesFile:
    trustees = json.load(trusteesFile)

    nbits = ((int(math.log(long(trustees[0]["public_key"]["p"]), 2)) - 1)
             & ~255) + 256
    cryptosystem = EGCryptoSystem.load(
        nbits, long(trustees[0]["public_key"]["p"]),
        int(trustees[0]["public_key"]
            ["g"]))  # The generator might be a long if it's big? I don't know.

setup = ThresholdEncryptionSetUp(cryptosystem, len(trustees),
                                 election["trustee_threshold"])

# Add trustee public keys
for i in xrange(0, len(trustees)):
    pk = PublicKey(cryptosystem, long(trustees[i]["public_key"]["y"]))
    setup.add_trustee_public_key(i, pk)

commitment = setup.generate_commitment()

print(
    json.dumps({
        "public_coefficients":
        [str(x) for x in commitment.public_coefficients],
        "encrypted_partial_private_keys": [
            # The partial private key is too big for one single (a, b) pair
            [{
if election["public_key"] is not None:
	nbits = ((int(math.log(long(election["public_key"]["p"]), 2)) - 1) & ~255) + 256
	cryptosystem = EGCryptoSystem.load(nbits, long(election["public_key"]["p"]), int(election["public_key"]["g"])) # The generator might be a long if it's big? I don't know.
	
	public_key = PublicKey(cryptosystem, long(election["public_key"]["y"]))
	pkf = public_key.get_fingerprint()
else:
	nbits = ((int(math.log(long(trustees[0]["public_key"]["p"]), 2)) - 1) & ~255) + 256
	cryptosystem = EGCryptoSystem.load(nbits, long(trustees[0]["public_key"]["p"]), int(trustees[0]["public_key"]["g"]))

with open(secretIn, 'r') as secretFile:
	secret_json = json.load(secretFile)
	
	secret = PrivateKey(cryptosystem, long(secret_json["x"]))

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))]
    public_key = PublicKey(cryptosystem, long(election["public_key"]["y"]))
    pkf = public_key.get_fingerprint()
else:
    nbits = ((int(math.log(long(trustees[0]["public_key"]["p"]), 2)) - 1)
             & ~255) + 256
    cryptosystem = EGCryptoSystem.load(nbits,
                                       long(trustees[0]["public_key"]["p"]),
                                       int(trustees[0]["public_key"]["g"]))

with open(secretIn, 'r') as secretFile:
    secret_json = json.load(secretFile)

    secret = PrivateKey(cryptosystem, long(secret_json["x"]))

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(
import json, math, sys

electionIn = sys.argv[1]
trusteesIn = sys.argv[2]

with open(electionIn, 'r') as electionFile:
	election = json.load(electionFile)

with open(trusteesIn, 'r') as trusteesFile:
	trustees = json.load(trusteesFile)
	
	nbits = ((int(math.log(long(trustees[0]["public_key"]["p"]), 2)) - 1) & ~255) + 256
	cryptosystem = EGCryptoSystem.load(nbits, long(trustees[0]["public_key"]["p"]), int(trustees[0]["public_key"]["g"])) # The generator might be a long if it's big? I don't know.

setup = ThresholdEncryptionSetUp(cryptosystem, len(trustees), election["trustee_threshold"])

# Add trustee public keys
for i in xrange(0, len(trustees)):
	pk = PublicKey(cryptosystem, long(trustees[i]["public_key"]["y"]))
	setup.add_trustee_public_key(i, pk)

commitment = setup.generate_commitment()

print(json.dumps({
	"public_coefficients": [str(x) for x in commitment.public_coefficients],
	"encrypted_partial_private_keys": [
		# The partial private key is too big for one single (a, b) pair
		[{"alpha": str(x.gamma[i]), "beta": str(x.delta[i])} for i in xrange(0, x.get_length())]
		for x in commitment.encrypted_partial_private_keys
	]
예제 #6
0
										AT = pow(long(ballot["choices"][block]["alpha"]), T, P)
										BFC = (long(proof["commitment"]["B"]) * pow(factor, C, P)) % P
				
										if AT != BFC:
											sc4.fail("alpha^t != B(factor)^c (mod p)")
										
										decryption_factor_combination *= factor
								
								# Check the claimed decryption
								decryption_factor_combination *= result[block]
								
								if (decryption_factor_combination % P) != (long(ballot["choices"][block]["beta"]) % P):
									sc3.fail("Claimed plaintext doesn't match decryption factors")
	else:
		# 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']))]