from playcrypt.primitives import * from playcrypt.tools import * from playcrypt.ideal.function_family import * from playcrypt.games.game_prf import GamePRF from playcrypt.simulator.world_sim import WorldSim from playcrypt.ideal.block_cipher import BlockCipher keyLen = 128 bcInLen = 3 bcOutLen = 3 PSEUDORANDOM = 1 RANDOM = 0 def A(fn): r""" The adversary to the PRF Challenger. :param fn: a function pointer that allows the adverseary to make queries to the challenger. The challenger will respond to query x by: evaluating the PRF on x if the challenge bit is 1 evaluating a random function on x if the challenge bit is 0 :return: 0 for RANDOM and 1 for PSEUDORANDOM. """ print('------ new challenge -------') q1 = random_string(bcInLen) y = fn(q1) print('y: ', string_to_binary(y)) print('q1: ', string_to_binary(q1)) if y == bitwise_complement_string(q1): print('must be the PRF! output is complement of input.') return PSEUDORANDOM else : print('must be a random function! output is not the complement of the input') return RANDOM # DO NOT EDIT THIS FUNCTION # def prf(k, x, b): r""" The candidate psuedorandom function. :param k: the secret key. :param x: the input value. :param b: a secure PRF. (not used in this example) :return: the psuedorandom output: F(k,x) """ return bitwise_complement_string(x) # Use this block for any printing/testing, do not leave stray print # statements outside this block. if __name__ == "__main__": g = GamePRF(2, prf, keyLen, bcInLen, bcOutLen, None) s = WorldSim(g, A) # print(s.compute_success_ratio(PSEUDORANDOM,1024)) # print(s.compute_success_ratio(RANDOM,1024)) print(str(s.compute_advantage(3)))