forked from etotheipi/BitcoinArmory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockUtilsTest.cpp
More file actions
1010 lines (814 loc) · 40.7 KB
/
BlockUtilsTest.cpp
File metadata and controls
1010 lines (814 loc) · 40.7 KB
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
////////////////////////////////////////////////////////////////////////////////
// //
// Copyright (C) 2011-2012, Alan C. Reiner <alan.reiner@gmail.com> //
// Distributed under the GNU Affero General Public License (AGPL v3) //
// See LICENSE or http://www.gnu.org/licenses/agpl.html //
// //
////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "UniversalTimer.h"
#include "BinaryData.h"
#include "BtcUtils.h"
#include "BlockUtils.h"
#include "EncryptionUtils.h"
using namespace std;
void copyFile(string src, string dst)
{
fstream fin(src.c_str(), ios::in | ios::binary);
fstream fout(dst.c_str(), ios::out | ios::binary);
if(fin == NULL || fout == NULL) { cout <<"error"; return; }
// read from the first file then write to the second file
char c;
while(!fin.eof()) { fin.get(c); fout.put(c); }
}
////////////////////////////////////////////////////////////////////////////////
void TestReadAndOrganizeChain(string blkfile);
void TestFindNonStdTx(string blkfile);
void TestReadAndOrganizeChainWithWallet(string blkfile);
void TestScanForWalletTx(string blkfile);
void TestReorgBlockchain(string blkfile);
void TestZeroConf(void);
void TestCrypto(void);
void TestECDSA(void);
void TestPointCompression(void);
////////////////////////////////////////////////////////////////////////////////
void printTestHeader(string TestName)
{
cout << endl;
for(int i=0; i<80; i++) cout << "*";
cout << endl << "Execute test: " << TestName << endl;
for(int i=0; i<80; i++) cout << "*";
cout << endl;
}
int main(void)
{
BlockDataManager_MMAP::GetInstance().SelectNetwork("Main");
string blkfile("/home/alan/.bitcoin/blk0001.dat");
//string blkfile("/home/alan/.bitcoin/testnet/blk0001.dat");
//string blkfile("C:/Documents and Settings/VBox/Application Data/Bitcoin/testnet/blk0001.dat");
//printTestHeader("Read-and-Organize-Blockchain");
//TestReadAndOrganizeChain(blkfile);
//printTestHeader("Wallet-Relevant-Tx-Scan");
//TestScanForWalletTx(blkfile);
//printTestHeader("Find-Non-Standard-Tx");
//TestFindNonStdTx(blkfile);
//printTestHeader("Read-and-Organize-Blockchain-With-Wallet");
//TestReadAndOrganizeChainWithWallet(blkfile);
//printTestHeader("Blockchain-Reorg-Unit-Test");
//TestReorgBlockchain(blkfile);
//printTestHeader("Testing Zero-conf handling");
//TestZeroConf();
//printTestHeader("Crypto-KDF-and-AES-methods");
//TestCrypto();
//printTestHeader("Crypto-ECDSA-sign-verify");
//TestECDSA();
printTestHeader("ECDSA Point Compression");
TestPointCompression();
/////////////////////////////////////////////////////////////////////////////
// ***** Print out all timings to stdout and a csv file *****
// Any method, anywhere, that called UniversalTimer
// will end up having it's named timers printed out
// This file can be loaded into a spreadsheet,
// but it's not the prettiest thing...
UniversalTimer::instance().print();
UniversalTimer::instance().printCSV("timings.csv");
cout << endl << endl;
char pause[256];
cout << "enter anything to exit" << endl;
cin >> pause;
}
void TestReadAndOrganizeChain(string blkfile)
{
BlockDataManager_MMAP & bdm = BlockDataManager_MMAP::GetInstance();
/////////////////////////////////////////////////////////////////////////////
cout << "Reading data from blockchain..." << endl;
TIMER_START("BDM_Load_and_Scan_BlkChain");
bdm.readBlkFile_FromScratch(blkfile);
TIMER_STOP("BDM_Load_and_Scan_BlkChain");
cout << endl << endl;
/////////////////////////////////////////////////////////////////////////////
// Organizing the chain is always really fast (sub-second), and has now been
// removed as an OPTION to the "readBlkFile" methods. It will be done
// automatically
//cout << endl << "Organizing blockchain: " ;
//TIMER_START("BDM_Organize_Chain");
//bool isGenOnMainChain = bdm.organizeChain();
//TIMER_STOP("BDM_Organize_Chain");
//cout << (isGenOnMainChain ? "No Reorg!" : "Reorg Detected!") << endl;
//cout << endl << endl;
/////////////////////////////////////////////////////////////////////////////
// TESTNET has some 0.125-difficulty blocks which violates the assumption
// that it never goes below 1. So, need to comment this out for testnet
/* For whatever reason, this doesn't work on testnet...
cout << "Verify integrity of blockchain file (merkleroots leading zeros on headers)" << endl;
TIMER_START("Verify blk0001.dat integrity");
bool isVerified = bdm.verifyBlkFileIntegrity();
TIMER_STOP("Verify blk0001.dat integrity");
cout << "Done! Your blkfile " << (isVerified ? "is good!" : " HAS ERRORS") << endl;
cout << endl << endl;
*/
}
void TestFindNonStdTx(string blkfile)
{
BlockDataManager_MMAP & bdm = BlockDataManager_MMAP::GetInstance();
bdm.readBlkFile_FromScratch(blkfile);
// This is mostly just for debugging...
bdm.findAllNonStdTx();
// At one point I had code to print out nonstd txinfo... not sure
// what happened to it...
}
void TestScanForWalletTx(string blkfile)
{
BlockDataManager_MMAP & bdm = BlockDataManager_MMAP::GetInstance();
bdm.readBlkFile_FromScratch(blkfile);
/////////////////////////////////////////////////////////////////////////////
BinaryData myAddress;
BtcWallet wlt;
// Main-network addresses
myAddress.createFromHex("604875c897a079f4db88e5d71145be2093cae194"); wlt.addAddress(myAddress);
myAddress.createFromHex("8996182392d6f05e732410de4fc3fa273bac7ee6"); wlt.addAddress(myAddress);
myAddress.createFromHex("b5e2331304bc6c541ffe81a66ab664159979125b"); wlt.addAddress(myAddress);
myAddress.createFromHex("ebbfaaeedd97bc30df0d6887fd62021d768f5cb8"); wlt.addAddress(myAddress);
myAddress.createFromHex("11b366edfc0a8b66feebae5c2e25a7b6a5d1cf31"); wlt.addAddress(myAddress);
// This address contains a tx with a non-std TxOut, but the other TxOuts are valid
myAddress.createFromHex("6c27c8e67b7376f3ab63553fe37a4481c4f951cf"); wlt.addAddress(myAddress);
// More testnet addresses, with only a few transactions
myAddress.createFromHex("0c6b92101c7025643c346d9c3e23034a8a843e21"); wlt.addAddress(myAddress);
myAddress.createFromHex("34c9f8dc91dfe1ae1c59e76cbe1aa39d0b7fc041"); wlt.addAddress(myAddress);
myAddress.createFromHex("d77561813ca968270d5f63794ddb6aab3493605e"); wlt.addAddress(myAddress);
myAddress.createFromHex("0e0aec36fe2545fb31a41164fb6954adcd96b342"); wlt.addAddress(myAddress);
TIMER_WRAP(bdm.scanBlockchainForTx(wlt));
TIMER_WRAP(bdm.scanBlockchainForTx(wlt));
TIMER_WRAP(bdm.scanBlockchainForTx(wlt));
cout << "Checking balance of all addresses: " << wlt.getNumAddr() << " addrs" << endl;
for(uint32_t i=0; i<wlt.getNumAddr(); i++)
{
BinaryData addr20 = wlt.getAddrByIndex(i).getAddrStr20();
cout << " Addr: " << wlt.getAddrByIndex(i).getFullBalance() << ","
<< wlt.getAddrByHash160(addr20).getFullBalance() << endl;
vector<LedgerEntry> const & ledger = wlt.getAddrByIndex(i).getTxLedger();
for(uint32_t j=0; j<ledger.size(); j++)
{
cout << " Tx: "
<< ledger[j].getAddrStr20().getSliceCopy(0,4).toHexStr() << " "
<< ledger[j].getValue()/(float)(CONVERTBTC) << " ("
<< ledger[j].getBlockNum()
<< ") TxHash: " << ledger[j].getTxHash().getSliceCopy(0,4).toHexStr() << endl;
}
}
cout << endl << endl;
cout << "Printing SORTED allAddr ledger..." << endl;
wlt.sortLedger();
vector<LedgerEntry> const & ledgerAll = wlt.getTxLedger();
for(uint32_t j=0; j<ledgerAll.size(); j++)
{
cout << " Tx: "
<< ledgerAll[j].getAddrStr20().toHexStr() << " "
<< ledgerAll[j].getValue()/1e8 << " ("
<< ledgerAll[j].getBlockNum()
<< ") TxHash: " << ledgerAll[j].getTxHash().getSliceCopy(0,4).toHexStr() << endl;
}
/////////////////////////////////////////////////////////////////////////////
cout << "Test txout aggregation, with different prioritization schemes" << endl;
BtcWallet myWallet;
#ifndef TEST_NETWORK
// TODO: I somehow borked my list of test addresses. Make sure I have some
// test addresses in here for each network that usually has lots of
// unspent TxOuts
// Main-network addresses
myAddress.createFromHex("0e0aec36fe2545fb31a41164fb6954adcd96b342"); myWallet.addAddress(myAddress);
#else
// Testnet addresses
//myAddress.createFromHex("d184cea7e82c775d08edd288344bcd663c3f99a2"); myWallet.addAddress(myAddress);
//myAddress.createFromHex("205fa00890e6898b987de6ff8c0912805416cf90"); myWallet.addAddress(myAddress);
//myAddress.createFromHex("fc0ef58380e6d4bcb9599c5369ce82d0bc01a5c4"); myWallet.addAddress(myAddress);
#endif
cout << "Rescanning the blockchain for new addresses." << endl;
bdm.scanBlockchainForTx(myWallet);
//vector<UnspentTxOut> sortedUTOs = bdm.getUnspentTxOutsForWallet(myWallet, 1);
vector<UnspentTxOut> sortedUTOs = myWallet.getSpendableTxOutList();
int i=1;
cout << " Sorting Method: " << i << endl;
cout << " Value\t#Conf\tTxHash\tTxIdx" << endl;
for(int j=0; j<sortedUTOs.size(); j++)
{
cout << " "
<< sortedUTOs[j].getValue()/1e8 << "\t"
<< sortedUTOs[j].getNumConfirm() << "\t"
<< sortedUTOs[j].getTxHash().toHexStr() << "\t"
<< sortedUTOs[j].getTxOutIndex() << endl;
}
cout << endl;
// Test the zero-conf ledger-entry detection
//le.pprint();
//vector<LedgerEntry> levect = wlt.getAddrLedgerEntriesForTx(txSelf);
//for(int i=0; i<levect.size(); i++)
//{
//levect[i].pprint();
//}
}
void TestReadAndOrganizeChainWithWallet(string blkfile)
{
cout << endl << "Starting blockchain loading with wallets..." << endl;
/////////////////////////////////////////////////////////////////////////////
BlockDataManager_MMAP & bdm = BlockDataManager_MMAP::GetInstance();
BinaryData myAddress;
BtcWallet wlt1;
BtcWallet wlt2;
// Main-network addresses
myAddress.createFromHex("604875c897a079f4db88e5d71145be2093cae194"); wlt2.addAddress(myAddress);
myAddress.createFromHex("8996182392d6f05e732410de4fc3fa273bac7ee6"); wlt2.addAddress(myAddress);
myAddress.createFromHex("b5e2331304bc6c541ffe81a66ab664159979125b"); wlt2.addAddress(myAddress);
myAddress.createFromHex("ebbfaaeedd97bc30df0d6887fd62021d768f5cb8"); wlt2.addAddress(myAddress);
myAddress.createFromHex("11b366edfc0a8b66feebae5c2e25a7b6a5d1cf31"); wlt2.addAddress(myAddress);
// Add some relevant testnet addresses
myAddress.createFromHex("0c6b92101c7025643c346d9c3e23034a8a843e21"); wlt2.addAddress(myAddress);
myAddress.createFromHex("34c9f8dc91dfe1ae1c59e76cbe1aa39d0b7fc041"); wlt1.addAddress(myAddress);
myAddress.createFromHex("d77561813ca968270d5f63794ddb6aab3493605e"); wlt1.addAddress(myAddress);
myAddress.createFromHex("0e0aec36fe2545fb31a41164fb6954adcd96b342"); wlt1.addAddress(myAddress);
vector<BtcWallet*> wltList;
wltList.push_back(&wlt1);
wltList.push_back(&wlt2);
bdm.registerWallet(&wlt1);
bdm.registerWallet(&wlt2);
/////////////////////////////////////////////////////////////////////////////
cout << "Reading data from blockchain... (with wallet scan)" << endl;
TIMER_START("BDM_Load_Scan_Blockchain_With_Wallet");
bdm.readBlkFile_FromScratch(blkfile);
TIMER_STOP("BDM_Load_Scan_Blockchain_With_Wallet");
cout << endl << endl;
/////////////////////////////////////////////////////////////////////////////
cout << endl << "Organizing blockchain: " ;
TIMER_START("BDM_Organize_Chain");
bool isGenOnMainChain = bdm.organizeChain();
TIMER_STOP("BDM_Organize_Chain");
cout << (isGenOnMainChain ? "No Reorg!" : "Reorg Detected!") << endl;
cout << endl << endl;
cout << endl << "Updating wallet (1) based on initial MMAP blockchain scan" << endl;
TIMER_WRAP(bdm.scanBlockchainForTx(wlt1));
cout << "Printing Wallet(1) Ledger" << endl;
wlt1.pprintLedger();
cout << endl << "Updating wallet (2) based on initial MMAP blockchain scan" << endl;
TIMER_WRAP(bdm.scanBlockchainForTx(wlt2));
cout << "Printing Wallet(2) Ledger" << endl;
wlt2.pprintLedger();
cout << endl << "Rescanning wlt2 multiple times" << endl;
TIMER_WRAP(bdm.scanBlockchainForTx(wlt2));
TIMER_WRAP(bdm.scanBlockchainForTx(wlt2));
cout << "Printing Wallet(2) Ledger AGAIN" << endl;
wlt2.pprintLedger();
cout << endl << "ADD a new address to Wlt(1) requiring a blockchain rescan..." << endl;
// This address contains a tx with a non-std TxOut, but the other TxOuts are valid
myAddress.createFromHex("6c27c8e67b7376f3ab63553fe37a4481c4f951cf");
wlt1.addAddress(myAddress);
bdm.scanBlockchainForTx(wlt1);
wlt1.pprintLedger();
cout << endl << "ADD new address to wlt(2) but as a just-created addr not requiring rescan" << endl;
myAddress.createFromHex("6c27c8e67b7376f3ab63553fe37a4481c4f951cf");
wlt2.addNewAddress(myAddress);
bdm.scanBlockchainForTx(wlt2);
wlt2.pprintLedger();
cout << endl << "Create new, unregistered wallet, scan it once (should rescan)..." << endl;
BtcWallet wlt3;
myAddress.createFromHex("72e20a94d6b2ed34a3b4d3757c1fed5152071993"); wlt3.addAddress(myAddress);
wlt3.addAddress(myAddress);
bdm.scanBlockchainForTx(wlt3);
wlt3.pprintLedger();
cout << endl << "Rescan unregistered wallet: addr should be registered, so no rescan..." << endl;
bdm.scanBlockchainForTx(wlt3);
wlt3.pprintLedger();
}
void TestReorgBlockchain(string blkfile)
{
BlockDataManager_MMAP & bdm = BlockDataManager_MMAP::GetInstance();
/////////////////////////////////////////////////////////////////////////////
//
// BLOCKCHAIN REORGANIZATION UNIT-TEST
//
/////////////////////////////////////////////////////////////////////////////
//
// NOTE: The unit-test files (blk_0_to_4, blk_3A, etc) are located in
// cppForSwig/reorgTest. These files represent a very small
// blockchain with a double-spend and a couple invalidated
// coinbase tx's. All tx-hashes & OutPoints are consistent,
// all transactions have real ECDSA signatures, and blockheaders
// have four leading zero-bytes to be valid at difficulty=1
//
// If you were to set COINBASE_MATURITY=1 (not applicable here)
// this would be a *completely valid* blockchain--just a very
// short blockchain.
//
// FYI: The first block is the *actual* main-network genesis block
//
string blk04("reorgTest/blk_0_to_4.dat");
string blk3A("reorgTest/blk_3A.dat");
string blk4A("reorgTest/blk_4A.dat");
string blk5A("reorgTest/blk_5A.dat");
BtcWallet wlt2;
wlt2.addAddress(BinaryData::CreateFromHex("62e907b15cbf27d5425399ebf6f0fb50ebb88f18"));
wlt2.addAddress(BinaryData::CreateFromHex("ee26c56fc1d942be8d7a24b2a1001dd894693980"));
wlt2.addAddress(BinaryData::CreateFromHex("cb2abde8bccacc32e893df3a054b9ef7f227a4ce"));
wlt2.addAddress(BinaryData::CreateFromHex("c522664fb0e55cdc5c0cea73b4aad97ec8343232"));
cout << endl << endl;
cout << "Preparing blockchain-reorganization test!" << endl;
cout << "Resetting block-data mgr...";
bdm.Reset();
cout << "Done!" << endl;
cout << "Reading in initial block chain (Blocks 0 through 4)..." ;
bdm.readBlkFile_FromScratch("reorgTest/blk_0_to_4.dat");
bdm.organizeChain();
cout << "Done" << endl;
// TODO: Let's look at the address ledger after the first chain
// Then look at it again after the reorg. What we want
// to see is the presence of an invalidated tx, not just
// a disappearing tx -- the user must be informed that a
// tx they previously thought they owned is now invalid.
// If the user is not informed, they could go crazy trying
// to figure out what happened to this money they thought
// they had.
cout << "Constructing address ledger for the to-be-invalidated chain:" << endl;
bdm.scanBlockchainForTx(wlt2);
vector<LedgerEntry> const & ledgerAll2 = wlt2.getTxLedger();
for(uint32_t j=0; j<ledgerAll2.size(); j++)
{
cout << " Tx: "
<< ledgerAll2[j].getValue()/1e8
<< " (" << ledgerAll2[j].getBlockNum() << ")"
<< " TxHash: " << ledgerAll2[j].getTxHash().getSliceCopy(0,4).toHexStr();
if(!ledgerAll2[j].isValid()) cout << " (INVALID) ";
if( ledgerAll2[j].isSentToSelf()) cout << " (SENT_TO_SELF) ";
if( ledgerAll2[j].isChangeBack()) cout << " (RETURNED CHANGE) ";
cout << endl;
}
cout << "Checking balance of all addresses: " << wlt2.getNumAddr() << "addrs" << endl;
cout << " Balance: " << wlt2.getFullBalance()/1e8 << endl;
for(uint32_t i=0; i<wlt2.getNumAddr(); i++)
{
BinaryData addr20 = wlt2.getAddrByIndex(i).getAddrStr20();
cout << " Addr: " << wlt2.getAddrByIndex(i).getFullBalance()/1e8 << ","
<< wlt2.getAddrByHash160(addr20).getFullBalance() << endl;
vector<LedgerEntry> const & ledger = wlt2.getAddrByIndex(i).getTxLedger();
for(uint32_t j=0; j<ledger.size(); j++)
{
cout << " Tx: "
<< ledger[j].getAddrStr20().getSliceCopy(0,4).toHexStr() << " "
<< ledger[j].getValue()/(float)(CONVERTBTC) << " ("
<< ledger[j].getBlockNum()
<< ") TxHash: " << ledger[j].getTxHash().getSliceCopy(0,4).toHexStr();
if( ! ledger[j].isValid()) cout << " (INVALID) ";
cout << endl;
}
}
// prepare the other block to be read in
ifstream is;
BinaryData blk3a, blk4a, blk5a;
assert(blk3a.readBinaryFile("reorgTest/blk_3A.dat") != -1);
assert(blk4a.readBinaryFile("reorgTest/blk_4A.dat") != -1);
assert(blk5a.readBinaryFile("reorgTest/blk_5A.dat") != -1);
vector<bool> result;
/////
cout << "Pushing Block 3A into the BDM:" << endl;
result = bdm.addNewBlockData(blk3a);
/////
cout << "Pushing Block 4A into the BDM:" << endl;
result = bdm.addNewBlockData(blk4a);
/////
cout << "Pushing Block 5A into the BDM:" << endl;
result = bdm.addNewBlockData(blk5a);
if(result[ADD_BLOCK_CAUSED_REORG] == true)
{
cout << "Reorg happened after pushing block 5A" << endl;
bdm.scanBlockchainForTx(wlt2);
bdm.updateWalletAfterReorg(wlt2);
}
cout << "Checking balance of entire wallet: " << wlt2.getFullBalance()/1e8 << endl;
vector<LedgerEntry> const & ledgerAll3 = wlt2.getTxLedger();
for(uint32_t j=0; j<ledgerAll3.size(); j++)
{
cout << " Tx: "
<< ledgerAll3[j].getValue()/1e8
<< " (" << ledgerAll3[j].getBlockNum() << ")"
<< " TxHash: " << ledgerAll3[j].getTxHash().getSliceCopy(0,4).toHexStr();
if(!ledgerAll3[j].isValid()) cout << " (INVALID) ";
if( ledgerAll3[j].isSentToSelf()) cout << " (SENT_TO_SELF) ";
if( ledgerAll3[j].isChangeBack()) cout << " (RETURNED CHANGE) ";
cout << endl;
}
cout << "Checking balance of all addresses: " << wlt2.getNumAddr() << "addrs" << endl;
for(uint32_t i=0; i<wlt2.getNumAddr(); i++)
{
BinaryData addr20 = wlt2.getAddrByIndex(i).getAddrStr20();
cout << " Addr: " << wlt2.getAddrByIndex(i).getFullBalance()/1e8 << ","
<< wlt2.getAddrByHash160(addr20).getFullBalance()/1e8 << endl;
vector<LedgerEntry> const & ledger = wlt2.getAddrByIndex(i).getTxLedger();
for(uint32_t j=0; j<ledger.size(); j++)
{
cout << " Tx: "
<< ledger[j].getAddrStr20().getSliceCopy(0,4).toHexStr() << " "
<< ledger[j].getValue()/(float)(CONVERTBTC) << " ("
<< ledger[j].getBlockNum()
<< ") TxHash: " << ledger[j].getTxHash().getSliceCopy(0,4).toHexStr();
if( ! ledger[j].isValid())
cout << " (INVALID) ";
cout << endl;
}
}
/////////////////////////////////////////////////////////////////////////////
//
// END BLOCKCHAIN REORG UNIT-TEST
//
/////////////////////////////////////////////////////////////////////////////
}
void TestZeroConf(void)
{
BlockDataManager_MMAP & bdm = BlockDataManager_MMAP::GetInstance();
BinaryData myAddress;
BtcWallet wlt;
/*
bdm.Reset();
bdm.readBlkFile_FromScratch("zctest/blk0001.dat");
// More testnet addresses, with only a few transactions
myAddress.createFromHex("4c98e1fb7aadce864b310b2e52b685c09bdfd5e7"); wlt.addAddress(myAddress);
myAddress.createFromHex("08ccdf1ef9269b95f6ce93899ece9f68cd5afb22"); wlt.addAddress(myAddress);
myAddress.createFromHex("edf6bbd7ba7aad222c2b28e6d8d5001178e3680c"); wlt.addAddress(myAddress);
myAddress.createFromHex("18d9cae7ee0be5c6d58f02a992442d2cdb9914fa"); wlt.addAddress(myAddress);
bdm.scanBlockchainForTx(wlt);
bdm.enableZeroConf("zctest/mempool_new.bin");
uint32_t currBlk = bdm.getTopBlockHeader().getBlockHeight();
ifstream zcIn("zctest/mempool.bin", ios::in | ios::binary);
zcIn.seekg(0, ios::end);
uint64_t filesize = (size_t)zcIn.tellg();
zcIn.seekg(0, ios::beg);
BinaryData memPool(filesize);
zcIn.read((char*)memPool.getPtr(), filesize);
BinaryRefReader brr(memPool);
cout << "Starting Wallet:" << endl;
bdm.rescanWalletZeroConf(wlt);
wlt.pprintLedger();
while(brr.getSizeRemaining() > 8)
{
cout << endl << endl;
cout << "Inserting another 0-conf tx..." << endl;
uint64_t txtime = brr.get_uint64_t();
TxRef zcTx(brr);
bool wasAdded = bdm.addNewZeroConfTx(zcTx.serialize(), txtime, true);
if(wasAdded)
bdm.rescanWalletZeroConf(wlt);
cout << "UltBal: " << wlt.getFullBalance() << endl;
cout << "SpdBal: " << wlt.getSpendableBalance() << endl;
cout << "UncBal: " << wlt.getUnconfirmedBalance(currBlk) << endl;
wlt.pprintLedger();
cout << "Unspent TxOuts:" << endl;
vector<UnspentTxOut> utxoList = wlt.getSpendableTxOutList(currBlk);
uint64_t bal = 0;
for(uint32_t i=0; i<utxoList.size(); i++)
{
bal += utxoList[i].getValue();
utxoList[i].pprintOneLine(currBlk);
}
cout << "Sum of TxOuts: " << bal/1e8 << endl;
}
*/
ifstream is("zctest/mempool_new.bin", ios::in | ios::binary);
ofstream os("zctest/mempool.bin", ios::out | ios::binary);
is.seekg(0, ios::end);
uint64_t filesize = (size_t)is.tellg();
is.seekg(0, ios::beg);
BinaryData mempool(filesize);
is.read ((char*)mempool.getPtr(), filesize);
os.write((char*)mempool.getPtr(), filesize);
is.close();
os.close();
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
// Start testing balance/wlt update after a new block comes in
bdm.Reset();
bdm.readBlkFile_FromScratch("zctest/blk0001.dat");
// More testnet addresses, with only a few transactions
wlt = BtcWallet();
myAddress.createFromHex("4c98e1fb7aadce864b310b2e52b685c09bdfd5e7"); wlt.addAddress(myAddress);
myAddress.createFromHex("08ccdf1ef9269b95f6ce93899ece9f68cd5afb22"); wlt.addAddress(myAddress);
myAddress.createFromHex("edf6bbd7ba7aad222c2b28e6d8d5001178e3680c"); wlt.addAddress(myAddress);
myAddress.createFromHex("18d9cae7ee0be5c6d58f02a992442d2cdb9914fa"); wlt.addAddress(myAddress);
uint32_t topBlk = bdm.getTopBlockHeader().getBlockHeight();
// This will load the memory pool into the zeroConfPool_ in BDM
bdm.enableZeroConf("zctest/mempool.bin");
// Now scan all transactions, which ends with scanning zero-conf
bdm.scanBlockchainForTx(wlt);
wlt.pprintAlot(topBlk, true);
// The new blkfile has about 10 new blocks, one of which has these tx
bdm.readBlkFileUpdate("zctest/blk0001_updated.dat");
bdm.scanBlockchainForTx(wlt, topBlk);
topBlk = bdm.getTopBlockHeader().getBlockHeight();
wlt.pprintAlot(topBlk, true);
}
void TestCrypto(void)
{
SecureBinaryData a("aaaaaaaaaa");
SecureBinaryData b; b.resize(5);
SecureBinaryData c; c.resize(0);
a.copyFrom(b);
b.copyFrom(c);
c.copyFrom(a);
a.resize(0);
b = a;
SecureBinaryData d(a);
cout << "a=" << a.toHexStr() << endl;
cout << "b=" << b.toHexStr() << endl;
cout << "c=" << c.toHexStr() << endl;
cout << "d=" << d.toHexStr() << endl;
SecureBinaryData e("eeeeeeeeeeeeeeee");
SecureBinaryData f("ffffffff");
SecureBinaryData g(0);
e = g.copy();
e = f.copy();
cout << "e=" << e.toHexStr() << endl;
cout << "f=" << f.toHexStr() << endl;
cout << "g=" << g.toHexStr() << endl;
/////////////////////////////////////////////////////////////////////////////
// Start Key-Derivation-Function (KDF) Tests.
// ROMIX is the provably memory-hard (GPU-resistent) algorithm proposed by
// Colin Percival, who is the creator of Scrypt.
cout << endl << endl;
cout << "Executing Key-Derivation-Function (KDF) tests" << endl;
KdfRomix kdf;
kdf.computeKdfParams();
kdf.printKdfParams();
SecureBinaryData passwd1("This is my first password");
SecureBinaryData passwd2("This is my first password.");
SecureBinaryData passwd3("This is my first password");
SecureBinaryData key;
cout << " Password1: '" << passwd1.toBinStr() << "'" << endl;
key = kdf.DeriveKey(passwd1);
cout << " MasterKey: '" << key.toHexStr() << endl << endl;
cout << " Password2: '" << passwd2.toBinStr() << "'" << endl;
key = kdf.DeriveKey(passwd2);
cout << " MasterKey: '" << key.toHexStr() << endl << endl;
cout << " Password1: '" << passwd3.toBinStr() << "'" << endl;
key = kdf.DeriveKey(passwd3);
cout << " MasterKey: '" << key.toHexStr() << endl << endl;
/////////////////////////////////////////////////////////////////////////////
cout << "Executing KDF tests with longer compute time" << endl;
kdf.computeKdfParams(1.0);
kdf.printKdfParams();
cout << " Password1: '" << passwd1.toBinStr() << "'" << endl;
key = kdf.DeriveKey(passwd1);
cout << " MasterKey: '" << key.toHexStr() << endl << endl;
cout << " Password2: '" << passwd2.toBinStr() << "'" << endl;
key = kdf.DeriveKey(passwd2);
cout << " MasterKey: '" << key.toHexStr() << endl << endl;
cout << " Password1: '" << passwd3.toBinStr() << "'" << endl;
key = kdf.DeriveKey(passwd3);
cout << " MasterKey: '" << key.toHexStr() << endl << endl;
/////////////////////////////////////////////////////////////////////////////
cout << "Executing KDF tests with limited memory target" << endl;
kdf.computeKdfParams(1.0, 256*1024);
kdf.printKdfParams();
cout << " Password1: '" << passwd1.toBinStr() << "'" << endl;
key = kdf.DeriveKey(passwd1);
cout << " MasterKey: '" << key.toHexStr() << endl << endl;
cout << " Password2: '" << passwd2.toBinStr() << "'" << endl;
key = kdf.DeriveKey(passwd2);
cout << " MasterKey: '" << key.toHexStr() << endl << endl;
cout << " Password1: '" << passwd3.toBinStr() << "'" << endl;
key = kdf.DeriveKey(passwd3);
cout << " MasterKey: '" << key.toHexStr() << endl << endl;
/////////////////////////////////////////////////////////////////////////////
cout << "KDF with min memory requirement (1 kB)" << endl;
kdf.computeKdfParams(1.0, 0);
kdf.printKdfParams();
cout << " Password1: '" << passwd1.toBinStr() << "'" << endl;
key = kdf.DeriveKey(passwd1);
cout << " MasterKey: '" << key.toHexStr() << endl << endl;
cout << " Password2: '" << passwd2.toBinStr() << "'" << endl;
key = kdf.DeriveKey(passwd2);
cout << " MasterKey: '" << key.toHexStr() << endl << endl;
cout << " Password1: '" << passwd3.toBinStr() << "'" << endl;
key = kdf.DeriveKey(passwd3);
cout << " MasterKey: '" << key.toHexStr() << endl << endl;
/////////////////////////////////////////////////////////////////////////////
cout << "KDF with 0 compute time" << endl;
kdf.computeKdfParams(0, 0);
kdf.printKdfParams();
cout << " Password1: '" << passwd1.toBinStr() << "'" << endl;
key = kdf.DeriveKey(passwd1);
cout << " MasterKey: '" << key.toHexStr() << endl << endl;
cout << " Password2: '" << passwd2.toBinStr() << "'" << endl;
key = kdf.DeriveKey(passwd2);
cout << " MasterKey: '" << key.toHexStr() << endl << endl;
cout << " Password1: '" << passwd3.toBinStr() << "'" << endl;
key = kdf.DeriveKey(passwd3);
cout << " MasterKey: '" << key.toHexStr() << endl << endl;
// Test AES code using NIST test vectors
/// *** Test 1 *** ///
cout << endl << endl;
SecureBinaryData testIV, plaintext, cipherTarg, cipherComp, testKey, rtPlain;
testKey.createFromHex ("0000000000000000000000000000000000000000000000000000000000000000");
testIV.createFromHex ("80000000000000000000000000000000");
plaintext.createFromHex ("00000000000000000000000000000000");
cipherTarg.createFromHex("ddc6bf790c15760d8d9aeb6f9a75fd4e");
cout << " Plain : " << plaintext.toHexStr() << endl;
cipherComp = CryptoAES().EncryptCFB(plaintext, testKey, testIV);
cout << " CipherTarget : " << cipherComp.toHexStr() << endl;
cout << " CipherCompute: " << cipherComp.toHexStr() << endl;
rtPlain = CryptoAES().DecryptCFB(cipherComp, testKey, testIV);
cout << " Plain : " << rtPlain.toHexStr() << endl;
/// *** Test 2 *** ///
cout << endl << endl;
testKey.createFromHex ("0000000000000000000000000000000000000000000000000000000000000000");
testIV.createFromHex ("014730f80ac625fe84f026c60bfd547d");
plaintext.createFromHex ("00000000000000000000000000000000");
cipherTarg.createFromHex("5c9d844ed46f9885085e5d6a4f94c7d7");
cout << " Plain : " << plaintext.toHexStr() << endl;
cipherComp = CryptoAES().EncryptCFB(plaintext, testKey, testIV);
cout << " CipherTarget : " << cipherComp.toHexStr() << endl;
cout << " CipherCompute: " << cipherComp.toHexStr() << endl;
rtPlain = CryptoAES().DecryptCFB(cipherComp, testKey, testIV);
cout << " Plain : " << rtPlain.toHexStr() << endl;
/// *** Test 3 *** ///
cout << endl << endl;
testKey.createFromHex ("ffffffffffff0000000000000000000000000000000000000000000000000000");
testIV.createFromHex ("00000000000000000000000000000000");
plaintext.createFromHex ("00000000000000000000000000000000");
cipherTarg.createFromHex("225f068c28476605735ad671bb8f39f3");
cout << " Plain : " << plaintext.toHexStr() << endl;
cipherComp = CryptoAES().EncryptCFB(plaintext, testKey, testIV);
cout << " CipherTarget : " << cipherComp.toHexStr() << endl;
cout << " CipherCompute: " << cipherComp.toHexStr() << endl;
rtPlain = CryptoAES().DecryptCFB(cipherComp, testKey, testIV);
cout << " Plain : " << rtPlain.toHexStr() << endl;
/// My own test, for sanity (can only check the roundtrip values)
// This test is a lot more exciting with the couts uncommented in Encrypt/Decrypt
cout << endl << endl;
cout << "Starting some kdf-aes-combined tests..." << endl;
kdf.printKdfParams();
testKey = kdf.DeriveKey(SecureBinaryData("This passphrase is tough to guess"));
SecureBinaryData secret, cipher;
secret.createFromHex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
SecureBinaryData randIV(0); // tell the crypto to generate a random IV for me.
cout << "Encrypting:" << endl;
cipher = CryptoAES().EncryptCFB(secret, testKey, randIV);
cout << endl << endl;
cout << "Decrypting:" << endl;
secret = CryptoAES().DecryptCFB(cipher, testKey, randIV);
cout << endl << endl;
// Now encrypting so I can store the encrypted data in file
cout << "Encrypting again:" << endl;
cipher = CryptoAES().EncryptCFB(secret, testKey, randIV);
ofstream testfile("safefile.txt", ios::out);
testfile << "KdfParams " << endl;
testfile << " MemReqts " << kdf.getMemoryReqtBytes() << endl;
testfile << " NumIters " << kdf.getNumIterations() << endl;
testfile << " HexSalt " << kdf.getSalt().toHexStr() << endl;
testfile << "EncryptedData" << endl;
testfile << " HexIV " << randIV.toHexStr() << endl;
testfile << " Cipher " << cipher.toHexStr() << endl;
testfile.close();
ifstream infile("safefile.txt", ios::in);
uint32_t mem, nIters;
SecureBinaryData salt, iv;
char deadstr[256];
char hexstr[256];
infile >> deadstr;
infile >> deadstr >> mem;
infile >> deadstr >> nIters;
infile >> deadstr >> hexstr;
salt.copyFrom( SecureBinaryData::CreateFromHex(string(hexstr, 64)));
infile >> deadstr;
infile >> deadstr >> hexstr;
iv.copyFrom( SecureBinaryData::CreateFromHex(string(hexstr, 64)));
infile >> deadstr >> hexstr;
cipher.copyFrom( SecureBinaryData::CreateFromHex(string(hexstr, 64)));
infile.close();
cout << endl << endl;
// Will try this twice, once with correct passphrase, once without
SecureBinaryData cipherTry1 = cipher;
SecureBinaryData cipherTry2 = cipher;
SecureBinaryData newKey;
KdfRomix newKdf(mem, nIters, salt);
newKdf.printKdfParams();
// First test with the wrong passphrase
cout << "Attempting to decrypt with wrong passphrase" << endl;
SecureBinaryData passphrase = SecureBinaryData("This is the wrong passphrase");
newKey = newKdf.DeriveKey( passphrase );
CryptoAES().DecryptCFB(cipherTry1, newKey, iv);
// Now try correct passphrase
cout << "Attempting to decrypt with CORRECT passphrase" << endl;
passphrase = SecureBinaryData("This passphrase is tough to guess");
newKey = newKdf.DeriveKey( passphrase );
CryptoAES().DecryptCFB(cipherTry2, newKey, iv);
}
void TestECDSA(void)
{
SecureBinaryData msgToSign("This message came from me!");
SecureBinaryData privData = SecureBinaryData().GenerateRandom(32);
BTC_PRIVKEY privKey = CryptoECDSA().ParsePrivateKey(privData);
BTC_PUBKEY pubKey = CryptoECDSA().ComputePublicKey(privKey);
// Test key-match check
cout << "Do the pub-priv keypair we just created match? ";
cout << (CryptoECDSA().CheckPubPrivKeyMatch(privKey, pubKey) ? 1 : 0) << endl;
cout << endl;
SecureBinaryData signature = CryptoECDSA().SignData(msgToSign, privKey);
cout << "Signature = " << signature.toHexStr() << endl;
cout << endl;
bool isValid = CryptoECDSA().VerifyData(msgToSign, signature, pubKey);
cout << "SigValid? = " << (isValid ? 1 : 0) << endl;
cout << endl;
// Test signature from blockchain:
SecureBinaryData msg = SecureBinaryData::CreateFromHex("0100000001bb664ff716b9dfc831bcc666c1767f362ad467fcfbaf4961de92e45547daab870100000062537a7652a269537a829178a91480677c5392220db736455533477d0bc2fba65502879b69537a829178a91402d7aa2e76d9066fb2b3c41ff8839a5c81bdca19879b69537a829178a91410039ce4fdb5d4ee56148fe3935b9bfbbe4ecc89879b6953aeffffffff0280969800000000001976a9140817482d2e97e4be877efe59f4bae108564549f188ac7015a7000000000062537a7652a269537a829178a91480677c5392220db736455533477d0bc2fba65502879b69537a829178a91402d7aa2e76d9066fb2b3c41ff8839a5c81bdca19879b69537a829178a91410039ce4fdb5d4ee56148fe3935b9bfbbe4ecc89879b6953ae0000000001000000");
SecureBinaryData px = SecureBinaryData::CreateFromHex("8c006ff0d2cfde86455086af5a25b88c2b81858aab67f6a3132c885a2cb9ec38");
SecureBinaryData py = SecureBinaryData::CreateFromHex("e700576fd46c7d72d7d22555eee3a14e2876c643cd70b1b0a77fbf46e62331ac");
SecureBinaryData pub65 = SecureBinaryData::CreateFromHex("048c006ff0d2cfde86455086af5a25b88c2b81858aab67f6a3132c885a2cb9ec38e700576fd46c7d72d7d22555eee3a14e2876c643cd70b1b0a77fbf46e62331ac");
//SecureBinaryData sig = SecureBinaryData::CreateFromHex("3046022100d73f633f114e0e0b324d87d38d34f22966a03b072803afa99c9408201f6d6dc6022100900e85be52ad2278d24e7edbb7269367f5f2d6f1bd338d017ca4600087766144");
SecureBinaryData sig = SecureBinaryData::CreateFromHex("d73f633f114e0e0b324d87d38d34f22966a03b072803afa99c9408201f6d6dc6900e85be52ad2278d24e7edbb7269367f5f2d6f1bd338d017ca4600087766144");
pubKey = CryptoECDSA().ParsePublicKey(px,py);
isValid = CryptoECDSA().VerifyData(msg, sig, pubKey);
cout << "SigValid? = " << (isValid ? 1 : 0) << endl;
// Test speed on signature:
uint32_t nTest = 50;
cout << "Test signature and verification speeds" << endl;
cout << "\nTiming Signing";
TIMER_START("SigningTime");
for(uint32_t i=0; i<nTest; i++)
{
// This timing includes key parsing
CryptoECDSA().SignData(msgToSign, privData);
}
TIMER_STOP("SigningTime");
// Test speed of verification
TIMER_START("VerifyTime");
cout << "\nTiming Verify";
for(uint32_t i=0; i<nTest; i++)
{
// This timing includes key parsing
CryptoECDSA().VerifyData(msg, sig, pub65);
}
TIMER_STOP("VerifyTime");
cout << endl;
cout << "Timing (Signing): " << 1/(TIMER_READ_SEC("SigningTime")/nTest)
<< " signatures/sec" << endl;
cout << "Timing (Verify): " << 1/(TIMER_READ_SEC("VerifyTime")/nTest)
<< " verifies/sec" << endl;
// Test deterministic key generation
SecureBinaryData privDataOrig = SecureBinaryData().GenerateRandom(32);
BTC_PRIVKEY privOrig = CryptoECDSA().ParsePrivateKey(privDataOrig);
BTC_PUBKEY pubOrig = CryptoECDSA().ComputePublicKey(privOrig);
cout << "Testing deterministic key generation" << endl;
cout << " Verify again that pub/priv objects pair match : ";
cout << (CryptoECDSA().CheckPubPrivKeyMatch(privOrig, pubOrig) ? 1 : 0) << endl;
SecureBinaryData binPriv = CryptoECDSA().SerializePrivateKey(privOrig);
SecureBinaryData binPub = CryptoECDSA().SerializePublicKey(pubOrig);
cout << " Verify again that binary pub/priv pair match : ";
cout << (CryptoECDSA().CheckPubPrivKeyMatch(binPriv, binPub) ? 1 : 0) << endl;
cout << endl;
SecureBinaryData chaincode = SecureBinaryData().GenerateRandom(32);
cout << " Starting privKey:" << binPriv.toHexStr() << endl;
cout << " Starting pubKey :" << binPub.toHexStr() << endl;
cout << " Chaincode :" << chaincode.toHexStr() << endl;
cout << endl;
SecureBinaryData newBinPriv = CryptoECDSA().ComputeChainedPrivateKey(binPriv, chaincode);
SecureBinaryData newBinPubA = CryptoECDSA().ComputePublicKey(newBinPriv);
SecureBinaryData newBinPubB = CryptoECDSA().ComputeChainedPublicKey(binPub, chaincode);
cout << " Verify new binary pub/priv pair match: ";
cout << (CryptoECDSA().CheckPubPrivKeyMatch(newBinPriv, newBinPubA) ? 1 : 0) << endl;
cout << " Verify new binary pub/priv pair match: ";
cout << (CryptoECDSA().CheckPubPrivKeyMatch(newBinPriv, newBinPubB) ? 1 : 0) << endl;
cout << " New privKey:" << newBinPriv.toHexStr() << endl;
cout << " New pubKeyA:" << newBinPubA.getSliceCopy(0,30).toHexStr() << "..." << endl;
cout << " New pubKeyB:" << newBinPubB.getSliceCopy(0,30).toHexStr() << "..." << endl;
cout << endl;
// Test arbitrary scalar/point operations
BinaryData a = BinaryData::CreateFromHex("8c006ff0d2cfde86455086af5a25b88c2b81858aab67f6a3132c885a2cb9ec38");
BinaryData b = BinaryData::CreateFromHex("e700576fd46c7d72d7d22555eee3a14e2876c643cd70b1b0a77fbf46e62331ac");
BinaryData c = BinaryData::CreateFromHex("f700576fd46c7d72d7d22555eee3a14e2876c643cd70b1b0a77fbf46e62331ac");
BinaryData d = BinaryData::CreateFromHex("8130904787384d72d7d22555eee3a14e2876c643cd70b1b0a77fbf46e62331ac");
BinaryData e = CryptoECDSA().ECMultiplyScalars(a,b);
BinaryData f = CryptoECDSA().ECMultiplyPoint(a, b, c);
BinaryData g = CryptoECDSA().ECAddPoints(a, b, c, d);
}
void TestPointCompression(void)
{
vector<BinaryData> testPubKey(3);
testPubKey[0].createFromHex("044f355bdcb7cc0af728ef3cceb9615d90684bb5b2ca5f859ab0f0b704075871aa385b6b1b8ead809ca67454d9683fcf2ba03456d6fe2c4abe2b07f0fbdbb2f1c1");
testPubKey[1].createFromHex("04ed83704c95d829046f1ac27806211132102c34e9ac7ffa1b71110658e5b9d1bdedc416f5cefc1db0625cd0c75de8192d2b592d7e3b00bcfb4a0e860d880fd1fc");
testPubKey[2].createFromHex("042596957532fc37e40486b910802ff45eeaa924548c0e1c080ef804e523ec3ed3ed0a9004acf927666eee18b7f5e8ad72ff100a3bb710a577256fd7ec81eb1cb3");
CryptoPP::ECP & ecp = CryptoECDSA::Get_secp256k1_ECP();
for(uint32_t i=0; i<3; i++)
{
CryptoPP::Integer pubX, pubY;
pubX.Decode(testPubKey[i].getPtr()+1, 32, UNSIGNED);
pubY.Decode(testPubKey[i].getPtr()+33, 32, UNSIGNED);
BTC_ECPOINT ptPub(pubX, pubY);
BinaryData ptFlat(65);
BinaryData ptComp(33);
ecp.EncodePoint((byte*)ptFlat.getPtr(), ptPub, false);
ecp.EncodePoint((byte*)ptComp.getPtr(), ptPub, true);