Esempio n. 1
0
    def pseudo_share_participant(self, i_secret, q_group, participant):
        """ pseudo share generation for a single participant
            U = hash(master_share_x) XOR master_share_x
        """
        print('Pseudo share computation for secret s%r, access group A%r,'
              'participant P%r' % (i_secret, q_group, participant))

        # convert master share to bytes
        if isinstance(self.master_shares_x[participant - 1], bytes):
            bytes_x = self.master_shares_x[participant - 1]
            int_x = int.from_bytes(self.master_shares_x[participant - 1],
                                   byteorder='big')
        else:
            bytes_x = bytes([self.master_shares_x[participant - 1]])
            int_x = self.master_shares_x[participant - 1]

        # hash the master share
        hash_of_master_share = common.hash(bytes_x, self.hash_len,
                                           self.hash_aes_nonce)
        hash_of_master_share = common.modulo_p(self.p, hash_of_master_share)

        hash_of_master_share_int = int.from_bytes(hash_of_master_share,
                                                  byteorder='big')

        # XOR hashed value with master share
        int_pseudo_share = hash_of_master_share_int ^ int_x

        print('XOR output =', int_pseudo_share)
        int_pseudo_share = common.modulo_p(self.p, int_pseudo_share)
        pseudo_share = int_pseudo_share.to_bytes(
            bytehelper.bytelen(int_pseudo_share), byteorder='big')

        assert isinstance(pseudo_share, bytes)
        return pseudo_share
    def pseudo_share_participant(self, i_secret, q_group, participant):
        """ pseudo share generation for a single participant
            U = h(x || i_U || q_v)
        """
        print('Pseudo share computation for secret s%r, access group A%r,'
              'participant P%r' % (i_secret, q_group, participant))

        # l = length of longest access group for this secret
        lengths = []
        gamma = self.access_structures[i_secret]
        for A in gamma:
            lengths.append(len(A)-1)
        l = max(lengths)
        u = floor(log2(self.k)) + 1 # u = bit length of number of secrets k
        v = floor(log2(l)) + 1  # v = bit length of l
        
        # concatenate x, i and q binary
        if isinstance(self.master_shares_x[participant-1], bytes):
            bytes_x = self.master_shares_x[participant-1]
        else:
            bytes_x = bytes([ self.master_shares_x[participant-1] ])
        bytes_i = bytes([i_secret])
        bytes_q = bytes([q_group])

        message = b''.join([bytes_x, bytes_i, bytes_q])  # python 3.x
        # hash the concatenated bytes
        hash_of_message = common.hash(message, self.hash_len, self.hash_aes_nonce)
        share = common.modulo_p(self.p, hash_of_message)
        #print('Pseudo share for secret s%d, access group A%d, participant P%d:\nU = ' % (i_secret, q_group, participant), share.hex())
        return share
Esempio n. 3
0
    def combine_secret(self, i_secret, q_group, obtained_pseudo_shares):
        """
        combine a single secret in Lin-Yeh algorithm
        """
        if isinstance(obtained_pseudo_shares[0], bytes):
            obtained_shares_int = []
            for obtained_share in obtained_pseudo_shares:
                obtained_shares_int.append(
                    int.from_bytes(obtained_share, byteorder='big'))
            obtained_pseudo_shares = obtained_shares_int

        print('Obtained pseudo shares:', obtained_pseudo_shares)

        print('Access group:', self.access_structures[i_secret])
        assert (q_group <= len(self.access_structures[i_secret]))

        combine_sum = 0

        for b, Pb in enumerate(self.access_structures[i_secret][q_group]):

            print('\tb =', b)
            part_sum_B = (obtained_pseudo_shares[b] +
                          self.public_shares_M[i_secret][q_group][b]) % self.p
            print('\tB = U+M, B = %d, M=%d' %
                  (part_sum_B, self.public_shares_M[i_secret][q_group][b]))

            combine_product = 1
            for r, Pr in enumerate(self.access_structures[i_secret][q_group]):
                if r != b:
                    print('\t\tr =', r)
                    print('\t\tID_(b=%d) : %d, ID_(r=%d) : %d' %
                          (Pb, self.get_id_int(Pb), Pr, self.get_id_int(Pr)))
                    denominator = (self.get_id_int(Pr) -
                                   self.get_id_int(Pb)) % self.p
                    den_inverse = bytehelper.inverse_modulo_p(
                        denominator, self.p)
                    print('\t\tdenominator = %d\n its inverse = %d' %
                          (denominator, den_inverse))
                    part_product = (
                        (self.get_id_int(Pr)) % self.p * den_inverse) % self.p

                    combine_product *= part_product
                    print('\t\tpart_product', part_product)
                    print('\t\tcombine_product', combine_product)

            combine_sum += (part_sum_B * combine_product) % self.p
            print('\tcomb prod=%d, part_sum_B=%d, combined_sum=%d' %
                  (combine_product, part_sum_B, combine_sum))

        print("Combined sum, s%d = %d" % (i_secret, combine_sum % self.p))

        return common.modulo_p(self.p, combine_sum)
Esempio n. 4
0
    def combine_secret_key(self, i_secret, obtained_shares):
        """
        combine a single key in Herraz-Ruiz-Saez algorithm
        """
        print('Obtained pseudo shares:', obtained_shares)
        q_group = 0

        combine_sum = 0

        print('combine_secret_key for access structure {}',
              self.access_structures)

        for b, Pb in enumerate(self.access_structures[i_secret][q_group]):

            print('\tcurrent user {} with index {} ='.format(Pb, b))
            part_sum = obtained_shares[b] % self.p

            combine_product = 1
            for r, Pr in enumerate(self.access_structures[i_secret][q_group]):
                if r != b:
                    print('\t\tr =', r)
                    print('\t\tID_(b=%d) : %d, ID_(r=%d) : %d' %
                          (Pb, self.get_id_int(Pb), Pr, self.get_id_int(Pr)))

                    denominator = (self.get_id_int(Pr) -
                                   self.get_id_int(Pb)) % self.p
                    den_inverse = bytehelper.inverse_modulo_p(
                        denominator, self.p)
                    print('\t\tdenominator = %d\n its inverse = %d' %
                          (denominator, den_inverse))

                    part_product = (
                        (self.get_id_int(Pr)) % self.p * den_inverse) % self.p
                    combine_product *= part_product

                    print('\t\tpart_product', part_product)
                    print('\t\tcombine_product', combine_product)

            combine_sum += (part_sum * combine_product) % self.p
            print('\tcomb prod=%d, part_sum_B=%d, combined_sum=%d' %
                  (combine_product, part_sum, combine_sum))

        print("Combined sum, s%d = %d" % (i_secret, combine_sum % self.p))

        return common.modulo_p(self.p, combine_sum)
Esempio n. 5
0
def test_modulo_p():
    p = 1009
    dealer = Dealer(p, n_participants, s_secrets, access_structures)
    assert_equal(common.modulo_p(p, 2011), 1002)