def onSuccess(response, request): self.logger.debug('Sending VCard (%s) with image id %s', ID, response.pictureId) image_hash = utils.sha1hash(response.pictureData) self.logger.debug('Image hash is %s', image_hash) self.backend.handleVCard(self.user, ID, buddy, "", "", response.pictureData) obuddy = self.buddies[buddy] self.updateBuddy(buddy, obuddy.nick, obuddy.groups, image_hash)
def multiSigBr(pid, N, t, msg, broadcast, receive, outputs, send): # Since all the parties we have are symmetric, so I implement this function for N instances of A-cast as a whole # Here msg is a set of transactions assert (isinstance(outputs, list)) for i in outputs: assert (isinstance(i, Queue)) keys = getECDSAKeys() Threshold = N - 2 * t Threshold2 = N - t zfecEncoder = zfec.Encoder(Threshold, N) zfecDecoder = zfec.Decoder(Threshold, N) def merkleTree(strList, someHash=coolSHA256Hash): # someHash is a mapping from a int to a int treeLength = 2**ceil(math.log(len(strList)) / math.log(2)) mt = [0] * (treeLength * 2) # find a place to put our leaves for i in range(len(strList)): mt[i + treeLength] = someHash( strList[i] ) # TODO: need to change strList[i] from a string to an integer here. for i in range(treeLength - 1, 0, -1): # 1, 2, 3, ..., treeLength - 1 # mt[i] = someHash(''.join([chr(ord(a) ^ ord(b)) for a, b in zip(mt[i*2], mt[i*2+1])])) # XOR is commutative mt[i] = someHash(mt[i * 2] + mt[i * 2 + 1]) # concat is not commutative return mt def getMerkleBranch(index, mt): res = [] t = index + (len(mt) >> 1) while t > 1: res.append(mt[t ^ 1]) # we are picking up the sibling t /= 2 return res def merkleVerify(val, rootHash, branch, someHash, index): # index has information on whether we are facing a left sibling or a right sibling tmp = someHash(val) tindex = index for br in branch: tmp = someHash((tindex & 1) and br + tmp or tmp + br) tindex >>= 1 if tmp != rootHash: print "Verification failed with", someHash( val), rootHash, branch, tmp == rootHash return tmp == rootHash def Listener(): opinions = [defaultdict(lambda: 0) for _ in range(N)] rootHashes = dict() readyCounter = [defaultdict(lambda: 0) for _ in range(N)] signed = [False] * N readySent = [False] * N reconstDone = [False] * N while True: # main loop sender, msgBundle = receive() if msgBundle[0] == 'i' and not signed[sender]: if keys[sender].verify( sha1hash(''.join([ msgBundle[1][0], msgBundle[1][1], ''.join(msgBundle[1][2]) ])), msgBundle[2]): assert isinstance(msgBundle[1], tuple) if not merkleVerify(msgBundle[1][0], msgBundle[1][1], msgBundle[1][2], coolSHA256Hash, pid): continue if sender in rootHashes: if rootHashes[sender] != msgBundle[1][1]: print "Cheating caught, exiting" sys.exit(0) else: rootHashes[sender] = msgBundle[1][1] newBundle = (sender, msgBundle[1][0], msgBundle[1][1], msgBundle[1][2] ) # assert each frag has a length of step broadcast(('e', newBundle, keys[pid].sign( sha1hash(''.join([ str(newBundle[0]), newBundle[1], newBundle[2], ''.join(newBundle[3]) ]))))) signed[sender] = True else: raise ECDSASignatureError() elif msgBundle[0] == 'e': if keys[sender].verify( sha1hash(''.join([ str(msgBundle[1][0]), msgBundle[1][1], msgBundle[1][2], ''.join(msgBundle[1][3]) ])), msgBundle[2]): originBundle = msgBundle[1] if not merkleVerify(originBundle[1], originBundle[2], originBundle[3], coolSHA256Hash, sender): continue if originBundle[0] in rootHashes: if rootHashes[originBundle[0]] != originBundle[2]: print "Cheating caught, exiting" sys.exit(0) else: rootHashes[originBundle[0]] = originBundle[2] opinions[originBundle[0]][sender] = originBundle[ 1] # We are going to move this part to kekeketktktktk if len(opinions[originBundle[0]] ) >= Threshold2 and not readySent[originBundle[0]]: readySent[originBundle[0]] = True broadcast( ('r', originBundle[0], originBundle[2])) # We are broadcasting its hash else: raise ECDSASignatureError() elif msgBundle[0] == 'r': readyCounter[msgBundle[1]][msgBundle[2]] += 1 tmp = readyCounter[msgBundle[1]][msgBundle[2]] if tmp >= t + 1 and not readySent[msgBundle[1]]: readySent[msgBundle[1]] = True broadcast(('r', msgBundle[1], msgBundle[2])) if tmp >= Threshold2 and not outputs[msgBundle[1]].full() and \ not reconstDone[msgBundle[1]] and len(opinions[msgBundle[1]]) >= Threshold: reconstDone[msgBundle[1]] = True if msgBundle[1] in rootHashes: if rootHashes[msgBundle[1]] != msgBundle[2]: print "Cheating caught, exiting" sys.exit(0) else: rootHashes[msgBundle[1]] = msgBundle[2] if opinions[msgBundle[1]].values()[0] == '': reconstruction = [''] else: reconstruction = zfecDecoder.decode( opinions[msgBundle[1]].values()[:Threshold], opinions[msgBundle[1]].keys()[:Threshold] ) # We only take the first [Threshold] fragments rawbuf = ''.join(reconstruction) buf = rawbuf[:-ord(rawbuf[-1])] # Check root hash step = len( buf ) / Threshold + 1 # len(buf) % Threshold == 0 and len(buf) / Threshold or (len(buf) / Threshold + 1) assert step * Threshold - len(buf) < 256 # assumption buf_ = buf.ljust(step * Threshold - 1, '\xFF') + chr(step * Threshold - len(buf)) fragList = [ buf_[i * step:(i + 1) * step] for i in range(Threshold) ] encodedFragList = zfecEncoder.encode(fragList) mt = merkleTree(encodedFragList, coolSHA256Hash) assert rootHashes[msgBundle[1]] == mt[ 1] # full binary tree if outputs[msgBundle[1]].empty(): outputs[msgBundle[1]].put(buf) greenletPacker(Greenlet(Listener), 'multiSigBr.Listener', (pid, N, t, msg, broadcast, receive, outputs)).start() buf = msg # We already assumed the proposals are byte strings step = len( buf ) / Threshold + 1 # len(buf) % Threshold == 0 and len(buf) / Threshold or (len(buf) / Threshold + 1) assert step * Threshold - len(buf) < 256 # assumption buf = buf.ljust(step * Threshold - 1, '\xFF') + chr(step * Threshold - len(buf)) fragList = [buf[i * step:(i + 1) * step] for i in range(Threshold)] encodedFragList = zfecEncoder.encode(fragList) mt = merkleTree(encodedFragList, coolSHA256Hash) rootHash = mt[1] # full binary tree for i in range(N): mb = getMerkleBranch( i, mt) # notice that index starts from 1 and pid starts from 0 newBundle = (encodedFragList[i], rootHash, mb) send(i, ('i', newBundle, keys[pid].sign( sha1hash(''.join( [newBundle[0], newBundle[1], ''.join(newBundle[2])])))))
def Listener(): opinions = [defaultdict(lambda: 0) for _ in range(N)] rootHashes = dict() readyCounter = [defaultdict(lambda: 0) for _ in range(N)] signed = [False] * N readySent = [False] * N reconstDone = [False] * N while True: # main loop sender, msgBundle = receive() if msgBundle[0] == 'i' and not signed[sender]: if keys[sender].verify( sha1hash(''.join([ msgBundle[1][0], msgBundle[1][1], ''.join(msgBundle[1][2]) ])), msgBundle[2]): assert isinstance(msgBundle[1], tuple) if not merkleVerify(msgBundle[1][0], msgBundle[1][1], msgBundle[1][2], coolSHA256Hash, pid): continue if sender in rootHashes: if rootHashes[sender] != msgBundle[1][1]: print "Cheating caught, exiting" sys.exit(0) else: rootHashes[sender] = msgBundle[1][1] newBundle = (sender, msgBundle[1][0], msgBundle[1][1], msgBundle[1][2] ) # assert each frag has a length of step broadcast(('e', newBundle, keys[pid].sign( sha1hash(''.join([ str(newBundle[0]), newBundle[1], newBundle[2], ''.join(newBundle[3]) ]))))) signed[sender] = True else: raise ECDSASignatureError() elif msgBundle[0] == 'e': if keys[sender].verify( sha1hash(''.join([ str(msgBundle[1][0]), msgBundle[1][1], msgBundle[1][2], ''.join(msgBundle[1][3]) ])), msgBundle[2]): originBundle = msgBundle[1] if not merkleVerify(originBundle[1], originBundle[2], originBundle[3], coolSHA256Hash, sender): continue if originBundle[0] in rootHashes: if rootHashes[originBundle[0]] != originBundle[2]: print "Cheating caught, exiting" sys.exit(0) else: rootHashes[originBundle[0]] = originBundle[2] opinions[originBundle[0]][sender] = originBundle[ 1] # We are going to move this part to kekeketktktktk if len(opinions[originBundle[0]] ) >= Threshold2 and not readySent[originBundle[0]]: readySent[originBundle[0]] = True broadcast( ('r', originBundle[0], originBundle[2])) # We are broadcasting its hash else: raise ECDSASignatureError() elif msgBundle[0] == 'r': readyCounter[msgBundle[1]][msgBundle[2]] += 1 tmp = readyCounter[msgBundle[1]][msgBundle[2]] if tmp >= t + 1 and not readySent[msgBundle[1]]: readySent[msgBundle[1]] = True broadcast(('r', msgBundle[1], msgBundle[2])) if tmp >= Threshold2 and not outputs[msgBundle[1]].full() and \ not reconstDone[msgBundle[1]] and len(opinions[msgBundle[1]]) >= Threshold: reconstDone[msgBundle[1]] = True if msgBundle[1] in rootHashes: if rootHashes[msgBundle[1]] != msgBundle[2]: print "Cheating caught, exiting" sys.exit(0) else: rootHashes[msgBundle[1]] = msgBundle[2] if opinions[msgBundle[1]].values()[0] == '': reconstruction = [''] else: reconstruction = zfecDecoder.decode( opinions[msgBundle[1]].values()[:Threshold], opinions[msgBundle[1]].keys()[:Threshold] ) # We only take the first [Threshold] fragments rawbuf = ''.join(reconstruction) buf = rawbuf[:-ord(rawbuf[-1])] # Check root hash step = len( buf ) / Threshold + 1 # len(buf) % Threshold == 0 and len(buf) / Threshold or (len(buf) / Threshold + 1) assert step * Threshold - len(buf) < 256 # assumption buf_ = buf.ljust(step * Threshold - 1, '\xFF') + chr(step * Threshold - len(buf)) fragList = [ buf_[i * step:(i + 1) * step] for i in range(Threshold) ] encodedFragList = zfecEncoder.encode(fragList) mt = merkleTree(encodedFragList, coolSHA256Hash) assert rootHashes[msgBundle[1]] == mt[ 1] # full binary tree if outputs[msgBundle[1]].empty(): outputs[msgBundle[1]].put(buf)
def Listener(): opinions = [defaultdict(lambda: 0) for _ in range(N)] rootHashes = dict() readyCounter = [defaultdict(lambda: 0) for _ in range(N)] signed = [False]*N readySent = [False] * N reconstDone = [False] * N while True: # main loop sender, msgBundle = receive() if msgBundle[0] == 'i' and not signed[sender]: if keys[sender].verify(sha1hash(''.join([msgBundle[1][0], msgBundle[1][1], ''.join(msgBundle[1][2])])), msgBundle[2]): assert isinstance(msgBundle[1], tuple) if not merkleVerify(msgBundle[1][0], msgBundle[1][1], msgBundle[1][2], coolSHA256Hash, pid): continue if sender in rootHashes: if rootHashes[sender]!= msgBundle[1][1]: print "Cheating caught, exiting" sys.exit(0) else: rootHashes[sender] = msgBundle[1][1] newBundle = (sender, msgBundle[1][0], msgBundle[1][1], msgBundle[1][2]) # assert each frag has a length of step broadcast(('e', newBundle, keys[pid].sign( sha1hash(''.join([str(newBundle[0]), newBundle[1], newBundle[2], ''.join(newBundle[3])])) ))) signed[sender] = True else: raise ECDSASignatureError() elif msgBundle[0] == 'e': if keys[sender].verify(sha1hash(''.join([str(msgBundle[1][0]), msgBundle[1][1], msgBundle[1][2], ''.join(msgBundle[1][3])])), msgBundle[2]): originBundle = msgBundle[1] if not merkleVerify(originBundle[1], originBundle[2], originBundle[3], coolSHA256Hash, sender): continue if originBundle[0] in rootHashes: if rootHashes[originBundle[0]]!= originBundle[2]: print "Cheating caught, exiting" sys.exit(0) else: rootHashes[originBundle[0]] = originBundle[2] opinions[originBundle[0]][sender] = originBundle[1] # We are going to move this part to kekeketktktktk if len(opinions[originBundle[0]]) >= Threshold2 and not readySent[originBundle[0]]: readySent[originBundle[0]] = True broadcast(('r', originBundle[0], originBundle[2])) # We are broadcasting its hash else: raise ECDSASignatureError() elif msgBundle[0] == 'r': readyCounter[msgBundle[1]][msgBundle[2]] += 1 tmp = readyCounter[msgBundle[1]][msgBundle[2]] if tmp >= t+1 and not readySent[msgBundle[1]]: readySent[msgBundle[1]] = True broadcast(('r', msgBundle[1], msgBundle[2])) if tmp >= Threshold2 and not outputs[msgBundle[1]].full() and \ not reconstDone[msgBundle[1]] and len(opinions[msgBundle[1]]) >= Threshold: reconstDone[msgBundle[1]] = True if msgBundle[1] in rootHashes: if rootHashes[msgBundle[1]]!= msgBundle[2]: print "Cheating caught, exiting" sys.exit(0) else: rootHashes[msgBundle[1]] = msgBundle[2] if opinions[msgBundle[1]].values()[0] == '': reconstruction = [''] else: reconstruction = zfecDecoder.decode(opinions[msgBundle[1]].values()[:Threshold], opinions[msgBundle[1]].keys()[:Threshold]) # We only take the first [Threshold] fragments rawbuf = ''.join(reconstruction) buf = rawbuf[:-ord(rawbuf[-1])] # Check root hash step = len(buf) / Threshold + 1 # len(buf) % Threshold == 0 and len(buf) / Threshold or (len(buf) / Threshold + 1) assert step * Threshold - len(buf) < 256 # assumption buf_ = buf.ljust(step * Threshold - 1, '\xFF') + chr(step * Threshold - len(buf)) fragList = [buf_[i*step : (i+1)*step] for i in range(Threshold)] encodedFragList = zfecEncoder.encode(fragList) mt = merkleTree(encodedFragList, coolSHA256Hash) assert rootHashes[msgBundle[1]] == mt[1] # full binary tree if outputs[msgBundle[1]].empty(): outputs[msgBundle[1]].put(buf)
def multiSigBr(pid, N, t, msg, broadcast, receive, outputs, send): # Since all the parties we have are symmetric, so I implement this function for N instances of A-cast as a whole # Here msg is a set of transactions assert(isinstance(outputs, list)) for i in outputs: assert(isinstance(i, Queue)) keys = getECDSAKeys() Threshold = N - 2 * t Threshold2 = N - t zfecEncoder = zfec.Encoder(Threshold, N) zfecDecoder = zfec.Decoder(Threshold, N) def merkleTree(strList, someHash = coolSHA256Hash): # someHash is a mapping from a int to a int treeLength = 2 ** ceil(math.log(len(strList)) / math.log(2)) mt = [0] * (treeLength * 2) # find a place to put our leaves for i in range(len(strList)): mt[i + treeLength] = someHash(strList[i]) # TODO: need to change strList[i] from a string to an integer here. for i in range(treeLength - 1, 0, -1): # 1, 2, 3, ..., treeLength - 1 # mt[i] = someHash(''.join([chr(ord(a) ^ ord(b)) for a, b in zip(mt[i*2], mt[i*2+1])])) # XOR is commutative mt[i] = someHash(mt[i*2] + mt[i*2+1]) # concat is not commutative return mt def getMerkleBranch(index, mt): res = [] t = index + (len(mt) >> 1) while t > 1: res.append(mt[t ^ 1]) # we are picking up the sibling t /= 2 return res def merkleVerify(val, rootHash, branch, someHash, index): # index has information on whether we are facing a left sibling or a right sibling tmp = someHash(val) tindex = index for br in branch: tmp = someHash((tindex & 1) and br + tmp or tmp + br) tindex >>= 1 if tmp != rootHash: print "Verification failed with", someHash(val), rootHash, branch, tmp == rootHash return tmp == rootHash def Listener(): opinions = [defaultdict(lambda: 0) for _ in range(N)] rootHashes = dict() readyCounter = [defaultdict(lambda: 0) for _ in range(N)] signed = [False]*N readySent = [False] * N reconstDone = [False] * N while True: # main loop sender, msgBundle = receive() if msgBundle[0] == 'i' and not signed[sender]: if keys[sender].verify(sha1hash(''.join([msgBundle[1][0], msgBundle[1][1], ''.join(msgBundle[1][2])])), msgBundle[2]): assert isinstance(msgBundle[1], tuple) if not merkleVerify(msgBundle[1][0], msgBundle[1][1], msgBundle[1][2], coolSHA256Hash, pid): continue if sender in rootHashes: if rootHashes[sender]!= msgBundle[1][1]: print "Cheating caught, exiting" sys.exit(0) else: rootHashes[sender] = msgBundle[1][1] newBundle = (sender, msgBundle[1][0], msgBundle[1][1], msgBundle[1][2]) # assert each frag has a length of step broadcast(('e', newBundle, keys[pid].sign( sha1hash(''.join([str(newBundle[0]), newBundle[1], newBundle[2], ''.join(newBundle[3])])) ))) signed[sender] = True else: raise ECDSASignatureError() elif msgBundle[0] == 'e': if keys[sender].verify(sha1hash(''.join([str(msgBundle[1][0]), msgBundle[1][1], msgBundle[1][2], ''.join(msgBundle[1][3])])), msgBundle[2]): originBundle = msgBundle[1] if not merkleVerify(originBundle[1], originBundle[2], originBundle[3], coolSHA256Hash, sender): continue if originBundle[0] in rootHashes: if rootHashes[originBundle[0]]!= originBundle[2]: print "Cheating caught, exiting" sys.exit(0) else: rootHashes[originBundle[0]] = originBundle[2] opinions[originBundle[0]][sender] = originBundle[1] # We are going to move this part to kekeketktktktk if len(opinions[originBundle[0]]) >= Threshold2 and not readySent[originBundle[0]]: readySent[originBundle[0]] = True broadcast(('r', originBundle[0], originBundle[2])) # We are broadcasting its hash else: raise ECDSASignatureError() elif msgBundle[0] == 'r': readyCounter[msgBundle[1]][msgBundle[2]] += 1 tmp = readyCounter[msgBundle[1]][msgBundle[2]] if tmp >= t+1 and not readySent[msgBundle[1]]: readySent[msgBundle[1]] = True broadcast(('r', msgBundle[1], msgBundle[2])) if tmp >= Threshold2 and not outputs[msgBundle[1]].full() and \ not reconstDone[msgBundle[1]] and len(opinions[msgBundle[1]]) >= Threshold: reconstDone[msgBundle[1]] = True if msgBundle[1] in rootHashes: if rootHashes[msgBundle[1]]!= msgBundle[2]: print "Cheating caught, exiting" sys.exit(0) else: rootHashes[msgBundle[1]] = msgBundle[2] if opinions[msgBundle[1]].values()[0] == '': reconstruction = [''] else: reconstruction = zfecDecoder.decode(opinions[msgBundle[1]].values()[:Threshold], opinions[msgBundle[1]].keys()[:Threshold]) # We only take the first [Threshold] fragments rawbuf = ''.join(reconstruction) buf = rawbuf[:-ord(rawbuf[-1])] # Check root hash step = len(buf) / Threshold + 1 # len(buf) % Threshold == 0 and len(buf) / Threshold or (len(buf) / Threshold + 1) assert step * Threshold - len(buf) < 256 # assumption buf_ = buf.ljust(step * Threshold - 1, '\xFF') + chr(step * Threshold - len(buf)) fragList = [buf_[i*step : (i+1)*step] for i in range(Threshold)] encodedFragList = zfecEncoder.encode(fragList) mt = merkleTree(encodedFragList, coolSHA256Hash) assert rootHashes[msgBundle[1]] == mt[1] # full binary tree if outputs[msgBundle[1]].empty(): outputs[msgBundle[1]].put(buf) greenletPacker(Greenlet(Listener), 'multiSigBr.Listener', (pid, N, t, msg, broadcast, receive, outputs)).start() buf = msg # We already assumed the proposals are byte strings step = len(buf) / Threshold + 1 # len(buf) % Threshold == 0 and len(buf) / Threshold or (len(buf) / Threshold + 1) assert step * Threshold - len(buf) < 256 # assumption buf = buf.ljust(step * Threshold - 1, '\xFF') + chr(step * Threshold - len(buf)) fragList = [buf[i*step : (i+1)*step] for i in range(Threshold)] encodedFragList = zfecEncoder.encode(fragList) mt = merkleTree(encodedFragList, coolSHA256Hash) rootHash = mt[1] # full binary tree for i in range(N): mb = getMerkleBranch(i, mt) # notice that index starts from 1 and pid starts from 0 newBundle = (encodedFragList[i], rootHash, mb) send(i, ('i', newBundle, keys[pid].sign(sha1hash(''.join([newBundle[0], newBundle[1], ''.join(newBundle[2])])))))