def create(self): if isinstance(self.outputs, enc_vec): rel_out = self.outputs[0] else: rel_out = self.outputs temp1 = enc_vec(name=self.name + 'out_bit', nb=len(self.inputs)) self.add( bi_assign(ass_type='and', lhs=[self.inputs[0], self.zero], rhs=temp1[0])) temp2 = enc_vec(name=self.name + 'flag_bit', nb=len(self.inputs)) for idx in range(len(self.inputs)): if idx < len(self.plaintext) and int(self.plaintext[idx]) == 1: self.add( tri_assign(ass_type='mux', lhs=[self.inputs[idx], temp1[idx], self.zero], rhs=temp2[idx])) else: self.add( tri_assign(ass_type='mux', lhs=[self.inputs[idx], self.one, temp1[idx]], rhs=temp2[idx])) if idx < len(self.inputs) - 1: self.add( mono_assign(ass_type='alias', lhs=[temp2[idx]], rhs=temp1[idx + 1])) else: self.add( mono_assign(ass_type='alias', lhs=[temp2[idx]], rhs=rel_out))
def operate(self): for idx in range(self.size[0]): for jdx in range(self.size[1]): self.add( mono_assign(ass_type=self.ass_types[self.cond[idx][jdx]], lhs=[self.inputs[idx][jdx]], rhs=self.outputs[idx][jdx]))
def create(self): inp_vec = self.inputs out = self.count_arr( lvl=int(math.ceil(math.log(len(inp_vec) * 1.0, 2)))) for idx in range(len(self.outputs)): self.add(mono_assign(ass_type='alias', lhs=[out[idx]], rhs=self.outputs[idx]))
def create(self, carry_out, carry_in): inp1 = self.inputs[0 + int(carry_in):self.nb + int(carry_in)] inp2 = self.inputs[self.nb + int(carry_in):] if carry_in: self.add( mono_assign(ass_type='alias', lhs=[self.inputs[0]], rhs=self.carry_list[0])) self.carry_list[0] = self.inputs[0] for bit_id in range(self.nb): if bit_id > 0 or carry_in: self.add( oneb_adder(name=self.name + '2b_adder_' + str(bit_id), inputs=[ self.carry_list[bit_id], inp1[bit_id], inp2[bit_id] ], outputs=[ self.outputs[bit_id], self.carry_list[bit_id + 1] ], carry=True)) else: self.add( oneb_adder(name=self.name + '2b_adder_' + str(bit_id), inputs=[inp1[bit_id], inp2[bit_id]], outputs=[ self.outputs[bit_id], self.carry_list[bit_id + 1] ], carry=bit_id > 0)) if carry_out: self.add( mono_assign(ass_type='alias', lhs=[self.carry_list[-1]], rhs=self.outputs[-1]))
def create(self, carry): if carry: cin = self.inputs[0] x = self.inputs[carry] y = self.inputs[carry + 1] temp1 = variables(self.name + '_tmp1', randomize_name=self.randomize_temps) temp2 = variables(self.name + '_tmp2', randomize_name=self.randomize_temps) temp3 = variables(self.name + '_tmp3', randomize_name=self.randomize_temps) self.add(bi_assign(ass_type='xor', lhs=[x, y], rhs=temp1)) if carry: self.add( bi_assign(ass_type='xor', lhs=[temp1, cin], rhs=self.outputs[0])) else: self.add( mono_assign(ass_type='alias', lhs=[temp1], rhs=self.outputs[0])) self.add(bi_assign(ass_type='and', lhs=[x, y], rhs=temp2)) if carry: self.add(bi_assign(ass_type='and', lhs=[cin, temp1], rhs=temp3)) self.add( bi_assign(ass_type='or', lhs=[temp3, temp2], rhs=self.outputs[1])) else: self.add( mono_assign(ass_type='alias', lhs=[temp2], rhs=self.outputs[1]))
def create(self, carry_out): inp1 = self.x inp2 = self.y for bit_id in range(self.nb): if bit_id > 0 and bit_id < self.len2: self.add( oneb_adder(name=self.name + '2b_adder_' + str(bit_id), inputs=[ self.carry_list[bit_id], inp1[bit_id], inp2[bit_id] ], outputs=[ self.outputs[bit_id], self.carry_list[bit_id + 1] ], carry=True)) elif bit_id == 0: self.add( oneb_adder(name=self.name + '2b_adder_' + str(bit_id), inputs=[inp1[bit_id], inp2[bit_id]], outputs=[ self.outputs[bit_id], self.carry_list[bit_id + 1] ], carry=bit_id > 0)) elif bit_id >= self.len2: self.add( oneb_adder(name=self.name + '2b_adder_' + str(bit_id), inputs=[self.carry_list[bit_id], inp1[bit_id]], outputs=[ self.outputs[bit_id], self.carry_list[bit_id + 1] ], carry=False)) if carry_out: self.add( mono_assign(ass_type='alias', lhs=[self.carry_list[-1]], rhs=self.outputs[-1]))
def col2im(circuit, mul, h_prime, w_prime, C, tgt_out): """ Args: mul: (h_prime*w_prime*w,F) matrix, each col should be reshaped to C*h_prime*w_prime when C>0, or h_prime*w_prime when C = 0 h_prime: reshaped filter height w_prime: reshaped filter width C: reshaped filter channel, if 0, reshape the filter to 2D, Otherwise reshape it to 3D Returns: if C == 0: (F,h_prime,w_prime) matrix Otherwise: (F,C,h_prime,w_prime) matrix """ F = mul.size[1] for i in range(0, F): map( lambda idx: circuit.add( mono_assign(ass_type='alias', lhs=[mul[idx][i]], rhs=tgt_out[i][idx / w_prime][idx % w_prime])), range(w_prime * h_prime))
def operate(self): for idx in range(len(self.inputs)): self.add( mono_assign(ass_type=self.ass_types[self.cond[idx]], lhs=[self.inputs[idx]], rhs=self.outputs[idx]))