forked from Veil-Project/veil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibzerocoin_tests.cpp
503 lines (442 loc) · 13.4 KB
/
libzerocoin_tests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/**
* @file Tests.cpp
*
* @brief Test routines 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.
**/
// Copyright (c) 2017-2018 The PIVX developers
#include <key_io.h>
#include <boost/test/unit_test.hpp>
#include "test_veil.h"
#include <string>
#include <iostream>
#include <fstream>
#include <cmath>
// #include <curses.h>
#include <exception>
#include "libzerocoin/ParamGeneration.h"
#include "libzerocoin/Denominations.h"
#include "libzerocoin/Coin.h"
#include "libzerocoin/CoinSpend.h"
#include "libzerocoin/Accumulator.h"
using namespace std;
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
#define NON_PRIME_TESTS 100
// Global test counters
uint32_t gNumTests = 0;
uint32_t gSuccessfulTests = 0;
// Proof size
uint32_t gProofSize = 0;
uint32_t gCoinSize = 0;
uint32_t gSerialNumberSize = 0;
// Global coin array
PrivateCoin *gCoins[TESTS_COINS_TO_ACCUMULATE];
// Global params
ZerocoinParams *g_Params;
//////////
// Utility routines
//////////
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++;
}
CBigNum
GetTestModulus()
{
static CBigNum testModulus(0);
// TODO: should use a hard-coded RSA modulus for testing
if (!testModulus) {
CBigNum p, q;
// Note: we are NOT using safe primes for testing because
// they take too long to generate. Don't do this in real
// usage. See the paramgen utility for better code.
p = CBigNum::generatePrime(1024, false);
q = CBigNum::generatePrime(1024, false);
testModulus = p * q;
}
return testModulus;
}
//////////
// Test routines
//////////
bool
Test_GenRSAModulus()
{
CBigNum 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 ((uint32_t)group.groupOrder.bitSize() < qLen || (uint32_t)group.modulus.bitSize() < pLen) {
return false;
}
CBigNum 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 {
// Instantiating testParams runs the parameter generation code
//ZerocoinParams testParams(GetTestModulus());
ZerocoinParams testParams(GetTestModulus(),ZEROCOIN_DEFAULT_SECURITYLEVEL);
} 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, CoinDenomination::ZQ_ONE);
// Accumulator accTwo(&g_Params->accumulatorParams,CoinDenomination::ZQ_ONE);
// Accumulator accThree(&g_Params->accumulatorParams,CoinDenomination::ZQ_ONE);
// Accumulator accFour(&g_Params->accumulatorParams,CoinDenomination::ZQ_ONE);
// 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, gCoins[0]->getPublicCoin()) ) {
// cout << "Witness not valid" << endl;
// return false;
// }
//
// // Serialization test: see if we can serialize the accumulator
// CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
// ss << accOne;
//
// // Deserialize it into a new object
// Accumulator newAcc(g_Params, ss);
//
// // Compare the results
// if (accOne.getValue() != newAcc.getValue()) {
// return false;
// }
//
// } catch (runtime_error e) {
// return false;
// }
//
// return true;
}
bool
Test_EqualityPoK()
{
// Run this test 10 times
for (uint32_t i = 0; i < 10; i++) {
try {
// Generate a random integer "val"
CBigNum val = CBigNum::randBignum(g_Params->coinCommitmentGroup.groupOrder);
// Manufacture two commitments to "val", both
// under different sets of parameters
Commitment one(&g_Params->accumulatorParams.accumulatorPoKCommitmentGroup, val);
Commitment two(&g_Params->serialNumberSoKCommitmentGroup, val);
// Now generate a proof of knowledge that "one" and "two" are
// both commitments to the same value
CommitmentProofOfKnowledge pok(&g_Params->accumulatorParams.accumulatorPoKCommitmentGroup,
&g_Params->serialNumberSoKCommitmentGroup,
one, two);
// Serialize the proof into a stream
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
ss << pok;
// Deserialize back into a PoK object
CommitmentProofOfKnowledge newPok(&g_Params->accumulatorParams.accumulatorPoKCommitmentGroup,
&g_Params->serialNumberSoKCommitmentGroup,
ss);
if (newPok.Verify(one.getCommitmentValue(), two.getCommitmentValue()) != true) {
return false;
}
// Just for fun, deserialize the proof a second time
CDataStream ss2(SER_NETWORK, PROTOCOL_VERSION);
ss2 << pok;
// This time tamper with it, then deserialize it back into a PoK
ss2[15] = 0;
CommitmentProofOfKnowledge newPok2(&g_Params->accumulatorParams.accumulatorPoKCommitmentGroup,
&g_Params->serialNumberSoKCommitmentGroup,
ss2);
// If the tampered proof verifies, that's a failure!
if (newPok2.Verify(one.getCommitmentValue(), two.getCommitmentValue()) == true) {
return false;
}
} catch (runtime_error &e) {
return false;
}
}
return true;
}
bool
Test_MintCoin()
{
// gCoinSize = 0;
//
// try {
// // Generate a list of coins
// for (uint32_t i = 0; i < TESTS_COINS_TO_ACCUMULATE; i++) {
// gCoins[i] = new PrivateCoin(g_Params,libzerocoin::CoinDenomination::ZQ_ONE);
//
// PublicCoin pc = gCoins[i]->getPublicCoin();
// CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
// ss << pc;
// gCoinSize += ss.size();
// }
//
// gCoinSize /= TESTS_COINS_TO_ACCUMULATE;
//
// } catch (exception &e) {
// return false;
// }
//
// return true;
}
bool Test_InvalidCoin()
{
// CBigNum coinValue;
//
// try {
// // Pick a random non-prime CBigNum
// for (uint32_t i = 0; i < NON_PRIME_TESTS; i++) {
// coinValue = CBigNum::randBignum(g_Params->coinCommitmentGroup.modulus);
// coinValue = coinValue * 2;
// if (!coinValue.isPrime()) break;
// }
//
// PublicCoin pubCoin(g_Params);
// if (pubCoin.validate()) {
// // A blank coin should not be valid!
// return false;
// }
//
// PublicCoin pubCoin2(g_Params, coinValue, ZQ_ONE);
// if (pubCoin2.validate()) {
// // A non-prime coin should not be valid!
// return false;
// }
//
// PublicCoin pubCoin3 = pubCoin2;
// if (pubCoin2.validate()) {
// // A copy of a non-prime coin should not be valid!
// return false;
// }
//
// // Serialize and deserialize the coin
// CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
// ss << pubCoin;
// PublicCoin pubCoin4(g_Params, ss);
// if (pubCoin4.validate()) {
// // A deserialized copy of a non-prime coin should not be valid!
// return false;
// }
//
// } catch (runtime_error &e) {
// cout << "Caught exception: " << e.what() << endl;
// return false;
// }
//
// 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,CoinDenomination::ZQ_ONE);
// AccumulatorWitness wAcc(g_Params, acc, gCoins[0]->getPublicCoin());
//
// for (uint32_t i = 0; i < TESTS_COINS_TO_ACCUMULATE; i++) {
// acc += gCoins[i]->getPublicCoin();
// wAcc +=gCoins[i]->getPublicCoin();
// }
//
// // Now spend the coin
// //SpendMetaData m(1,1);
// CDataStream cc(SER_NETWORK, PROTOCOL_VERSION);
// cc << *gCoins[0];
// PrivateCoin myCoin(g_Params,cc);
// //CoinSpend(const ZerocoinParams* paramsCoin, const ZerocoinParams* paramsAcc, const PrivateCoin& coin, Accumulator& a, const uint32_t& checksum,
// //const AccumulatorWitness& witness, const uint256& ptxHash, const SpendType& spendType);
// uint32_t checkSum;
// uint256 ptxHash;
// CoinSpend spend(g_Params, g_Params, myCoin, acc, checkSum, wAcc, ptxHash, SpendType::SPEND);
// bool ret = spend.Verify(acc);
//
// // Serialize the proof and deserialize into newSpend
// CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
// ss << spend;
// gProofSize = ss.size();
// //CoinSpend newSpend(g_Params, g_Params, ss);
//
// // See if we can verify the deserialized proof (return our result)
// //bool ret = newSpend.Verify(acc);
//
// // Extract the serial number
// //CBigNum serialNumber = newSpend.getCoinSerialNumber();
// //gSerialNumberSize = ceil((double)gSerialNumber.bitSize() / 8.0);
//
// 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 ZerocoinParams(GetTestModulus(), ZEROCOIN_DEFAULT_SECURITYLEVEL);
gNumTests = gSuccessfulTests = gProofSize = 0;
for (uint32_t i = 0; i < TESTS_COINS_TO_ACCUMULATE; i++) {
gCoins[i] = NULL;
}
const std::string strSecret1 = "L3Hq7a8FEQwJkW1M2GNKDW28546Vp5miewcCzSqUD9kCAXrJdS3g";
CKey key = DecodeSecret(strSecret1);
key.GetPubKey();
// 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("invalid coins will be rejected", Test_InvalidCoin);
LogTestResult("the accumulator works", Test_Accumulator);
LogTestResult("the commitment equality PoK works", Test_EqualityPoK);
LogTestResult("a minted coin can be spent", Test_MintAndSpend);
cout << endl << "Average coin size is " << gCoinSize << " bytes." << endl;
cout << "Serial number size is " << gSerialNumberSize << " bytes." << endl;
cout << "Spend proof size is " << gProofSize << " bytes." << endl;
// 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 << endl << gSuccessfulTests << " out of " << gNumTests << " tests passed." << endl << endl;
delete g_Params;
}
BOOST_FIXTURE_TEST_SUITE(libzerocoin, BasicTestingSetup)
BOOST_AUTO_TEST_CASE(libzerocoin_tests)
{
cout << "libzerocoin v" << ZEROCOIN_VERSION_STRING << " test utility." << endl << endl;
Test_RunAllTests();
}
BOOST_AUTO_TEST_SUITE_END()