def test_verify_election_partial_key_backup(self): # Arrange recipient_auxiliary_key_pair = generate_rsa_auxiliary_key_pair( RECIPIENT_GUARDIAN_ID, RECIPIENT_SEQUENCE_ORDER) sender_election_key_pair = generate_election_key_pair( SENDER_GUARDIAN_ID, SENDER_SEQUENCE_ORDER, QUORUM) recipient_auxiliary_public_key = AuxiliaryPublicKey( RECIPIENT_GUARDIAN_ID, RECIPIENT_SEQUENCE_ORDER, recipient_auxiliary_key_pair.public_key, ) partial_key_backup = generate_election_partial_key_backup( SENDER_GUARDIAN_ID, sender_election_key_pair.polynomial, recipient_auxiliary_public_key, identity_auxiliary_encrypt, ) # Act verification = verify_election_partial_key_backup( RECIPIENT_GUARDIAN_ID, partial_key_backup, sender_election_key_pair.share(), recipient_auxiliary_key_pair, identity_auxiliary_decrypt, ) # Assert self.assertIsNotNone(verification) self.assertEqual(verification.owner_id, SENDER_GUARDIAN_ID) self.assertEqual(verification.designated_id, RECIPIENT_GUARDIAN_ID) self.assertEqual(verification.verifier_id, RECIPIENT_GUARDIAN_ID) self.assertTrue(verification.verified)
def test_generate_election_partial_key_challenge(self): # Arrange recipient_auxiliary_key_pair = generate_rsa_auxiliary_key_pair( RECIPIENT_GUARDIAN_ID, RECIPIENT_SEQUENCE_ORDER) sender_election_key_pair = generate_election_key_pair( SENDER_GUARDIAN_ID, SENDER_SEQUENCE_ORDER, QUORUM) recipient_auxiliary_public_key = AuxiliaryPublicKey( RECIPIENT_GUARDIAN_ID, RECIPIENT_SEQUENCE_ORDER, recipient_auxiliary_key_pair.public_key, ) partial_key_backup = generate_election_partial_key_backup( SENDER_GUARDIAN_ID, sender_election_key_pair.polynomial, recipient_auxiliary_public_key, identity_auxiliary_encrypt, ) # Act challenge = generate_election_partial_key_challenge( partial_key_backup, sender_election_key_pair.polynomial) # Assert self.assertIsNotNone(challenge) self.assertEqual(challenge.designated_id, RECIPIENT_GUARDIAN_ID) self.assertEqual(challenge.designated_sequence_order, RECIPIENT_SEQUENCE_ORDER) self.assertIsNotNone(challenge.value) self.assertEqual(len(challenge.coefficient_commitments), QUORUM) self.assertEqual(len(challenge.coefficient_proofs), QUORUM) for proof in challenge.coefficient_proofs: self.assertTrue(proof.is_valid())
def test_generate_election_partial_key_backup(self): # Arrange election_key_pair = generate_election_key_pair(SENDER_GUARDIAN_ID, SENDER_SEQUENCE_ORDER, QUORUM) auxiliary_key_pair = generate_rsa_auxiliary_key_pair( RECIPIENT_GUARDIAN_ID, RECIPIENT_SEQUENCE_ORDER) auxiliary_public_key = AuxiliaryPublicKey( RECIPIENT_GUARDIAN_ID, RECIPIENT_SEQUENCE_ORDER, auxiliary_key_pair.public_key, ) # Act backup = generate_election_partial_key_backup( SENDER_GUARDIAN_ID, election_key_pair.polynomial, auxiliary_public_key, identity_auxiliary_encrypt, ) # Assert self.assertIsNotNone(backup) self.assertEqual(backup.designated_id, RECIPIENT_GUARDIAN_ID) self.assertEqual(backup.designated_sequence_order, RECIPIENT_SEQUENCE_ORDER) self.assertIsNotNone(backup.encrypted_value)
def test_generate_election_partial_key_backup(self): # Arrange election_key_pair = generate_election_key_pair(QUORUM) auxiliary_key_pair = generate_rsa_auxiliary_key_pair() auxiliary_public_key = AuxiliaryPublicKey( RECIPIENT_GUARDIAN_ID, RECIPIENT_SEQUENCE_ORDER, auxiliary_key_pair.public_key, ) # Act backup = generate_election_partial_key_backup( SENDER_GUARDIAN_ID, election_key_pair.polynomial, auxiliary_public_key, identity_auxiliary_encrypt, ) # Assert self.assertIsNotNone(backup) self.assertEqual(backup.designated_id, RECIPIENT_GUARDIAN_ID) self.assertEqual(backup.designated_sequence_order, RECIPIENT_SEQUENCE_ORDER) self.assertIsNotNone(backup.encrypted_value) self.assertEqual(len(backup.coefficient_commitments), QUORUM) self.assertEqual(len(backup.coefficient_proofs), QUORUM) for proof in backup.coefficient_proofs: self.assertTrue(proof.is_valid())
def test_generate_rsa_auxiliary_key_pair(self): # Act auxiliary_key_pair = generate_rsa_auxiliary_key_pair() # Assert self.assertIsNotNone(auxiliary_key_pair) self.assertIsNotNone(auxiliary_key_pair.public_key) self.assertIsNotNone(auxiliary_key_pair.secret_key)
def test_generate_rsa_auxiliary_key_pair(self): # Act auxiliary_key_pair = generate_rsa_auxiliary_key_pair( SENDER_GUARDIAN_ID, SENDER_SEQUENCE_ORDER) # Assert self.assertIsNotNone(auxiliary_key_pair) self.assertIsNotNone(auxiliary_key_pair.public_key) self.assertIsNotNone(auxiliary_key_pair.secret_key)
def generate_auxiliary_keys() -> AuxiliaryKeyPair: """ Generate auxiliary key pair for auxiliary uses during process :return: Auxiliary key pair """ keys = generate_rsa_auxiliary_key_pair() if not keys: raise HTTPException(status_code=500, detail="Auxiliary keys failed to be generated") return AuxiliaryKeyPair(public_key=keys.public_key, secret_key=keys.secret_key)
def _guardian_generates_keys( self, guardian_id: GUARDIAN_ID, sequence_order: int ) -> None: """Guardian generates their keys""" # Create Election Key Pair election_key_pair = generate_election_key_pair( guardian_id, sequence_order, self.QUORUM ) self.assertIsNotNone(election_key_pair) self.election_key_pairs[guardian_id] = election_key_pair # Create Auxiliary Key Pair auxiliary_key_pair = generate_rsa_auxiliary_key_pair( guardian_id, sequence_order ) self.assertIsNotNone(auxiliary_key_pair) self.auxiliary_key_pairs[guardian_id] = auxiliary_key_pair
def create_guardian(request: GuardianRequest) -> Guardian: """ Create a guardian for the election process with the associated keys """ election_keys = generate_election_key_pair( request.quorum, int_to_q_unchecked(request.nonce) if request.nonce is not None else None, ) if request.auxiliary_key_pair is None: auxiliary_keys = generate_rsa_auxiliary_key_pair() else: auxiliary_keys = request.auxiliary_key_pair if not election_keys: raise HTTPException( status_code=500, detail="Election keys failed to be generated", ) if not auxiliary_keys: raise HTTPException( status_code=500, detail="Auxiliary keys failed to be generated" ) return Guardian( id=request.id, sequence_order=request.sequence_order, number_of_guardians=request.number_of_guardians, quorum=request.quorum, election_key_pair=ElectionKeyPair( public_key=str(election_keys.key_pair.public_key), secret_key=str(election_keys.key_pair.secret_key), proof=write_json_object(election_keys.proof), polynomial=write_json_object(election_keys.polynomial), ), auxiliary_key_pair=AuxiliaryKeyPair( public_key=auxiliary_keys.public_key, secret_key=auxiliary_keys.secret_key ), )