Ejemplo n.º 1
0
bool TestOptimizations::doTest()
{
	typedef std::deque<unsigned int> TestSet;
	
	StringVector testVector = enumerateTests(getPTXFiles(path),
		getOptimizations(optimizations));
	
	status << " Enumerated " << testVector.size() << " tests\n";
	
	TestSet tests;
	
	for(auto test = testVector.begin(); test != testVector.end(); ++test)
	{
		tests.push_back(std::distance(testVector.begin(), test));
	}
	
	hydrazine::Timer timer;
	timer.start();
	
	unsigned int count = 0;
	
	for(unsigned int i = 0, e = tests.size(); i != e; ++i)
	{
		if(timer.seconds() > timeLimit) break;
		
		unsigned int index = random() % tests.size();
	
		TestSet::iterator testPosition = tests.begin() + index;
	
		std::string test = testVector[*testPosition];
	
		status << " Running test '" << test << "'\n";
	
		if(!runTest(test)) return false;
	
		tests.erase(testPosition);
	
		++count;
	}
	
	status << "Finished running " << count << " tests...\n";
	
	return true;
}
Ejemplo n.º 2
0
int main(int argc, char *argv[]) {

	double genStart = 0;
	double genEnd = 0;
	double verStart = 0;
	double verEnd = 0;
	
	
	// Sample crypto parameters
	
	ZZ publicKey;
	ZZ secretKey;
	
	Paillier::keyGen(publicKey, secretKey, KEY_SIZE);
	
	ZZ modulus = publicKey * publicKey;
	
	ZZ helpRecovery = InvMod(publicKey % secretKey, secretKey);
	
	srand ( unsigned ( time(0) ) );


	int numTestSets = 25; // Should be 2500, but that will take forever
	
	vector<int> permutation;
	for (int i = 0; i < numTestSets; i++) {
		permutation.push_back(i);
	}
	random_shuffle(permutation.begin(), permutation.end());



	// Generate test sets

	int t = NumBits(PRICE_LIMIT) + 1;
	vector<TestSet*> testSets;
	vector<OpenedTestSet*> openedTestSets;
	
	genStart += clock();
	
	for (int i = 0; i < numTestSets; i++) {
		OpenedTestSet* nextOpenedTestSet = new OpenedTestSet();
		
		// Add t encryptions of 0
		for (int i = 0; i < t; i++) {
			ZZ ciphertext, randomness, randomnessPow;
			Paillier::encAux(publicKey, ZZ(0), ciphertext, randomness, randomnessPow);
			OpenedTest nextOpenedTest(ciphertext, ZZ(0), randomness, randomnessPow);
			nextOpenedTestSet->push_back(nextOpenedTest);
		}
		
		// Add encryptions of 2^0, ..., 2^{t-1}
		for (int i = 0; i < t; i++) {
			ZZ plaintext = power2_ZZ(i);
			ZZ ciphertext, randomness, randomnessPow;
			Paillier::encAux(publicKey, plaintext, ciphertext, randomness, randomnessPow);
			OpenedTest nextOpenedTest(ciphertext, plaintext, randomness, randomnessPow);
			nextOpenedTestSet->push_back(nextOpenedTest);
		}
		
		random_shuffle(nextOpenedTestSet->begin(), nextOpenedTestSet->end());
		
		TestSet* nextTestSet = new TestSet();
		for (OpenedTestSet::iterator it = nextOpenedTestSet->begin() ; it != nextOpenedTestSet->end(); ++it) {
			nextTestSet->push_back(it->ciphertext);
		}
		
		testSets.push_back(nextTestSet);
		openedTestSets.push_back(nextOpenedTestSet);
	}
	
	genEnd += clock();



	// Reveal and verify test sets
	
	verStart += clock();

	int revealed = 5; // Should be 500
	int testSetIndex = 0;
	for (; testSetIndex < revealed; testSetIndex++) {
		int nextIndex = permutation[testSetIndex];
		if (!verifyTestSet(testSets[nextIndex], openedTestSets[nextIndex])) {
			cerr << "TEST SET " << testSetIndex << " FAILED TO VERIFY!!!" << endl;
		}
	}
	
	verEnd += clock();


	// Sample and publish bids
	vector<int> bids;
	int winner = 0;
	int winningBid = 0;
	for(int i = 0; i < N_BUYERS; i++) {
		int nextBid = RandomBnd(PRICE_LIMIT);
		if (nextBid > winningBid) {
			winner = i;
			winningBid = nextBid;
		}
		bids.push_back(nextBid);
	}
	
	vector<ZZ> commitments;
	vector<ZZ> openings;
	
	for(int i = 0; i < N_BUYERS; i++) {
		ZZ ciphertext;
		ZZ randomness;
		Paillier::enc(publicKey, ZZ(bids[i]), ciphertext, randomness);
		commitments.push_back(ciphertext);
		openings.push_back(randomness);
	}
	
	
	
	
	// Check encryptions
	
	for(int i = 0; i < N_BUYERS; i++) {
		if (Paillier::com(publicKey, ZZ(bids[i]), openings[i]) != commitments[i]) {
			cerr << "BID " << i << " FAILED TO VERIFY!!!" << endl;
		}
	}
	
	cout << "WINNER IS BIDDER #" << winner << " WITH BID " << winningBid << endl;
	
	
	for(int i = 0; i < N_BUYERS; i++) {
	
		// Generate and check proof that bids[i] < 2^t
		
		genStart += clock();
		
		RangeProof nextProof = 
			prove(publicKey, ZZ(bids[i]), commitments[i], openings[i], openedTestSets[permutation[testSetIndex]]);
			
		genEnd += clock();
		
		verStart += clock();
			
		if (!verifyProof(publicKey, nextProof, commitments[i], testSets[permutation[testSetIndex]])) {
			cerr << "PROOF THAT BID " << i << " IN RANGE FAILED TO VERIFY!!!" << endl;
		} else {
			cout << "VERIFIED BID " << i << " IN RANGE" << endl;
		}
		
		verEnd += clock();
			
		testSetIndex++;	
		
		
		if (i != winner) {
		
		// Generate and check proof that bids[i] <= winningBid
		
			ZZ ciphertext = MulMod(commitments[winner], InvMod(commitments[i], modulus), modulus);
			
			ZZ help = PowerMod(ciphertext % publicKey, helpRecovery, publicKey);
			
			genStart += clock();
		
			RangeProof nextProof = 
				prove(	publicKey,
						ZZ(winningBid - bids[i]),
						ciphertext,
						help,
						openedTestSets[permutation[testSetIndex]]);
						
			genEnd += clock();
						
			verStart += clock();
			if (!verifyProof(publicKey, nextProof, ciphertext, testSets[permutation[testSetIndex]])) {
				cerr << "PROOF THAT BID " << i << " LOSES FAILED TO VERIFY!!!" << endl;
			} else {
				cout << "VERIFIED BID " << i << " LOSES" << endl;
			}
			verEnd+= clock();
			testSetIndex++;	
			
		}
		
	cout << "Proof preparation time: " << 1.0 * (genEnd-genStart) / CLOCKS_PER_SEC << endl;
	cout << "Proof verification time: " << 1.0 * (verEnd-verStart) / CLOCKS_PER_SEC << endl;


	
	}

}