#NEWLINE# @ classmethod #NEWLINE# def fromElectionAndAnswer( cls , election , question_num , answer_indexes ) : #NEWLINE# #INDENT# '\n    Given an election, a question number, and a list of answers to that question\n    in the form of an array of 0-based indexes into the answer array,\n    produce an EncryptedAnswer that works.\n    ' #NEWLINE# question = election . questions [ question_num ] #NEWLINE# answers = question [ 'answers' ] #NEWLINE# pk = election . public_key #NEWLINE# choices = [ None for a in range ( len ( answers ) ) ] #NEWLINE# individual_proofs = [ None for a in range ( len ( answers ) ) ] #NEWLINE# overall_proof = None #NEWLINE# randomness = [ None for a in range ( len ( answers ) ) ] #NEWLINE# plaintexts = cls . generate_plaintexts ( pk ) #NEWLINE# num_selected_answers = 0 #NEWLINE# homomorphic_sum = 0 #NEWLINE# randomness_sum = 0 #NEWLINE# min_answers = 0 #NEWLINE# if question . has_key ( 'min' ) : #NEWLINE# #INDENT# min_answers = question [ 'min' ] #NEWLINE# #UNINDENT# max_answers = question [ 'max' ] #NEWLINE# for answer_num in range ( len ( answers ) ) : #NEWLINE# #INDENT# plaintext_index = 0 #NEWLINE# if ( answer_num in answer_indexes ) : #NEWLINE# #INDENT# plaintext_index = 1 #NEWLINE# num_selected_answers += 1 #NEWLINE# #UNINDENT# randomness [ answer_num ] = algs . Utils . random_mpz_lt ( pk . q ) #NEWLINE# choices [ answer_num ] = pk . encrypt_with_r ( plaintexts [ plaintext_index ] , randomness [ answer_num ] ) #NEWLINE# individual_proofs [ answer_num ] = choices [ answer_num ] . generate_disjunctive_encryption_proof ( plaintexts , plaintext_index , randomness [ answer_num ] , algs . EG_disjunctive_challenge_generator ) #NEWLINE# if ( max_answers != None ) : #NEWLINE# #INDENT# homomorphic_sum = ( choices [ answer_num ] * homomorphic_sum ) #NEWLINE# randomness_sum = ( ( randomness_sum + randomness [ answer_num ] ) % pk . q ) #NEWLINE# #UNINDENT# #UNINDENT# if ( num_selected_answers < min_answers ) : #NEWLINE# #INDENT# raise Exception ( ( 'Need to select at least %s answer(s)' % min_answers ) ) #NEWLINE# #UNINDENT# if ( max_answers != None ) : #NEWLINE# #INDENT# sum_plaintexts = cls . generate_plaintexts ( pk , min = min_answers , max = max_answers ) #NEWLINE# overall_proof = homomorphic_sum . generate_disjunctive_encryption_proof ( sum_plaintexts , ( num_selected_answers - min_answers ) , randomness_sum , algs . EG_disjunctive_challenge_generator ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# overall_proof = None #NEWLINE# #UNINDENT# return cls ( choices , individual_proofs , overall_proof , randomness , answer_indexes )