def test_generate_keypar(self):
        """
        Creates a new ThresholdEncryptionSetUp and generate commitments for 
        all trustees add them to the  ThresholdEncryptionSetUp
        and generate a key par
        """
        cryptosystem = get_cryptosystem()
        commitments = []

        # Generate a new instance of ThresholdEncryptionSetUp
        tSetUp = ThresholdEncryptionSetUp(cryptosystem, self.num_trustees,
                                          self.threshold)
        # Adding the keys from trustees
        for i in range(self.num_trustees):
            tSetUp.add_trustee_public_key(i, self.trustees[i].public_key)

        # Generate commitmes for trustees
        for i in range(self.num_trustees):
            commitments.append(tSetUp.generate_commitment())

        # generate_key_pair must raise ThresholdEncryptionSetUpStateError
        # if called without added commitments
        self.assertRaises(ThresholdEncryptionSetUpStateError,
                          tSetUp.generate_key_pair, 0,
                          self.trustees[0].private_key)

        # Adding the first  self.num_trustees - 1 commitments from trustees
        for i in range(self.num_trustees - 1):
            tSetUp.add_trustee_commitment(i, commitments[i])

        # generate_key_pair must raise ThresholdEncryptionSetUpStateError
        # if called without all added commitments
        self.assertRaises(ThresholdEncryptionSetUpStateError,
                          tSetUp.generate_key_pair, 0,
                          self.trustees[0].private_key)

        # Add the last commitments from trustees
        tSetUp.add_trustee_commitment(self.num_trustees - 1,
                                      commitments[self.num_trustees - 1])

        # Must fail if the trustee doesn't have his corresponding private_key
        self.assertRaises(InvalidCommitmentError, tSetUp.generate_key_pair, 0,
                          self.trustees[1].private_key)

        #TODO:
        # How to test malicious commitmens or invalid ones
        keypar = tSetUp.generate_key_pair(0, self.trustees[0].private_key)

        # Test that InvalidCommitmentError is raised when given corrupt or
        # malformed commitments.
        replaced_idx = 1
        malformed_commitment = copy.deepcopy(commitments[replaced_idx])

        # We swap the first two public coefficients of the commitment, this
        # generates an inconsistency between the stored public coefficients and
        # the encrypted partial private keys.
        temp = malformed_commitment.public_coefficients[1]
        malformed_commitment.public_coefficients[1] = \
            malformed_commitment.public_coefficients[0]
        malformed_commitment.public_coefficients[0] = temp

        tSetUp.add_trustee_commitment(replaced_idx, malformed_commitment)
        self.assertRaises(InvalidCommitmentError, tSetUp.generate_key_pair, 1,
                          self.trustees[1].private_key)
 def test_generate_keypar(self):
     """
     Creates a new ThresholdEncryptionSetUp and generate commitments for 
     all trustees add them to the  ThresholdEncryptionSetUp
     and generate a key par
     """
     cryptosystem = get_cryptosystem()
     commitments = []
     
     # Generate a new instance of ThresholdEncryptionSetUp
     tSetUp = ThresholdEncryptionSetUp(cryptosystem, 
                                       self.num_trustees, self.threshold)                         
     # Adding the keys from trustees
     for i in range(self.num_trustees):
         tSetUp.add_trustee_public_key(i, self.trustees[i].public_key)
     
     # Generate commitmes for trustees
     for i in range(self.num_trustees):
         commitments.append(tSetUp.generate_commitment()) 
         
     # generate_key_pair must raise ThresholdEncryptionSetUpStateError
     # if called without added commitments
     self.assertRaises(ThresholdEncryptionSetUpStateError, 
                       tSetUp.generate_key_pair,
                       0,self.trustees[0].private_key)
                       
     # Adding the first  self.num_trustees - 1 commitments from trustees
     for i in range(self.num_trustees - 1):
        tSetUp.add_trustee_commitment(i, commitments[i])
        
     # generate_key_pair must raise ThresholdEncryptionSetUpStateError
     # if called without all added commitments
     self.assertRaises(ThresholdEncryptionSetUpStateError, 
                       tSetUp.generate_key_pair,
                       0,self.trustees[0].private_key)
                      
     # Add the last commitments from trustees 
     tSetUp.add_trustee_commitment(self.num_trustees - 1, 
                             commitments[self.num_trustees - 1])
                             
     # Must fail if the trustee doesn't have his corresponding private_key
     self.assertRaises(InvalidCommitmentError,tSetUp.generate_key_pair,
                       0,self.trustees[1].private_key)
    
     #TODO:
     # How to test malicious commitmens or invalid ones                
     keypar = tSetUp.generate_key_pair(0,self.trustees[0].private_key)
     
     # Test that InvalidCommitmentError is raised when given corrupt or 
     # malformed commitments.
     replaced_idx = 1
     malformed_commitment = copy.deepcopy(commitments[replaced_idx])
     
     # We swap the first two public coefficients of the commitment, this 
     # generates an inconsistency between the stored public coefficients and 
     # the encrypted partial private keys.
     temp = malformed_commitment.public_coefficients[1]
     malformed_commitment.public_coefficients[1] = \
         malformed_commitment.public_coefficients[0]
     malformed_commitment.public_coefficients[0] = temp
     
     tSetUp.add_trustee_commitment(replaced_idx, malformed_commitment)
     self.assertRaises(InvalidCommitmentError, tSetUp.generate_key_pair, 
                       1, self.trustees[1].private_key)