Esempio n. 1
0
BOOL NNOBOTExtSnd::sender_routine(uint32_t id, uint64_t myNumOTs) {
	uint64_t myStartPos = id * myNumOTs;
	uint64_t wd_size_bits = m_nBlockSizeBits;
	uint64_t processedOTBlocks = min((uint64_t) NUMOTBLOCKS, ceil_divide(myNumOTs, wd_size_bits));
	uint64_t OTsPerIteration = processedOTBlocks * wd_size_bits;
	uint64_t tmpctr, tmpotlen;
	uint32_t nchans = 2;
	bool use_mat_chan = (m_eSndOTFlav == Snd_GC_OT || m_bUseMinEntCorRob);
	if(use_mat_chan) {
		nchans = 3;
	}

	channel* ot_chan = new channel(nchans*id, m_cRcvThread, m_cSndThread);
	channel* check_chan = new channel(nchans*id + 1, m_cRcvThread, m_cSndThread);
	channel* mat_chan;
	if(use_mat_chan) {
		mat_chan = new channel(nchans*id+2, m_cRcvThread, m_cSndThread);
	}

	myNumOTs = min(myNumOTs + myStartPos, m_nOTs) - myStartPos;
	uint64_t lim = myStartPos + myNumOTs;
	uint64_t** rndmat;

	// The vector with the received bits
	CBitVector vRcv(m_nBaseOTs * OTsPerIteration);
	vRcv.Reset();

	// Holds the reply that is sent back to the receiver
	uint32_t numsndvals = 2;
	CBitVector* vSnd;

	CBitVector* seedbuf = new CBitVector[m_nSndVals];
	for (uint32_t u = 0; u < m_nSndVals; u++)
		seedbuf[u].Create(OTsPerIteration * m_cCrypt->get_aes_key_bytes() * 8);
#ifdef ZDEBUG
	cout << "seedbuf size = " <<OTsPerIteration * AES_KEY_BITS << endl;
#endif
	vSnd = new CBitVector[numsndvals];
	for (uint32_t i = 0; i < numsndvals; i++) {
		vSnd[i].Create(OTsPerIteration * m_nBitLength);
	}

	// Contains the parts of the V matrix
	CBitVector Q(wd_size_bits * OTsPerIteration);
	mask_buf_t tmpmaskbuf;

	uint64_t OT_ptr = myStartPos;

	uint8_t *rcvbuftmpptr, *rcvbufptr;

	queue<nnob_snd_check_t> check_queue;
	queue<mask_buf_t> mask_queue;

	uint32_t startpos = 0;
	if(m_eRecOTFlav==Rec_R_OT) {
		startpos = 1;
	}

	if(m_eSndOTFlav == Snd_GC_OT) {
		initRndMatrix(&rndmat, m_nBitLength, m_nBaseOTs);
	}

#ifdef OTTiming
	double totalMtxTime = 0, totalTnsTime = 0, totalHshTime = 0, totalRcvTime = 0, totalSndTime = 0, totalUnMaskTime = 0,
			totalHashCheckTime = 0;
	timeval tempStart, tempEnd;
#endif

	while (OT_ptr < lim) //do while there are still transfers missing
	{
		processedOTBlocks = min((uint64_t) NUMOTBLOCKS, ceil_divide(lim - OT_ptr, wd_size_bits));
		OTsPerIteration = processedOTBlocks * wd_size_bits;

#ifdef ZDEBUG
		cout << "Processing block " << nProgress << " with length: " << OTsPerIteration << ", and limit: " << lim << endl;
#endif

#ifdef OTTiming
		gettimeofday(&tempStart, NULL);
#endif
		rcvbufptr = ot_chan->blocking_receive_id_len(&rcvbuftmpptr, &tmpctr, &tmpotlen);
		//vRcv.AttachBuf(rcvbuftmpptr, bits_in_bytes(m_nBaseOTs * OTsPerIteration));
		vRcv.SetBytes(rcvbuftmpptr, bits_in_bytes(OTsPerIteration*startpos), bits_in_bytes((m_nBaseOTs-startpos)*OTsPerIteration));
		free(rcvbufptr);
		//vRcv.PrintHex();
#ifdef OTTiming
		gettimeofday(&tempEnd, NULL);
		totalRcvTime += getMillies(tempStart, tempEnd);
		gettimeofday(&tempStart, NULL);
#endif
		BuildQMatrix(Q, OT_ptr, processedOTBlocks);
#ifdef OTTiming
		gettimeofday(&tempEnd, NULL);
		totalMtxTime += getMillies(tempStart, tempEnd);
		gettimeofday(&tempStart, NULL);
#endif
		check_queue.push(UpdateCheckBuf(Q.GetArr(), vRcv.GetArr(), OT_ptr, processedOTBlocks, check_chan));
		FillAndSendRandomMatrix(rndmat, mat_chan);
#ifdef OTTiming
		gettimeofday(&tempEnd, NULL);
		totalHashCheckTime += getMillies(tempStart, tempEnd);
		gettimeofday(&tempStart, NULL);
#endif
		UnMaskBaseOTs(Q, vRcv, processedOTBlocks);

		GenerateSendAndXORCorRobVector(Q, OTsPerIteration, mat_chan);

#ifdef OTTiming
		gettimeofday(&tempEnd, NULL);
		totalUnMaskTime += getMillies(tempStart, tempEnd);
		gettimeofday(&tempStart, NULL);
#endif
		Q.Transpose(wd_size_bits, OTsPerIteration);
#ifdef OTTiming
		gettimeofday(&tempEnd, NULL);
		totalTnsTime += getMillies(tempStart, tempEnd);
		gettimeofday(&tempStart, NULL);
#endif
		HashValues(Q, seedbuf, vSnd, OT_ptr, min(lim - OT_ptr, OTsPerIteration), rndmat);
#ifdef OTTiming
		gettimeofday(&tempEnd, NULL);
		totalHshTime += getMillies(tempStart, tempEnd);
		gettimeofday(&tempStart, NULL);
#endif

		//TODO: outsource into method
		tmpmaskbuf.otid = OT_ptr;
		tmpmaskbuf.otlen = min(lim - OT_ptr, OTsPerIteration);
		tmpmaskbuf.maskbuf = new CBitVector[numsndvals];
		for(uint32_t i = 0; i < numsndvals; i++)
			tmpmaskbuf.maskbuf[i].Copy(vSnd[i]);
		mask_queue.push(tmpmaskbuf);

		if(check_chan->data_available()) {
			assert(CheckConsistency(&check_queue, check_chan));//TODO assert
			tmpmaskbuf = mask_queue.front();
			mask_queue.pop();
			MaskAndSend(tmpmaskbuf.maskbuf, tmpmaskbuf.otid, tmpmaskbuf.otlen, ot_chan);
		}
#ifdef OTTiming
		gettimeofday(&tempEnd, NULL);
		totalSndTime += getMillies(tempStart, tempEnd);
#endif
		OT_ptr += min(lim - OT_ptr, OTsPerIteration);
		Q.Reset();

		//free(rcvbufptr);
	}

	while(!check_queue.empty()) {
		if(check_chan->data_available()) {
			assert(CheckConsistency(&check_queue, check_chan));//TODO assert
			tmpmaskbuf = mask_queue.front();
			mask_queue.pop();
			MaskAndSend(tmpmaskbuf.maskbuf, tmpmaskbuf.otid, tmpmaskbuf.otlen, ot_chan);
		}
	}

	//vRcv.delCBitVector();
	ot_chan->synchronize_end();
	check_chan->synchronize_end();

	Q.delCBitVector();
	for (uint32_t u = 0; u < m_nSndVals; u++)
		seedbuf[u].delCBitVector();

	for (uint32_t i = 0; i < numsndvals; i++)
		vSnd[i].delCBitVector();
	if (numsndvals > 0)
		free(vSnd);

	if(use_mat_chan) {
		mat_chan->synchronize_end();
	}

	if(m_eSndOTFlav == Snd_GC_OT) {
		freeRndMatrix(rndmat, m_nBaseOTs);
	}
#ifdef OTTiming
	cout << "Sender time benchmark for performing " << myNumOTs << " OTs on " << m_nBitLength << " bit strings" << endl;
	cout << "Time needed for: " << endl;
	cout << "\t Matrix Generation:\t" << totalMtxTime << " ms" << endl;
	cout << "\t BaseOT Unmasking:\t" << totalUnMaskTime << " ms" << endl;
	cout << "\t Check Hashing:\t" << totalHashCheckTime << " ms" << endl;
	cout << "\t Sending Matrix:\t" << totalSndTime << " ms" << endl;
	cout << "\t Transposing Matrix:\t" << totalTnsTime << " ms" << endl;
	cout << "\t Hashing Matrix:\t" << totalHshTime << " ms" << endl;
	cout << "\t Receiving Values:\t" << totalRcvTime << " ms" << endl;
#endif


	delete ot_chan;
	delete check_chan;
	return TRUE;
}
Esempio n. 2
0
BOOL KKOTExtSnd::sender_routine(uint32_t id, uint64_t myNumOTs) {
    uint32_t choicecodebitlen = ceil_log2(m_nSndVals);
    uint64_t myStartPos = id * myNumOTs;
    uint64_t myStartPos1ooN = ceil_divide(myStartPos, choicecodebitlen);

    uint64_t wd_size_bits = m_nBlockSizeBits;
    uint64_t processedOTBlocks =
        min((uint64_t)NUMOTBLOCKS, ceil_divide(myNumOTs, wd_size_bits));
    uint64_t OTsPerIteration = processedOTBlocks * wd_size_bits;
    channel *chan = new channel(id, m_cRcvThread, m_cSndThread);
    uint64_t tmpctr, tmpotlen;
    uint64_t **rndmat;
    uint64_t processedOTs;

    myNumOTs = min(myNumOTs + myStartPos, m_nOTs) - myStartPos;
    // TODO some re-formating of myNumOTs due to 1ooN OT
    uint64_t lim = myStartPos1ooN + ceil_divide(myNumOTs, choicecodebitlen);

    if (myStartPos1ooN * choicecodebitlen > m_nOTs) {
        cerr << "Thread " << id
             << " not doing any work to align to window size " << endl;
        return true;
    }

// The vector with the received bits
#ifdef GENERATE_T_EXPLICITELY
    CBitVector vRcv(2 * m_nBaseOTs * OTsPerIteration);
#else
    CBitVector vRcv(m_nBaseOTs * OTsPerIteration);
#endif

    // Holds the reply that is sent back to the receiver
    CBitVector *vSnd;

    CBitVector *seedbuf = new CBitVector[m_nSndVals];
    for (uint32_t u = 0; u < m_nSndVals; u++) {
        seedbuf[u].Create(OTsPerIteration * m_cCrypt->get_aes_key_bytes() * 8);
    }
#ifdef ZDEBUG
    cout << "seedbuf size = " << OTsPerIteration *AES_KEY_BITS << endl;
#endif
    vSnd = new CBitVector[m_nSndVals];
    for (uint32_t i = 0; i < m_nSndVals; i++) {
        vSnd[i].Create(OTsPerIteration * choicecodebitlen * m_nBitLength);
    }

    // Contains the parts of the V matrix
    CBitVector Q(wd_size_bits * OTsPerIteration);

    uint64_t otid = myStartPos1ooN;

    uint8_t *rcvbuftmpptr, *rcvbufptr;

#ifdef OTTiming
    double totalMtxTime = 0, totalTnsTime = 0, totalHshTime = 0,
           totalRcvTime = 0, totalSndTime = 0, totalUnMaskTime = 0;
    timeval tempStart, tempEnd;
#endif

    if (m_eSndOTFlav == Snd_GC_OT) {
        uint8_t *rnd_seed = (uint8_t *)malloc(m_nSymSecParam);
        m_cCrypt->gen_rnd(rnd_seed, m_nSymSecParam);
        chan->send(rnd_seed, m_nSymSecParam);
        initRndMatrix(&rndmat, m_nBitLength, m_nBaseOTs);
        fillRndMatrix(rnd_seed, rndmat, m_nBitLength, m_nBaseOTs, m_cCrypt);
        free(rnd_seed);
    }

    while (otid < lim) // do while there are still transfers missing
    {
        processedOTBlocks =
            min((uint64_t)NUMOTBLOCKS, ceil_divide(lim - otid, wd_size_bits));
        OTsPerIteration = processedOTBlocks * wd_size_bits;
        processedOTs = min(lim - otid, OTsPerIteration);

#ifdef ZDEBUG
        cout << "Processing block " << nProgress
             << " with length: " << OTsPerIteration << ", and limit: " << lim
             << endl;
#endif

#ifdef OTTiming
        gettimeofday(&tempStart, NULL);
#endif
        ReceiveMasks(vRcv, chan, OTsPerIteration);

#ifdef OTTiming
        gettimeofday(&tempEnd, NULL);
        totalRcvTime += getMillies(tempStart, tempEnd);
        gettimeofday(&tempStart, NULL);
#endif
        BuildQMatrix(Q, otid, processedOTBlocks);
#ifdef OTTiming
        gettimeofday(&tempEnd, NULL);
        totalMtxTime += getMillies(tempStart, tempEnd);
        gettimeofday(&tempStart, NULL);
#endif
        UnMaskBaseOTs(Q, vRcv, processedOTBlocks);
#ifdef OTTiming
        gettimeofday(&tempEnd, NULL);
        totalUnMaskTime += getMillies(tempStart, tempEnd);
        gettimeofday(&tempStart, NULL);
#endif
        Q.Transpose(wd_size_bits, OTsPerIteration);
#ifdef OTTiming
        gettimeofday(&tempEnd, NULL);
        totalTnsTime += getMillies(tempStart, tempEnd);
        gettimeofday(&tempStart, NULL);
#endif
        KKHashValues(Q, seedbuf, vSnd, otid, processedOTs, rndmat);
#ifdef OTTiming
        gettimeofday(&tempEnd, NULL);
        totalHshTime += getMillies(tempStart, tempEnd);
        gettimeofday(&tempStart, NULL);
#endif
        KKMaskAndSend(vSnd, otid, processedOTs, chan);
#ifdef OTTiming
        gettimeofday(&tempEnd, NULL);
        totalSndTime += getMillies(tempStart, tempEnd);
#endif
        otid += processedOTs;
        Q.Reset();
    }

    vRcv.delCBitVector();
    chan->synchronize_end();

    Q.delCBitVector();

    for (uint32_t i = 0; i < m_nSndVals; i++) {
        vSnd[i].delCBitVector();
    }
    delete vSnd;

    if (m_eSndOTFlav == Snd_GC_OT) {
        freeRndMatrix(rndmat, m_nBaseOTs);
    }

#ifdef OTTiming
    cout << "Sender time benchmark for performing " << myNumOTs << " OTs ("
         << lim - myStartPos1ooN << " 1ooN) on " << m_nBitLength
         << " bit strings" << endl;
    cout << "Time needed for: " << endl;
    cout << "\t Matrix Generation:\t" << totalMtxTime << " ms" << endl;
    cout << "\t Unmasking values:\t" << totalUnMaskTime << " ms" << endl;
    cout << "\t Sending Matrix:\t" << totalSndTime << " ms" << endl;
    cout << "\t Transposing Matrix:\t" << totalTnsTime << " ms" << endl;
    cout << "\t Hashing Matrix:\t" << totalHshTime << " ms" << endl;
    cout << "\t Receiving Values:\t" << totalRcvTime << " ms" << endl;
#endif

    delete chan;

    return TRUE;
}