Exemplo n.º 1
0
    def _decode_level(dct):

        if 'room_id' in dct:
            return room_spec.RoomSpec(dct, level_directory, state, selector)
        if 'gate_id' in dct:
            return gate.Gate(dct, level_directory, state, selector)
        return dct
Exemplo n.º 2
0
    def singles(cls, qubits, unitary=None):
        """
        Creates an instance of a single-slice circuit that applies unitary
        to the qubits

        Args:
            qubits (list): list of qubits
            unitary (nparray): unitary
        Returns:
            (Circuit): Returns a circuit that implements a set of gates on
                the qubits with the input unitary.
        """
        return Circuit([gt.Gate(qubit, unitary) for qubit in qubits])
Exemplo n.º 3
0
    def doubles(cls, qubits_c, qubits_t, unitary=None):
        """
        Creates an instance of a circuit that applies unitary to the two
        qubits.

        Args:
            qubits_c (list): list of control qubits
            qubits_t (list): list of target qubits
            unitary (nparray): unitary
        Returns:
            (Circuit): Returns a circuit that implements a set of gates with
                the unitary from the control to the target qubit.
        """
        return Circuit([
            gt.Gate([qubit_c, qubit_t], unitary)
            for qubit_c, qubit_t in zip(qubits_c, qubits_t)
        ])
Exemplo n.º 4
0
    def __init__(self, parent):
        GateMotorInterface.GateMotorInterfaceFrame.__init__(self, parent)

        # Init
        # Main window processes key events first
        self.Bind(wx.EVT_CHAR_HOOK, self.OnKeyDown)

        # Configure console
        self.m_textCtrl_console.SetFont(
            wx.Font(11, 76, 90, 90, False, "Ubuntu Mono"))
        self.m_textCtrl_consoleInput.SetFont(
            wx.Font(11, 76, 90, 90, False, "Ubuntu Mono"))

        # Gate state variables
        self.gate = g.Gate(g.GateStates.Closed)

        # Image animation variables
        self.gate_closed_position = 230
        self.gate_opened_position = 440
        self.gate_middle_position = 320
        self.gate_image_pos = self.gate_image.GetPosition()
        self.animation_timer = wx.Timer()
        self.animation_timer.Bind(wx.EVT_TIMER, self.animateGate)
        self.animation_timer.Start(10)

        # Indicator
        #self.stateTransitionLabel()

        # Change serial port text
        if isLinux():
            self.m_textCtrl_COM.Value = '/dev/ttyACM0'
        self.m_textCtrl_COM.SetFocus()

        # Micro controller
        self.uC = None

        # Auto request timer
        self.request_timer = wx.Timer()
        self.request_timer.Bind(wx.EVT_TIMER, self.autoRequest)
        self.request_timer.Start(100)
Exemplo n.º 5
0
	def setUp(self):
		self.gate = gate.Gate()
		self.gate.start()
