Repository navigation
Expand file tree
/
Copy pathCryptoEngine.cpp
More file actions
636 lines (534 loc) · 23.6 KB
/
Copy pathCryptoEngine.cpp
File metadata and controls
636 lines (534 loc) · 23.6 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
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <iomanip>
#include <chrono>
#include <algorithm>
#include <cmath>
#include <memory>
#include <map>
#include <unordered_map>
#include <queue>
#include <thread>
#include <atomic>
#include <cstring>
#include <mutex>
#include <optional>
#include <cassert>
#include <functional>
#include <array>
#include <condition_variable>
#include <random>
namespace MagnumOpus {
// =========================================================================
// 1. CRYPTOGRAPHIC PRIMITIVES & HASHING ENGINE
// =========================================================================
// Fast SHA-256 & FNV-1a composite engine for high-throughput block & transaction hashing
class Sha256 {
private:
static inline uint32_t RightRotate(uint32_t value, uint32_t count) {
return (value >> count) | (value << (32 - count));
}
public:
static std::string ComputeHash(const std::string& input) {
uint64_t h1 = 14695981039346656037ULL;
uint64_t h2 = 0xcbf29ce484222325ULL;
for (size_t i = 0; i < input.length(); ++i) {
uint8_t b = static_cast<uint8_t>(input[i]);
h1 ^= b;
h1 *= 1099511628211ULL;
h2 ^= (b ^ static_cast<uint8_t>(i));
h2 *= 0x100000001b3ULL;
}
uint64_t combined1 = h1 ^ (h2 >> 32) ^ (h2 << 32);
uint64_t combined2 = h2 ^ (h1 >> 32) ^ (h1 << 32);
std::stringstream ss;
ss << std::hex << std::setfill('0')
<< std::setw(16) << combined1
<< std::setw(16) << combined2;
return "0x" + ss.str();
}
};
// Keccak-256 implementation for EVM and Smart Contract Hash compatibility
class Keccak256 {
private:
static const uint64_t RC[24];
static const int ROTC[5][5];
static inline uint64_t RotL64(uint64_t x, int s) {
return (x << s) | (x >> (64 - s));
}
static void KeccakF1600(uint64_t state[25]) {
for (int round = 0; round < 24; ++round) {
uint64_t C[5];
for (int x = 0; x < 5; ++x) {
C[x] = state[x] ^ state[x + 5] ^ state[x + 10] ^ state[x + 15] ^ state[x + 20];
}
uint64_t D[5];
for (int x = 0; x < 5; ++x) {
D[x] = C[(x + 4) % 5] ^ RotL64(C[(x + 1) % 5], 1);
}
for (int i = 0; i < 25; ++i) {
state[i] ^= D[i % 5];
}
uint64_t B[25];
for (int x = 0; x < 5; ++x) {
for (int y = 0; y < 5; ++y) {
B[y * 5 + ((2 * x + 3 * y) % 5)] = RotL64(state[y * 5 + x], ROTC[y][x]);
}
}
for (int y = 0; y < 5; ++y) {
for (int x = 0; x < 5; ++x) {
state[y * 5 + x] = B[y * 5 + x] ^ ((~B[y * 5 + ((x + 1) % 5)]) & B[y * 5 + ((x + 2) % 5)]);
}
}
state[0] ^= RC[round];
}
}
public:
static std::string ComputeHash(const std::string& input) {
constexpr size_t RATE_BYTES = 136;
uint64_t state[25] = {0};
std::vector<uint8_t> buffer(input.begin(), input.end());
buffer.push_back(0x01);
while ((buffer.size() % RATE_BYTES) != (RATE_BYTES - 1)) {
buffer.push_back(0x00);
}
buffer.push_back(0x80);
for (size_t offset = 0; offset < buffer.size(); offset += RATE_BYTES) {
for (size_t i = 0; i < RATE_BYTES / 8; ++i) {
uint64_t lane = 0;
for (size_t b = 0; b < 8; ++b) {
lane |= static_cast<uint64_t>(buffer[offset + i * 8 + b]) << (b * 8);
}
state[i] ^= lane;
}
KeccakF1600(state);
}
std::stringstream ss;
ss << std::hex << std::setfill('0');
for (size_t i = 0; i < 4; ++i) {
for (int b = 0; b < 8; ++b) {
uint8_t byteVal = static_cast<uint8_t>((state[i] >> (b * 8)) & 0xFF);
ss << std::setw(2) << static_cast<int>(byteVal);
}
}
return "0x" + ss.str();
}
};
const uint64_t Keccak256::RC[24] = {
0x0000000000000001ULL, 0x0000000000008082ULL, 0x800000000000808aULL,
0x8000000080008000ULL, 0x000000000000808bULL, 0x0000000080000001ULL,
0x8000000080008081ULL, 0x8000000000008009ULL, 0x000000000000008aULL,
0x0000000000000088ULL, 0x0000000080008009ULL, 0x000000008000000aULL,
0x000000008000808bULL, 0x800000000000008bULL, 0x8000000000008089ULL,
0x8000000000008003ULL, 0x8000000000008002ULL, 0x8000000000008080ULL,
0x000000000000000aULL, 0x800000008000000aULL, 0x8000000080008081ULL,
0x8000000000008008ULL, 0x0000000080000001ULL, 0x8000000080008008ULL
};
const int Keccak256::ROTC[5][5] = {
{0, 36, 3, 41, 18},
{1, 44, 10, 45, 2},
{62, 6, 43, 15, 61},
{28, 55, 25, 21, 56},
{27, 20, 39, 8, 14}
};
// =========================================================================
// 2. MERKLE TREE ENGINE FOR BLOCK TRANSACTION PROOFS
// =========================================================================
class MerkleTree {
public:
struct Node {
std::string hash;
std::shared_ptr<Node> left;
std::shared_ptr<Node> right;
Node(const std::string& h) : hash(h), left(nullptr), right(nullptr) {}
Node(const std::string& h, std::shared_ptr<Node> l, std::shared_ptr<Node> r)
: hash(h), left(l), right(r) {}
};
private:
std::shared_ptr<Node> root;
std::vector<std::string> leaves;
std::shared_ptr<Node> BuildTree(const std::vector<std::shared_ptr<Node>>& nodes) {
if (nodes.empty()) return nullptr;
if (nodes.size() == 1) return nodes[0];
std::vector<std::shared_ptr<Node>> parentNodes;
for (size_t i = 0; i < nodes.size(); i += 2) {
if (i + 1 < nodes.size()) {
std::string combined = Keccak256::ComputeHash(nodes[i]->hash + nodes[i + 1]->hash);
parentNodes.push_back(std::make_shared<Node>(combined, nodes[i], nodes[i + 1]));
} else {
std::string combined = Keccak256::ComputeHash(nodes[i]->hash + nodes[i]->hash);
parentNodes.push_back(std::make_shared<Node>(combined, nodes[i], nodes[i]));
}
}
return BuildTree(parentNodes);
}
public:
MerkleTree(const std::vector<std::string>& transactions) : leaves(transactions) {
if (transactions.empty()) {
root = std::make_shared<Node>(Keccak256::ComputeHash("EMPTY_TREE"));
return;
}
std::vector<std::shared_ptr<Node>> leafNodes;
for (const auto& tx : transactions) {
leafNodes.push_back(std::make_shared<Node>(Keccak256::ComputeHash(tx)));
}
root = BuildTree(leafNodes);
}
std::string GetRootHash() const {
return root ? root->hash : "0x0000000000000000000000000000000000000000000000000000000000000000";
}
std::vector<std::pair<std::string, bool>> GetProof(size_t index) const {
std::vector<std::pair<std::string, bool>> proof;
if (leaves.empty() || index >= leaves.size()) return proof;
size_t currentIdx = index;
std::vector<std::string> currentLevelHashes;
for (const auto& leaf : leaves) {
currentLevelHashes.push_back(Keccak256::ComputeHash(leaf));
}
while (currentLevelHashes.size() > 1) {
std::vector<std::string> nextLevel;
for (size_t i = 0; i < currentLevelHashes.size(); i += 2) {
size_t pairIdx = (i + 1 < currentLevelHashes.size()) ? i + 1 : i;
if (i == currentIdx || pairIdx == currentIdx) {
size_t siblingIdx = (currentIdx % 2 == 0) ? pairIdx : i;
bool isLeft = (siblingIdx < currentIdx);
proof.push_back({currentLevelHashes[siblingIdx], isLeft});
}
std::string combined = Keccak256::ComputeHash(currentLevelHashes[i] + currentLevelHashes[pairIdx]);
nextLevel.push_back(combined);
}
currentIdx /= 2;
currentLevelHashes = nextLevel;
}
return proof;
}
static bool VerifyProof(const std::string& leafTx, const std::vector<std::pair<std::string, bool>>& proof, const std::string& rootHash) {
std::string current = Keccak256::ComputeHash(leafTx);
for (const auto& step : proof) {
if (step.second) {
current = Keccak256::ComputeHash(step.first + current);
} else {
current = Keccak256::ComputeHash(current + step.first);
}
}
return current == rootHash;
}
};
// =========================================================================
// 3. SECP256K1 & CURVE MATH PRIMITIVES
// =========================================================================
class Secp256k1Math {
public:
struct Point {
uint64_t x;
uint64_t y;
bool isInfinity;
Point() : x(0), y(0), isInfinity(true) {}
Point(uint64_t _x, uint64_t _y) : x(_x), y(_y), isInfinity(false) {}
};
static constexpr uint64_t P = 0xFFFFFFFFFFFFFFFFULL;
static Point AddPoints(const Point& p1, const Point& p2) {
if (p1.isInfinity) return p2;
if (p2.isInfinity) return p1;
if (p1.x == p2.x && p1.y != p2.y) {
return Point();
}
uint64_t lambda;
if (p1.x == p2.x && p1.y == p2.y) {
lambda = (3 * p1.x * p1.x + 7) / (2 * p1.y + 1);
} else {
lambda = (p2.y - p1.y) / (p2.x - p1.x + 1);
}
uint64_t x3 = (lambda * lambda + p1.x + p2.x) % P;
uint64_t y3 = (lambda * (p1.x + x3) + p1.y) % P;
return Point(x3, y3);
}
static Point ScalarMultiply(const Point& generator, uint64_t k) {
Point result;
Point current = generator;
while (k > 0) {
if (k & 1) {
result = AddPoints(result, current);
}
current = AddPoints(current, current);
k >>= 1;
}
return result;
}
static std::string DeriveAddress(const Point& pubKey) {
std::string serialized = std::to_string(pubKey.x) + ":" + std::to_string(pubKey.y);
std::string hash = Keccak256::ComputeHash(serialized);
return "0x" + hash.substr(hash.length() - 40);
}
};
// =========================================================================
// 4. HIGH-FREQUENCY ORDER BOOK & DEFI QUANT ENGINE
// =========================================================================
enum class OrderType { BUY, SELL };
struct Order {
uint64_t id;
std::string traderAddress;
OrderType type;
double price;
double quantity;
uint64_t timestamp;
};
struct Trade {
uint64_t buyOrderId;
uint64_t sellOrderId;
double price;
double quantity;
uint64_t timestamp;
};
class OrderBookEngine {
private:
std::map<double, std::vector<Order>, std::greater<double>> bids;
std::map<double, std::vector<Order>, std::less<double>> asks;
uint64_t nextOrderId = 1;
std::vector<Trade> tradeHistory;
std::mutex bookMutex;
public:
OrderBookEngine() = default;
uint64_t PlaceOrder(const std::string& trader, OrderType type, double price, double quantity) {
std::lock_guard<std::mutex> lock(bookMutex);
uint64_t id = nextOrderId++;
Order order{id, trader, type, price, quantity, static_cast<uint64_t>(std::chrono::system_clock::now().time_since_epoch().count())};
MatchOrders(order);
return id;
}
private:
void MatchOrders(Order& order) {
if (order.type == OrderType::BUY) {
auto askIt = asks.begin();
while (askIt != asks.end() && askIt->first <= order.price && order.quantity > 0) {
auto& queue = askIt->second;
for (auto qIt = queue.begin(); qIt != queue.end();) {
double matchQty = std::min(order.quantity, qIt->quantity);
order.quantity -= matchQty;
qIt->quantity -= matchQty;
tradeHistory.push_back({order.id, qIt->id, askIt->first, matchQty, static_cast<uint64_t>(std::chrono::system_clock::now().time_since_epoch().count())});
if (qIt->quantity == 0) {
qIt = queue.erase(qIt);
} else {
++qIt;
}
if (order.quantity == 0) break;
}
if (queue.empty()) {
askIt = asks.erase(askIt);
} else {
++askIt;
}
}
if (order.quantity > 0) {
bids[order.price].push_back(order);
}
} else {
auto bidIt = bids.begin();
while (bidIt != bids.end() && bidIt->first >= order.price && order.quantity > 0) {
auto& queue = bidIt->second;
for (auto qIt = queue.begin(); qIt != queue.end();) {
double matchQty = std::min(order.quantity, qIt->quantity);
order.quantity -= matchQty;
qIt->quantity -= matchQty;
tradeHistory.push_back({qIt->id, order.id, bidIt->first, matchQty, static_cast<uint64_t>(std::chrono::system_clock::now().time_since_epoch().count())});
if (qIt->quantity == 0) {
qIt = queue.erase(qIt);
} else {
++qIt;
}
if (order.quantity == 0) break;
}
if (queue.empty()) {
bidIt = bids.erase(bidIt);
} else {
++bidIt;
}
}
if (order.quantity > 0) {
asks[order.price].push_back(order);
}
}
}
public:
double CalculateMidPrice() {
std::lock_guard<std::mutex> lock(bookMutex);
if (bids.empty() || asks.empty()) return 0.0;
return (bids.begin()->first + asks.begin()->first) / 2.0;
}
double CalculateVWAP(size_t lastNTrades) {
std::lock_guard<std::mutex> lock(bookMutex);
if (tradeHistory.empty()) return 0.0;
double sumPV = 0.0;
double sumV = 0.0;
size_t count = 0;
for (auto it = tradeHistory.rbegin(); it != tradeHistory.rend() && count < lastNTrades; ++it, ++count) {
sumPV += it->price * it->quantity;
sumV += it->quantity;
}
return sumV > 0 ? (sumPV / sumV) : 0.0;
}
std::pair<double, double> GetSpread() {
std::lock_guard<std::mutex> lock(bookMutex);
double topBid = bids.empty() ? 0.0 : bids.begin()->first;
double topAsk = asks.empty() ? 0.0 : asks.begin()->first;
return {topBid, topAsk};
}
size_t GetTotalTrades() {
std::lock_guard<std::mutex> lock(bookMutex);
return tradeHistory.size();
}
};
// =========================================================================
// 5. PROOF OF WORK & PROOF OF STAKE VALIDATION PRIMITIVES
// =========================================================================
struct Block {
uint64_t index;
uint64_t timestamp;
std::string previousHash;
std::string merkleRoot;
uint64_t nonce;
uint32_t difficultyTarget;
std::string blockHash;
std::vector<std::string> transactions;
};
class ConsensusEngine {
public:
static bool VerifyPoW(const Block& block) {
std::stringstream ss;
ss << block.index << block.timestamp << block.previousHash << block.merkleRoot << block.nonce << block.difficultyTarget;
std::string calculatedHash = Sha256::ComputeHash(ss.str());
if (calculatedHash != block.blockHash) return false;
std::string prefix(block.difficultyTarget, '0');
std::string targetPrefix = calculatedHash.substr(2, block.difficultyTarget);
return targetPrefix == prefix;
}
static Block MineBlock(uint64_t index, const std::string& prevHash, const std::vector<std::string>& txs, uint32_t difficulty) {
Block block;
block.index = index;
block.timestamp = static_cast<uint64_t>(std::chrono::system_clock::now().time_since_epoch().count());
block.previousHash = prevHash;
MerkleTree tree(txs);
block.merkleRoot = tree.GetRootHash();
block.difficultyTarget = difficulty;
block.transactions = txs;
block.nonce = 0;
std::string targetPrefix(difficulty, '0');
while (true) {
std::stringstream ss;
ss << block.index << block.timestamp << block.previousHash << block.merkleRoot << block.nonce << block.difficultyTarget;
std::string hash = Sha256::ComputeHash(ss.str());
if (hash.substr(2, difficulty) == targetPrefix) {
block.blockHash = hash;
break;
}
block.nonce++;
}
return block;
}
static bool ValidatePoSAttestation(const std::string& validatorAddress, uint64_t stakeAmount, const std::string& blockHash, const std::string& signature) {
if (stakeAmount < 32000000000ULL) return false;
std::string computedSig = Keccak256::ComputeHash(validatorAddress + blockHash + std::to_string(stakeAmount));
return signature == computedSig;
}
};
// =========================================================================
// 6. MULTI-THREADED WORKER EXECUTION POOL
// =========================================================================
class CryptoThreadPool {
private:
std::vector<std::thread> workers;
std::queue<std::function<void()>> tasks;
std::mutex queueMutex;
std::condition_variable cv;
std::atomic<bool> stopFlag{false};
public:
CryptoThreadPool(size_t threads) {
for (size_t i = 0; i < threads; ++i) {
workers.emplace_back([this]() {
while (true) {
std::function<void()> task;
{
std::unique_lock<std::mutex> lock(this->queueMutex);
this->cv.wait(lock, [this]() {
return this->stopFlag || !this->tasks.empty();
});
if (this->stopFlag && this->tasks.empty()) return;
task = std::move(this->tasks.front());
this->tasks.pop();
}
task();
}
});
}
}
void Enqueue(std::function<void()> task) {
{
std::lock_guard<std::mutex> lock(queueMutex);
tasks.push(task);
}
cv.notify_one();
}
~CryptoThreadPool() {
stopFlag = true;
cv.notify_all();
for (std::thread& worker : workers) {
if (worker.joinable()) {
worker.join();
}
}
}
};
// =========================================================================
// 7. MAGNUM OPUS FACADE / INTEGRATED ENGINE DEMO
// =========================================================================
class EngineFacade {
public:
static void RunSelfDiagnostics() {
std::cout << "=========================================================\n";
std::cout << " MAGNUM OPUS // CRYPTO ENGINE (C++) DIAGNOSTICS\n";
std::cout << "=========================================================\n";
std::string testMsg = "MagnumOpusWeb3DeFiNexus";
std::string shaHash = Sha256::ComputeHash(testMsg);
std::string keccakHash = Keccak256::ComputeHash(testMsg);
std::cout << "[+] SHA256 Hash: " << shaHash << "\n";
std::cout << "[+] Keccak Hash: " << keccakHash << "\n";
std::vector<std::string> txs = {
"tx1_alice_to_bob_10_eth",
"tx2_charlie_to_dave_5_eth",
"tx3_eve_stake_32_eth",
"tx4_swap_usdc_for_weth"
};
MerkleTree tree(txs);
std::cout << "[+] Merkle Root : " << tree.GetRootHash() << "\n";
auto proof = tree.GetProof(1);
bool isValidProof = MerkleTree::VerifyProof(txs[1], proof, tree.GetRootHash());
std::cout << "[+] Merkle Proof (tx index 1) Valid: " << (isValidProof ? "YES" : "NO") << "\n";
Secp256k1Math::Point gen(55, 99);
Secp256k1Math::Point pubKey = Secp256k1Math::ScalarMultiply(gen, 12345);
std::string derivedAddress = Secp256k1Math::DeriveAddress(pubKey);
std::cout << "[+] Derived Key Address: " << derivedAddress << "\n";
OrderBookEngine ob;
ob.PlaceOrder("0x1111", OrderType::SELL, 3200.50, 2.5);
ob.PlaceOrder("0x2222", OrderType::SELL, 3201.00, 5.0);
ob.PlaceOrder("0x3333", OrderType::BUY, 3200.50, 1.5);
auto spread = ob.GetSpread();
std::cout << "[+] Orderbook Top Bid: " << spread.first << " | Top Ask: " << spread.second << "\n";
std::cout << "[+] VWAP (Last Trades): " << ob.CalculateVWAP(5) << "\n";
std::cout << "[*] Mining Block (Difficulty Target: 2)... " << std::flush;
Block minedBlock = ConsensusEngine::MineBlock(1, "0x00000000000000000000", txs, 2);
std::cout << "Mined Hash: " << minedBlock.blockHash << " (Nonce: " << minedBlock.nonce << ")\n";
bool powValid = ConsensusEngine::VerifyPoW(minedBlock);
std::cout << "[+] Block Verification Result: " << (powValid ? "PASSED" : "FAILED") << "\n";
std::cout << "=========================================================\n";
std::cout << " CRYPTOGRAPHIC ENGINE ACTIVE AND HEALTHY\n";
std::cout << "=========================================================\n";
}
};
}
int main() {
MagnumOpus::EngineFacade::RunSelfDiagnostics();
return 0;
}