Example #1
0
int createInputLabels(InputLabels inputLabels, int n) {
	int i;
	block R = randomBlock();
	*((short *) (&R)) = 1;
	char temp[20];
	char temp2[20];
	long *tempS = temp + 10;
	long *temp2S = temp2 + 10;

	for (i = 0; i < 2 * n; i += 2) {
		*((block *) temp) = randomBlock();
		*((block *) temp2) = xorBlocks(*((block *)temp), R);
		tempS[0] = 0;
		temp2S[0] = 0;
		/*		temp[10] = '\0';
		 temp[11] = '\0';
		 temp[12] = '\0';
		 temp[13] = '\0';
		 temp[14] = '\0';
		 temp[15] = '\0';
		 temp2[10] = '\0';
		 temp2[11] = '\0';
		 temp2[12] = '\0';
		 temp2[13] = '\0';
		 temp2[14] = '\0';
		 temp2[15] = '\0';*/
		inputLabels[i] = *((block *) temp);
		inputLabels[i + 1] = *((block *) temp2);
	}
	return 0;

}
Example #2
0
int createInputLabels(InputLabels inputLabels, int n) {
	int i;
	block R = randomBlock();
	*((short *) (&R)) = 1;
	for (i = 0; i < 2 * n; i += 2) {
		inputLabels[i] = randomBlock();
		inputLabels[i + 1] = xorBlocks(R, inputLabels[i]);
	}
	return 0;

}
int fixedOneWire(GarbledCircuit *garbledCircuit,
		GarblingContext *garblingContext) {
	int ind = getNextWire(garblingContext);
	garblingContext->fixedWires[ind] = FIXED_ONE_GATE;
	Wire *wire = &garbledCircuit->wires[ind];
	wire->id = ind;
	wire->label0 = randomBlock();
	wire->label1 = xorBlocks(garblingContext->R, wire->label0);
	return ind;
}
bool randomTest(){
	const int SAMPLE_N = 512;
	SecureRandomDevice<> randomBlock(SAMPLE_N);
	char* dummy = randomBlock.gen();
	for(int i=1;i<SAMPLE_N;i++){
		if(dummy[i]!=dummy[i-1])
			break;
		if(i==SAMPLE_N-1) //create a robust test? Ain't nobody got time for dat? lol.
			return false;
	}
	return true;
}
Example #5
0
int fixedZeroWire(GarbledCircuit *garbledCircuit,
		GarblingContext *garblingContext) {
	int ind = getNextWire(garblingContext);
	garblingContext->fixedWires[ind] = FIXED_ZERO_GATE;
	Wire *wire = &garbledCircuit->wires[ind];
	if (wire->id != 0)
		printf("ERROR: Reusing output at wire %d\n", ind);
	wire->id = ind;
	wire->label0 = randomBlock();
	wire->label1 = xorBlocks(garblingContext->R, wire->label0);
	return ind;

}
Example #6
0
int startBuilding(GarbledCircuit *garbledCircuit,
		GarblingContext *garblingContext) {
	garblingContext->gateIndex = 0;
	garblingContext->tableIndex = 0;
	garblingContext->wireIndex = garbledCircuit->n + 1;
	block key = randomBlock();
	garblingContext->R =
			xorBlocks(garbledCircuit->wires[0].label0, garbledCircuit->wires[0].label1);
	garblingContext->fixedWires = (int *) malloc(
			sizeof(int) * garbledCircuit->r);
	garbledCircuit->globalKey = key;

    DKCipherInit(&key, &(garblingContext->dkCipherContext));

	return 0;
}
static bool randomBoard(Board& board) {
    for (int r = 0; r < BOARD_ROWS; ++r) {
        for (int c = 0; c < BOARD_COLS; ++c) {
            board.digits[r][c] = 0;
            board.fixed[r][c] = false;
        }
    }
    LOGI("random in the middle middle");
    // Random the board in the middle.
    if (!randomBlock(board, 3, 3)) {
        LOGI("fail in 3 3");
        return false;
    }
    if (!randomBlock(board, 3, 0)) {
        LOGI("fail in 3 0");
        return false;
    }
    if (!randomBlock(board, 3, 6)) {
        LOGI("fail in 3 6");
        return false;
    }
    if (!randomBlock(board, 0, 3)) {
        LOGI("fail in 0 3");
        return false;
    }

    if (!randomBlock(board, 0, 0)) {
        LOGI("fail in 0 0");
        return false;
    }

    if (!randomBlock(board, 0, 6)) {
        LOGI("fail in 0 6");
        return false;
    }

    /*
    if (!randomBlock(board, 6, 3)) {
        LOGI("fail in 6 3");
        return false;
    }
     */
    for (int r = 0; r < BOARD_ROWS; ++r) {
        for (int c = 0; c < BOARD_COLS; ++c) {
            LOGI("board[%d][%d] = %d", r, c, board.digits[r][c]);
        }
    }

    return true;
}
bool randomFilterTest(){
	const int SAMPLE_N = 128*1024;
	SecureRandomDevice<unsigned int> randomBlock(SAMPLE_N);
	std::vector<std::pair<std::string,std::function<bool(unsigned int&)>>> regexCases =
	{
		{ R"([a-z])", [](unsigned int& i){ return 'a'<=i&&i<='z';}},
		{ R"([A-Z])", [](unsigned int& i){ return 'A'<=i&&i<='Z';}},
		{ R"([\d!])", [](unsigned int& i){ return ('0'<=i&&i<='9')||(i=='!');}},
		{ R"([01])", [](unsigned int& i){ return i=='0'||i=='1';}},
		//{ "[]", [](unsigned int& i){ return 'A'<=i&&i<='Z';}},
	};
	for(auto& i:regexCases){
		//print message
		std::cout<<i.first<<"\t";
		//generate random block
		unsigned int* dummy = randomBlock.gen();
		//convert it into std::vector and filter it.
		std::vector<unsigned int> randomChars(dummy, dummy+SAMPLE_N);
		RandomFilter(i.first).filter<unsigned int>(randomChars);
		//If there is an character that is not included in the expression, return false.
		if(std::find_if_not(randomChars.begin(), randomChars.end(), i.second) != randomChars.end())
			return false;
	}
	return true;
}