Exemplo n.º 6
0
def garbled_circuit(input_circuit, input_value):
	global random_table
	random_table = list()
	NANDList = list()
	ANDList = list()
	NORList = list()
	ORList = list()
	XORList = list()


	for i in range(2):
		for j in range(2):
			a = gate.Gate(i,j,'AND')
			ANDList.append(a.call())
			b = gate.Gate(i,j,'OR')
			ORList.append(b.call())
			c = gate.Gate(i,j,'XOR')
			XORList.append(c.call())
			d = gate.Gate(i,j,'NAND')
			NANDList.append(d.call())
			e = gate.Gate(i,j,'NOR')
			NORList.append(e.call())

	a = [0,0,1,1]
	b = [0,1,0,1]
	nimplyList = [0,0,1,0]
	gate_table = {	'and':[a,b,ANDList],
					'or':[a,b,ORList],
					'xor':[a,b,XORList],
					'nand':[a,b,NANDList],
					'nor':[a,b,NORList],
					'nimply':[a,b,nimplyList]	}

	#input_circuit = input("please input the circuit: ")
	op_table = ['or', 'and', 'xor', 'nand', 'nor', 'nimply']

	lst = input_circuit.split(' ')
	circuit = dict()
	operator = list()
	inp = list()
	truth_table = dict()

	enc_table = dict()
	enc_t_table = dict()
	enc_to_org = dict()
	input_to_enc = dict()

	input_table = dict()
	input_table_num = list()
	count = 1
	for i in lst:
		if i == "(":
			continue
		elif i == ")":
			index = len(circuit)
			circuit[index] = dict()
			truth_table[index] = dict()
			op = operator.pop()
			circuit[index]['input2'] = inp.pop()
			circuit[index]['input1'] = inp.pop()
			circuit[index]['output'] = count
			truth_table[index]['truth_table'] = gate_table[op]
			inp.append(count)
			count += 1
		elif i in op_table:
			operator.append(i)
		else:
			inp.append(count)
			input_table[i] = count
			input_table_num.append(count)
			count += 1

	label = dict()
	for i in range(1, inp[0]+1,1):
		label[i] = [get_random(), get_random()]
		input_to_enc[i] = dict()
		input_to_enc[i][0] = label[i][0]
		input_to_enc[i][1] = label[i][1]
		enc_to_org[label[i][0]] = 0
		enc_to_org[label[i][1]] = 1


	for index in range(0, len(circuit), 1):
		g = circuit[index]
		input1 = label[g['input1']]
		input2 = label[g['input2']]
		output = label[g['output']]
		
		org_truth_table = truth_table[index]['truth_table']
		enc_t_table[index] = dict()
		lst = list()
		ans = list()
		for i in input1:
			for j in input2:

				out_org = get_out_org(enc_to_org[i], enc_to_org[j], org_truth_table)

				out_val = input_to_enc[g['output']][out_org]
				t2 = (i, j)
				lst.append([enigma.enigma([i, j, 0], out_val), hash((i, j, out_val))])
				ans.append([enigma.enigma([i, j, 0], lst[len(lst)-1][0]), out_val, out_org])

		enc_table[g['output']] = lst
		#print(lst)
		#print(ans)

	#print("input_to_enc", input_to_enc)
	#print("enc_to_org", enc_to_org)
	c_index = 0
	#input_value = input("input enter value (0 or 1): ")
	value_list = dict()
	#input_value = input_value.split(' ')
	#input_value = [int(x) for x in input_value]
	for index in range(len(circuit)):
		g = circuit[index]
		input1 = g['input1']
		input2 = g['input2']
		output = g['output']
		if input1 in input_table_num:

			value_list[input1] = input_to_enc[input1][input_value[c_index]]
			c_index += 1
		if input2 in input_table_num:
			value_list[input2] = input_to_enc[input2][input_value[c_index]]
			c_index += 1
		if c_index == len(input_value):
			break
	#print(value_list)
	for index in range(len(circuit)):
		g = circuit[index]
		index1 = value_list[g['input1']]
		index2 = value_list[g['input2']]
		for i in enc_table[g['output']]:
			dec = enigma.enigma([index1, index2, 0], i[0])
			if hash((index1, index2, dec)) == i[1]:
				value_list[g['output']] = dec
				break
	return enc_to_org[value_list[len(value_list)]]	
Exemplo n.º 7
0
def create_garbled_circuit(input_circuit):
    global random_table
    random_table = list()
    NANDList = list()
    ANDList = list()
    NORList = list()
    ORList = list()
    XORList = list()

    for i in range(2):
        for j in range(2):
            a = gate.Gate(i, j, 'AND')
            ANDList.append(a.call())
            b = gate.Gate(i, j, 'OR')
            ORList.append(b.call())
            c = gate.Gate(i, j, 'XOR')
            XORList.append(c.call())
            d = gate.Gate(i, j, 'NAND')
            NANDList.append(d.call())
            e = gate.Gate(i, j, 'NOR')
            NORList.append(e.call())

    a = [0, 0, 1, 1]
    b = [0, 1, 0, 1]
    nimplyList = [0, 0, 1, 0]
    gate_table = {
        'and': [a, b, ANDList],
        'or': [a, b, ORList],
        'xor': [a, b, XORList],
        'nand': [a, b, NANDList],
        'nor': [a, b, NORList],
        'nimply': [a, b, nimplyList]
    }

    #input_circuit = input("please input the circuit: ")
    op_table = ['or', 'and', 'xor', 'nand', 'nor', 'nimply']

    lst = input_circuit.split(' ')
    circuit = dict()
    operator = list()
    inp = list()
    truth_table = dict()

    enc_table = dict()
    enc_t_table = dict()
    enc_to_org = dict()
    input_to_enc = dict()

    input_table = dict()
    input_table_num = list()
    count = 1
    for i in lst:
        if i == "(":
            continue
        elif i == ")":
            index = len(circuit)
            circuit[index] = dict()
            truth_table[index] = dict()
            op = operator.pop()
            circuit[index]['input2'] = inp.pop()
            circuit[index]['input1'] = inp.pop()
            circuit[index]['output'] = count
            truth_table[index]['truth_table'] = gate_table[op]
            inp.append(count)
            count += 1
        elif i in op_table:
            operator.append(i)
        else:
            inp.append(count)
            input_table[i] = count
            input_table_num.append(count)
            count += 1

    label = dict()
    for i in range(1, inp[0] + 1, 1):
        label[i] = [get_random(), get_random()]
        input_to_enc[i] = dict()
        input_to_enc[i][0] = label[i][0]
        input_to_enc[i][1] = label[i][1]
        enc_to_org[label[i][0]] = 0
        enc_to_org[label[i][1]] = 1

    for index in range(0, len(circuit), 1):
        g = circuit[index]
        input1 = label[g['input1']]
        input2 = label[g['input2']]
        output = label[g['output']]

        org_truth_table = truth_table[index]['truth_table']
        enc_t_table[index] = dict()
        lst = list()
        #ans = list()
        for i in input1:
            for j in input2:

                out_org = get_out_org(enc_to_org[i], enc_to_org[j],
                                      org_truth_table)

                out_val = input_to_enc[g['output']][out_org]
                #t2 = (i, j)
                lst.append(
                    [enigma.enigma([i, j, 0], out_val),
                     hash((i, j, out_val))])
                #ans.append([enigma.enigma([i, j, 0], lst[len(lst)-1][0]), out_val, out_org])

        enc_table[g['output']] = lst
        #print(lst)
        #print(ans)
    return circuit, input_to_enc, enc_table, enc_to_org, input_table_num
