Esempio n. 1
0
    def iterate_cubically(self, maxterm, private_assignment=None):

        if not private_assignment:
            private_assignment = {}

        # logging.debug("iterating cubically " + maxterm + " " + str(private_assignment))

        n_terms = maxterm.count('v')

        answer = 0

        #print()

        for i in range(2**n_terms):

            assign_string = format(i, 'b')

            while len(assign_string) != n_terms:
                assign_string = '0' + assign_string

            terms = maxterm.split('v')[1:]

            assignment = private_assignment

            for j, term in enumerate(terms):
                assignment['v' + term] = int(assign_string[j])

            # logging.debug(assignment)

            answer = sum_mod2(
                self.bbpoly.evaluate(assignment, self.index_to_take), answer)
            #print(answer)

        return answer
Esempio n. 2
0
    def iterate_cubically(self, maxterm, private_assignment=None,out_bit=None):

        if out_bit is not None:
            self.index_to_take = out_bit

        if not private_assignment:
            private_assignment = {}

        # print(private_assignment)

        logging.info("iterating cubically " + maxterm + " " + str(private_assignment))

        n_terms = maxterm.count('v')

        answer = 0

        truth_table = []

        for i in range(2 ** n_terms):

            assign_string = format(i, 'b')

            while len(assign_string) != n_terms:
                assign_string = '0' + assign_string
            terms = maxterm.split('v')[1:]
            # print (terms)
            assignment = private_assignment

            for j, term in enumerate(terms):
                assignment['v' + term] = int(assign_string[j])

            # logging.debug(assignment)

            answer = sum_mod2(self.bbpoly.evaluate(assignment, self.index_to_take), answer)
            truth_table.append(answer)

        if out_bit is not None:           
            return truth_table
        else:
            return answer
Esempio n. 3
0
    def test_maxterm(self, maxterm, index=0):

        self.index_to_take = index

        logging.debug("... testing maxterm ... " + maxterm)

        assign_strings = []

        for i in range(min(2**self.degree, 80)):

            assign_string = format(i, 'b')
            while len(assign_string) != self.degree:
                assign_string = '0' + assign_string

            assign_strings.append(assign_string)

        ################################################################################

        logging.debug(" ... checking if not constant ... " + maxterm)

        not_constant = True

        n_checked = 0

        for i, assign_string in enumerate(assign_strings):

            if n_checked >= 0:
                break
            logging.debug("constant check " + str(n_checked))

            n_checked += 1

            current_assignment = {}

            for index, secret_var in enumerate(self.bbpoly.secretvariables):
                current_assignment[secret_var] = int(assign_string[index])

            if i == 0:
                first_answer = self.iterate_cubically(maxterm,
                                                      current_assignment)
                continue

            else:
                answer = self.iterate_cubically(maxterm, current_assignment)
                if answer != first_answer:
                    not_constant = True
                    break

            logging.debug(str(answer))

            if n_checked >= 0:
                break

        if not not_constant:
            logging.debug('False ' + maxterm)
            return False

        #################################################################################

        logging.debug(" ... checking if linear ... " + maxterm)

        linear = True

        n_checked = 0

        my_flag = True

        for assign1 in assign_strings:
            for assign2 in assign_strings:

                if n_checked >= 0:
                    my_flag = False
                    break

                if assign1 == assign2:
                    continue

                logging.debug("linear check " + str(n_checked))
                logging.debug('assign1: ' + assign1)
                logging.debug('assign2: ' + assign2)

                current_assignment = {}

                for index, secret_var in enumerate(
                        self.bbpoly.secretvariables):
                    current_assignment[secret_var] = 0

                ans_1 = self.iterate_cubically(maxterm, current_assignment)
                print(ans_1)

                current_assignment = {}

                for index, secret_var in enumerate(
                        self.bbpoly.secretvariables):
                    current_assignment[secret_var] = int(assign1[index])

                ans_2 = self.iterate_cubically(maxterm, current_assignment)
                print(ans_2)

                current_assignment = {}

                for index, secret_var in enumerate(
                        self.bbpoly.secretvariables):
                    current_assignment[secret_var] = int(assign2[index])

                ans_3 = self.iterate_cubically(maxterm, current_assignment)
                print(ans_3)

                lhs = sum_mod2(sum_mod2(ans_1, ans_2), ans_3)
                # logging.debug('lhs: ' +  str(lhs))

                rhs_argument = format(int(assign1, 2) ^ int(assign2, 2), 'b')

                while len(rhs_argument) != self.degree:
                    rhs_argument = '0' + rhs_argument

                # logging.debug('rhs argument: ' + str(rhs_argument))

                current_assignment = {}

                for index, secret_var in enumerate(
                        self.bbpoly.secretvariables):
                    current_assignment[secret_var] = int(rhs_argument[index])

                rhs = self.iterate_cubically(maxterm, current_assignment)
                print(rhs)

                if n_checked >= 0:
                    my_flag = False
                    break

                if lhs != rhs:
                    linear = False
                    break

                n_checked += 1

            if not linear:
                break

            if not my_flag:
                break

        if not linear:
            logging.debug('False ' + maxterm)
            return False

            #################################################################################

        logging.debug('True ' + maxterm)
        return True