Beispiel #1
0
int
chksumDataObjUtil (rcComm_t *conn, char *srcPath, 
rodsEnv *myRodsEnv, rodsArguments_t *rodsArgs, 
dataObjInp_t *dataObjInp)
{
    int status;
    struct timeval startTime, endTime;
    char *chksumStr = NULL;
    char myDir[MAX_NAME_LEN], myFile[MAX_NAME_LEN];
 
    if (srcPath == NULL) {

       rodsLog (LOG_ERROR,
          "chksumDataObjUtil: NULL srcPath input");
        return (USER__NULL_INPUT_ERR);
    }

    if (rodsArgs->verbose == True) {
        (void) gettimeofday(&startTime, (struct timezone *)0);
    }

    rstrcpy (dataObjInp->objPath, srcPath, MAX_NAME_LEN);

    status = rcDataObjChksum (conn, dataObjInp, &chksumStr);

    if (status < 0) {
#if 0
	if (status == CAT_NO_ROWS_FOUND && rodsArgs->resource == True) {
	    if (rodsArgs->verbose == True) {
		printf ("%s does not exist in resource %s\n",
		  dataObjInp->objPath, rodsArgs->resourceString);
	    }
	    return 0;
	} else {
#endif
	    ChksumCnt++;
	    FailedChksumCnt++;
	    rodsLogError (LOG_ERROR, status,
	     "chksumDataObjUtil: rcDataObjChksum error for %s",
	     dataObjInp->objPath);
	    return status;
#if 0
	}
#endif
    } else {
	ChksumCnt++;
    }
    splitPathByKey (dataObjInp->objPath, myDir, myFile, '/');
    if (rodsArgs->silent == False) {
        printf ("    %-30.30s    %s\n", myFile, chksumStr);
        free (chksumStr);     
        if (rodsArgs->verbose == True) {
            (void) gettimeofday(&endTime, (struct timezone *)0);
            printTiming (conn, dataObjInp->objPath, -1, NULL,
             &startTime, &endTime);
	}
    }

    return (status);
}
int
phymvDataObjUtil( rcComm_t *conn, char *srcPath,
                  rodsEnv *myRodsEnv, rodsArguments_t *rodsArgs,
                  dataObjInp_t *dataObjInp ) {
    int status;
    struct timeval startTime, endTime;

    if ( srcPath == NULL ) {
        rodsLog( LOG_ERROR,
                 "phymvDataObjUtil: NULL srcPath input" );
        return ( USER__NULL_INPUT_ERR );
    }

    if ( rodsArgs->verbose == True ) {
        ( void ) gettimeofday( &startTime, ( struct timezone * )0 );
    }

    rstrcpy( dataObjInp->objPath, srcPath, MAX_NAME_LEN );

    status = rcDataObjPhymv( conn, dataObjInp );

    if ( status >= 0 && rodsArgs->verbose == True ) {
        ( void ) gettimeofday( &endTime, ( struct timezone * )0 );
        printTiming( conn, dataObjInp->objPath, conn->transStat.bytesWritten,
                     NULL, &startTime, &endTime );
    }

    return ( status );
}
Beispiel #3
0
void webMain(struct sqlConnection *conn, struct trackDb *tdb)
/* Set up fancy web page with hotlinks bar and
 * sections. */
{
struct section *sectionList = NULL;
printDescription(curGeneId, conn, tdb);
sectionList = loadSectionList(conn);
printIndex(sectionList);
printUpdateTime(database, tdb, NULL);
printSections(sectionList, conn, curGeneId);
printTiming(sectionList);
}
Beispiel #4
0
int
mvObjUtil( rcComm_t *conn, char *srcPath, char *targPath, objType_t objType,
           rodsArguments_t *rodsArgs, dataObjCopyInp_t *dataObjRenameInp ) {
    int status;
    struct timeval startTime, endTime;

    if ( srcPath == NULL || targPath == NULL ) {
        rodsLog( LOG_ERROR,
                 "mvFileUtil: NULL srcPath or targPath incp" );
        return USER__NULL_INPUT_ERR;
    }

    if ( objType == DATA_OBJ_T ) {
        dataObjRenameInp->srcDataObjInp.oprType =
            dataObjRenameInp->destDataObjInp.oprType = RENAME_DATA_OBJ;
    }
    else if ( objType == COLL_OBJ_T ) {
        dataObjRenameInp->srcDataObjInp.oprType =
            dataObjRenameInp->destDataObjInp.oprType = RENAME_COLL;
    }
    else {
        /* should not be here */
        rodsLog( LOG_ERROR,
                 "mvObjUtil: invalid cp dest objType %d for %s",
                 objType, targPath );
        return USER_INPUT_PATH_ERR;
    }


    if ( rodsArgs->verbose == True ) {
        ( void ) gettimeofday( &startTime, ( struct timezone * )0 );
    }

    rstrcpy( dataObjRenameInp->destDataObjInp.objPath, targPath, MAX_NAME_LEN );
    rstrcpy( dataObjRenameInp->srcDataObjInp.objPath, srcPath, MAX_NAME_LEN );

    status = rcDataObjRename( conn, dataObjRenameInp );

    if ( status >= 0 && rodsArgs->verbose == True ) {
        ( void ) gettimeofday( &endTime, ( struct timezone * )0 );
        printTiming( conn, dataObjRenameInp->destDataObjInp.objPath,
                     0, NULL, &startTime, &endTime );
    }

    return status;
}
Beispiel #5
0
int
cpFileUtil( rcComm_t *conn, char *srcPath, char *targPath, rodsLong_t srcSize,
            rodsArguments_t *rodsArgs, dataObjCopyInp_t *dataObjCopyInp ) {
    int status;
    struct timeval startTime, endTime;

    if ( srcPath == NULL || targPath == NULL ) {
        rodsLog( LOG_ERROR,
                 "cpFileUtil: NULL srcPath or targPath in cp" );
        return USER__NULL_INPUT_ERR;
    }

    if ( rodsArgs->verbose == True ) {
        ( void ) gettimeofday( &startTime, ( struct timezone * )0 );
    }

    if ( gGuiProgressCB != NULL ) {
        rstrcpy( conn->operProgress.curFileName, srcPath, MAX_NAME_LEN );
        conn->operProgress.curFileSize = srcSize;
        conn->operProgress.curFileSizeDone = 0;
        conn->operProgress.flag = 0;
        gGuiProgressCB( &conn->operProgress );
    }

    rstrcpy( dataObjCopyInp->destDataObjInp.objPath, targPath, MAX_NAME_LEN );
    rstrcpy( dataObjCopyInp->srcDataObjInp.objPath, srcPath, MAX_NAME_LEN );
    dataObjCopyInp->srcDataObjInp.dataSize = -1;

    status = rcDataObjCopy( conn, dataObjCopyInp );

    if ( status >= 0 ) {
        if ( rodsArgs->verbose == True ) {
            ( void ) gettimeofday( &endTime, ( struct timezone * )0 );
            printTiming( conn, dataObjCopyInp->destDataObjInp.objPath,
                         conn->transStat.bytesWritten, NULL, &startTime, &endTime );
        }
        if ( gGuiProgressCB != NULL ) {
            conn->operProgress.totalNumFilesDone++;
            conn->operProgress.totalFileSizeDone += srcSize;
        }
    }

    return status;
}
Beispiel #6
0
int
sendBulkPut( rcComm_t *conn, bulkOprInp_t *bulkOprInp,
             bulkOprInfo_t *bulkOprInfo, rodsArguments_t *rodsArgs ) {
    struct timeval startTime, endTime;
    int status = 0;

    if ( bulkOprInfo == NULL || bulkOprInfo->count <= 0 ) {
        return 0;
    }

    if ( rodsArgs->verbose == True ) {
        ( void ) gettimeofday( &startTime, ( struct timezone * )0 );
    }

    /* send it */
    if ( bulkOprInfo->bytesBuf.buf != NULL ) {
        status = rcBulkDataObjPut( conn, bulkOprInp, &bulkOprInfo->bytesBuf );
    }
    /* reset the row count */
    bulkOprInp->attriArray.rowCnt = 0;
    if ( bulkOprInfo->forceFlagAdded == 1 ) {
        rmKeyVal( &bulkOprInp->condInput, FORCE_FLAG_KW );
        bulkOprInfo->forceFlagAdded = 0;
    }
    if ( status >= 0 ) {
        if ( rodsArgs->verbose == True ) {
            printf( "Bulk upload %d files.\n", bulkOprInfo->count );
            ( void ) gettimeofday( &endTime, ( struct timezone * )0 );
            printTiming( conn, bulkOprInfo->cachedTargPath,
                         bulkOprInfo->size, bulkOprInfo->cachedTargPath,
                         &startTime, &endTime );
        }
        if ( gGuiProgressCB != NULL ) {
            rstrcpy( conn->operProgress.curFileName,
                     bulkOprInfo->cachedTargPath, MAX_NAME_LEN );
            conn->operProgress.totalNumFilesDone +=  bulkOprInfo->count;
            conn->operProgress.totalFileSizeDone += bulkOprInfo->size;
            gGuiProgressCB( &conn->operProgress );
        }
    }

    return status;
}
Beispiel #7
0
int main(int argc, char *argv[]) {
	
    size_t size = 100000; //make this arbitrary / dynamic
    std::vector<double> v(size);
    std::default_random_engine generator;
    
	// start timing
	TimeStamp start = std::chrono::system_clock::now();
    std::uniform_real_distribution<double> distribution(0.0, 1.0);
    double random_val = distribution(generator);
    double temp;
    for(std::vector<double>::const_iterator i = v.begin(); i != v.end(); ++i) {
        //*i = distribution(generator);
        v[i-v.begin()] = distribution(generator);
    }
    std::sort(v.begin(), v.end(), compare);
	
	printTiming(start, "SORTING - STL");
	
	return 0;
}
Beispiel #8
0
int
getDataObjUtil (rcComm_t *conn, char *srcPath, char *targPath,
rodsLong_t srcSize, uint dataMode, rodsEnv *myRodsEnv, 
rodsArguments_t *rodsArgs, dataObjInp_t *dataObjOprInp)
{
    int status;
    struct timeval startTime, endTime;
 
    if (srcPath == NULL || targPath == NULL) {
       rodsLog (LOG_ERROR,
          "getDataObjUtil: NULL srcPath or targPath input");
        return (USER__NULL_INPUT_ERR);
    }

    if (conn->fileRestart.info.status == FILE_RESTARTED &&
      strcmp (conn->fileRestart.info.objPath, srcPath) == 0) {
        /* it was restarted */
        conn->fileRestart.info.status = FILE_NOT_RESTART;
        return 0;
    }

    if (rodsArgs->verbose == True) {
        (void) gettimeofday(&startTime, (struct timezone *)0);
    }

    if (gGuiProgressCB != NULL) {
	rstrcpy (conn->operProgress.curFileName, srcPath, MAX_NAME_LEN);
	conn->operProgress.curFileSize = srcSize;
	conn->operProgress.curFileSizeDone = 0;
	conn->operProgress.flag = 0;
	gGuiProgressCB (&conn->operProgress); 
    }

    rstrcpy (dataObjOprInp->objPath, srcPath, MAX_NAME_LEN);
    /* rcDataObjGet verifies dataSize if given */
    if (rodsArgs->replNum == True || rodsArgs->resource == True) {
	/* don't verify because it may be an old copy and hence the size  
	 * could be wrong */
	dataObjOprInp->dataSize = 0;
    } else {
        dataObjOprInp->dataSize = srcSize;
    }

    status = rcDataObjGet (conn, dataObjOprInp, targPath);

    if (status >= 0) {
        /* old objState use numCopies in place of dataMode.
         * Just a sanity check */
	myChmod (targPath, dataMode);
	if (rodsArgs->verbose == True) {
            (void) gettimeofday(&endTime, (struct timezone *)0);
            printTiming (conn, dataObjOprInp->objPath, srcSize, targPath,
             &startTime, &endTime);
	}
        if (gGuiProgressCB != NULL) {
	    conn->operProgress.totalNumFilesDone++;
	    conn->operProgress.totalFileSizeDone += srcSize;
	}
    }

    return (status);
}
struct secCompExecutorOutput *SC_DetectCheatingExecutor(int writeSocket, int readSocket, struct RawCircuit *rawInputCircuit,
														unsigned char *deltaPrime, int lengthDelta,
														int checkStatSecParam, gmp_randstate_t *state )
{
	struct Circuit **circuitsArray = (struct Circuit **) calloc(checkStatSecParam, sizeof(struct Circuit*));
	struct publicInputsWithGroup *pubInputGroup;
	struct eccParams *params;
	struct idAndValue *startOfInputChain = convertArrayToChain(deltaPrime, lengthDelta, 0);
	struct revealedCheckSecrets *secretsRevealed;
	struct eccPoint **builderInputs;
	struct secCompExecutorOutput *returnStruct;

	unsigned char *commBuffer, *J_set, **OT_Outputs, *output, *delta, inputBit = 0x00;
	int commBufferLen = 0, i, J_setSize = 0, arrayLen = 0, circuitsChecked = 0;

	struct timespec int_t_0, int_t_1;
	clock_t int_c_0, int_c_1;

	struct params_CnC_ECC *OT_params_R;
	struct otKeyPair_ECC **OT_keyPairs_R;

	// Zero the sub-computation bytes sent/received counters.
	zeroBothSubCounters();

	params = initBrainpool_256_Curve();


	int_t_0 = timestamp();
	int_c_0 = clock();

	// deltaPrime = (unsigned char *) calloc(128, sizeof(unsigned char));
	OT_params_R = OT_CnC_Receiver_Setup_Params(lengthDelta, state, deltaPrime,
											checkStatSecParam, 1024);
	OT_keyPairs_R = OT_CnC_Receiver_Produce_Queries(OT_params_R, lengthDelta,
													state, deltaPrime, checkStatSecParam);
	J_set = OT_params_R -> crs -> J_set;

	int_c_1 = clock();
	int_t_1 = timestamp();
	printTiming(&int_t_0, &int_t_1, int_c_0, int_c_1, "subOT - Receiver Prep");
	printZeroBothSubCounters();


	receiveInt(readSocket);

	int_t_0 = timestamp();
	int_c_0 = clock();

	OT_CnC_Receiver_Send_Queries(writeSocket, readSocket, OT_params_R, OT_keyPairs_R, lengthDelta,
								state, deltaPrime, checkStatSecParam);
	OT_Outputs = OT_CnC_Receiver_Transfer(writeSocket, readSocket, OT_params_R, OT_keyPairs_R,
									lengthDelta, state, deltaPrime, checkStatSecParam);

	int_c_1 = clock();
	int_t_1 = timestamp();
	printTiming(&int_t_0, &int_t_1, int_c_0, int_c_1, "subOT - Receiver Transfer");
	printZeroBothSubCounters();


	int_t_0 = timestamp();
	int_c_0 = clock();

	pubInputGroup = receivePublicCommitments(writeSocket, readSocket);

	for(i = 0; i < checkStatSecParam; i ++)
	{
		circuitsArray[i] = receiveFullCircuit(writeSocket, readSocket);
	}
	int_c_1 = clock();
	int_t_1 = timestamp();
	printTiming(&int_t_0, &int_t_1, int_c_0, int_c_1, "Received Sub-circuits etc.");
	printZeroBothSubCounters();


	// OT_Inputs
	int_t_0 = timestamp();
	int_c_0 = clock();

	// THIS SHOULD ONLY BE UNCOMMENTED FOR TESTING, MEANS WE CAN GET AN EASY DELTA = DELTA'

	commBuffer = receiveBoth(readSocket, commBufferLen);
	delta = deserialiseK0sAndDelta(commBuffer, circuitsArray, rawInputCircuit -> numInputs_P1, checkStatSecParam);

	if(0 == memcmp(delta, deltaPrime, 128))
	{
		inputBit = 0x01;
	}


	setDeltaXOR_onCircuitInputs(circuitsArray, OT_Outputs, inputBit, delta, deltaPrime, J_set,
								rawInputCircuit -> numInputs_P1, lengthDelta, checkStatSecParam);
	int_c_1 = clock();
	int_t_1 = timestamp();
	printTiming(&int_t_0, &int_t_1, int_c_0, int_c_1, "Get Delta and k_0s.");
	printZeroBothSubCounters();


	int_t_0 = timestamp();
	int_c_0 = clock();

	secretsRevealed = executor_decommitToJ_Set(writeSocket, readSocket, circuitsArray, pubInputGroup -> public_inputs,
							pubInputGroup -> params, J_set, &J_setSize, checkStatSecParam);

	circuitsChecked = secretInputsToCheckCircuits(circuitsArray, rawInputCircuit, pubInputGroup -> public_inputs,
												secretsRevealed -> revealedSecrets,
												secretsRevealed -> revealedCircuitSeeds, pubInputGroup -> params,
												J_set, J_setSize, checkStatSecParam);

	int_c_1 = clock();
	int_t_1 = timestamp();
	printTiming(&int_t_0, &int_t_1, int_c_0, int_c_1, "Sub-circuit correctness and decommit to J-Set");
	printZeroBothSubCounters();

	printf("\nSub-circuits Correct = %d\n", circuitsChecked);

	int_t_0 = timestamp();
	int_c_0 = clock();

	// Receive the points representing the Builder's input to the evaluation circuits
	commBuffer = receiveBoth(readSocket, commBufferLen);
	commBufferLen = 0;
	builderInputs = deserialise_ECC_Point_Array(commBuffer, &arrayLen, &commBufferLen);
	free(commBuffer);

	// And the permuted input bits for the builder on the evaluation circuits.
	commBuffer = receiveBoth(readSocket, commBufferLen);
	setBuilderInputs(builderInputs, commBuffer, J_set, J_setSize, circuitsArray,
					pubInputGroup -> public_inputs, pubInputGroup -> params);
	free(commBuffer);

	int_c_1 = clock();
	int_t_1 = timestamp();
	printTiming(&int_t_0, &int_t_1, int_c_0, int_c_1, "Sub-circuit get builder inputs.");
	printZeroBothSubCounters();


	int_t_0 = timestamp();
	int_c_0 = clock();

	printf("\nEvaluating Circuits ");
	for(i = 0; i < checkStatSecParam; i ++)
	{
		if(0x00 == J_set[i])
		{
			printf("%d, ", i);
			fflush(stdout);
			runCircuitExec( circuitsArray[i], writeSocket, readSocket );
		}
	}
	printf("\n");

	int_c_1 = clock();
	int_t_1 = timestamp();
	printTiming(&int_t_0, &int_t_1, int_c_0, int_c_1, "Evaluate Sub-circuits.");
	printZeroBothSubCounters();


	output = getMajorityOutput(circuitsArray, checkStatSecParam, J_set);
	if(inputBit == 0)
	{
		output = NULL;
	}

	for(i = 0; i < checkStatSecParam; i ++)
	{
		freeCircuitStruct(circuitsArray[i], 0);
	}

	returnStruct = getSecCompReturnStruct_L_2013_E(pubInputGroup, builderInputs, J_set, J_setSize, output);

	return returnStruct;
}
unsigned char *SC_DetectCheatingExecutor_HKE_Alt(int writeSocket, int readSocket, struct RawCircuit *rawInputCircuit,
											int length_X_Input, unsigned char *deltaPrime, int lengthDelta,
											struct eccPoint **queries_Partner, struct eccPoint *C, struct eccPoint *cTilde,
											int checkStatSecParam, gmp_randstate_t *state, randctx *ctx)
{
	struct Circuit **circuitsArray_Partner, **circuitsArray_Own;
	struct idAndValue *startOfInputChain, *curItem;
	struct jSetRevealHKE *partnerReveals;
	struct secCompExecutorOutput *returnStruct;

	struct builderInputCommitStruct *commitStruct, *partnersCommitStruct;
	struct HKE_Output_Struct_Builder *outputStruct_Own, *outputStruct_Partner;
	struct DDH_Group *groupOwn, *groupPartner;
	struct eccParams *params;
	struct wire *tempWire;

	struct OT_NP_Receiver_Query **deltaPrimeQueries;
	struct eccPoint **concated_P1_Queries, **deltaPartnerQueries, ***deltaCommits, ***NP_Inputs_Partner;

	ub4 **circuitSeeds = (ub4 **) calloc(checkStatSecParam, sizeof(ub4*));
	randctx **circuitCTXs = (randctx **) calloc(checkStatSecParam, sizeof(randctx*)), *tempCTX;
	struct eccPoint ***NP_consistentInputs;
	mpz_t **aList;

	unsigned char *commBuffer, *J_SetOwn, *J_SetPartner, *deltaPrimeExpanded, **J_setPair;
	unsigned char **OT_Outputs, *binaryOutput, *deltaFromPartner, equalityBit = 0x00, ***OT_Inputs;
	int commBufferLen = 0, i, j, k, J_setSize = 0, arrayLen = 0, circuitsChecked = 0, bufferOffset = 0;
	int jSetChecks = 0, logChecks = 0, partyID = 0;

	struct timespec int_t_0, int_t_1;
	clock_t int_c_0, int_c_1;


	// Let's go grab some randomness.
	for(i = 0; i < checkStatSecParam; i ++)
	{
		circuitCTXs[i] = (randctx*) calloc(1, sizeof(randctx));
		circuitSeeds[i] = getIsaacContext(circuitCTXs[i]);
	}

	// Zero the sub-computation bytes sent/received counters.
	zeroBothSubCounters();

	params = initBrainpool_256_Curve();
	groupOwn = get_128_Bit_Group(*state);


	int_t_0 = timestamp();
	int_c_0 = clock();

	outputStruct_Own = getOutputSecretsAndScheme(rawInputCircuit -> numOutputs, checkStatSecParam, *state, groupOwn);
	aList = getNaorPinkasInputs(rawInputCircuit -> numInputs_P2, checkStatSecParam, *state, params);
	NP_consistentInputs = computeNaorPinkasInputs(cTilde, aList, rawInputCircuit -> numInputs_P2, checkStatSecParam, params);

	int_c_1 = clock();
	int_t_1 = timestamp();
	printTiming(&int_t_0, &int_t_1, int_c_0, int_c_1, "Prep for building Own Circuits");
	printZeroBothSubCounters();


	int_t_0 = timestamp();
	int_c_0 = clock();


	unsigned char *keyPlusDeltaPrime = generateXOR_Key_Bits(ctx, length_X_Input, deltaPrime, lengthDelta);
	startOfInputChain = convertArrayToChain(keyPlusDeltaPrime, length_X_Input + lengthDelta,
											rawInputCircuit -> numInputs_P1);
	// startOfInputChain = convertArrayToChain(deltaPrime, lengthDelta, rawInputCircuit -> numInputs_P1);
	// Build the circuits HKE
	circuitsArray_Own = buildAll_HKE_Circuits_Alt(rawInputCircuit, NP_consistentInputs, outputStruct_Own, params,
											circuitCTXs, checkStatSecParam, partyID);

	int_c_1 = clock();
	int_t_1 = timestamp();
	printTiming(&int_t_0, &int_t_1, int_c_0, int_c_1, "Building Own Circuits");
	printZeroBothSubCounters();



	int_t_0 = timestamp();
	int_c_0 = clock();

	circuitsArray_Partner = (struct Circuit **) calloc(checkStatSecParam, sizeof(struct Circuit*));
	for(i = 0; i < checkStatSecParam; i ++)
	{
		circuitsArray_Partner[i] = receiveFullCircuit(writeSocket, readSocket);
		setCircuitsInputs_Values(startOfInputChain, circuitsArray_Partner[i], 0xFF);
	}
	for(i = 0; i < checkStatSecParam; i++)
	{
		sendCircuit(writeSocket, readSocket, circuitsArray_Own[i]);
	}

	commBufferLen = 0;
	bufferOffset = 0;
	commBuffer = receiveBoth(readSocket, commBufferLen);
	deltaCommits = deserialiseToDeltaKeys(commBuffer, lengthDelta, checkStatSecParam, &bufferOffset);

	int_c_1 = clock();
	int_t_1 = timestamp();
	printTiming(&int_t_0, &int_t_1, int_c_0, int_c_1, "Exchanging Circuits + delta commit.");
	printZeroBothSubCounters();


	int_t_0 = timestamp();
	int_c_0 = clock();

	// Each party now commits to their input values.
	commBufferLen = 0;
	commitStruct = makeCommitmentsBuilder(ctx, circuitsArray_Own, state, checkStatSecParam);
	commBuffer = serialiseC_Boxes(commitStruct, &commBufferLen);
	sendBoth(writeSocket, commBuffer, commBufferLen);
	free(commBuffer);

	commBufferLen = 0;
	bufferOffset = 0;
	commBuffer = receiveBoth(readSocket, commBufferLen);
	partnersCommitStruct = deserialiseC_Boxes(commBuffer, state, &bufferOffset);

	int_c_1 = clock();
	int_t_1 = timestamp();
	printTiming(&int_t_0, &int_t_1, int_c_0, int_c_1, "Make commitments.");
	printZeroBothSubCounters();


	int_t_0 = timestamp();
	int_c_0 = clock();

	// Send the public commitments for the Verified Secret Sharing Scheme and exchange groups.
	outputStruct_Partner = exchangePublicBoxesOfSchemes(writeSocket, readSocket, outputStruct_Own,
														rawInputCircuit -> numOutputs, checkStatSecParam);
	sendDDH_Group(writeSocket, readSocket, groupOwn);
	groupPartner = receiveDDH_Group(writeSocket, readSocket);

	int_c_1 = clock();
	int_t_1 = timestamp();
	printTiming(&int_t_0, &int_t_1, int_c_0, int_c_1, "Exchange Secret Sharing Schemes.");
	printZeroBothSubCounters();


	int_t_0 = timestamp();
	int_c_0 = clock();

	// THIS SHOULD ONLY BE UNCOMMENTED FOR TESTING, MEANS WE CAN GET AN EASY DELTA = DELTA'
	// deltaPrime = (unsigned char *) calloc(128, sizeof(unsigned char));
	OT_Inputs = getAllInputKeysSymm(circuitsArray_Own, checkStatSecParam, partyID);

	deltaPrimeQueries = NaorPinkas_OT_Produce_Queries(rawInputCircuit -> numInputs_P2, keyPlusDeltaPrime,
													state, params, cTilde);
	deltaPartnerQueries = NaorPinkas_OT_Exchange_Queries(writeSocket, readSocket, rawInputCircuit -> numInputs_P2, 
														lengthDelta, deltaPrimeQueries);

	concated_P1_Queries = concatQueriesExecutor(queries_Partner, length_X_Input,
											deltaPartnerQueries, lengthDelta);

	NaorPinkas_OT_Sender_Transfer(writeSocket, readSocket, rawInputCircuit -> numInputs_P1, OT_Inputs,
								state, checkStatSecParam, concated_P1_Queries, params, C);
	OT_Outputs = NaorPinkas_OT_Receiver_Transfer(writeSocket, readSocket, rawInputCircuit -> numInputs_P2,
												keyPlusDeltaPrime, state, checkStatSecParam, deltaPrimeQueries, params, cTilde);


	deltaFromPartner = receiveBoth(readSocket, commBufferLen);
	if(0 == memcmp(deltaFromPartner, deltaPrime, lengthDelta))
	{
		equalityBit = 0x01;
	}

	setInputsFromNaorPinkas(circuitsArray_Partner, OT_Outputs, checkStatSecParam, partyID);

	int_c_1 = clock();
	int_t_1 = timestamp();
	printTiming(&int_t_0, &int_t_1, int_c_0, int_c_1, "subOT - Receiver");
	printZeroBothSubCounters();


	int_t_0 = timestamp();
	int_c_0 = clock();

	tempCTX = getRandCtxByCoinToss(writeSocket, readSocket, ctx, state, partyID);
	J_setPair = getJ_Set(tempCTX, partyID, checkStatSecParam);
	J_SetOwn = J_setPair[0];
	J_SetPartner = J_setPair[1];

	int_c_1 = clock();
	int_t_1 = timestamp();
	printTiming(&int_t_0, &int_t_1, int_c_0, int_c_1, "Coin flips");
	printZeroBothSubCounters();


	int_t_0 = timestamp();
	int_c_0 = clock();

	// Having exchanged J_sets the parties now reveal information needed to open the check circuits.
	commBuffer = jSetRevealSerialise(circuitsArray_Own, startOfInputChain, NP_consistentInputs, aList,
									keyPlusDeltaPrime, outputStruct_Own, circuitSeeds, J_SetPartner,
									rawInputCircuit -> numInputs_P2, rawInputCircuit -> numOutputs,
									checkStatSecParam, &commBufferLen);
	sendBoth(writeSocket, commBuffer, commBufferLen);
	free(commBuffer);

	commBuffer = receiveBoth(readSocket, commBufferLen);
	partnerReveals = jSetRevealDeserialise(circuitsArray_Partner, commBuffer, J_SetOwn, rawInputCircuit -> numInputs_P1,
										rawInputCircuit -> numOutputs, checkStatSecParam);
	free(commBuffer);


	commBuffer = serialiseSeeds(circuitSeeds, J_SetPartner, checkStatSecParam, &commBufferLen);
	sendBoth(writeSocket, commBuffer, commBufferLen);
	free(commBuffer);

	commBuffer = receiveBoth(readSocket, commBufferLen);
	partnerReveals -> revealedSeeds = deserialiseSeeds(commBuffer, J_SetOwn, checkStatSecParam);
	free(commBuffer);



	// Having received all that they need to verify check circuits, they now verifiy them.
	NP_Inputs_Partner = computeNaorPinkasInputsForJSet(C, partnerReveals -> aListRevealed, circuitsArray_Partner[0] -> numInputsBuilder,
													checkStatSecParam, params, J_SetOwn);
	
	printf("Delta Keys commitment : %d\n", testDeltaKeys(deltaCommits, NP_Inputs_Partner, J_SetOwn, deltaFromPartner,
														length_X_Input, lengthDelta, checkStatSecParam));

	jSetChecks = HKE_Step5_Checks(writeSocket, readSocket, rawInputCircuit, circuitsArray_Partner, C, partnerReveals,
								NP_consistentInputs, NP_Inputs_Partner, outputStruct_Partner, commitStruct,
								partnersCommitStruct, keyPlusDeltaPrime, J_SetOwn, J_SetPartner, checkStatSecParam,
								groupPartner, params, 0);

	setBuildersInputsNaorPinkas(circuitsArray_Partner, rawInputCircuit, partnerReveals -> builderInputsEval,
								J_SetOwn, checkStatSecParam, partyID);
	int_c_1 = clock();
	int_t_1 = timestamp();
	printTiming(&int_t_0, &int_t_1, int_c_0, int_c_1, "Initial J-Set checks");
	printZeroBothSubCounters();


	int_t_0 = timestamp();
	int_c_0 = clock();

	printf("\nEvaluating Circuits ");
	for(i = 0; i < checkStatSecParam; i ++)
	{
		if(0x00 == J_SetOwn[i])
		{
			printf("%d, ", i);
			fflush(stdout);
			runCircuitExec( circuitsArray_Partner[i], writeSocket, readSocket );
		}
	}
	printf("\n");

	int_c_1 = clock();
	int_t_1 = timestamp();
	printTiming(&int_t_0, &int_t_1, int_c_0, int_c_1, "Evaluate Circuits");
	printZeroBothSubCounters();


	// Check the Builder's inputs to the evaluation circuits is correct.
	int_t_0 = timestamp();
	int_c_0 = clock();

	bufferOffset = 0;
	commBufferLen = 0;
	commBuffer = receiveBoth(readSocket, commBufferLen);
	logChecks |= Step5_CheckLogarithms(commBuffer, partnerReveals -> builderInputsEval, concated_P1_Queries,
									params, J_SetOwn, checkStatSecParam, rawInputCircuit -> numInputs_P1,
									&bufferOffset);

	commBuffer = Step5_CalculateLogarithms(NP_consistentInputs, aList, deltaPrimeQueries, params, keyPlusDeltaPrime,
										J_SetPartner, checkStatSecParam, rawInputCircuit -> numInputs_P2, &commBufferLen);
	sendBoth(writeSocket, commBuffer, commBufferLen);
	free(commBuffer);
	printf("Sub-Log Checks: %d\n", logChecks);

	int_c_1 = clock();
	int_t_1 = timestamp();
	printTiming(&int_t_0, &int_t_1, int_c_0, int_c_1, "Sub-Log checks");
	printZeroBothSubCounters();


	// 
	int_t_0 = timestamp();
	int_c_0 = clock();

	binaryOutput = HKE_OutputDetermination(writeSocket, readSocket, state, circuitsArray_Partner, rawInputCircuit, groupPartner,
										partnerReveals, outputStruct_Own, outputStruct_Partner, checkStatSecParam, J_SetOwn, &commBufferLen, partyID);

	int_c_1 = clock();
	int_t_1 = timestamp();
	printTiming(&int_t_0, &int_t_1, int_c_0, int_c_1, "Output Determination");
	printZeroBothSubCounters();


	// We now XOR the output with the key we provided, giving us the true output.
	printf("Sub-Computation Output binary : ");
	for(i = 0; i < rawInputCircuit -> numOutputs; i ++)
	{
		binaryOutput[i] ^= keyPlusDeltaPrime[i];
		printf("%X", binaryOutput[i]);
	}
	printf("\n");


	// Check, did we enter a correct deltaPrime? If not we know the output is worthless.
	if(equalityBit == 0)
	{
		binaryOutput = NULL;
	}

	/*
	// Tidy up after ourselves.
	for(i = 0; i < checkStatSecParam; i ++)
	{
		freeCircuitStruct(circuitsArray_Partner[i], 0);
	}
	*/

	return binaryOutput;
}
Beispiel #11
0
void runBuilder_L_2013_CnC_OT(struct RawCircuit *rawInputCircuit, struct idAndValue *startOfInputChain, char *portNumStr, randctx *ctx)
{
	struct sockaddr_in destWrite, destRead;
	int writeSocket, readSocket, mainWriteSock, mainReadSock;
	int writePort = atoi(portNumStr), readPort = writePort + 1;

	struct Circuit **circuitsArray;
	struct RawCircuit *rawCheckCircuit = createRawCheckCircuit(rawInputCircuit -> numInputs_P1);
	int i, arrayLen, commBufferLen = 0, J_setSize = 0;
	const int lengthDelta = 40;

	struct timespec ext_t_0, ext_t_1;
	struct timespec int_t_0, int_t_1;
	clock_t ext_c_0, ext_c_1;
	clock_t int_c_0, int_c_1;

	gmp_randstate_t *state;
	struct public_builderPRS_Keys *public_inputs;
	struct secret_builderPRS_Keys *secret_inputs, *checkSecretInputs;
	struct eccParams *params;
	struct secCompBuilderOutput *SC_ReturnStruct;

	struct eccPoint **builderInputs, ***consistentInputs;
	unsigned char *commBuffer, *J_set, ***bLists, ***hashedB_Lists, *delta;
	unsigned char **Xj_checkValues, ***OT_Inputs, *inputArray;

	ub4 **circuitSeeds = (ub4 **) calloc(stat_SecParam, sizeof(ub4*));
	randctx **circuitCTXs = (randctx **) calloc(stat_SecParam, sizeof(randctx*));


	initRandGen();
	state = seedRandGen();
	Xj_checkValues = (unsigned char **) calloc(stat_SecParam, sizeof(unsigned char *));


	// Some initialisation of randomness and input arrays.
	inputArray = convertChainIntoArray(startOfInputChain, rawInputCircuit -> numInputs_P1);
	for(i = 0; i < stat_SecParam; i ++)
	{
		circuitCTXs[i] = (randctx*) calloc(1, sizeof(randctx));
		circuitSeeds[i] = getIsaacContext(circuitCTXs[i]);
	}


	set_up_server_socket(destWrite, writeSocket, mainWriteSock, writePort);
	set_up_server_socket(destRead, readSocket, mainReadSock, readPort);


	ext_t_0 = timestamp();
	ext_c_0 = clock();

	printf("Executor has connected to us.\n");


	int_t_0 = timestamp();
	int_c_0 = clock();

	// Input/Output generation
	params = initBrainpool_256_Curve();
	secret_inputs = generateSecrets(rawInputCircuit -> numInputs_P1, stat_SecParam, params, *state);
	public_inputs = computePublicInputs(secret_inputs, params);
	delta = generateRandBytes(16, 16);

	bLists = generateConsistentOutputs(delta, rawInputCircuit -> numOutputs);
	hashedB_Lists = generateConsistentOutputsHashTables(bLists, rawInputCircuit -> numOutputs);

	// Check values for use in the modified CnC OT.
	for(i = 0; i < stat_SecParam; i ++)
	{
		Xj_checkValues[i] = generateRandBytes(16, 16);		
	}

	int_c_1 = clock();
	int_t_1 = timestamp();
	printTiming(&int_t_0, &int_t_1, int_c_0, int_c_1, "Circuit Input Prep");
	printAndZeroBothCounters();


	int_t_0 = timestamp();
	int_c_0 = clock();

	circuitsArray = buildAllCircuitsConsistentOutput(rawInputCircuit, startOfInputChain, *state, stat_SecParam, bLists[0], bLists[1],
													params, secret_inputs, public_inputs, circuitCTXs, circuitSeeds);

	int_c_1 = clock();
	int_t_1 = timestamp();
	printTiming(&int_t_0, &int_t_1, int_c_0, int_c_1, "Circuit Building");
	printAndZeroBothCounters();


	int_t_0 = timestamp();
	int_c_0 = clock();

	OT_Inputs = getAllInputKeys(circuitsArray, stat_SecParam);
	full_CnC_OT_Mod_Sender_ECC(writeSocket, readSocket, circuitsArray[0] -> numInputsExecutor, OT_Inputs, Xj_checkValues, state, stat_SecParam, 1024);

	int_c_1 = clock();
	int_t_1 = timestamp();
	printTiming(&int_t_0, &int_t_1, int_c_0, int_c_1, "OT - Sender");
	printAndZeroBothCounters();


	int_t_0 = timestamp();
	int_c_0 = clock();

	// Send all the public_builder_PRS_keys, thus commiting the Builder to the soon to follow circuits.
	sendPublicCommitments(writeSocket, readSocket, public_inputs, params);

	for(i = 0; i < stat_SecParam; i++)
	{
		sendCircuit(writeSocket, readSocket, circuitsArray[i]);
	}

	commBufferLen = 0;
	commBuffer = serialise3D_UChar_Array(hashedB_Lists, rawInputCircuit -> numOutputs, 16, &commBufferLen);
	sendBoth(writeSocket, commBuffer, commBufferLen);
	free(commBuffer);

	int_c_1 = clock();
	int_t_1 = timestamp();
	printTiming(&int_t_0, &int_t_1, int_c_0, int_c_1, "Sending Circuit and Commitments.");
	fflush(stdout);
	printAndZeroBothCounters();

	int_t_0 = timestamp();
	int_c_0 = clock();

	// At this point receive from the Executor the proof of the J-set. Then provide the relevant r_j's.
	J_set = builder_decommitToJ_Set(writeSocket, readSocket, circuitsArray, secret_inputs, stat_SecParam, &J_setSize, circuitSeeds);


	builderInputs = computeBuilderInputs(public_inputs, secret_inputs,
								J_set, J_setSize, startOfInputChain, 
								params, &arrayLen);

	commBuffer = serialise_ECC_Point_Array(builderInputs, arrayLen, &commBufferLen);
	sendBoth(writeSocket, commBuffer, commBufferLen);
	free(commBuffer);

	// Serialise and send the permuted input bits for the Builder's inputs on the evaluation circuits.
	commBuffer = serialiseBuilderInputBits(circuitsArray, inputArray, rawInputCircuit -> numInputs_P1,
										J_set, stat_SecParam, &commBufferLen);
	sendBoth(writeSocket, commBuffer, commBufferLen);
	free(commBuffer);


	checkSecretInputs = generateSecretsCheckComp(rawInputCircuit -> numInputs_P1, 3 * stat_SecParam,
												secret_inputs, params, *state);

	int_c_1 = clock();
	int_t_1 = timestamp();
	printTiming(&int_t_0, &int_t_1, int_c_0, int_c_1, "Decommit to J_Sets secrets");
	printAndZeroBothCounters();


	int_t_0 = timestamp();
	int_c_0 = clock();

	// Run the sub-computation with 3 * S many circuits.
	SC_ReturnStruct = SC_DetectCheatingBuilder(writeSocket, readSocket, rawCheckCircuit,
											startOfInputChain, delta, lengthDelta,
	 										checkSecretInputs, (3 * stat_SecParam), state);

	int_c_1 = clock();
	int_t_1 = timestamp();
	printTiming(&int_t_0, &int_t_1, int_c_0, int_c_1, "Full cheating detection sub-computation");
	printAndZeroBothCounters();


	int_t_0 = timestamp();
	int_c_0 = clock();

	commBufferLen = 0;
	commBuffer = serialise3D_UChar_Array(bLists, rawInputCircuit -> numOutputs, 16, &commBufferLen);
	sendBoth(writeSocket, commBuffer, commBufferLen);
	free(commBuffer);

	int_c_1 = clock();
	int_t_1 = timestamp();
	printTiming(&int_t_0, &int_t_1, int_c_0, int_c_1, "Sending bLists");
	printAndZeroBothCounters();


	int_t_0 = timestamp();
	int_c_0 = clock();

	proveConsistencyEvaluationKeys_Builder_L_2013(writeSocket, readSocket, J_set, J_setSize,
											startOfInputChain, builderInputs,
											public_inputs, secret_inputs, params, SC_ReturnStruct, state);

	int_c_1 = clock();
	int_t_1 = timestamp();
	printTiming(&int_t_0, &int_t_1, int_c_0, int_c_1, "Proving consistency");
	printAndZeroBothCounters();


	ext_c_1 = clock();
	ext_t_1 = timestamp();

	freeRawCircuit(rawInputCircuit);

	printTiming(&ext_t_0, &ext_t_1, ext_c_0, ext_c_1, "Total time without connection setup");
	printBothTotalCounters();

	free_idAndValueChain(startOfInputChain);

	/*
	Freeing here is pointless because we're so close to finish when the OS will do it for us.

	for(i = 0; i < stat_SecParam; i ++)
	{
		freeCircuitStruct(circuitsArray[i], 0);
	}
	*/

	close_server_socket(writeSocket, mainWriteSock);
	close_server_socket(readSocket, mainReadSock);
}
Beispiel #12
0
int
putFileUtil( rcComm_t *conn, char *srcPath, char *targPath, rodsLong_t srcSize,
             rodsArguments_t *rodsArgs, dataObjInp_t *dataObjOprInp ) {
    int status;
    struct timeval startTime, endTime;

    if ( srcPath == NULL || targPath == NULL ) {
        rodsLog( LOG_ERROR,
                 "putFileUtil: NULL srcPath or targPath input" );
        return USER__NULL_INPUT_ERR;
    }

    if ( conn->fileRestart.info.status == FILE_RESTARTED &&
            strcmp( conn->fileRestart.info.objPath, targPath ) == 0 ) {
        /* it was restarted */
        conn->fileRestart.info.status = FILE_NOT_RESTART;
        return 0;
    }

    if ( rodsArgs->verbose == True ) {
        ( void ) gettimeofday( &startTime, ( struct timezone * )0 );
    }

    if ( gGuiProgressCB != NULL ) {
        rstrcpy( conn->operProgress.curFileName, srcPath, MAX_NAME_LEN );
        conn->operProgress.curFileSize = srcSize;
        conn->operProgress.curFileSizeDone = 0;
        conn->operProgress.flag = 0;
        gGuiProgressCB( &conn->operProgress );
    }

    /* have to take care of checksum here since it needs to be recalculated */
    if ( rodsArgs->checksum == True ) {
        // set the expected flag to indicate that we want a server-side
        // checksum computed and stored in the catalog
        addKeyVal( &dataObjOprInp->condInput, REG_CHKSUM_KW, "" );

    }
    else if ( rodsArgs->verifyChecksum == True ) {
        rodsEnv env;
        int ret = getRodsEnv( &env );
        if ( ret < 0 ) {
            rodsLog(
                LOG_ERROR,
                "putFileUtil - failed to capture rods env %d",
                ret );
            return ret;
        }

        status = rcChksumLocFile( srcPath,
                                  VERIFY_CHKSUM_KW,
                                  &dataObjOprInp->condInput,
                                  env.rodsDefaultHashScheme );
        if ( status < 0 ) {
            rodsLogError( LOG_ERROR, status,
                          "putFileUtil: rcChksumLocFile error for %s, status = %d",
                          srcPath, status );
            return status;
        }
    }
    if ( strlen( targPath ) >= MAX_PATH_ALLOWED - 1 ) {
        return USER_PATH_EXCEEDS_MAX;
    }
    rstrcpy( dataObjOprInp->objPath, targPath, MAX_NAME_LEN );
    dataObjOprInp->dataSize = srcSize;
    status = rcDataObjPut( conn, dataObjOprInp, srcPath );

    if ( status >= 0 ) {
        if ( rodsArgs->verbose == True ) {
            ( void ) gettimeofday( &endTime, ( struct timezone * )0 );
            printTiming( conn, dataObjOprInp->objPath, srcSize, srcPath,
                         &startTime, &endTime );
        }
        if ( gGuiProgressCB != NULL ) {
            conn->operProgress.totalNumFilesDone++;
            conn->operProgress.totalFileSizeDone += srcSize;
        }
    }

    return status;
}
Beispiel #13
0
void RenderTimer::Notify()
{
    canvas->draw();
    printTiming();
}
Beispiel #14
0
void runExecutor_L_2013_CnC_OT(struct RawCircuit *rawInputCircuit, struct idAndValue *startOfInputChain, char *ipAddress, char *portNumStr, randctx *ctx)
{
	struct sockaddr_in serv_addr_write, serv_addr_read;
	int writeSocket, readSocket;
	int readPort = atoi(portNumStr), writePort = readPort + 1;
	const int lengthDelta = 40;
	int i, consistency;

	struct RawCircuit *rawCheckCircuit;
	struct Circuit **circuitsArray = (struct Circuit**) calloc(stat_SecParam, sizeof(struct Circuit*));

	struct revealedCheckSecrets *secretsRevealed;
	struct publicInputsWithGroup *pubInputGroup;
	struct secCompExecutorOutput *SC_ReturnStruct;
	unsigned char *J_set, *deltaPrime, *permedInputs, **OT_Outputs;

	gmp_randstate_t *state;

	struct eccPoint **builderInputs, ***consistentInputs;
	int arrayLen, commBufferLen = 0, bufferOffset, J_setSize = 0, circuitsChecked = 0;
	unsigned char *commBuffer, ***bLists, ***outputHashTable;

	struct timespec ext_t_0, ext_t_1;
	struct timespec int_t_0, int_t_1;
	clock_t ext_c_0, ext_c_1;
	clock_t int_c_0, int_c_1;


	set_up_client_socket(readSocket, ipAddress, readPort, serv_addr_read);
	set_up_client_socket(writeSocket, ipAddress, writePort, serv_addr_write);

	ext_t_0 = timestamp();
	ext_c_0 = clock();

	printf("Connected to builder.\n");

	rawCheckCircuit = createRawCheckCircuit(rawInputCircuit -> numInputs_P1);

	state = seedRandGen();
	permedInputs = convertChainIntoArray(startOfInputChain, rawInputCircuit -> numInputs_P2);


	int_t_0 = timestamp();
	int_c_0 = clock();

	J_set = full_CnC_OT_Mod_Receiver_ECC_Alt(writeSocket, readSocket, &OT_Outputs, rawInputCircuit -> numInputs_P2,
											state, startOfInputChain, permedInputs, stat_SecParam, 1024);

	int_c_1 = clock();
	int_t_1 = timestamp();
	printTiming(&int_t_0, &int_t_1, int_c_0, int_c_1, "OT - Receiver");
	printAndZeroBothCounters();



	int_t_0 = timestamp();
	int_c_0 = clock();
	pubInputGroup = receivePublicCommitments(writeSocket, readSocket);

	for(i = 0; i < stat_SecParam; i ++)
	{
		circuitsArray[i] = receiveFullCircuit(writeSocket, readSocket);
	}

	commBufferLen = 0;
	bufferOffset = 0;
	commBuffer = receiveBoth(readSocket, commBufferLen);
	outputHashTable = deserialise3D_UChar_Array(commBuffer, rawInputCircuit -> numOutputs, 16, &bufferOffset);
	free(commBuffer);

	int_c_1 = clock();
	int_t_1 = timestamp();
	printTiming(&int_t_0, &int_t_1, int_c_0, int_c_1, "Received Circuits + Hash tables + Public inputs");
	printAndZeroBothCounters();


	// Input our own bit values.
	for(i = 0; i < stat_SecParam; i ++)
	{
		setCircuitsInputs_Values(startOfInputChain, circuitsArray[i], 0x00);
	}
	setInputsFromCharArray(circuitsArray, OT_Outputs, stat_SecParam);


	int_t_0 = timestamp();
	int_c_0 = clock();

	// Here we do the decommit...
	secretsRevealed = executor_decommitToJ_Set(writeSocket, readSocket, circuitsArray, pubInputGroup -> public_inputs,
							pubInputGroup -> params, J_set, &J_setSize, stat_SecParam);

	commBufferLen = 0;
	bufferOffset = 0;
	commBuffer = receiveBoth(readSocket, commBufferLen);
	builderInputs = deserialise_ECC_Point_Array(commBuffer, &arrayLen, &bufferOffset);
	free(commBuffer);

	commBuffer = receiveBoth(readSocket, commBufferLen);
	setBuilderInputs(builderInputs, commBuffer, J_set, J_setSize, circuitsArray,
					pubInputGroup -> public_inputs, pubInputGroup -> params);
	free(commBuffer);

	int_c_1 = clock();
	int_t_1 = timestamp();
	printTiming(&int_t_0, &int_t_1, int_c_0, int_c_1, "Decommit to and receive check circuit secrets.");
	printAndZeroBothCounters();


	int_t_0 = timestamp();
	int_c_0 = clock();

	printf("\nEvaluating Circuits ");
	fflush(stdout);

	for(i = 0; i < stat_SecParam; i ++)
	{
		if(0x00 == J_set[i])
		{
			printf("%d, ", i);
			fflush(stdout);
			runCircuitExec( circuitsArray[i], writeSocket, readSocket );
		}
	}

	int_c_1 = clock();
	int_t_1 = timestamp();
	printTiming(&int_t_0, &int_t_1, int_c_0, int_c_1, "Evaluating all circuits");
	printAndZeroBothCounters();


	int_t_0 = timestamp();
	int_c_0 = clock();

	// Perform the cheating detection function.
	deltaPrime = expandDeltaPrim(circuitsArray, J_set, stat_SecParam);
	SC_ReturnStruct = SC_DetectCheatingExecutor(writeSocket, readSocket, rawCheckCircuit,
	 											deltaPrime, lengthDelta, 3 * stat_SecParam, state );

	int_c_1 = clock();
	int_t_1 = timestamp();
	printTiming(&int_t_0, &int_t_1, int_c_0, int_c_1, "Full sub-computation");
	printAndZeroBothCounters();

	int_t_0 = timestamp();
	int_c_0 = clock();

	commBufferLen = 0;
	bufferOffset = 0;
	commBuffer = receiveBoth(readSocket, commBufferLen);
	bLists = deserialise3D_UChar_Array(commBuffer, rawInputCircuit -> numOutputs, 16, &bufferOffset);
	free(commBuffer);

	printf("\nVerified B_Lists = %d\n", verifyB_Lists(outputHashTable, bLists, circuitsArray[0] -> numInputsExecutor));

	int_c_1 = clock();
	int_t_1 = timestamp();
	printTiming(&int_t_0, &int_t_1, int_c_0, int_c_1, "Verifying B Lists");
	printAndZeroBothCounters();


	int_t_0 = timestamp();
	int_c_0 = clock();


	consistentInputs = getAllConsistentInputsAsPoints(secretsRevealed -> revealedSecrets, pubInputGroup -> public_inputs -> public_keyPairs, pubInputGroup -> params, J_set, stat_SecParam, rawInputCircuit -> numInputs_P1);
	circuitsChecked = secretInputsToCheckCircuitsConsistentOutputs(circuitsArray, rawInputCircuit, consistentInputs, 
																secretsRevealed -> revealedCircuitSeeds, bLists[0], bLists[1], pubInputGroup -> params,
																J_set, J_setSize, stat_SecParam);

	printf("Circuits Correct = %d\n", circuitsChecked);

	int_c_1 = clock();
	int_t_1 = timestamp();
	printTiming(&int_t_0, &int_t_1, int_c_0, int_c_1, "Checking Circuits Correct");
	printAndZeroBothCounters();


	int_t_0 = timestamp();
	int_c_0 = clock();

	proveConsistencyEvaluationKeys_Exec_L_2013(writeSocket, readSocket, J_set, J_setSize,
											builderInputs, pubInputGroup -> public_inputs,
											pubInputGroup -> params,
											SC_ReturnStruct, state);
	
	int_c_1 = clock();
	int_t_1 = timestamp();
	printTiming(&int_t_0, &int_t_1, int_c_0, int_c_1, "Builder Inputs consistency checked");
	printAndZeroBothCounters();

	clearPublicInputsWithGroup(pubInputGroup);

	#pragma omp parallel for private(i) schedule(auto)
	for(i = 0; i < arrayLen; i ++)
	{
		clearECC_Point(builderInputs[i]);
	}
	free(builderInputs);


	ext_c_1 = clock();
	ext_t_1 = timestamp();
	printTiming(&ext_t_0, &ext_t_1, ext_c_0, ext_c_1, "Total time without connection setup");
	printBothTotalCounters();

	close_client_socket(readSocket);
	close_client_socket(writeSocket);


	// If the secure computation to detect cheating returned NULL it means everything is fine.
	// Just use the normal majority output. Else the Secure Comp. to detect cheating returned X.
	// Then we use the Raw Binary Circuit to compute the true answer. 
	if(NULL == SC_ReturnStruct -> output)
	{
		printMajorityOutputAsBinary(circuitsArray, stat_SecParam, J_set);
	}
	else
	{
		setRawCircuitsInputs_Hardcode(startOfInputChain, rawInputCircuit -> gates);
		setRawCircuitsInputs_Hardcode(SC_ReturnStruct -> output, 0, rawInputCircuit -> numInputs_P1, rawInputCircuit -> gates);
		evaluateRawCircuit(rawInputCircuit);
		printOutputHexString_Raw(rawInputCircuit);
	}

	
	/*
	for(i = 0; i < stat_SecParam; i ++)
	{
		freeCircuitStruct(circuitsArray[i], 1);
	}
	*/
	freeRawCircuit(rawInputCircuit);
	free_idAndValueChain(startOfInputChain);

}
Beispiel #15
0
int
rsyncDataToDataUtil (rcComm_t *conn, rodsPath_t *srcPath, 
rodsPath_t *targPath, rodsEnv *myRodsEnv, rodsArguments_t *myRodsArgs, 
dataObjCopyInp_t *dataObjCopyInp)
{
    int status;
    struct timeval startTime, endTime;
    int syncFlag = 0;
    int cpFlag = 0;
 
    if (srcPath == NULL || targPath == NULL) {
       rodsLog (LOG_ERROR,
          "rsyncDataToDataUtil: NULL srcPath or targPath input");
        return (USER__NULL_INPUT_ERR);
    }
    /* check the age */
    if (myRodsArgs->age == True) {
        if (srcPath->rodsObjStat != NULL) {
           if (ageExceeded (myRodsArgs->agevalue,
              atoi (srcPath->rodsObjStat->modifyTime), myRodsArgs->verbose,
              srcPath->outPath, srcPath->size)) return 0;
        }
    }
    if (myRodsArgs->verbose == True) {
        (void) gettimeofday(&startTime, (struct timezone *)0);
        bzero (&conn->transStat, sizeof (transStat_t));
    }

    if (targPath->objState == NOT_EXIST_ST) {
	cpFlag = 1;
    } else if (myRodsArgs->sizeFlag == True) {
	/* sync by size */
	if (targPath->size != srcPath->size) {
	    cpFlag = 1;
	}
    } else if (strlen (srcPath->chksum) > 0 && strlen (targPath->chksum) > 0) {
	/* src and trg has a checksum value */
	if (strcmp (targPath->chksum, srcPath->chksum) != 0) {
	    cpFlag = 1;
	}
    } else { 
	syncFlag = 1;
    }

    if (cpFlag == 1) {
        rstrcpy (dataObjCopyInp->srcDataObjInp.objPath, srcPath->outPath, 
          MAX_NAME_LEN);
        dataObjCopyInp->srcDataObjInp.dataSize = srcPath->size;
        rstrcpy (dataObjCopyInp->destDataObjInp.objPath, targPath->outPath, 
          MAX_NAME_LEN);
	/* only do the sync if no -l option specified */
	if ( myRodsArgs->longOption != True ) {
	    status = rcDataObjCopy (conn, dataObjCopyInp);
	} else {
	    status = 0;
	    printf ("%s   %lld   N\n", srcPath->outPath, srcPath->size);
	}
    } else if (syncFlag == 1) {
	dataObjInp_t *dataObjOprInp = &dataObjCopyInp->destDataObjInp;
        rstrcpy (dataObjOprInp->objPath, srcPath->outPath, 
          MAX_NAME_LEN);
        dataObjOprInp->dataSize = srcPath->size;
        addKeyVal (&dataObjOprInp->condInput, RSYNC_DEST_PATH_KW,
          targPath->outPath);
	addKeyVal (&dataObjOprInp->condInput, RSYNC_MODE_KW, IRODS_TO_IRODS);
	/* only do the sync if no -l option specified */
	if ( myRodsArgs->longOption != True ) {
	    status = rcDataObjRsync (conn, dataObjOprInp);
	} else {
	    status = 0;
	    printf ("%s   %lld   N\n", srcPath->outPath, srcPath->size);
	}
        rmKeyVal (&dataObjOprInp->condInput, RSYNC_MODE_KW);
        rmKeyVal (&dataObjOprInp->condInput, RSYNC_DEST_PATH_KW);
    } else {
	status = 0;
    }

    if (status >= 0 && myRodsArgs->verbose == True) {
	if (cpFlag > 0 ||
	  (syncFlag > 0 && status == SYS_RSYNC_TARGET_MODIFIED)) {
            (void) gettimeofday(&endTime, (struct timezone *)0);
            printTiming (conn, srcPath->outPath, srcPath->size, 
	      targPath->outPath, &startTime, &endTime);
	} else {
            printNoSync (srcPath->outPath, srcPath->size, "a match");
        }
    }

    return (status);
}
Beispiel #16
0
int
rsyncFileToDataUtil (rcComm_t *conn, rodsPath_t *srcPath, 
rodsPath_t *targPath, rodsEnv *myRodsEnv, rodsArguments_t *myRodsArgs, 
dataObjInp_t *dataObjOprInp)
{
    int status;
    struct timeval startTime, endTime;
    int putFlag = 0;
    int syncFlag = 0;
    char *chksum;
 
    if (srcPath == NULL || targPath == NULL) {
       rodsLog (LOG_ERROR,
          "rsyncFileToDataUtil: NULL srcPath or targPath input");
        return (USER__NULL_INPUT_ERR);
    }
    /* check the age */
    if (myRodsArgs->age == True) {
#ifndef windows_platform
        struct stat statbuf;
        status = stat (srcPath->outPath, &statbuf);
#else
        struct irodsntstat statbuf;
        status = iRODSNt_stat(srcPath->outPath, &statbuf);
#endif
	if (status < 0) {
            rodsLog (LOG_ERROR,
              "rsyncFileToDataUtil: stat error for %s, errno = %d\n",
              srcPath->outPath, errno);
            return (USER_INPUT_PATH_ERR);
        }
	if (ageExceeded (myRodsArgs->agevalue, statbuf.st_mtime,
          myRodsArgs->verbose, srcPath->outPath, srcPath->size)) return 0;
    }

    if (myRodsArgs->verbose == True) {
        (void) gettimeofday(&startTime, (struct timezone *)0);
        bzero (&conn->transStat, sizeof (transStat_t));
    }

    if (targPath->objState == NOT_EXIST_ST) {
	putFlag = 1;
    } else if (myRodsArgs->sizeFlag == True) {
	/* sync by size */
	if (targPath->size != srcPath->size) {
	    putFlag = 1;
	}
    } else if (strlen (targPath->chksum) > 0) {
	/* src has a checksum value */
        status = rcChksumLocFile (srcPath->outPath, RSYNC_CHKSUM_KW,
          &dataObjOprInp->condInput, extractHashFunction3(myRodsArgs));
        if (status < 0) {
            rodsLogError (LOG_ERROR, status,
              "rsyncFileToDataUtil: rcChksumLocFile error for %s, status = %d",
              srcPath->outPath, status);
            return (status);
        } else {
	    chksum = getValByKey (&dataObjOprInp->condInput, RSYNC_CHKSUM_KW);
	    if (strcmp (chksum, targPath->chksum) != 0) {
                if (myRodsArgs->verifyChecksum == True) {
		    addKeyVal (&dataObjOprInp->condInput, VERIFY_CHKSUM_KW,
                      chksum);
                }
		putFlag = 1;
	    }
	}
    } else { 
	/* exist but no chksum */
        status = rcChksumLocFile (srcPath->outPath, RSYNC_CHKSUM_KW,
          &dataObjOprInp->condInput, extractHashFunction3(myRodsArgs));
        if (status < 0) {
            rodsLogError (LOG_ERROR, status,
              "rsyncFileToDataUtil: rcChksumLocFile error for %s, status = %d",
              srcPath->outPath, status);
            return (status);
        } else {
            chksum = getValByKey (&dataObjOprInp->condInput, RSYNC_CHKSUM_KW);
            if (myRodsArgs->verifyChecksum == True) {
                addKeyVal (&dataObjOprInp->condInput, VERIFY_CHKSUM_KW, chksum);
            }
	    syncFlag = 1;
	}
    }

    if (putFlag + syncFlag > 0) {
        rstrcpy (dataObjOprInp->objPath, targPath->outPath, MAX_NAME_LEN);
        dataObjOprInp->dataSize = srcPath->size;
        dataObjOprInp->openFlags = O_WRONLY;
    }

    if (putFlag == 1) {
	/* only do the sync if no -l option specified */
	if ( myRodsArgs->longOption != True ) { 
            status = rcDataObjPut (conn, dataObjOprInp, srcPath->outPath);
	} else {
	    status = 0;
            printf ("%s   %lld   N\n", srcPath->outPath, srcPath->size);
	}
        rmKeyVal (&dataObjOprInp->condInput, RSYNC_CHKSUM_KW);
    } else if (syncFlag == 1) {
	addKeyVal (&dataObjOprInp->condInput, RSYNC_DEST_PATH_KW, 
	  srcPath->outPath);
	addKeyVal (&dataObjOprInp->condInput, RSYNC_MODE_KW, IRODS_TO_LOCAL);
	/* only do the sync if no -l option specified */
	if ( myRodsArgs->longOption != True ) {
	    status = rcDataObjRsync (conn, dataObjOprInp);
	} else {
            status = 0;
            printf ("%s   %lld   N\n", srcPath->outPath, srcPath->size);
	}
        rmKeyVal (&dataObjOprInp->condInput, RSYNC_DEST_PATH_KW);
        rmKeyVal (&dataObjOprInp->condInput, RSYNC_MODE_KW);
    } else {
        status = 0;
    }

    if (status >= 0 && myRodsArgs->verbose == True) {
	if (putFlag > 0 ||
	  (syncFlag > 0 && status == SYS_RSYNC_TARGET_MODIFIED)) {
            (void) gettimeofday(&endTime, (struct timezone *)0);
            printTiming (conn, srcPath->outPath, srcPath->size,
              targPath->outPath, &startTime, &endTime);
        } else {
            printNoSync (srcPath->outPath, srcPath->size, "a match");
        }
    }

    return (status);
}
void runBeagle(int resource, 
               int stateCount, 
               int ntaxa, 
               int nsites, 
               bool manualScaling, 
               bool autoScaling,
               bool dynamicScaling,
               int rateCategoryCount,
               int nreps,
               bool fullTiming,
               bool requireDoublePrecision,
               bool requireSSE,
               int compactTipCount,
               int randomSeed,
               int rescaleFrequency,
               bool unrooted,
               bool calcderivs,
               bool logscalers,
               int eigenCount,
               bool eigencomplex,
               bool ievectrans,
               bool setmatrix)
{
    
    int edgeCount = ntaxa*2-2;
    int internalCount = ntaxa-1;
    int partialCount = ((ntaxa+internalCount)-compactTipCount)*eigenCount;
    int scaleCount = ((manualScaling || dynamicScaling) ? ntaxa : 0);
    
    BeagleInstanceDetails instDetails;
    
    // create an instance of the BEAGLE library
	int instance = beagleCreateInstance(
			    ntaxa,			  /**< Number of tip data elements (input) */
				partialCount, /**< Number of partials buffers to create (input) */
                compactTipCount,	/**< Number of compact state representation buffers to create (input) */
				stateCount,		  /**< Number of states in the continuous-time Markov chain (input) */
				nsites,			  /**< Number of site patterns to be handled by the instance (input) */
				eigenCount,		          /**< Number of rate matrix eigen-decomposition buffers to allocate (input) */
                (calcderivs ? (3*edgeCount*eigenCount) : edgeCount*eigenCount),/**< Number of rate matrix buffers (input) */
                rateCategoryCount,/**< Number of rate categories */
                scaleCount*eigenCount,          /**< scaling buffers */
				&resource,		  /**< List of potential resource on which this instance is allowed (input, NULL implies no restriction */
				1,			      /**< Length of resourceList list (input) */
                0,         /**< Bit-flags indicating preferred implementation charactertistics, see BeagleFlags (input) */
                (ievectrans ? BEAGLE_FLAG_INVEVEC_TRANSPOSED : BEAGLE_FLAG_INVEVEC_STANDARD) |
                (logscalers ? BEAGLE_FLAG_SCALERS_LOG : BEAGLE_FLAG_SCALERS_RAW) |
                (eigencomplex ? BEAGLE_FLAG_EIGEN_COMPLEX : BEAGLE_FLAG_EIGEN_REAL) |
                (dynamicScaling ? BEAGLE_FLAG_SCALING_DYNAMIC : 0) | 
                (autoScaling ? BEAGLE_FLAG_SCALING_AUTO : 0) |
                (requireDoublePrecision ? BEAGLE_FLAG_PRECISION_DOUBLE : BEAGLE_FLAG_PRECISION_SINGLE) |
                (requireSSE ? BEAGLE_FLAG_VECTOR_SSE : BEAGLE_FLAG_VECTOR_NONE),	  /**< Bit-flags indicating required implementation characteristics, see BeagleFlags (input) */
				&instDetails);
    if (instance < 0) {
	    fprintf(stderr, "Failed to obtain BEAGLE instance\n\n");
	    return;
    }
        
    int rNumber = instDetails.resourceNumber;
    fprintf(stdout, "Using resource %i:\n", rNumber);
    fprintf(stdout, "\tRsrc Name : %s\n",instDetails.resourceName);
    fprintf(stdout, "\tImpl Name : %s\n", instDetails.implName);    
    
    if (!(instDetails.flags & BEAGLE_FLAG_SCALING_AUTO))
        autoScaling = false;
    
    // set the sequences for each tip using partial likelihood arrays
	gt_srand(randomSeed);	// fix the random seed...
    for(int i=0; i<ntaxa; i++)
    {
        if (i >= compactTipCount) {
            double* tmpPartials = getRandomTipPartials(nsites, stateCount);
            beagleSetTipPartials(instance, i, tmpPartials);
            free(tmpPartials);
        } else {
            int* tmpStates = getRandomTipStates(nsites, stateCount);
            beagleSetTipStates(instance, i, tmpStates);
            free(tmpStates);                
        }
    }
    
#ifdef _WIN32
	std::vector<double> rates(rateCategoryCount);
#else
    double rates[rateCategoryCount];
#endif
	
    for (int i = 0; i < rateCategoryCount; i++) {
        rates[i] = gt_rand() / (double) GT_RAND_MAX;
    }
    
	beagleSetCategoryRates(instance, &rates[0]);
    
	double* patternWeights = (double*) malloc(sizeof(double) * nsites);
    
    for (int i = 0; i < nsites; i++) {
        patternWeights[i] = gt_rand() / (double) GT_RAND_MAX;
    }    

    beagleSetPatternWeights(instance, patternWeights);
    
    free(patternWeights);
	
    // create base frequency array

#ifdef _WIN32
	std::vector<double> freqs(stateCount);
#else
    double freqs[stateCount];
#endif
    
    // create an array containing site category weights
#ifdef _WIN32
	std::vector<double> weights(rateCategoryCount);
#else
    double weights[rateCategoryCount];
#endif

    for (int eigenIndex=0; eigenIndex < eigenCount; eigenIndex++) {
        for (int i = 0; i < rateCategoryCount; i++) {
            weights[i] = gt_rand() / (double) GT_RAND_MAX;
        } 
    
        beagleSetCategoryWeights(instance, eigenIndex, &weights[0]);
    }
    
    double* eval;
    if (!eigencomplex)
        eval = (double*)malloc(sizeof(double)*stateCount);
    else
        eval = (double*)malloc(sizeof(double)*stateCount*2);
    double* evec = (double*)malloc(sizeof(double)*stateCount*stateCount);
    double* ivec = (double*)malloc(sizeof(double)*stateCount*stateCount);
    
    for (int eigenIndex=0; eigenIndex < eigenCount; eigenIndex++) {
        if (!eigencomplex && ((stateCount & (stateCount-1)) == 0)) {
            
            for (int i=0; i<stateCount; i++) {
                freqs[i] = 1.0 / stateCount;
            }

            // an eigen decomposition for the general state-space JC69 model
            // If stateCount = 2^n is a power-of-two, then Sylvester matrix H_n describes
            // the eigendecomposition of the infinitesimal rate matrix
             
            double* Hn = evec;
            Hn[0*stateCount+0] = 1.0; Hn[0*stateCount+1] =  1.0; 
            Hn[1*stateCount+0] = 1.0; Hn[1*stateCount+1] = -1.0; // H_1
         
            for (int k=2; k < stateCount; k <<= 1) {
                // H_n = H_1 (Kronecker product) H_{n-1}
                for (int i=0; i<k; i++) {
                    for (int j=i; j<k; j++) {
                        double Hijold = Hn[i*stateCount + j];
                        Hn[i    *stateCount + j + k] =  Hijold;
                        Hn[(i+k)*stateCount + j    ] =  Hijold;
                        Hn[(i+k)*stateCount + j + k] = -Hijold;
                        
                        Hn[j    *stateCount + i + k] = Hn[i    *stateCount + j + k];
                        Hn[(j+k)*stateCount + i    ] = Hn[(i+k)*stateCount + j    ];
                        Hn[(j+k)*stateCount + i + k] = Hn[(i+k)*stateCount + j + k];                                
                    }
                }        
            }
            
            // Since evec is Hadamard, ivec = (evec)^t / stateCount;    
            for (int i=0; i<stateCount; i++) {
                for (int j=i; j<stateCount; j++) {
                    ivec[i*stateCount+j] = evec[j*stateCount+i] / stateCount;
                    ivec[j*stateCount+i] = ivec[i*stateCount+j]; // Symmetric
                }
            }
           
            eval[0] = 0.0;
            for (int i=1; i<stateCount; i++) {
                eval[i] = -stateCount / (stateCount - 1.0);
            }
       
        } else if (!eigencomplex) {
            for (int i=0; i<stateCount; i++) {
                freqs[i] = gt_rand() / (double) GT_RAND_MAX;
            }
        
            double** qmat=New2DArray<double>(stateCount, stateCount);    
            double* relNucRates = new double[(stateCount * stateCount - stateCount) / 2];
            
            int rnum=0;
            for(int i=0;i<stateCount;i++){
                for(int j=i+1;j<stateCount;j++){
                    relNucRates[rnum] = gt_rand() / (double) GT_RAND_MAX;
                    qmat[i][j]=relNucRates[rnum] * freqs[j];
                    qmat[j][i]=relNucRates[rnum] * freqs[i];
                    rnum++;
                }
            }

            //set diags to sum rows to 0
            double sum;
            for(int x=0;x<stateCount;x++){
                sum=0.0;
                for(int y=0;y<stateCount;y++){
                    if(x!=y) sum+=qmat[x][y];
                        }
                qmat[x][x]=-sum;
            } 
            
            double* eigvalsimag=new double[stateCount];
            double** eigvecs=New2DArray<double>(stateCount, stateCount);//eigenvecs
            double** teigvecs=New2DArray<double>(stateCount, stateCount);//temp eigenvecs
            double** inveigvecs=New2DArray<double>(stateCount, stateCount);//inv eigenvecs    
            int* iwork=new int[stateCount];
            double* work=new double[stateCount];
            
            EigenRealGeneral(stateCount, qmat, eval, eigvalsimag, eigvecs, iwork, work);
            memcpy(*teigvecs, *eigvecs, stateCount*stateCount*sizeof(double));
            InvertMatrix(teigvecs, stateCount, work, iwork, inveigvecs);
            
            for(int x=0;x<stateCount;x++){
                for(int y=0;y<stateCount;y++){
                    evec[x * stateCount + y] = eigvecs[x][y];
                    if (ievectrans)
                        ivec[x * stateCount + y] = inveigvecs[y][x];
                    else
                        ivec[x * stateCount + y] = inveigvecs[x][y];
                }
            } 
            
            Delete2DArray(qmat);
            delete relNucRates;
            
            delete eigvalsimag;
            Delete2DArray(eigvecs);
            Delete2DArray(teigvecs);
            Delete2DArray(inveigvecs);
            delete iwork;
            delete work;
        } else if (eigencomplex && stateCount==4 && eigenCount==1) {
            // create base frequency array
            double temp_freqs[4] = { 0.25, 0.25, 0.25, 0.25 };
            
            // an eigen decomposition for the 4-state 1-step circulant infinitesimal generator
            double temp_evec[4 * 4] = {
                -0.5,  0.6906786606674509,   0.15153543380548623, 0.5,
                0.5, -0.15153543380548576,  0.6906786606674498,  0.5,
                -0.5, -0.6906786606674498,  -0.15153543380548617, 0.5,
                0.5,  0.15153543380548554, -0.6906786606674503,  0.5
            };
            
            double temp_ivec[4 * 4] = {
                -0.5,  0.5, -0.5,  0.5,
                0.6906786606674505, -0.15153543380548617, -0.6906786606674507,   0.15153543380548645,
                0.15153543380548568, 0.6906786606674509,  -0.15153543380548584, -0.6906786606674509,
                0.5,  0.5,  0.5,  0.5
            };
            
            double temp_eval[8] = { -2.0, -1.0, -1.0, 0, 0, 1, -1, 0 };
            
            for(int x=0;x<stateCount;x++){
                freqs[x] = temp_freqs[x];
                eval[x] = temp_eval[x];
                eval[x+stateCount] = temp_eval[x+stateCount];
                for(int y=0;y<stateCount;y++){
                    evec[x * stateCount + y] = temp_evec[x * stateCount + y];
                    if (ievectrans)
                        ivec[x * stateCount + y] = temp_ivec[x + y * stateCount];
                    else
                        ivec[x * stateCount + y] = temp_ivec[x * stateCount + y];
                }
            } 
        } else {
            abort("should not be here");
        }
            
        beagleSetStateFrequencies(instance, eigenIndex, &freqs[0]);
        
        if (!setmatrix) {
            // set the Eigen decomposition
            beagleSetEigenDecomposition(instance, eigenIndex, &evec[0], &ivec[0], &eval[0]);
        }
    }
    
    free(eval);
    free(evec);
    free(ivec);


    
    // a list of indices and edge lengths
	int* edgeIndices = new int[edgeCount*eigenCount];
	int* edgeIndicesD1 = new int[edgeCount*eigenCount];
	int* edgeIndicesD2 = new int[edgeCount*eigenCount];
	for(int i=0; i<edgeCount*eigenCount; i++) {
        edgeIndices[i]=i;
        edgeIndicesD1[i]=(edgeCount*eigenCount)+i;
        edgeIndicesD2[i]=2*(edgeCount*eigenCount)+i;
    }
	double* edgeLengths = new double[edgeCount];
	for(int i=0; i<edgeCount; i++) {
        edgeLengths[i]=gt_rand() / (double) GT_RAND_MAX;
    }
    
    // create a list of partial likelihood update operations
    // the order is [dest, destScaling, source1, matrix1, source2, matrix2]
	int* operations = new int[(internalCount)*BEAGLE_OP_COUNT*eigenCount];
    int* scalingFactorsIndices = new int[(internalCount)*eigenCount]; // internal nodes
	for(int i=0; i<internalCount*eigenCount; i++){
		operations[BEAGLE_OP_COUNT*i+0] = ntaxa+i;
        operations[BEAGLE_OP_COUNT*i+1] = (dynamicScaling ? i : BEAGLE_OP_NONE);
        operations[BEAGLE_OP_COUNT*i+2] = (dynamicScaling ? i : BEAGLE_OP_NONE);
        
        int child1Index;
        if (((i % internalCount)*2) < ntaxa)
            child1Index = (i % internalCount)*2;
        else
            child1Index = i*2 - internalCount * (int)(i / internalCount);
        operations[BEAGLE_OP_COUNT*i+3] = child1Index;
        operations[BEAGLE_OP_COUNT*i+4] = child1Index;

        int child2Index;
        if (((i % internalCount)*2+1) < ntaxa)
            child2Index = (i % internalCount)*2+1;
        else
            child2Index = i*2+1 - internalCount * (int)(i / internalCount);
		operations[BEAGLE_OP_COUNT*i+5] = child2Index;
		operations[BEAGLE_OP_COUNT*i+6] = child2Index;

        scalingFactorsIndices[i] = i;
        
//        printf("i %d dest %d c1 %d c2 %d\n", i, ntaxa+i, child1Index, child2Index);
        
        if (autoScaling)
            scalingFactorsIndices[i] += ntaxa;
	}	

    int* rootIndices = new int[eigenCount];
	int* lastTipIndices = new int[eigenCount];
    int* categoryWeightsIndices = new int[eigenCount];
    int* stateFrequencyIndices = new int[eigenCount];
    int* cumulativeScalingFactorIndices = new int[eigenCount];
    
    for (int eigenIndex=0; eigenIndex < eigenCount; eigenIndex++) {
        rootIndices[eigenIndex] = ntaxa+(internalCount*(eigenIndex+1))-1;//ntaxa*2-2;
        lastTipIndices[eigenIndex] = ntaxa-1;
        categoryWeightsIndices[eigenIndex] = eigenIndex;
        stateFrequencyIndices[eigenIndex] = 0;
        cumulativeScalingFactorIndices[eigenIndex] = ((manualScaling || dynamicScaling) ? (scaleCount*eigenCount-1)-eigenCount+eigenIndex+1 : BEAGLE_OP_NONE);
        
        if (dynamicScaling)
            beagleResetScaleFactors(instance, cumulativeScalingFactorIndices[eigenIndex]);
    }

    // start timing!
	struct timeval time1, time2, time3, time4, time5;
    double bestTimeUpdateTransitionMatrices, bestTimeUpdatePartials, bestTimeAccumulateScaleFactors, bestTimeCalculateRootLogLikelihoods, bestTimeTotal;
    
    double logL = 0.0;
    double deriv1 = 0.0;
    double deriv2 = 0.0;
    
    double previousLogL = 0.0;
    double previousDeriv1 = 0.0;
    double previousDeriv2 = 0.0;

    for (int i=0; i<nreps; i++){
        if (manualScaling && (!(i % rescaleFrequency) || !((i-1) % rescaleFrequency))) {
            for(int j=0; j<internalCount*eigenCount; j++){
                operations[BEAGLE_OP_COUNT*j+1] = (((manualScaling && !(i % rescaleFrequency))) ? j : BEAGLE_OP_NONE);
                operations[BEAGLE_OP_COUNT*j+2] = (((manualScaling && (i % rescaleFrequency))) ? j : BEAGLE_OP_NONE);
            }
        }
        
        gettimeofday(&time1,NULL);

        for (int eigenIndex=0; eigenIndex < eigenCount; eigenIndex++) {
            if (!setmatrix) {
                // tell BEAGLE to populate the transition matrices for the above edge lengths
                beagleUpdateTransitionMatrices(instance,     // instance
                                               eigenIndex,             // eigenIndex
                                               &edgeIndices[eigenIndex*edgeCount],   // probabilityIndices
                                               (calcderivs ? &edgeIndicesD1[eigenIndex*edgeCount] : NULL), // firstDerivativeIndices
                                               (calcderivs ? &edgeIndicesD2[eigenIndex*edgeCount] : NULL), // secondDerivativeIndices
                                               edgeLengths,   // edgeLengths
                                               edgeCount);            // count
            } else {
                double* inMatrix = new double[stateCount*stateCount*rateCategoryCount];
                for (int matrixIndex=0; matrixIndex < edgeCount; matrixIndex++) {
                    for(int z=0;z<rateCategoryCount;z++){
                        for(int x=0;x<stateCount;x++){
                            for(int y=0;y<stateCount;y++){
                                inMatrix[z*stateCount*stateCount + x*stateCount + y] = gt_rand() / (double) GT_RAND_MAX;
                            }
                        } 
                    }
                    beagleSetTransitionMatrix(instance, edgeIndices[eigenIndex*edgeCount + matrixIndex], inMatrix, 1);
                    if (calcderivs) {
                        beagleSetTransitionMatrix(instance, edgeIndicesD1[eigenIndex*edgeCount + matrixIndex], inMatrix, 0);
                        beagleSetTransitionMatrix(instance, edgeIndicesD2[eigenIndex*edgeCount + matrixIndex], inMatrix, 0);
                    }
                }
            }
        }

        gettimeofday(&time2, NULL);
        
        // update the partials
        beagleUpdatePartials( instance,      // instance
                        (BeagleOperation*)operations,     // eigenIndex
                        internalCount*eigenCount,              // operationCount
                        (dynamicScaling ? internalCount : BEAGLE_OP_NONE));             // cumulative scaling index

        gettimeofday(&time3, NULL);

        int scalingFactorsCount = internalCount;
                
        for (int eigenIndex=0; eigenIndex < eigenCount; eigenIndex++) {
            if (manualScaling && !(i % rescaleFrequency)) {
                beagleResetScaleFactors(instance,
                                        cumulativeScalingFactorIndices[eigenIndex]);
                
                beagleAccumulateScaleFactors(instance,
                                       &scalingFactorsIndices[eigenIndex*internalCount],
                                       scalingFactorsCount,
                                       cumulativeScalingFactorIndices[eigenIndex]);
            } else if (autoScaling) {
                beagleAccumulateScaleFactors(instance, &scalingFactorsIndices[eigenIndex*internalCount], scalingFactorsCount, BEAGLE_OP_NONE);
            }
        }
        
        gettimeofday(&time4, NULL);
                
        // calculate the site likelihoods at the root node
        if (!unrooted) {
            beagleCalculateRootLogLikelihoods(instance,               // instance
                                        rootIndices,// bufferIndices
                                        categoryWeightsIndices,                // weights
                                        stateFrequencyIndices,                 // stateFrequencies
                                        cumulativeScalingFactorIndices,
                                        eigenCount,                      // count
                                        &logL);         // outLogLikelihoods
        } else {
            // calculate the site likelihoods at the root node
            beagleCalculateEdgeLogLikelihoods(instance,               // instance
                                              rootIndices,// bufferIndices
                                              lastTipIndices,
                                              lastTipIndices,
                                              (calcderivs ? edgeIndicesD1 : NULL),
                                              (calcderivs ? edgeIndicesD2 : NULL),
                                              categoryWeightsIndices,                // weights
                                              stateFrequencyIndices,                 // stateFrequencies
                                              cumulativeScalingFactorIndices,
                                              eigenCount,                      // count
                                              &logL,    // outLogLikelihood
                                              (calcderivs ? &deriv1 : NULL),
                                              (calcderivs ? &deriv2 : NULL));
        }
        // end timing!
        gettimeofday(&time5,NULL);
        
        if (i == 0 || getTimeDiff(time1, time2) < bestTimeUpdateTransitionMatrices)
            bestTimeUpdateTransitionMatrices = getTimeDiff(time1, time2);
        if (i == 0 || getTimeDiff(time2, time3) < bestTimeUpdatePartials)
            bestTimeUpdatePartials = getTimeDiff(time2, time3);
        if (i == 0 || getTimeDiff(time3, time4) < bestTimeAccumulateScaleFactors)
            bestTimeAccumulateScaleFactors = getTimeDiff(time3, time4);
        if (i == 0 || getTimeDiff(time4, time5) < bestTimeUpdateTransitionMatrices)
            bestTimeCalculateRootLogLikelihoods = getTimeDiff(time4, time5);
        if (i == 0 || getTimeDiff(time1, time5) < bestTimeTotal)
            bestTimeTotal = getTimeDiff(time1, time5);
        
        if (!(logL - logL == 0.0))
            abort("error: invalid lnL");
        
        if (i > 0 && abs(logL - previousLogL) > MAX_DIFF)
            abort("error: large lnL difference between reps");
        
        if (calcderivs) {
            if (!(deriv1 - deriv1 == 0.0) || !(deriv2 - deriv2 == 0.0))
                abort("error: invalid deriv");
            
            if (i > 0 && ((abs(deriv1 - previousDeriv1) > MAX_DIFF) || (abs(deriv2 - previousDeriv2) > MAX_DIFF)) )
                abort("error: large deriv difference between reps");
        }

        previousLogL = logL;
        previousDeriv1 = deriv1;
        previousDeriv2 = deriv2;        
    }

    if (resource == 0) {
        cpuTimeUpdateTransitionMatrices = bestTimeUpdateTransitionMatrices;
        cpuTimeUpdatePartials = bestTimeUpdatePartials;
        cpuTimeAccumulateScaleFactors = bestTimeAccumulateScaleFactors;
        cpuTimeCalculateRootLogLikelihoods = bestTimeCalculateRootLogLikelihoods;
        cpuTimeTotal = bestTimeTotal;
    }
    
    if (!calcderivs)
        fprintf(stdout, "logL = %.5f \n", logL);
    else
        fprintf(stdout, "logL = %.5f d1 = %.5f d2 = %.5f\n", logL, deriv1, deriv2);
    
    std::cout.setf(std::ios::showpoint);
    std::cout.setf(std::ios::floatfield, std::ios::fixed);
    int timePrecision = 6;
    int speedupPrecision = 2;
    int percentPrecision = 2;
	std::cout << "best run: ";
    printTiming(bestTimeTotal, timePrecision, resource, cpuTimeTotal, speedupPrecision, 0, 0, 0);
    if (fullTiming) {
        std::cout << " transMats:  ";
        printTiming(bestTimeUpdateTransitionMatrices, timePrecision, resource, cpuTimeUpdateTransitionMatrices, speedupPrecision, 1, bestTimeTotal, percentPrecision);
        std::cout << " partials:   ";
        printTiming(bestTimeUpdatePartials, timePrecision, resource, cpuTimeUpdatePartials, speedupPrecision, 1, bestTimeTotal, percentPrecision);
        if (manualScaling || autoScaling) {
            std::cout << " accScalers: ";
            printTiming(bestTimeAccumulateScaleFactors, timePrecision, resource, cpuTimeAccumulateScaleFactors, speedupPrecision, 1, bestTimeTotal, percentPrecision);
        }
        std::cout << " rootLnL:    ";
        printTiming(bestTimeCalculateRootLogLikelihoods, timePrecision, resource, cpuTimeCalculateRootLogLikelihoods, speedupPrecision, 1, bestTimeTotal, percentPrecision);
    }
    std::cout << "\n";
    
	beagleFinalizeInstance(instance);
}