class uC_interface():

    connected = False
    gate = g.Gate(GS.Closed)  # Internal representation, don't use outside

    def __init__(self, COM='sep', baudrate=115200):
        # Simulates incorrect port
        # Correct port is 'sep'
        if COM != 'sep':
            return

        self.connected = True
        self.timer = Timer(TRANSITION_DURATION, self.transitionTimer)

    def initTimer(self):
        self.timer.cancel()  # Cancel previous call
        self.timer = Timer(TRANSITION_DURATION, self.transitionTimer)
        self.timer.setDaemon(True)
        self.timer.start()

    def transitionTimer(self):
        if self.gate.state not in [GS.Opening, GS.Closing]:
            return

        # Simulates an error
        if int(time.time()) % 10 == 2:
            self.gate.setState(GS.Error)

        else:
            if self.gate.state == GS.Opening:
                self.gate.setState(GS.Opened)
            if self.gate.state == GS.Closing:
                self.gate.setState(GS.Closed)

    def sendMsg(self, msg):
        if msg == '0':
            self.sendSignal()

    def sendSignal(self):
        sg = self.gate

        # Abrir si esta cerrado
        if sg.state == GS.Closed:
            sg.setState(GS.Opening)

        # Cerrar si esta abierto
        elif sg.state == GS.Opened:
            sg.setState(GS.Closing)

        # Si esta detenido, moverse en sentido opuesto a lo anterior
        elif sg.state == GS.Stopped:
            # Si estaba abriendose, ahora se cierra
            if sg.prev_state == GS.Opening:
                sg.setState(GS.Closing)
            # Si estaba cerrandose o hubo error, ahora se abre
            else:
                sg.setState(GS.Opening)

        # Si se esta abriendo o cerrando, detener
        elif sg.state in [GS.Opening, GS.Closing]:
            sg.setState(GS.Stopped)

        elif sg.state == GS.Error:
            sg.setState(GS.Closing)
            sg.setState(GS.Stopped)

        self.initTimer()

        #last_signal_time = time.time()
        return True

    def requestState(self, asString=False):
        if asString:
            return GS.ToString(self.gate.state)
        else:
            return self.gate.state
            lst.append(i)
    for j in lst:
        if input2_index == t_table[1][j]:
            return t_table[2][j]


random_table = list()
NANDList = list()
ANDList = list()
NORList = list()
ORList = list()
XORList = list()

for i in range(2):
    for j in range(2):
        a = gate.Gate(i, j, 'AND')
        ANDList.append(a.call())
        b = gate.Gate(i, j, 'OR')
        ORList.append(b.call())
        c = gate.Gate(i, j, 'XOR')
        XORList.append(c.call())
        d = gate.Gate(i, j, 'NAND')
        NANDList.append(d.call())
        e = gate.Gate(i, j, 'NOR')
        NORList.append(e.call())

