def Insert(self, key): num_tries = 0 whichTable = 0 hash_key = self.CuckooTables[whichTable].Hash(key) currentKey = key successfulInsert = False while successfulInsert == False: if self.CuckooTables[whichTable].Slot[hash_key].head == None: LinkedList.Insert(self.CuckooTables[whichTable].Slot[hash_key], currentKey) num_tries += 1 successfulInsert = True else: LinkedList.Insert(self.CuckooTables[whichTable].Slot[hash_key], currentKey) currentElement = self.CuckooTables[whichTable].Slot[ hash_key].head.next currentKey = currentElement.key LinkedList.Delete(self.CuckooTables[whichTable].Slot[hash_key], currentElement) whichTable = (whichTable + 1) % 2 hash_key = self.CuckooTables[whichTable].Hash(currentKey) num_tries += 1 if num_tries > self.maxInsertionTries: whichTable = self.stash LinkedList.Insert(self.CuckooTables[whichTable], currentKey) break
class Alice(Cuckoo2d): def __init__(self, a, b, num_of_keys, prime): Cuckoo2d.__init__(self, a, b, num_of_keys, prime) self.queue = LinkedList() self.secretBits = [ ] # index: k = i*n + j, some of whichTable are set to -1 for empty table slots def Insert(self, key, whichTable): self.CuckooTables[whichTable].Insert(key) self.CuckooTables[whichTable + 2].Insert(key) def Relocate(self, whichTable): for i in range(0, self.NumOfSlots): currentElement = self.CuckooTables[whichTable].Slot[i].head while currentElement != None and currentElement.next != None: self.queue.Insert(currentElement.next.key) self.CuckooTables[whichTable].Slot[i].Delete( currentElement.next) # delete from other subtable of same side / active table currentQueueElement = self.queue.head while currentQueueElement != None: tableElement = self.CuckooTables[(whichTable + 2) % 4].Search( currentQueueElement.key) self.CuckooTables[(whichTable + 2) % 4].Delete(tableElement) currentQueueElement = currentQueueElement.next # Alice's insertion protocol def IterativeInsert(self, keySpace, whichTable=0): for key in keySpace: self.Insert(key, whichTable) self.Relocate(whichTable) self.Relocate((whichTable + 2) % 4) while self.queue.head != None: # do something while the relocation pool is nonempty whichTable = (whichTable + 1) % 2 # change active table currentQueueElement = self.queue.head while currentQueueElement != None: self.Insert( currentQueueElement.key, whichTable) # inserts to both subCuckooTables on one side self.queue.Delete(currentQueueElement) currentQueueElement = currentQueueElement.next self.Relocate(whichTable) self.Relocate((whichTable + 2) % 4) # Alice's sharing protocol def getSecretAndSharedBits(self): self.secretBits, sharedBits = self._getSecretAndSharedBits() return sharedBits
def Delete(self, element): whichTable, searchedElement = self.Search(element.key) if whichTable != self.stash: HashTable.Delete(self.CuckooTables[whichTable], searchedElement) else: LinkedList.Delete(self.CuckooTables[whichTable], searchedElement)