Example #1
0
 def test_decryption_invalid_key(self) -> None:
     """
     Tests that ONLY the owner of the receiver key can decrypt the ciphertext.
     """
     sk_1 = encryption.decode_encryption_secret_key(_TEST_SECRET_KEY_1_BYTES)
     sk = encryption.decode_encryption_secret_key(_TEST_SECRET_KEY_BYTES)
     pk = encryption.get_encryption_public_key(sk)
     ciphertext = encryption.encrypt(_TEST_PLAINTEXT, pk)
     with self.assertRaises(encryption.InvalidSignature):
         encryption.decrypt(ciphertext, sk_1)
Example #2
0
 def test_encrypt_decrypt(self) -> None:
     """
     Tests the correct encrypt-decrypt flow: decrypt(encrypt(m)) == m
     where m is encoded on NOTE_LENGTH/BYTE_LEN bytes.
     """
     sk = encryption.decode_encryption_secret_key(_TEST_SECRET_KEY_BYTES)
     pk = encryption.get_encryption_public_key(sk)
     ciphertext = encryption.encrypt(_TEST_PLAINTEXT, pk)
     plaintext = encryption.decrypt(ciphertext, sk)
     self.assertEqual(_TEST_PLAINTEXT, plaintext)
Example #3
0
def receive_note(
    out_ev: MixOutputEvents, receiver_k_sk: EncryptionSecretKey
) -> Optional[Tuple[bytes, api.ZethNote]]:
    """
    Given the receivers secret key, and the event data from a transaction
    (encrypted notes), decrypt any that are intended for the receiver. Return
    tuples `(<address-in-merkle-tree>, ZethNote)`. Callers should record the
    address-in-merkle-tree along with ZethNote information, for convenience
    when spending the notes.
    """
    try:
        plaintext = decrypt(out_ev.ciphertext, receiver_k_sk)
        return (out_ev.commitment, proto_utils.zeth_note_from_bytes(plaintext))
    except InvalidSignature:
        return None
    except ValueError:
        return None