a = [0, 0, 1, 1]
b = [0, 1, 0, 1]
nimplyList = [0, 0, 1, 0]
gate_table = {
    'and': [a, b, ANDList],
Exemplo n.º 10
0
import gate, sys

if __name__ == '__main__':
	gate = gate.Gate()
	gate.start()
	result = gate.loadAsJSON(sys.argv[1])
	gate.stop()
	with open(sys.argv[2], "w") as f:
		print >> f, result
Exemplo n.º 11
0
def load_megaman_objects(props=None, coll_boxes=None, enemies=None, all_items=None, spawn_megaman=False, m_x=0, m_y=0):
   # making all the game objects at once
   if spawn_megaman:
      megaman = Megaman('megaman', m_x, m_y, controls=[pygame.K_RIGHT, pygame.K_UP, pygame.K_LEFT, pygame.K_DOWN, pygame.K_z, pygame.K_x])

   n = 0
   if props != None:
      for lst in props:
         prop_name = lst[0]

         if prop_name == 'bg':
            x, y, width, height, display_layer, rgb = lst[1][0], lst[1][1], lst[2][0], lst[2][1], lst[4], lst[5]
            frames = [universal_var.background_images[i] for i in lst[3][0]]  
            s = sprite.Sprite(universal_var.main_sprite, x, y, width, height, 
               active_frames=[('bg_{}'.format(n), frames, lst[3][1])], change_rgb_value=True, rgb=rgb)
            obj = megaman_object.Megaman_object('bg_{}'.format(n), x, y, sprites=[s], coll_boxes=None, width=width, height=height, display_layer=display_layer)
            n += 1

         elif prop_name == 'gate':
            x, y = lst[1][0], lst[1][1]
            gate.Gate(x, y)


   if all_items != None:
      for lst in all_items:
         objType, x, y = lst[0], lst[1], lst[2]
         if objType == 'e_life':
            items.Extra_life(x, y, is_drop=False)

         elif objType == 's_hcap':
            items.Health_capsule(x, y, False, is_drop=False)  

         elif objType == 'l_hcap':
            items.Health_capsule(x, y, True, is_drop=False)


   if coll_boxes != None:
      for lst in coll_boxes:
         x, y, objType = lst[0][0], lst[0][1], lst[2]
         if objType == 'platform':
            width, height = lst[1][0], lst[1][1]
            c = [sprite.Collision_box(universal_var.hitbox, x, y, width, height)]
            ground = megaman_object.Megaman_object('platform', x, y, sprites=None, coll_boxes=c,
                           width=width, height=height, display_layer=5)
            megaman_object.Megaman_object.add_to_class_lst(ground, megaman_object.Megaman_object.platforms, ground.ID)

         elif objType == 'c_box':
            width, height = lst[1][0], lst[1][1]
            camera_box = camera.Camera_box('c_box', x, y, width, height)

         elif objType == 't_box':
            size, direction = lst[1], lst[3]
            transition_box = camera.Transition_box('t_box', x, y, size=size, direction=direction)


   if enemies != None:
      for lst in enemies:
         enemy_name = lst[1]

         if enemy_name == 'met':
            x, y = lst[0][0], lst[0][1]
            direction, trigger_width, trigger_height = lst[2], lst[3], lst[4]
            enemy.Met('met', x, y, direction, trigger_width, trigger_height)

         if enemy_name == 'det':
            x, y = lst[0][0], lst[0][1]
            start_time, time_to_apex, trigger_width, trigger_height = lst[2], lst[3], lst[4], lst[5]
            enemy.Detarnayappa('Detarnayappa', x, y, start_time, time_to_apex, trigger_width, trigger_height)

         if enemy_name == 'lasor':
            x, y = lst[0][0], lst[0][1]
            start_offset, x_vel = lst[2], lst[3]
            enemy.Lasor('lasor', x, y, start_offset, x_vel)

         if enemy_name == 'hoohoo':
            hoohoo_y, start_time = lst[0], lst[2]
            collbox_x, collbox_y, collbox_width, collbox_height = lst[3][0], lst[3][1], lst[4][0], lst[4][1]

            c = [sprite.Collision_box(universal_var.hitbox, collbox_x, collbox_y, collbox_width, collbox_height, (130, 190, 40))]
            trigg_collbox = megaman_object.Megaman_object('platform', collbox_x, collbox_y, sprites=None, coll_boxes=c,
                                                   width=collbox_width, height=collbox_height, display_layer=5)

            enemy.Hoohoo('hoohoo', hoohoo_y, start_time, trigg_collbox)

         if enemy_name == 'paozo':
            x, y, direction = lst[0][0], lst[0][1], lst[2]
            enemy.Paozo(x, y, direction)

         if enemy_name == 'big_stomper':
            x, y, damage_points = lst[0][0], lst[0][1], lst[2]
            enemy.Big_stomper(x, y, damage_points)

         if enemy_name == 'concrete_man':
            x, y, spawn = lst[0][0], lst[0][1], lst[4]
            collbox_x, collbox_y, collbox_width, collbox_height = lst[2][0], lst[2][1], lst[3][0], lst[3][1]

            c = [sprite.Collision_box(universal_var.hitbox, collbox_x, collbox_y, collbox_width, collbox_height, (130, 190, 140))]
            trigg_collbox = megaman_object.Megaman_object('platform', collbox_x, collbox_y, sprites=None, coll_boxes=c,
                                                   width=collbox_width, height=collbox_height, display_layer=5)

            concrete_man.Concrete_man(x, y, trigg_collbox, spawn)

   items.Item.drop_list_init()