#include "intTest.h"

int main(){
	assertWithMsg(randomTest);
	assertWithMsg(randomFilterTest);
	assertWithMsg(integrationTests);

	//assertWithMsg([](){ return false; }); //This works.

	std::cout<<"All tests were completed successfully. Hooray! :D"<<std::endl;
	return 0;
}
Example #9
0
int main()
{
    PS2Controller psData;
    static uint8 c_flag = 0;
    static uint8 right_flag = 0;
    static uint8 left_flag = 0;
    static uint8 down_flag = 0;
    static uint8 up_flag = 0;
    uint8 counter = 0;
    uint8 cmax = 50;
    
    int blocks[7][4][4] = {  //ブロックの宣言
        {
            {0,0,0,0},
            {0,1,1,0},
            {0,0,1,0},
            {0,0,1,0}
        },
        {
            {0,0,0,0},
            {0,1,1,0},
            {0,1,0,0},
            {0,1,0,0}
        },
        {
            {0,0,0,0},
            {0,1,0,0},
            {0,1,1,0},
            {0,0,1,0}
        },
        {
            {0,0,0,0},
            {0,0,1,0},
            {0,1,1,0},
            {0,1,0,0}
        },
        {
            {0,0,0,0},
            {0,0,1,0},
            {0,1,1,0},
            {0,0,1,0}
        },
        {
            {0,0,0,0},
            {0,1,1,0},
            {0,1,1,0},
            {0,0,0,0}
        },
        {
            {0,0,1,0},
            {0,0,1,0},
            {0,0,1,0},
            {0,0,1,0}
        }
    };
    
    int i, j, bflg = 0, gflg = 1;
    int block[4][4] = {}, rotated[4][4];
    signed int slidex = 0, slidey = 0;
    char string[12] = {};

	CyGlobalIntEnable;
    PS2_Start();
    CyIntSetSysVector(SysTick_IRQn + 16, timer_isr);
    SysTick_Config(0x00ffff);

	//USBUART_Start(0, USBUART_5V_OPERATION);
	//while (USBUART_GetConfiguration() == 0);
    //USBUART_CDC_Init();

    DotMat_init();
    randomBlock(blocks, block);
    paintBlock(block[4][4], slidey, slidex);
    //DotMat_paint(&Map);
    
    CyDelay(100);
     
    for (;;){
        psData = PS2_Controller_get();
        dotMatrix_print(&Map);
        dotMatrix_print(&dotMat);
        if(isr_flg){
            if(gflg){
                if(counter == cmax){
                    if(check(block, slidey - 1, slidex) == 1){
                        slidey--;
                    } else{
                        mergeMatrix(block, slidey , slidex);
                        clearRow(&cmax);
                        dotMatrix_dataToArray(&Map);
                        if(slidey == 0){
                            myprint(string);
                            gflg = 0;
                        }
                        randomBlock(blocks, block);
                        slidey = 0;
                        slidex = 0;
                    }
                    if(gflg){
                        DotMat_paint(block, slidey, slidex);
                    }
                    counter = 0;
                }
                counter++;
            } else{
                myprint(string);
            }
            isr_flg = 0;
        }
        
        if(psData.CIRCLE || psData.CROSS){
            rand();
                if(c_flag){
                    if(psData.CIRCLE){
                        rotate(block, rotated, 1);
                    }
                    if(psData.CROSS){
                        rotate(block, rotated, 2);
                    }
                    if(check(rotated, slidey , slidex) == 1){
                        if(bflg == 1){
                            slidex--;
                            bflg = 0;
                        }
                        if(bflg == 2){
                            slidex++;
                            bflg = 0;
                        }
                        rotate2block(block, rotated);
                        DotMat_paint(block, slidey, slidex);
                    }
                    if(check(rotated, slidey , slidex) == 2){
                        bflg = 1;
                        if(check(rotated, slidey , slidex + 1) == 2){
                            if(check(rotated, slidey , slidex + 2) == 1){
                                slidex++;
                            }
                        }
                        if(check(rotated, slidey , slidex + 1) == 1){
                            slidex++;
                            rotate2block(block, rotated);
                            DotMat_paint(block, slidey, slidex);
                        }
                    }
                    if(check(rotated, slidey , slidex) == 3){
                        bflg = 2;
                        if(check(rotated, slidey , slidex - 1) == 3){
                            if(check(rotated, slidey , slidex - 2) == 1){
                                slidex--;
                            }
                        }
                        if(check(rotated, slidey , slidex - 1) == 1){
                            slidex--;
                            rotate2block(block, rotated);
                            DotMat_paint(block, slidey, slidex);
                        }
                    }
                }
                c_flag = 0;
        } else{
            c_flag = 1;
        }
        
        if(psData.LEFT){
            bflg = 0;
            rand();
            if(left_flag){
                if(check(block, slidey , slidex - 1) == 1){
                    slidex--;
                    DotMat_paint(block, slidey, slidex);
                }
            }
            left_flag = 0;
        } else{
            left_flag = 1;
        }
        
        if(psData.RIGHT){
            bflg = 0;
            rand();
            if(right_flag){
                if(check(block, slidey , slidex + 1) == 1){
                    slidex++;
                    DotMat_paint(block, slidey, slidex);
                }
            }
            right_flag = 0;
        } else{
            right_flag = 1;
        }
        
        if(psData.UP){
            rand();
            if(up_flag){
                while(check(block, slidey - 1, slidex) == 1){
                    slidey--;
                }
                DotMat_paint(block, slidey, slidex);
            }
            up_flag = 0;
        } else{
            up_flag = 1;
        }
        
        if(psData.DOWN){
            rand();
            if(psData.L1){
                if(psData.R1){
                    while(check(block, slidey - 1, slidex) == 1){
                        slidey++;
                        DotMat_paint(block, slidey, slidex);
                    }
                }
            }
            if(down_flag){
                while(check(block, slidey - 1, slidex) == 1){
                    slidey--;
                    DotMat_paint(block, slidey, slidex);
                }                
            }
            down_flag = 0;
        } else{
            down_flag = 1;
        }
        
        if(psData.SELECT){
            if(psData.START){
                slidey = 0;
                slidex = 0;
                DotMat_init();
                randomBlock(blocks, block);
                paintBlock(block[4][4], slidey, slidex);
                gflg = 1;
                cmax = 50;
            }
        }
    }
}
Example #10
0
//main
int main() {
  REG_DISPCNT = MODE_3 | BG2_ENABLE;

  enum GBAState state = START;
  enum GBAState prevState = state;

  while(1) {
		waitForVblank();
		switch(state) {
		case START:
			drawImage3(0,0,SPLASH_WIDTH,SPLASH_HEIGHT,splash);
			prevState = START;
			state = NODRAW;
			break;
		case NODRAW:

			if (prevState == START) {
				if (KEY_DOWN_NOW(BUTTON_START)) {
					state = GAME;
				}
				if (KEY_DOWN_NOW(BUTTON_A)) {
					state = HELP;
				}
			}
			if (prevState == HELP) {
				if (KEY_DOWN_NOW(BUTTON_SELECT)) {
					state = START;
				}
			}
			if (prevState == GAMEOVER) {
				if (KEY_DOWN_NOW(BUTTON_SELECT)) {
					state = START;
				}
			}

			break;
		case GAME:
			drawImage3(0,0,GAME2_WIDTH,GAME2_HEIGHT,game2);

			char stuff[4] = "000\0";
		  stuff[0] = 48 + (clearedRows/100)%10;
		  stuff[1] = 48 + (clearedRows/10)%10;
		  stuff[2] = 48 + clearedRows%10;
		  drawImagePartial(25, 180, 20, 30, GAME2_WIDTH, game2);
		  drawString(25,180,stuff, WHITE);

      block curr = randomBlock();
			block next = randomBlock();
      drawBlock(curr);
			int button = 0;
      while(1) {
				delay(20);
        if (KEY_DOWN_NOW(BUTTON_SELECT)) {
					prevState = GAME;
					state = START;
          break;
        }

        block old = curr;
        tick++;
        if (collisionDetectBottom(curr) == 1) {
          rowCheck(curr);
					curr = next;
					next = randomBlock();
					if(collisionDetect(curr) == 1) {
						prevState = GAME;
						state = GAMEOVER;
						break;
					}
        } else {
          if(KEY_DOWN_NOW(BUTTON_LEFT) && collisionDetectLeft(curr) == 0){
              curr.x--;
          } else if(KEY_DOWN_NOW(BUTTON_RIGHT) && collisionDetectRight(curr) == 0){
              curr.x++;
          } else if(KEY_DOWN_NOW(BUTTON_DOWN) && collisionDetectBottom(curr) == 0){
              curr.y--;
          } else if(button == 0 && KEY_DOWN_NOW(BUTTON_UP) && rotationDetect(curr) == 0){
							button = 1;
              rotateBlock(&curr);
          } else if (tick > 20) {
            tick = 0;
            curr.y--;
          } else if (!KEY_DOWN_NOW(BUTTON_DOWN)){
						//drawString(25,70,"stuff stuff", WHITE);
						button = 0;
					}
          waitForVblank();
          eraseBlock(old);
          drawBlock(curr);
        }
      }
      clearBoard();
      drawRect(4,4,70,10, BLACK);
			break;
		case GAMEOVER:
      drawImage3(0,0,END_WIDTH,END_HEIGHT,end);
			prevState = GAMEOVER;
			state = NODRAW;
			break;
		case HELP:
			drawImage3(0,0,HELP_WIDTH,HELP_HEIGHT,help);
			prevState = HELP;
			state = NODRAW;
			break;
		}
	}

  return 0;
}
Example #11
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
}
Example #12
0
long garbleCircuit(GarbledCircuit *garbledCircuit, InputLabels inputLabels,
		OutputMap outputMap) {

	GarblingContext garblingContext;
	GarbledGate *garbledGate;
	GarbledTable *garbledTable;
	DKCipherContext dkCipherContext;
	const block *sched = ((block *) (dkCipherContext.K.rd_key));
	block val;

	block *A, *B, *plainText, *cipherText;
	block tweak;
	long i, j, rnds = 10;
	block blocks[4];
	block keys[4];
	long lsb0, lsb1;
	int input0, input1, output;
	srand_sse(time(NULL));


    
	createInputLabels(inputLabels, garbledCircuit->n);
	garbledCircuit->id = getFreshId();

	for (i = 0; i < 2 * garbledCircuit->n; i += 2) {
		garbledCircuit->wires[i / 2].label0 = inputLabels[i];
		garbledCircuit->wires[i / 2].label1 = inputLabels[i + 1];
	}
	garbledTable = garbledCircuit->garbledTable;
	garblingContext.gateIndex = 0;
	garblingContext.wireIndex = garbledCircuit->n + 1;
	block key = randomBlock();
	block rkey = randomBlock();
	AES_KEY KR;
	AES_set_encrypt_key((unsigned char *) &rkey, 128, &KR);
	const __m128i *sched2 = ((__m128i *) (KR.rd_key));
	garblingContext.R =
			xorBlocks(garbledCircuit->wires[0].label0, garbledCircuit->wires[0].label1);
	garbledCircuit->globalKey = key;
	DKCipherInit(&key, &(garblingContext.dkCipherContext));
	int tableIndex = 0;

	for (i = 0; i < garbledCircuit->q; i++) {
		garbledGate = &(garbledCircuit->garbledGates[i]);
		input0 = garbledGate->input0;
		input1 = garbledGate->input1;
		output = garbledGate->output;

#ifdef FREE_XOR
		if (garbledGate->type == XORGATE) {
			garbledCircuit->wires[output].label0 =
					xorBlocks(garbledCircuit->wires[input0].label0, garbledCircuit->wires[input1].label0);
			garbledCircuit->wires[output].label1 =
					xorBlocks(garbledCircuit->wires[input0].label1, garbledCircuit->wires[input1].label0);
			continue;
		}
#endif
		tweak = makeBlock(i, (long)0);
		lsb0 = getLSB(garbledCircuit->wires[input0].label0);
		lsb1 = getLSB(garbledCircuit->wires[input1].label0);

		block val = _mm_xor_si128(tweak, sched[0]);
		for (j = 1; j < rnds; j++)
			val = _mm_aesenc_si128(val, sched2[j]);
		garbledCircuit->wires[garbledGate->output].label0 =
				_mm_aesenclast_si128(val, sched[j]);

		garbledCircuit->wires[garbledGate->output].label1 =
				xorBlocks(garblingContext.R,
						garbledCircuit->wires[garbledGate->output].label0);
		block A0, A1, B0, B1;
		A0 = DOUBLE(garbledCircuit->wires[input0].label0);
		A1 = DOUBLE(garbledCircuit->wires[input0].label1);
		B0 = DOUBLE(DOUBLE(garbledCircuit->wires[input1].label0));
		B1 = DOUBLE(DOUBLE(garbledCircuit->wires[input1].label1));

		keys[0] = xorBlocks(xorBlocks(A0, B0) , tweak);
		keys[1] = xorBlocks(xorBlocks(A0,B1), tweak);
		keys[2] = xorBlocks(xorBlocks(A1, B0), tweak);
		keys[3] = xorBlocks(xorBlocks(A1, B1), tweak);

		block *temp[2];
		temp[0] = &garbledCircuit->wires[garbledGate->output].label0;
		temp[1] = &garbledCircuit->wires[garbledGate->output].label1;
		int bp = 0;
		blocks[0] =
				xorBlocks(keys[0], *(temp[(garbledGate->type & (1<<bp))>>bp]));
		bp++;
		blocks[1] =
				xorBlocks(keys[1], *(temp[(garbledGate->type & (1<<bp))>>bp]));
		bp++;
		blocks[2] =
				xorBlocks(keys[2], *(temp[(garbledGate->type & (1<<bp))>>bp]));
		bp++;
		blocks[3] =
				xorBlocks(keys[3], *(temp[(garbledGate->type & (1<<bp))>>bp]));

		write:

		AES_ecb_encrypt_blks_4(keys,  &(garblingContext.dkCipherContext.K));

		garbledTable[tableIndex].table[2 * lsb0 + lsb1] =
				xorBlocks(blocks[0], keys[0]);
		garbledTable[tableIndex].table[2 * lsb0 + 1 - lsb1] =
				xorBlocks(blocks[1], keys[1]);
		garbledTable[tableIndex].table[2 * (1 - lsb0) + lsb1] =
				xorBlocks(blocks[2], keys[2]);
		garbledTable[tableIndex].table[2 * (1 - lsb0) + (1 - lsb1)] =
				xorBlocks(blocks[3], keys[3]);

		tableIndex++;

	}
	for (i = 0; i < garbledCircuit->m; i++) {
		outputMap[2 * i] =
				garbledCircuit->wires[garbledCircuit->outputs[i]].label0;
		outputMap[2 * i + 1] =
				garbledCircuit->wires[garbledCircuit->outputs[i]].label1;
	}
	return 0;
}
Example #13
0
long garbleCircuit(GarbledCircuit *garbledCircuit, InputLabels inputLabels, OutputMap outputMap) {

	GarblingContext garblingContext;
	GarbledGate *garbledGate;
	GarbledTable *garbledTable;
	DKCipherContext dkCipherContext;
	const __m128i *sched = ((__m128i *)(dkCipherContext.K.rd_key));
	block val;

	block *A, *B, *plainText,*cipherText;
	block tweak;
	long a, b, i, j,rnds = 10;
	block blocks[4];
	block keys[4];
	long lsb0,lsb1;
	block keyToEncrypt;
	int input0, input1, output;
	srand_sse( time(NULL));


    
	createInputLabels(inputLabels, garbledCircuit->n);

	garbledCircuit->id = getFreshId();

	for(i=0;i<2*garbledCircuit->n;i+=2) {
		garbledCircuit->wires[i/2].id = i+1;
		garbledCircuit->wires[i/2].label0 = inputLabels[i];
		garbledCircuit->wires[i/2].label1 = inputLabels[i+1];
	}
	garbledTable = garbledCircuit->garbledTable;
	garblingContext.gateIndex = 0;
	garblingContext.wireIndex = garbledCircuit->n + 1;
	block key = randomBlock();
	garblingContext.R = xorBlocks(garbledCircuit->wires[0].label0, garbledCircuit->wires[0].label1);
	garbledCircuit->globalKey = key;
	DKCipherInit(&key, &(garblingContext.dkCipherContext));
	int tableIndex = 0;

	for(i=0; i< garbledCircuit->q;i++) {
		garbledGate = &(garbledCircuit->garbledGates[i]);
		input0 = garbledGate->input0; input1 = garbledGate->input1;
		output = garbledGate->output;

#ifdef FREE_XOR
		if (garbledGate->type == XORGATE) {
			garbledCircuit->wires[output].label0 = xorBlocks(garbledCircuit->wires[input0].label0, garbledCircuit->wires[input1].label0);
			garbledCircuit->wires[output].label1 = xorBlocks(garbledCircuit->wires[input0].label1, garbledCircuit->wires[input1].label0);
			continue;
		}
#endif
		tweak = makeBlock(i, (long)0);
		input0 = garbledGate->input0; input1 = garbledGate->input1;
		lsb0 = getLSB(garbledCircuit->wires[input0].label0);
		lsb1 = getLSB(garbledCircuit->wires[input1].label0);

		block A0, A1, B0, B1;
		A0 = DOUBLE(garbledCircuit->wires[input0].label0);
		A1 = DOUBLE(garbledCircuit->wires[input0].label1);
		B0 = DOUBLE(DOUBLE(garbledCircuit->wires[input1].label0));
		B1 = DOUBLE(DOUBLE(garbledCircuit->wires[input1].label1));

		keys[0] = xorBlocks(A0, B0);
		keys[0] = xorBlocks(keys[0], tweak);
		keys[1] = xorBlocks(A0,B1);
		keys[1] = xorBlocks(keys[1], tweak);
		keys[2] = xorBlocks(A1, B0);
		keys[2] = xorBlocks(keys[2], tweak);
		keys[3] = xorBlocks(A1, B1);
		keys[3] = xorBlocks(keys[3], tweak);

		block mask[4]; block newToken;
		mask[0] = keys[0];
		mask[1] = keys[1];
		mask[2] = keys[2];
		mask[3] = keys[3];
		AES_ecb_encrypt_blks(keys, 4, &(garblingContext.dkCipherContext.K));
		mask[0] = xorBlocks(mask[0], keys[0]);
		mask[1] = xorBlocks(mask[1],keys[1]);
		mask[2] = xorBlocks(mask[2],keys[2]);
		mask[3] = xorBlocks(mask[3],keys[3]);

		if (2*lsb0 + lsb1 ==0)
		newToken = mask[0];
		if (2*lsb0 + 1-lsb1 ==0)
		newToken = mask[1];
		if (2*(1-lsb0) + lsb1 ==0)
		newToken = mask[2];
		if (2*(1-lsb0) + 1-lsb1 ==0)
		newToken = mask[3];

		block newToken2 = xorBlocks(garblingContext.R, newToken);

		if (garbledGate->type == ANDGATE) {
			if (lsb1 ==1 & lsb0 ==1) {
				garbledCircuit->wires[garbledGate->output].label1 = newToken;
				garbledCircuit->wires[garbledGate->output].label0 = newToken2;
			}
			else {
				garbledCircuit->wires[garbledGate->output].label0 = newToken;
				garbledCircuit->wires[garbledGate->output].label1 = newToken2;
			}
		}

		if (garbledGate->type == ORGATE) {
			if (!(lsb1 ==0 & lsb0 ==0)) {
				garbledCircuit->wires[garbledGate->output].label1 = newToken;
				garbledCircuit->wires[garbledGate->output].label0 = newToken2;
			}
			else {
				garbledCircuit->wires[garbledGate->output].label0 = newToken;
				garbledCircuit->wires[garbledGate->output].label1 = newToken2;
			}
		}

		if (garbledGate->type == XORGATE) {
			if ((lsb1 ==0 & lsb0 ==1) ||(lsb1 ==1 & lsb0 ==0) ) {
				garbledCircuit->wires[garbledGate->output].label1 = newToken;
				garbledCircuit->wires[garbledGate->output].label0 = newToken2;
			}
			else {
				garbledCircuit->wires[garbledGate->output].label0 = newToken;
				garbledCircuit->wires[garbledGate->output].label1 = newToken2;
			}
		}

		if (garbledGate->type == NOTGATE) {
			if (lsb0 ==0) {
				garbledCircuit->wires[garbledGate->output].label1 = newToken;
				garbledCircuit->wires[garbledGate->output].label0 = newToken2;
			}
			else {
				garbledCircuit->wires[garbledGate->output].label0 = newToken;
				garbledCircuit->wires[garbledGate->output].label1 = newToken2;
			}
		}

		block *label0 = &garbledCircuit->wires[garbledGate->output].label0;
		block *label1 = &garbledCircuit->wires[garbledGate->output].label1;

		if (garbledGate->type == ANDGATE) {

			blocks[0] = *label0;
			blocks[1] = *label0;
			blocks[2] = *label0;
			blocks[3] = *label1;
			goto write;
		}

		if (garbledGate->type == ORGATE) {

			blocks[0] = *label0;
			blocks[1] = *label1;
			blocks[2] = *label1;
			blocks[3] = *label1;
			goto write;

		}

		if (garbledGate->type == XORGATE) {

			blocks[0] = *label0;
			blocks[1] = *label1;
			blocks[2] = *label1;
			blocks[3] = *label0;
			goto write;

		}

		if (garbledGate->type == NOTGATE) {

			blocks[0] = *label1;
			blocks[1] = *label0;
			blocks[2] = *label1;
			blocks[3] = *label0;
			goto write;

		}
		write:
		if (2*lsb0 + lsb1 !=0)
		garbledTable[tableIndex].table[2*lsb0 + lsb1 -1] = xorBlocks(blocks[0], mask[0]);
		if (2*lsb0 + 1-lsb1 !=0)
		garbledTable[tableIndex].table[2*lsb0 + 1-lsb1-1] = xorBlocks(blocks[1], mask[1]);
		if (2*(1-lsb0) + lsb1 !=0)
		garbledTable[tableIndex].table[2*(1-lsb0) + lsb1-1] = xorBlocks(blocks[2], mask[2]);
		if (2*(1-lsb0) + (1-lsb1) !=0)
		garbledTable[tableIndex].table[2*(1-lsb0) + (1-lsb1)-1] = xorBlocks(blocks[3], mask[3]);

		tableIndex++;

	}
	for(i=0;i<garbledCircuit->m;i++) {
		outputMap[2*i] = garbledCircuit->wires[garbledCircuit->outputs[i]].label0;
		outputMap[2*i+1] = garbledCircuit->wires[garbledCircuit->outputs[i]].label1;
	}
	return 0;
}
Example #14
0
long garbleCircuit(GarbledCircuit *garbledCircuit, InputLabels inputLabels, OutputMap outputMap) {

	GarblingContext garblingContext;
	GarbledGate *garbledGate;
	GarbledTable *garbledTable;
	DKCipherContext dkCipherContext;
	const __m128i *sched = ((__m128i *)(dkCipherContext.K.rd_key));
	block val;

	block *A, *B, *plainText,*cipherText;
	block tweak;
	long a, b, i, j,rnds = 10;
	block blocks[4];
	block keys[4];
	long lsb0,lsb1;
	block keyToEncrypt;
	int input0, input1,output;
	srand_sse( time(NULL));


    
	createInputLabels(inputLabels, garbledCircuit->n);

	garbledCircuit->id = getFreshId();

	for(i=0;i<2*garbledCircuit->n;i+=2) {
		garbledCircuit->wires[i/2].id = i+1;
		garbledCircuit->wires[i/2].label0 = inputLabels[i];
		garbledCircuit->wires[i/2].label1 = inputLabels[i+1];
	}
	garbledTable = garbledCircuit->garbledTable;
	garblingContext.gateIndex = 0;
	garblingContext.wireIndex = garbledCircuit->n + 1;
	block key = randomBlock();
	block rkey = randomBlock();
	AES_KEY KR;
	AES_set_encrypt_key(&rkey, 128, &KR);
	const __m128i *sched2 = ((__m128i *)(KR.rd_key));
	garblingContext.R = xorBlocks(garbledCircuit->wires[0].label0, garbledCircuit->wires[0].label1);
	garbledCircuit->globalKey = key;
	DKCipherInit(&key, &(garblingContext.dkCipherContext));
	int tableIndex = 0;

	for(i=0; i< garbledCircuit->q;i++) {
		garbledGate = &(garbledCircuit->garbledGates[i]);
		input0 = garbledGate->input0; input1 = garbledGate->input1;
		output = garbledGate->output;

#ifdef FREE_XOR
		if (garbledGate->type == XORGATE) {
			garbledCircuit->wires[output].label0 = xorBlocks(garbledCircuit->wires[input0].label0, garbledCircuit->wires[input1].label0);
			garbledCircuit->wires[output].label1 = xorBlocks(garbledCircuit->wires[input0].label1, garbledCircuit->wires[input1].label0);
			continue;
		}
#endif
		tweak = makeBlock(i, (long)0);
		lsb0 = getLSB(garbledCircuit->wires[input0].label0);
		lsb1 = getLSB(garbledCircuit->wires[input1].label0);
		char templ[20];
		char templ2[20];
		block val = _mm_xor_si128 (tweak, sched[0]);
		for (j=1; j<rnds; j++) val = _mm_aesenc_si128 (val,sched2[j]);
		*((block*)templ) = _mm_aesenclast_si128 (val, sched[j]);
		val = _mm_aesenclast_si128 (val, sched[j]);
		*((block *)templ2) = xorBlocks(*((block *)templ), garblingContext.R);

		TRUNCATE(templ);
		TRUNCATE(templ2);

		block *label0 = (block *)templ;
		block *label1 = (block *)templ2;
		garbledCircuit->wires[garbledGate->output].label0 = *((block*)templ);
		garbledCircuit->wires[garbledGate->output].label1 = *((block*)templ2);
		block A0, A1, B0, B1;
		A0 = DOUBLE(garbledCircuit->wires[input0].label0);
		A1 = DOUBLE(garbledCircuit->wires[input0].label1);
		B0 = DOUBLE(DOUBLE(garbledCircuit->wires[input1].label0));
		B1 = DOUBLE(DOUBLE(garbledCircuit->wires[input1].label1));

		keys[0] = xorBlocks(A0, B0);
		keys[0] = xorBlocks(keys[0], tweak);
		keys[1] = xorBlocks(A0,B1);
		keys[1] = xorBlocks(keys[1], tweak);
		keys[2] = xorBlocks(A1, B0);
		keys[2] = xorBlocks(keys[2], tweak);
		keys[3] = xorBlocks(A1, B1);
		keys[3] = xorBlocks(keys[3], tweak);

		if (garbledGate->type == ANDGATE) {

			blocks[0] = xorBlocks(keys[0], *label0);
			blocks[1] = xorBlocks(keys[1], *label0);
			blocks[2] = xorBlocks(keys[2], *label0);
			blocks[3] = xorBlocks(keys[3], *label1);
			goto write;
		}

		if (garbledGate->type == ORGATE) {

			blocks[0] = xorBlocks(keys[0], *label0);
			blocks[1] = xorBlocks(keys[1], *label1);
			blocks[2] = xorBlocks(keys[2], *label1);
			blocks[3] = xorBlocks(keys[3], *label1);
			goto write;

		}

		if (garbledGate->type == XORGATE) {

			blocks[0] = xorBlocks(keys[0], *label0);
			blocks[1] = xorBlocks(keys[1], *label1);
			blocks[2] = xorBlocks(keys[2], *label1);
			blocks[3] = xorBlocks(keys[3], *label0);
			goto write;

		}

		if (garbledGate->type == NOTGATE) {

			blocks[0] = xorBlocks(keys[0], *label1);
			blocks[1] = xorBlocks(keys[1], *label0);
			blocks[2] = xorBlocks(keys[2], *label1);
			blocks[3] = xorBlocks(keys[3], *label0);
			goto write;

		}
		write:
		AES_ecb_encrypt_blks(keys, 4, &(garblingContext.dkCipherContext.K));

		char toWrite[4][16];
		char **dest[4];

		*((block *) toWrite[0]) = xorBlocks(blocks[0], keys[0]);
		*((block *) toWrite[1]) = xorBlocks(blocks[1], keys[1]);
		*((block *) toWrite[2]) = xorBlocks(blocks[2], keys[2]);
		*((block *) toWrite[3]) = xorBlocks(blocks[3], keys[3]);

		short *cpsrc; short *cpdst;
		cpsrc = (short *)toWrite[0];
		cpdst = (short *)&garbledTable[tableIndex].table[2*lsb0 + lsb1];
		cpdst[0]=cpsrc[0];
		cpdst[1]=cpsrc[1];
		cpdst[2]=cpsrc[2];
		cpdst[3]=cpsrc[3];
		cpdst[4]=cpsrc[4];

		cpsrc = (short *)toWrite[1];
		cpdst = (short *)&garbledTable[tableIndex].table[2*(lsb0) + (1-lsb1)];
		cpdst[0]=cpsrc[0];
		cpdst[1]=cpsrc[1];
		cpdst[2]=cpsrc[2];
		cpdst[3]=cpsrc[3];
		cpdst[4]=cpsrc[4];

		cpsrc = (short *)toWrite[2];
		cpdst = (short *)&garbledTable[tableIndex].table[2*(1-lsb0) + (lsb1)];
		cpdst[0]=cpsrc[0];
		cpdst[1]=cpsrc[1];
		cpdst[2]=cpsrc[2];
		cpdst[3]=cpsrc[3];
		cpdst[4]=cpsrc[4];

		cpsrc = (short *)toWrite[3];
		cpdst = (short *)&garbledTable[tableIndex].table[2*(1-lsb0) + (1-lsb1)];
		cpdst[0]=cpsrc[0];
		cpdst[1]=cpsrc[1];
		cpdst[2]=cpsrc[2];
		cpdst[3]=cpsrc[3];
		cpdst[4]=cpsrc[4];

		tableIndex++;
	}
	for(i=0;i<garbledCircuit->m;i++) {
		outputMap[2*i] = garbledCircuit->wires[garbledCircuit->outputs[i]].label0;
		outputMap[2*i+1] = garbledCircuit->wires[garbledCircuit->outputs[i]].label1;
	}

	return 0;
}
Example #15
0
int createNewWire(Wire *in, GarblingContext *garblingContext, int id) {
	in->id = id; //getNextWire(garblingContext);
	in->label0 = randomBlock();
	in->label1 = xorBlocks(garblingContext->R, in->label0);
	return 0;
}