from playcrypt.primitives import * from playcrypt.tools import * from playcrypt.ideal.function_family import * from playcrypt.games.game_lr import GameLR from playcrypt.games.game_privk_cpa import * from playcrypt.simulator.privk_cpa_sim import * from playcrypt.simulator.world_sim import WorldSim from playcrypt.ideal.block_cipher import BlockCipher def A(challenge, encQuery): r""" The adversary to the CPA Challenger. :param challenge: a function pointer that allows the adverseary to make a query with a pair of plaintexts to be challenged on. It returns a challenge ciphertext, and the adversary has to guess which of the plaintexts was encrypted in the challenge. : param enc: a function pointer that allows the adversary to make encryption queries. :return: 0 if the first of the pair was encrypted, return 1 if the 2nd of the pair was encrypted. """ c = challenge('0', '1') y = encQuery('0') if y == c : return 0 else: return 1 # DO NOT EDIT THIS FUNCTION # def encrypt(k, m): """ This uses a secure block cipher (PRF) b, and key k to encrypt message m """ return b.encode(k, m) # Use this block for any printing/testing, do not leave stray print # statements outside this block. if __name__ == "__main__": keyLen = 128 b = BlockCipher(keyLen, 1) g = GamePrivKCPA(1, encrypt, keyLen) s = PrivKCPASim(g, A) print(s.compute_success_ratio(0,1024)) print(s.compute_success_ratio(1,1024)) print(str(s.compute_advantage(1024)))