int do_test (int argc, char *argv[]) { struct stat st; char buf[1000]; memset (buf, '\0', sizeof (buf)); if (write (fd, buf, sizeof (buf)) != sizeof (buf)) error (EXIT_FAILURE, errno, "during write"); if (fstat (fd, &st) < 0 || st.st_size != sizeof (buf)) error (EXIT_FAILURE, 0, "initial size wrong"); if (FTRUNCATE (fd, 800) < 0) error (EXIT_FAILURE, errno, "size reduction with %s failed", STRINGIFY (FTRUNCATE)); if (fstat (fd, &st) < 0 || st.st_size != 800) error (EXIT_FAILURE, 0, "size after reduction with %s incorrect", STRINGIFY (FTRUNCATE)); /* The following test covers more than POSIX. POSIX does not require that ftruncate() can increase the file size. But we are testing Unix systems. */ if (FTRUNCATE (fd, 1200) < 0) error (EXIT_FAILURE, errno, "size increase with %s failed", STRINGIFY (FTRUNCATE)); if (fstat (fd, &st) < 0 || st.st_size != 1200) error (EXIT_FAILURE, 0, "size after increase with %s incorrect", STRINGIFY (FTRUNCATE)); if (TRUNCATE (name, 800) < 0) error (EXIT_FAILURE, errno, "size reduction with %s failed", STRINGIFY (TRUNCATE)); if (fstat (fd, &st) < 0 || st.st_size != 800) error (EXIT_FAILURE, 0, "size after reduction with %s incorrect", STRINGIFY (TRUNCATE)); /* The following test covers more than POSIX. POSIX does not require that truncate() can increase the file size. But we are testing Unix systems. */ if (TRUNCATE (name, 1200) < 0) error (EXIT_FAILURE, errno, "size increase with %s failed", STRINGIFY (TRUNCATE)); if (fstat (fd, &st) < 0 || st.st_size != 1200) error (EXIT_FAILURE, 0, "size after increase with %s incorrect", STRINGIFY (TRUNCATE)); close (fd); unlink (name); return 0; }
int float_digits(double fl, int digits) { unsigned long num; int i; /* 2^64 = 18446744073709551616 (18 useful) * 2^32 = 4294967296 (9 useful) */ if (digits == -1) digits = (sizeof(unsigned long) >= 8) ? 18 : 9; fl = ((fl < 0) ? -fl : fl); /* The main part of the algorithm: Floating point numbers are not very exact. * We need to do something to determine how close we are to the right number * We multiply our floating point value by an error factor. If we see a * string of 9's or 0's in a row, we stop. For example, if the error factor * is 1000, if we see 3 9's or 0's we stop. Every time through the loop we * multiply by 10 to shift over one digit and repeat. */ for (i = 0; i < digits; i++) { num = TRUNCATE((fl - TRUNCATE(fl)) * FLOAT_DIGITS_ERROR_FACTOR); if ((num < 1) || (num >= (long)(FLOAT_DIGITS_ERROR_FACTOR-1.0))) break; fl *= 10.0; } return i; }
inline TRWS::REAL UpdateMessageGENERAL(TRWS::REAL* M, TRWS::REAL* Di_hat, int K, TRWS::REAL gamma, TRWS::SmoothCostGeneralFn fn, int i, int j, void* buf) { TRWS::REAL* Di = (TRWS::REAL*) buf; int ki, kj; TRWS::REAL delta; for (ki=0; ki<K; ki++) { Di[ki] = (gamma*Di_hat[ki] - M[ki]); } for (kj=0; kj<K; kj++) { M[kj] = Di[0] + fn(i, j, 0, kj); for (ki=1; ki<K; ki++) { delta = Di[ki] + fn(i, j, ki, kj); TRUNCATE(M[kj], delta); } } delta = M[0]; for (kj=1; kj<K; kj++) TRUNCATE(delta, M[kj]); for (kj=0; kj<K; kj++) M[kj] -= delta; return delta; }
inline TRWS::REAL UpdateMessageFIXED_MATRIX(TRWS::REAL* M, TRWS::REAL* Di_hat, int K, TRWS::REAL gamma, MRF::CostVal lambda, MRF::CostVal* V, void* buf) { TRWS::REAL* Di = (TRWS::REAL*) buf; int ki, kj; TRWS::REAL delta; if (lambda == 0) { delta = gamma*Di_hat[0] - M[0]; M[0] = 0; for (ki=1; ki<K; ki++) { TRUNCATE(delta, gamma*Di_hat[ki] - M[ki]); M[ki] = 0; } return delta; } for (ki=0; ki<K; ki++) { Di[ki] = (gamma*Di_hat[ki] - M[ki]) * (1/(TRWS::REAL)lambda); } if (lambda > 0) { for (kj=0; kj<K; kj++) { M[kj] = Di[0] + V[0]; V ++; for (ki=1; ki<K; ki++) { TRUNCATE(M[kj], Di[ki] + V[0]); V ++; } M[kj] *= lambda; } } else { for (kj=0; kj<K; kj++) { M[kj] = Di[0] + V[0]; V ++; for (ki=1; ki<K; ki++) { TRUNCATE_MAX(M[kj], Di[ki] + V[0]); V ++; } M[kj] *= lambda; } } delta = M[0]; for (kj=1; kj<K; kj++) TRUNCATE(delta, M[kj]); for (kj=0; kj<K; kj++) M[kj] -= delta; return delta; }
inline TRWS::REAL UpdateMessageGENERAL(TRWS::REAL* M, TRWS::REAL* Di_hat, int K, TRWS::REAL gamma, int dir, MRF::CostVal* V, void* buf) { TRWS::REAL* Di = (TRWS::REAL*) buf; int ki, kj; TRWS::REAL delta; for (ki=0; ki<K; ki++) { Di[ki] = (gamma*Di_hat[ki] - M[ki]); } if (dir == 0) { for (kj=0; kj<K; kj++) { M[kj] = Di[0] + V[0]; V ++; for (ki=1; ki<K; ki++) { TRUNCATE(M[kj], Di[ki] + V[0]); V ++; } } } else { for (kj=0; kj<K; kj++) { M[kj] = Di[0] + V[0]; V += K; for (ki=1; ki<K; ki++) { TRUNCATE(M[kj], Di[ki] + V[0]); V += K; } V -= K*K - 1; } } delta = M[0]; for (kj=1; kj<K; kj++) TRUNCATE(delta, M[kj]); for (kj=0; kj<K; kj++) M[kj] -= delta; return delta; }
inline TRWS::REAL SubtractMin(TRWS::REAL *D, int K) { int k; TRWS::REAL delta; delta = D[0]; for (k=1; k<K; k++) TRUNCATE(delta, D[k]); for (k=0; k<K; k++) D[k] -= delta; return delta; }
BdwInetError bdw_inet_client_connect (const BdwInetClient *self, uint8 tries) { int8 try_number = 0; int64 status = BDW_INET_ERROR_OK; if (self->socket_type == BDW_SOCKET_TYPE_UDP) return BDW_INET_ERROR_OK; while (try_number < TRUNCATE (tries, BDW_INET_MAX_CONNECTION_TRIES)) { status = connect (self->socket_id, (struct sockaddr *) &self->host, sizeof (self->host)); try_number++; } if (status == BDW_INET_ERROR_KO) { switch (errno) { case EISCONN: return BDW_INET_ERROR_ALREADY_CONNECTED; case EBADF: return BDW_INET_ERROR_INVALID_SOCKET; case ENOTSOCK: return BDW_INET_ERROR_NOT_A_SOCKET; case EADDRNOTAVAIL: return BDW_INET_ERROR_ADDRESS_NOT_AVAILABLE; case ECONNREFUSED: return BDW_INET_ERROR_CONNECTION_REFUSED; case ENETUNREACH: return BDW_INET_ERROR_NETWORK_UNREACHABLE; case EPROTOTYPE: return BDW_INET_ERROR_NOT_SUPPORTED; case ETIMEDOUT: return BDW_INET_ERROR_TIMEOUT; default: return BDW_INET_ERROR_UNDEFINED; } } return BDW_INET_ERROR_OK; }
inline TRWS::REAL UpdateMessageL1(TRWS::REAL* M, TRWS::REAL* Di_hat, int K, TRWS::REAL gamma, MRF::CostVal lambda, MRF::CostVal smoothMax) { int k; TRWS::REAL delta; delta = M[0] = gamma*Di_hat[0] - M[0]; for (k=1; k<K; k++) { M[k] = gamma*Di_hat[k] - M[k]; TRUNCATE(delta, M[k]); TRUNCATE(M[k], M[k-1] + lambda); } M[--k] -= delta; TRUNCATE(M[k], lambda*smoothMax); for (k--; k>=0; k--) { M[k] -= delta; TRUNCATE(M[k], M[k+1] + lambda); TRUNCATE(M[k], lambda*smoothMax); } return delta; }
inline TRWS::REAL UpdateMessageL2(TRWS::REAL* M, TRWS::REAL* Di_hat, int K, TRWS::REAL gamma, MRF::CostVal lambda, MRF::CostVal smoothMax, void *buf) { TRWS::REAL* Di = (TRWS::REAL*) buf; int* parabolas = (int*) ((char*)buf + K*sizeof(TRWS::REAL)); int* intersections = parabolas + K; TypeTruncatedQuadratic2D::Edge* tmp = NULL; int k; TRWS::REAL delta; assert(lambda >= 0); Di[0] = gamma*Di_hat[0] - M[0]; delta = Di[0]; for (k=1; k<K; k++) { Di[k] = gamma*Di_hat[k] - M[k]; TRUNCATE(delta, Di[k]); } if (lambda == 0) { for (k=0; k<K; k++) M[k] = 0; return delta; } tmp->DistanceTransformL2(K, 1, lambda, Di, M, parabolas, intersections); for (k=0; k<K; k++) { M[k] -= delta; TRUNCATE(M[k], lambda*smoothMax); } return delta; }
/* Truncate a file using ftruncate rather than fork/exec of /bin/cp. #1951 sjc 10Dec87 */ ftnint t_runc (unit *ftnunit, flag xerr) { extern void exit(); /* DAG -- added */ #if 0 /* not used. */ char buf[128], nm[16]; FILE *tmp; int n, m; int fd; #endif #ifdef _BSD off_t ftell(); #endif /* _BSD */ #if defined (_SYSV) || defined(_SYSTYPE_SVR4) #ifndef sgi long ftell(); #endif #endif /* SYSV */ ftnll loc, len; if (ftnunit->uacc == DIRECT) return (0); /* don't truncate direct files */ if (ftnunit->useek == 0 || ftnunit->ufnm == NULL) return (0); /* do not truncate "/dev/null" */ if (strncmp("/dev/null", ftnunit->ufnm, 9) == 0) return (0); loc = FTELL (ftnunit->ufd); (void) fseek (ftnunit->ufd, 0L, 2); len = FTELL (ftnunit->ufd); /* if ftell() fails then ignore the pipe/device */ if (loc == len || loc < 0) return (0); if (TRUNCATE (ftnunit->ufnm, loc)) err (xerr, 111, "endfile"); (void) FSEEK (ftnunit->ufd, loc, 0); return 0; }
/** * Try to read the HELLOs in the given filename and discard expired * addresses. Removes the file if one the HELLO is malformed. If all * addresses are expired, the HELLO is also removed (but the HELLO * with the public key is still returned if it was found and valid). * The file can contain multiple HELLO messages. * * @param fn name of the file * @param unlink_garbage if #GNUNET_YES, try to remove useless files * @param r ReadHostFileContext to store the resutl */ static void read_host_file (const char *fn, int unlink_garbage, struct ReadHostFileContext *r) { char buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1] GNUNET_ALIGN; unsigned int size_total; struct GNUNET_TIME_Absolute now; unsigned int left; const struct GNUNET_HELLO_Message *hello; struct GNUNET_HELLO_Message *hello_clean; unsigned read_pos; int size_hello; r->friend_only_hello = NULL; r->hello = NULL; if (GNUNET_YES != GNUNET_DISK_file_test (fn)) return; size_total = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer)); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Read %u bytes from `%s'\n", size_total, fn); if (size_total < sizeof (struct GNUNET_MessageHeader)) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to parse HELLO in file `%s': %s\n"), fn, "Fail has invalid size"); if ( (GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)) && (ENOENT != errno) ) GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn); return; } read_pos = 0; while (read_pos < size_total) { hello = (const struct GNUNET_HELLO_Message *) &buffer[read_pos]; size_hello = GNUNET_HELLO_size (hello); if ( (0 == size_hello) || (size_total - read_pos < size_hello) ) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to parse HELLO in file `%s'\n"), fn); if (0 == read_pos) { if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)) && (ENOENT != errno) ) GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn); } else { if ((GNUNET_YES == unlink_garbage) && (0 != TRUNCATE (fn, read_pos)) && (ENOENT != errno) ) GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "truncate", fn); } return; } now = GNUNET_TIME_absolute_get (); hello_clean = GNUNET_HELLO_iterate_addresses (hello, GNUNET_YES, &discard_expired, &now); if (NULL == hello_clean) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to parse HELLO in file `%s'\n"), fn); if ((GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)) && (ENOENT != errno) ) GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn); return; } left = 0; (void) GNUNET_HELLO_iterate_addresses (hello_clean, GNUNET_NO, &count_addresses, &left); if (0 == left) { GNUNET_free (hello_clean); break; } if (GNUNET_NO == GNUNET_HELLO_is_friend_only (hello_clean)) { if (NULL == r->hello) r->hello = hello_clean; else { GNUNET_break (0); GNUNET_free (r->hello); r->hello = hello_clean; } } else { if (NULL == r->friend_only_hello) r->friend_only_hello = hello_clean; else { GNUNET_break (0); GNUNET_free (r->friend_only_hello); r->friend_only_hello = hello_clean; } } read_pos += size_hello; } if (0 == left) { /* no addresses left, remove from disk */ if ( (GNUNET_YES == unlink_garbage) && (0 != UNLINK (fn)) ) GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn); } GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found `%s' and `%s' HELLO message in file\n", (NULL != r->hello) ? "public" : "NON-public", (NULL != r->friend_only_hello) ? "friend only" : "NO friend only"); }
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; }
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]); TRUNCATE(&mask[0]); TRUNCATE(&mask[3]); TRUNCATE(&mask[1]); TRUNCATE(&mask[2]); 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; TRUNCATE(&garbledCircuit->wires[garbledGate->output].label0); TRUNCATE(&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:; char toWrite[4][16]; char **dest[4]; *((block *) toWrite[0]) = xorBlocks(blocks[0], mask[0]); *((block *) toWrite[1]) = xorBlocks(blocks[1], mask[1]); *((block *) toWrite[2]) = xorBlocks(blocks[2], mask[2]); *((block *) toWrite[3]) = xorBlocks(blocks[3], mask[3]); short *cpsrc; short *cpdst; int sub = 1; if (2 * lsb0 + lsb1 != 0) { cpsrc = (short *) toWrite[0]; cpdst = (short *) &garbledTable[tableIndex].table[2 * lsb0 + lsb1 - sub]; cpdst[0] = cpsrc[0]; cpdst[1] = cpsrc[1]; cpdst[2] = cpsrc[2]; cpdst[3] = cpsrc[3]; cpdst[4] = cpsrc[4]; } if (2 * (lsb0) + (1 - lsb1) != 0) { cpsrc = (short *) toWrite[1]; cpdst = (short *) &garbledTable[tableIndex].table[2 * (lsb0) + (1 - lsb1) - sub]; cpdst[0] = cpsrc[0]; cpdst[1] = cpsrc[1]; cpdst[2] = cpsrc[2]; cpdst[3] = cpsrc[3]; cpdst[4] = cpsrc[4]; } if (2 * (1 - lsb0) + (lsb1) != 0) { cpsrc = (short *) toWrite[2]; cpdst = (short *) &garbledTable[tableIndex].table[2 * (1 - lsb0) + (lsb1) - sub]; cpdst[0] = cpsrc[0]; cpdst[1] = cpsrc[1]; cpdst[2] = cpsrc[2]; cpdst[3] = cpsrc[3]; cpdst[4] = cpsrc[4]; } if (2 * (1 - lsb0) + (1 - lsb1) != 0) { cpsrc = (short *) toWrite[3]; cpdst = (short *) &garbledTable[tableIndex].table[2 * (1 - lsb0) + (1 - lsb1) - sub]; 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; }