def test_hash_same(): """ set up Dealer object and check hash repeatability """ # Create a Dealer dealer = Dealer(p256, n_participants, s_secrets, access_structures) # test hash function - it should be repeatable for the same Dealer object hash1 = common.hash(b'BYTESEQUENCE', dealer.hash_len, dealer.hash_aes_nonce) hash2 = common.hash(b'BYTESEQUENCE', dealer.hash_len, dealer.hash_aes_nonce) assert_equal(hash1, hash2)
def test_hash_different(): """ different instances of Dealer should have different hash results as they have separate random AES nonces""" # Create a Dealer dealer1 = Dealer(p256, n_participants, s_secrets, access_structures) dealer2 = Dealer(p256, n_participants, s_secrets, access_structures) # test hash function - it should be different for distinct Dealers hash1 = common.hash(b'BYTESEQUENCE', dealer1.hash_len, dealer1.hash_aes_nonce) hash2 = common.hash(b'BYTESEQUENCE', dealer2.hash_len, dealer2.hash_aes_nonce) assert_not_equal(hash1, hash2)
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
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