예제 #1
0
int main(int argc, char** argv)
{
	string* addr = new string("127.0.0.1");
	uint16_t port = 7766;

	//Determines whether the program is executed in the sender or receiver role
	m_nPID = atoi(argv[1]);
	//the number of OTs that are performed. Has to be initialized to a certain minimum size due to
	uint64_t numOTs = 1000000;
	//bitlength of the values that are transferred - NOTE that when bitlength is not 1 or a multiple of 8, the endianness has to be observed
	uint32_t bitlength = 8;

	uint32_t runs = 1;

	//Use elliptic curve cryptography in the base-OTs
	m_eFType = ECC_FIELD;
	//The symmetric security parameter (80, 112, 128)
	uint32_t m_nSecParam = 128;

	//Number of threads that will be used in OT extension
	m_nNumOTThreads = 1;

	//Specifies which OT flavor should be used
	snd_ot_flavor stype = Snd_OT;
	rec_ot_flavor rtype = Rec_OT;


	m_nBaseOTs = 190;
	m_nChecks = 380;

	m_bUseMinEntCorAssumption = false;

	m_eProt = IKNP;

	read_test_options(&argc, &argv, &m_nPID, &numOTs, &bitlength, &m_nSecParam, addr, &port, &m_eProt, &stype, &rtype,
			&m_nNumOTThreads, &m_nBaseOTs, &m_nChecks, &m_bUseMinEntCorAssumption, &runs);

	/*int32_t read_test_options(int32_t* argcp, char*** argvp, uint32_t* role, uint64_t* numots, uint32_t* bitlen,
			uint32_t* secparam, string* address, uint16_t* port, ot_ext_prot* protocol, snd_ot_flavor* sndflav,
			rec_ot_flavor* rcvflav, uint32_t* nthreads, uint32_t* nbaseots, uint32_t* nchecks, bool* usemecr, uint32_t* runs) {*/

	crypto *crypt = new crypto(m_nSecParam, (uint8_t*) m_cConstSeed[m_nPID]);


	if(m_nPID == SERVER_ID) //Play as OT sender
	{
		InitOTSender(addr->c_str(), port, crypt);

		CBitVector delta, X1, X2;

		//The masking function with which the values that are sent in the last communication step are processed
		m_fMaskFct = new XORMasking(bitlength, delta);

		//creates delta as an array with "numOTs" entries of "bitlength" bit-values and fills delta with random values
		delta.Create(numOTs, bitlength, crypt);

		//Create X1 and X2 as two arrays with "numOTs" entries of "bitlength" bit-values and resets them to 0
		X1.Create(numOTs, bitlength, crypt);
		X2.Create(numOTs, bitlength, crypt);

#ifndef BATCH
		cout << getProt(m_eProt) << " Sender performing " << numOTs << " " << getSndFlavor(stype) << " / " <<
				getRecFlavor(rtype) << " extensions on " << bitlength << " bit elements with " <<	m_nNumOTThreads << " threads, " <<
				getFieldType(m_eFType) << " and" << (m_bUseMinEntCorAssumption ? "": " no" ) << " min-ent-corr-robustness " <<
				runs << " times" << endl;
#endif
		for(uint32_t i = 0; i < runs; i++) {
			ObliviouslySend(X1, X2, numOTs, bitlength, stype, rtype, crypt);
		}
	}
	else //Play as OT receiver
	{
		InitOTReceiver(addr->c_str(), port, crypt);

		CBitVector choices, response;

		//The masking function with which the values that are sent in the last communication step are processed
		m_fMaskFct = new XORMasking(bitlength);

		//Create the bitvector choices as a bitvector with numOTs entries
		choices.Create(numOTs, crypt);

		//Pre-generate the respose vector for the results
		response.Create(numOTs, bitlength);
		response.Reset();

		/* 
		 * The inputs of the receiver in G_OT, C_OT and R_OT are the same. The only difference is the version
		 * variable that has to match the version of the sender. 
		*/
#ifndef BATCH
		cout << getProt(m_eProt) << " Receiver performing " << numOTs << " " << getSndFlavor(stype) << " / " <<
				getRecFlavor(rtype) << " extensions on " << bitlength << " bit elements with " <<	m_nNumOTThreads << " threads, " <<
				getFieldType(m_eFType) << " and" << (m_bUseMinEntCorAssumption ? "": " no" ) << " min-ent-corr-robustness " <<
				runs << " times" << endl;
#endif
		for(uint32_t i = 0; i < runs; i++) {
			ObliviouslyReceive(choices, response, numOTs, bitlength, stype, rtype, crypt);
		}
	}

	//Cleanup();
	delete crypt;

	return 1;
}
예제 #2
0
int main(int argc, char* argv[]) {

	//----------------------

#ifndef DEBUG
	srand(time(NULL));
	srand_sse(time(NULL));
#else
	srand(1);
	srand_sse(1111);
#endif

	if (argc < 3) {
		printf("Usage: %s <scd file name> <port> \n", argv[0]);
		return -1;
	}

	int port = atoi(argv[2]);
	int connfd = server_init(port);
	if (connfd == -1) {
		printf("Something's wrong with the socket!\n");
		return -1;
	}

#define GARBLING

#ifndef GARBLING
	server_close(connfd);
	return 0;

#else
	//----------------------------------------- Garbling
	GarbledCircuit garbledCircuit;
	long i, j, cid;

	readCircuitFromFile(&garbledCircuit, argv[1]);

	printf("garbledCircuit.I[0] = %d\n", garbledCircuit.I[0]);

	int n = garbledCircuit.n;
	int g = garbledCircuit.g;
	int p = garbledCircuit.p;
	int m = garbledCircuit.m;
	int c = garbledCircuit.c;
	int e = n - g;

	int *garbler_inputs = (int *) malloc(sizeof(int) * (g) * c);
	block *inputLabels = (block *) malloc(sizeof(block) * 2 * n * c);
	block *initialDFFLable = (block *) malloc(sizeof(block) * 2 * p);
	block *outputLabels = (block *) malloc(sizeof(block) * 2 * m * c);

	printf("\n\ninputs:\n");
	for (cid = 0; cid < c; cid++) {
		for (j = 0; j < g; j++) {
			garbler_inputs[cid * g + j] = rand() % 2;
			printf("%d ", garbler_inputs[cid * g + j]);
		}
	}
	printf("\n\n");

#ifndef DEBUG
	block R = randomBlock();
	*((short *) (&R)) |= 1;
#else
	block R = makeBlock((long )(-1), (long )(-1));
#endif
	uint8_t * rptr = (uint8_t*) &R;
	for (int i = 0; i < 16; i++)
		rptr[i] = 0xff;

//	*((short *) (&R)) |= 1;
	rptr[0] |= 1;

	createInputLabels(inputLabels, R, n * c);
	createInputLabels(initialDFFLable, R, p);

	///--------------------------------------------------------------- OT Extension
	//Parameters
	int numOTs = c * e;
	int bitlength = 128;
	m_nSecParam = 128;
	m_nNumOTThreads = 1;
	BYTE version;
	crypto *crypt = new crypto(m_nSecParam, (uint8_t*) m_vSeed);
	InitOTSender(connfd, crypt);
	CBitVector delta, X1, X2;

	delta.Create(numOTs, bitlength, crypt);
	m_fMaskFct = new XORMasking(bitlength, delta);
	for (int i=0;i<numOTs;i++)
		delta.SetBytes(rptr, i*16, 16);

	printf("Delta: ");
	for (int i = 0; i < 16; i++) {
		printf("%02x", delta.GetByte(i));
	}
	printf("\n");

	printf("R: ");
	print__m128i(R);
	printf("\n");



	X1.Create(numOTs, bitlength);
	X1.Reset();
	X2.Create(numOTs, bitlength);
	X2.Reset();
	uint8_t* b = new BYTE[16];
	BYTE* b2 = new BYTE[16];

	cout << "Sender performing " << numOTs << " C_OT extensions on "
			<< bitlength << " bit elements" << endl;

	version = C_OT;
	ObliviouslySend(X1, X2, numOTs, bitlength, version, crypt);



	//putting X1 & X2 into inputLabels
	printf("printing inputLabels after copy from X1 and X2:\n\n");
	uint8_t* inputLabelsptr;
	for (cid = 0; cid < c; cid++) {
		for (j = 0; j < e; j++) {
			inputLabelsptr = (uint8_t*) &inputLabels[2 * (cid * n + g + j)];
			X1.GetBytes(inputLabelsptr, 16*(cid * e + j), 16);
			print__m128i(inputLabels[2 * (cid * n + g + j)]);

			inputLabelsptr = (uint8_t*) &inputLabels[2 * (cid * n + g + j) + 1];
			X2.GetBytes(inputLabelsptr, 16*(cid * e + j), 16);
			print__m128i(inputLabels[2 * (cid * n + g + j) + 1]);

		}
	}

	//print
	printf("Printing X1:\n");
	for (int j = 0; j < numOTs; j++) {
		for (int i = 0; i < 16; i++) {
			b[i] = X1.GetByte(i + j * 16);
			printf("%02x", b[i]);
		}
		printf("\n");
	}
	printf("\n\n");
	printf("Printing X2:\n");
	for (int j = 0; j < numOTs; j++) {
		for (int i = 0; i < 16; i++) {
			b[i] = X2.GetByte(i + j * 16);
			printf("%02x", b[i]);
		}
		printf("\n");
	}
	printf("\n\n");
	printf("Printing delta:\t");
	for (int i = 0; i < 16; i++) {
		b[i] = delta.GetByte(i);
		printf("%02x", b[i]);
	}
	printf("\n");

//----------------------------------------------------end of OT Extension

	for (cid = 0; cid < c; cid++) {
		for (j = 0; j < g; j++) {
			if (garbler_inputs[cid * g + j] == 0)
				send_block(connfd, inputLabels[2 * (cid * n + j)]);
			else
				send_block(connfd, inputLabels[2 * (cid * n + j) + 1]);

			printf("i(%ld, %ld, %d)\n", cid, j, garbler_inputs[cid * g + j]);
			print__m128i(inputLabels[2 * (cid * n + j)]);
			print__m128i(inputLabels[2 * (cid * n + j) + 1]);
		}

		//------------------------------------------------------------------------------------------ CHANGE 1
		for (j = 0; j < e; j++) {
//			int ev_input;
//			read(connfd, &ev_input, sizeof(int));
//			if (!ev_input)
//				send_block(connfd, inputLabels[2 * (cid * n + g + j)]);
//			else
//				send_block(connfd, inputLabels[2 * (cid * n + g + j) + 1]);

			printf("evaluator : i(%ld, %ld, ?)\n", cid, j);
			print__m128i(inputLabels[2 * (cid * n + g + j)]);
			print__m128i(inputLabels[2 * (cid * n + g + j) + 1]);

		}

		printf("Compare to:   ");

		printf("\n");
		//----------------------------------------------------------------------end

	}
	printf("\n\n");

	for (j = 0; j < p; j++) //p:#DFF
			{
		printf("garbledCircuit.I[j] = %d\n", garbledCircuit.I[j]);
		if (garbledCircuit.I[j] == CONST_ZERO) // constant zero
		{
			send_block(connfd, initialDFFLable[2 * j]);
			printf("dffi(%ld, %ld, %d)\n", cid, j, 0);
			print__m128i(initialDFFLable[2 * j]);
			print__m128i(initialDFFLable[2 * j + 1]);

		} else if (garbledCircuit.I[j] == CONST_ONE) // constant zero
		{
			send_block(connfd, initialDFFLable[2 * j + 1]);
			printf("dffi(%ld, %ld, %d)\n", cid, j, 0);
			print__m128i(inputLabels[2 * j]);
			print__m128i(inputLabels[2 * j + 1]);

		} else if (garbledCircuit.I[j] < g) //belongs to Alice (garbler)
				{
			int index = garbledCircuit.I[j];

			if (garbler_inputs[index] == 0)
				send_block(connfd, initialDFFLable[2 * j]);
			else
				send_block(connfd, initialDFFLable[2 * j + 1]);

			printf("dffi(%ld, %ld, %d)\n", cid, j, garbler_inputs[index]);
			print__m128i(initialDFFLable[2 * j]);
			print__m128i(initialDFFLable[2 * j + 1]);

		}
		//------------------------------------------------------------------------------------------ CHANGE 2
		else //**** belongs to Bob
		{
//			int ev_input;
//			read(connfd, &ev_input, sizeof(int));
//			if (!ev_input)
//				send_block(connfd, initialDFFLable[2 * j]);
//			else
//				send_block(connfd, initialDFFLable[2 * j + 1]);

//			printf("dffi(%ld, %ld, %d)\n", cid, j, ev_input);
			print__m128i(initialDFFLable[2 * j]);
			print__m128i(initialDFFLable[2 * j + 1]);
			printf("\n");
		}
		//----------------------------------------------------------------------end
	}
	printf("\n\n");

	///--------------------------------------------------------------- OT Extension
		//Parameters
		numOTs = p;
		delta.Create(numOTs, bitlength, crypt);
		m_fMaskFct = new XORMasking(bitlength, delta);
		for (int i=0;i<numOTs;i++)
			delta.SetBytes(rptr, i*16, 16);

		printf("Delta: ");
		for (int i = 0; i < 16; i++) {
			printf("%02x", delta.GetByte(i));
		}
		printf("\n");

		printf("R: ");
		print__m128i(R);
		printf("\n");



		X1.Create(numOTs, bitlength);
		X1.Reset();
		X2.Create(numOTs, bitlength);
		X2.Reset();


		cout << "Sender performing " << numOTs << " C_OT extensions on "
				<< bitlength << " bit elements" << endl;

		version = C_OT;
		ObliviouslySend(X1, X2, numOTs, bitlength, version, crypt);



		//putting X1 & X2 into inputLabels
		printf("printing inputLabels after copy from X1 and X2:\n\n");


			for (j = 0; j < p; j++) {
				inputLabelsptr = (uint8_t*) &initialDFFLable[2 * j];
				X1.GetBytes(inputLabelsptr, 16*(j), 16);


				inputLabelsptr = (uint8_t*) &initialDFFLable[2 * j +1];
				X2.GetBytes(inputLabelsptr, 16*( j), 16);
			}

		delete crypt;
	//----------------------------------------------------end of OT Extension



	garbledCircuit.globalKey = randomBlock();
	send_block(connfd, garbledCircuit.globalKey); // send DKC key

	printf("R: ");
	print__m128i(R);
	printf("\n");

	garble(&garbledCircuit, inputLabels, initialDFFLable, outputLabels, &R,
			connfd);

	printf("***************** InputLabels\n");
	for (int i=0;i<n*c*2;i++)
		print__m128i(inputLabels[i]);

	for (cid = 0; cid < c; cid++) {
		for (i = 0; i < m; i++) {
			short outputType = getLSB(outputLabels[2 * (m * cid + i) + 0]);
			send_type(connfd, outputType);
		}
	}

	server_close(connfd);
	removeGarbledCircuit(&garbledCircuit);

	return 0;
#endif
}