2 Star 0 Fork 0

mirrors_jedisct1/libzerocoin

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Benchmark.cpp 10.88 KB
一键复制 编辑 原始数据 按行查看 历史
Ian Miers 提交于 2013-07-04 23:21 . libzerocoin initial commit
/**
* @file Benchmark.cpp
*
* @brief Benchmarking tests for Zerocoin.
*
* @author Ian Miers, Christina Garman and Matthew Green
* @date June 2013
*
* @copyright Copyright 2013 Ian Miers, Christina Garman and Matthew Green
* @license This project is released under the MIT license.
**/
using namespace std;
#include <string>
#include <iostream>
#include <fstream>
#include <curses.h>
#include <exception>
#include <cstdlib>
#include <sys/time.h>
#include "Zerocoin.h"
#include "bitcoin_bignum/bignum.h"
#include "bitcoin_bignum/serialize.h"
#include "Params.h"
#include "Coin.h"
#include "CoinSpend.h"
#include "ParamGeneration.h"
#include "Accumulator.h"
using namespace libzerocoin;
#define COLOR_STR_GREEN "\033[32m"
#define COLOR_STR_NORMAL "\033[0m"
#define COLOR_STR_RED "\033[31m"
#define TESTS_COINS_TO_ACCUMULATE 10
// Global test counters
uint32_t gNumTests = 0;
uint32_t gSuccessfulTests = 0;
// Global coin array
PrivateCoin *gCoins[TESTS_COINS_TO_ACCUMULATE];
// Global params
Params *g_Params;
//////////
// Utility routines
//////////
class Timer
{
timeval timer[2];
public:
timeval start()
{
gettimeofday(&this->timer[0], NULL);
return this->timer[0];
}
timeval stop()
{
gettimeofday(&this->timer[1], NULL);
return this->timer[1];
}
int duration() const
{
int secs(this->timer[1].tv_sec - this->timer[0].tv_sec);
int usecs(this->timer[1].tv_usec - this->timer[0].tv_usec);
if(usecs < 0)
{
--secs;
usecs += 1000000;
}
return static_cast<int>(secs * 1000 + usecs / 1000.0 + 0.5);
}
};
// Global timer
Timer timer;
void
LogTestResult(string testName, bool (*testPtr)())
{
string colorGreen(COLOR_STR_GREEN);
string colorNormal(COLOR_STR_NORMAL);
string colorRed(COLOR_STR_RED);
cout << "Testing if " << testName << "..." << endl;
bool testResult = testPtr();
if (testResult == true) {
cout << "\t" << colorGreen << "[PASS]" << colorNormal << endl;
gSuccessfulTests++;
} else {
cout << colorRed << "\t[FAIL]" << colorNormal << endl;
}
gNumTests++;
}
Bignum
GetTestModulus()
{
static Bignum testModulus(0);
// TODO: should use a hard-coded RSA modulus for testing
if (!testModulus) {
Bignum p, q;
p = Bignum::generatePrime(1024, false);
q = Bignum::generatePrime(1024, false);
testModulus = p * q;
}
return testModulus;
}
//////////
// Test routines
//////////
bool
Test_GenRSAModulus()
{
Bignum result = GetTestModulus();
if (!result) { return false; } else { return true; }
}
bool
Test_CalcParamSizes()
{
bool result = true;
#if 0
uint32_t pLen, qLen;
try {
calculateGroupParamLengths(4000, 80, &pLen, &qLen);
if (pLen < 1024 || qLen < 256) { result = false; }
calculateGroupParamLengths(4000, 96, &pLen, &qLen);
if (pLen < 2048 || qLen < 256) { result = false; }
calculateGroupParamLengths(4000, 112, &pLen, &qLen);
if (pLen < 3072 || qLen < 320) { result = false; }
calculateGroupParamLengths(4000, 120, &pLen, &qLen);
if (pLen < 3072 || qLen < 320) { result = false; }
calculateGroupParamLengths(4000, 128, &pLen, &qLen);
if (pLen < 3072 || qLen < 320) { result = false; }
} catch (exception &e) {
result = false;
}
#endif
return result;
}
bool
Test_GenerateGroupParams()
{
uint32_t pLen = 1024, qLen = 256, count;
IntegerGroupParams group;
for (count = 0; count < 1; count++) {
try {
group = deriveIntegerGroupParams(calculateSeed(GetTestModulus(), "test", ZEROCOIN_DEFAULT_SECURITYLEVEL, "TEST GROUP"), pLen, qLen);
} catch (std::runtime_error e) {
cout << "Caught exception " << e.what() << endl;
return false;
}
// Now perform some simple tests on the resulting parameters
if (group.groupOrder.bitSize() < qLen || group.modulus.bitSize() < pLen) {
return false;
}
Bignum c = group.g.pow_mod(group.groupOrder, group.modulus);
//cout << "g^q mod p = " << c << endl;
if (!(c.isOne())) return false;
// Try at multiple parameter sizes
pLen = pLen * 1.5;
qLen = qLen * 1.5;
}
return true;
}
bool
Test_ParamGen()
{
bool result = true;
try {
timer.start();
// Instantiating testParams runs the parameter generation code
Params testParams(GetTestModulus(),ZEROCOIN_DEFAULT_SECURITYLEVEL);
timer.stop();
cout << "\tPARAMGEN ELAPSED TIME: " << timer.duration() << " ms\t" << timer.duration()*0.001 << " s" << endl;
} catch (runtime_error e) {
cout << e.what() << endl;
result = false;
}
return result;
}
bool
Test_Accumulator()
{
// This test assumes a list of coins were generated during
// the Test_MintCoin() test.
if (gCoins[0] == NULL) { return false; }
try {
// Accumulate the coin list from first to last into one accumulator
Accumulator accOne(&g_Params->accumulatorParams);
Accumulator accTwo(&g_Params->accumulatorParams);
Accumulator accThree(&g_Params->accumulatorParams);
Accumulator accFour(&g_Params->accumulatorParams);
AccumulatorWitness wThree(g_Params, accThree, gCoins[0]->getPublicCoin());
for (uint32_t i = 0; i < TESTS_COINS_TO_ACCUMULATE; i++) {
accOne += gCoins[i]->getPublicCoin();
accTwo += gCoins[TESTS_COINS_TO_ACCUMULATE - (i+1)]->getPublicCoin();
accThree += gCoins[i]->getPublicCoin();
wThree += gCoins[i]->getPublicCoin();
if(i != 0) {accFour += gCoins[i]->getPublicCoin();}
}
// Compare the accumulated results
if (accOne.getValue() != accTwo.getValue() || accOne.getValue() != accThree.getValue()) {
cout << "Accumulators don't match" << endl;
return false;
}
if(accFour.getValue() != wThree.getValue()){
cout << "Witness math not working," << endl;
return false;
}
// Verify that the witness is correct
if (!wThree.VerifyWitness(accThree) ) {
cout << "Witness not valid" << endl;
return false;
}
} catch (runtime_error e) {
return false;
}
return true;
}
bool
Test_MintCoin()
{
try {
// Generate a list of coins
timer.start();
for (uint32_t i = 0; i < TESTS_COINS_TO_ACCUMULATE; i++) {
gCoins[i] = new PrivateCoin(g_Params);
}
timer.stop();
} catch (exception &e) {
return false;
}
cout << "\tMINT ELAPSED TIME:\n\t\tTotal: " << timer.duration() << " ms\t" << timer.duration()*0.001 << " s\n\t\tPer Coin: " << timer.duration()/TESTS_COINS_TO_ACCUMULATE << " ms\t" << (timer.duration()/TESTS_COINS_TO_ACCUMULATE)*0.001 << " s" << endl;
return true;
}
bool
Test_MintAndSpend()
{
try {
// This test assumes a list of coins were generated in Test_MintCoin()
if (gCoins[0] == NULL)
{
// No coins: mint some.
Test_MintCoin();
if (gCoins[0] == NULL) { return false; }
}
// Accumulate the list of generated coins into a fresh accumulator.
// The first one gets marked as accumulated for a witness, the
// others just get accumulated normally.
Accumulator acc(&g_Params->accumulatorParams);
AccumulatorWitness wAcc(g_Params, acc, gCoins[0]->getPublicCoin());
timer.start();
for (uint32_t i = 0; i < TESTS_COINS_TO_ACCUMULATE; i++) {
acc += gCoins[i]->getPublicCoin();
}
timer.stop();
cout << "\tACCUMULATOR ELAPSED TIME:\n\t\tTotal: " << timer.duration() << " ms\t" << timer.duration()*0.001 << " s\n\t\tPer Element: " << timer.duration()/TESTS_COINS_TO_ACCUMULATE << " ms\t" << (timer.duration()/TESTS_COINS_TO_ACCUMULATE)*0.001 << " s" << endl;
timer.start();
for (uint32_t i = 0; i < TESTS_COINS_TO_ACCUMULATE; i++) {
wAcc +=gCoins[i]->getPublicCoin();
}
timer.stop();
cout << "\tWITNESS ELAPSED TIME: \n\t\tTotal: " << timer.duration() << " ms\t" << timer.duration()*0.001 << " s\n\t\tPer Element: " << timer.duration()/TESTS_COINS_TO_ACCUMULATE << " ms\t" << (timer.duration()/TESTS_COINS_TO_ACCUMULATE)*0.001 << " s" << endl;
// Now spend the coin
SpendMetaData m(1,1);
timer.start();
CoinSpend spend(g_Params, *(gCoins[0]), acc, wAcc, m);
timer.stop();
cout << "\tSPEND ELAPSED TIME: " << timer.duration() << " ms\t" << timer.duration()*0.001 << " s" << endl;
// Serialize the proof and deserialize into newSpend
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
timer.start();
ss << spend;
timer.stop();
CoinSpend newSpend(g_Params, ss);
cout << "\tSERIALIZE ELAPSED TIME: " << timer.duration() << " ms\t" << timer.duration()*0.001 << " s" << endl;
// Finally, see if we can verify the deserialized proof (return our result)
timer.start();
bool ret = newSpend.Verify(acc);
timer.stop();
cout << "\tSPEND VERIFY ELAPSED TIME: " << timer.duration() << " ms\t" << timer.duration()*0.001 << " s" << endl;
return ret;
} catch (runtime_error &e) {
cout << e.what() << endl;
return false;
}
return false;
}
void
Test_RunAllTests()
{
// Make a new set of parameters from a random RSA modulus
g_Params = new Params(GetTestModulus());
gNumTests = gSuccessfulTests = 0;
for (uint32_t i = 0; i < TESTS_COINS_TO_ACCUMULATE; i++) {
gCoins[i] = NULL;
}
// Run through all of the Zerocoin tests
LogTestResult("an RSA modulus can be generated", Test_GenRSAModulus);
LogTestResult("parameter sizes are correct", Test_CalcParamSizes);
LogTestResult("group/field parameters can be generated", Test_GenerateGroupParams);
LogTestResult("parameter generation is correct", Test_ParamGen);
LogTestResult("coins can be minted", Test_MintCoin);
LogTestResult("the accumulator works", Test_Accumulator);
LogTestResult("a minted coin can be spent", Test_MintAndSpend);
// Summarize test results
if (gSuccessfulTests < gNumTests) {
cout << endl << "ERROR: SOME TESTS FAILED" << endl;
}
// Clear any generated coins
for (uint32_t i = 0; i < TESTS_COINS_TO_ACCUMULATE; i++) {
delete gCoins[i];
}
cout << gSuccessfulTests << " out of " << gNumTests << " tests passed." << endl << endl;
delete g_Params;
}
int main(int argc, char **argv)
{
cout << "libzerocoin v" << ZEROCOIN_VERSION_STRING << " benchmark utility." << endl << endl;
Test_RunAllTests();
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors_jedisct1/libzerocoin.git
git@gitee.com:mirrors_jedisct1/libzerocoin.git
mirrors_jedisct1
libzerocoin
libzerocoin
master

搜索帮助

D67c1975 1850385 1daf7b77 1850385