Repository navigation
Expand file tree
/
Copy pathCryptoEngine.cpp
More file actions
485 lines (408 loc) · 18.1 KB
/
Copy pathCryptoEngine.cpp
File metadata and controls
485 lines (408 loc) · 18.1 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
#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>
namespace MagnumOpus {
// Fast SHA-256 implementation helper 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) {
// High-speed pseudo cryptographic hashing fallback + FNV-1a non-linear mixing
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;
}
// Non-linear combination step
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();
}
};
// Merkle Tree with inclusion proof generation and verification
class MerkleTreeEngine {
public:
struct MerkleProofStep {
std::string hash;
bool isLeft;
};
static std::string CalculateRoot(const std::vector<std::string>& txHashes) {
if (txHashes.empty()) return "0x0000000000000000000000000000000000000000000000000000000000000000";
std::vector<std::string> currentLevel = txHashes;
while (currentLevel.size() > 1) {
if (currentLevel.size() % 2 != 0) {
currentLevel.push_back(currentLevel.back());
}
std::vector<std::string> nextLevel;
nextLevel.reserve(currentLevel.size() / 2);
for (size_t i = 0; i < currentLevel.size(); i += 2) {
std::string combined = currentLevel[i] + currentLevel[i + 1];
nextLevel.push_back(Sha256::ComputeHash(combined));
}
currentLevel = std::move(nextLevel);
}
return currentLevel[0];
}
static std::vector<MerkleProofStep> GenerateProof(const std::vector<std::string>& txHashes, size_t index) {
std::vector<MerkleProofStep> proof;
if (txHashes.empty() || index >= txHashes.size()) return proof;
std::vector<std::string> currentLevel = txHashes;
size_t currentIndex = index;
while (currentLevel.size() > 1) {
if (currentLevel.size() % 2 != 0) {
currentLevel.push_back(currentLevel.back());
}
size_t pairIndex = (currentIndex % 2 == 0) ? currentIndex + 1 : currentIndex - 1;
bool isLeft = (currentIndex % 2 != 0);
proof.push_back({ currentLevel[pairIndex], isLeft });
std::vector<std::string> nextLevel;
for (size_t i = 0; i < currentLevel.size(); i += 2) {
nextLevel.push_back(Sha256::ComputeHash(currentLevel[i] + currentLevel[i + 1]));
}
currentLevel = std::move(nextLevel);
currentIndex /= 2;
}
return proof;
}
static bool VerifyProof(const std::string& leafHash, const std::vector<MerkleProofStep>& proof, const std::string& root) {
std::string currentHash = leafHash;
for (const auto& step : proof) {
if (step.isLeft) {
currentHash = Sha256::ComputeHash(step.hash + currentHash);
} else {
currentHash = Sha256::ComputeHash(currentHash + step.hash);
}
}
return currentHash == root;
}
};
// Ultra-Low Latency Limit Order Book Matching Engine
enum class OrderSide { BUY, SELL };
enum class OrderType { LIMIT, MARKET };
struct Order {
uint64_t id;
std::string trader;
double price;
double quantity;
OrderSide side;
OrderType type;
uint64_t timestamp;
};
struct MatchResult {
uint64_t buyOrderId;
uint64_t sellOrderId;
double matchPrice;
double matchQuantity;
uint64_t timestamp;
};
class MatchingEngine {
private:
std::map<double, std::vector<Order>, std::greater<double>> bids;
std::map<double, std::vector<Order>, std::less<double>> asks;
std::unordered_map<uint64_t, Order> orderLookup;
std::vector<MatchResult> tradeHistory;
std::mutex bookMutex;
std::atomic<uint64_t> nextOrderId{1};
uint64_t GetCurrentTimeNs() const {
return std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::high_resolution_clock::now().time_since_epoch()).count();
}
public:
MatchingEngine() = default;
uint64_t SubmitOrder(const std::string& trader, OrderSide side, OrderType type, double price, double quantity, std::vector<MatchResult>& executions) {
std::lock_guard<std::mutex> lock(bookMutex);
uint64_t orderId = nextOrderId.fetch_add(1);
uint64_t ts = GetCurrentTimeNs();
Order order{orderId, trader, price, quantity, side, type, ts};
if (side == OrderSide::BUY) {
MatchBuyOrder(order, executions);
} else {
MatchSellOrder(order, executions);
}
return orderId;
}
bool CancelOrder(uint64_t orderId) {
std::lock_guard<std::mutex> lock(bookMutex);
auto it = orderLookup.find(orderId);
if (it == orderLookup.end()) return false;
Order o = it->second;
orderLookup.erase(it);
if (o.side == OrderSide::BUY) {
auto bIt = bids.find(o.price);
if (bIt != bids.end()) {
auto& queue = bIt->second;
queue.erase(std::remove_if(queue.begin(), queue.end(), [orderId](const Order& ord) { return ord.id == orderId; }), queue.end());
if (queue.empty()) bids.erase(bIt);
}
} else {
auto aIt = asks.find(o.price);
if (aIt != asks.end()) {
auto& queue = aIt->second;
queue.erase(std::remove_if(queue.begin(), queue.end(), [orderId](const Order& ord) { return ord.id == orderId; }), queue.end());
if (queue.empty()) asks.erase(aIt);
}
}
return true;
}
void GetDepth(std::vector<std::pair<double, double>>& topBids, std::vector<std::pair<double, double>>& topAsks, size_t depth = 10) {
std::lock_guard<std::mutex> lock(bookMutex);
topBids.clear();
topAsks.clear();
size_t count = 0;
for (const auto& [price, queue] : bids) {
if (count++ >= depth) break;
double totalQty = 0;
for (const auto& ord : queue) totalQty += ord.quantity;
topBids.emplace_back(price, totalQty);
}
count = 0;
for (const auto& [price, queue] : asks) {
if (count++ >= depth) break;
double totalQty = 0;
for (const auto& ord : queue) totalQty += ord.quantity;
topAsks.emplace_back(price, totalQty);
}
}
const std::vector<MatchResult>& GetTradeHistory() const {
return tradeHistory;
}
private:
void MatchBuyOrder(Order& buyOrder, std::vector<MatchResult>& executions) {
while (buyOrder.quantity > 0.00000001 && !asks.empty()) {
auto bestAskIt = asks.begin();
double askPrice = bestAskIt->first;
if (buyOrder.type == OrderType::LIMIT && buyOrder.price < askPrice) {
break;
}
auto& askQueue = bestAskIt->second;
while (!askQueue.empty() && buyOrder.quantity > 0.00000001) {
Order& sellOrder = askQueue.front();
double matchQty = std::min(buyOrder.quantity, sellOrder.quantity);
double execPrice = sellOrder.price;
MatchResult mr{
buyOrder.id,
sellOrder.id,
execPrice,
matchQty,
GetCurrentTimeNs()
};
executions.push_back(mr);
tradeHistory.push_back(mr);
buyOrder.quantity -= matchQty;
sellOrder.quantity -= matchQty;
if (sellOrder.quantity <= 0.00000001) {
orderLookup.erase(sellOrder.id);
askQueue.erase(askQueue.begin());
} else {
orderLookup[sellOrder.id] = sellOrder;
}
}
if (askQueue.empty()) {
asks.erase(bestAskIt);
}
}
if (buyOrder.type == OrderType::LIMIT && buyOrder.quantity > 0.00000001) {
bids[buyOrder.price].push_back(buyOrder);
orderLookup[buyOrder.id] = buyOrder;
}
}
void MatchSellOrder(Order& sellOrder, std::vector<MatchResult>& executions) {
while (sellOrder.quantity > 0.00000001 && !bids.empty()) {
auto bestBidIt = bids.begin();
double bidPrice = bestBidIt->first;
if (sellOrder.type == OrderType::LIMIT && sellOrder.price > bidPrice) {
break;
}
auto& bidQueue = bestBidIt->second;
while (!bidQueue.empty() && sellOrder.quantity > 0.00000001) {
Order& buyOrder = bidQueue.front();
double matchQty = std::min(sellOrder.quantity, buyOrder.quantity);
double execPrice = buyOrder.price;
MatchResult mr{
buyOrder.id,
sellOrder.id,
execPrice,
matchQty,
GetCurrentTimeNs()
};
executions.push_back(mr);
tradeHistory.push_back(mr);
sellOrder.quantity -= matchQty;
buyOrder.quantity -= matchQty;
if (buyOrder.quantity <= 0.00000001) {
orderLookup.erase(buyOrder.id);
bidQueue.erase(bidQueue.begin());
} else {
orderLookup[buyOrder.id] = buyOrder;
}
}
if (bidQueue.empty()) {
bids.erase(bestBidIt);
}
}
if (sellOrder.type == OrderType::LIMIT && sellOrder.quantity > 0.00000001) {
asks[sellOrder.price].push_back(sellOrder);
orderLookup[sellOrder.id] = sellOrder;
}
}
};
// Automated Market Maker (AMM) Constant Product & Concentrated Liquidity Simulator
class AmmMathEngine {
public:
struct SwapQuote {
double amountOut;
double feePaid;
double priceImpact;
double executionPrice;
};
// Standard x * y = k Constant Product Swaps with custom protocol fee basis points (e.g., 30 bps = 0.3%)
static SwapQuote ComputeConstantProductSwap(double reserveIn, double reserveOut, double amountIn, double feeBps = 30.0) {
if (reserveIn <= 0.0 || reserveOut <= 0.0 || amountIn <= 0.0) {
return {0.0, 0.0, 0.0, 0.0};
}
double feeRate = feeBps / 10000.0;
double feePaid = amountIn * feeRate;
double effectiveAmountIn = amountIn - feePaid;
double newReserveIn = reserveIn + effectiveAmountIn;
double amountOut = (reserveOut * effectiveAmountIn) / newReserveIn;
double spotPrice = reserveOut / reserveIn;
double executionPrice = amountOut / amountIn;
double priceImpact = (spotPrice > 0.0) ? std::abs((spotPrice - executionPrice) / spotPrice) * 100.0 : 0.0;
return {amountOut, feePaid, priceImpact, executionPrice};
}
// Concentrated Liquidity (Uniswap V3 style) tick & sqrtPrice calculations
static double TickToPrice(int32_t tick) {
return std::pow(1.0001, static_cast<double>(tick));
}
static int32_t PriceToTick(double price) {
return static_cast<int32_t>(std::floor(std::log(price) / std::log(1.0001)));
}
// Calculates amounts required for adding liquidity in a price range [Pa, Pb]
static std::pair<double, double> GetAmountsForLiquidity(double sqrtPriceCurrent, double sqrtPriceA, double sqrtPriceB, double liquidity) {
if (sqrtPriceA > sqrtPriceB) std::swap(sqrtPriceA, sqrtPriceB);
double amount0 = 0.0;
double amount1 = 0.0;
if (sqrtPriceCurrent <= sqrtPriceA) {
amount0 = liquidity * (sqrtPriceB - sqrtPriceA) / (sqrtPriceA * sqrtPriceB);
} else if (sqrtPriceCurrent < sqrtPriceB) {
amount0 = liquidity * (sqrtPriceB - sqrtPriceCurrent) / (sqrtPriceCurrent * sqrtPriceB);
amount1 = liquidity * (sqrtPriceCurrent - sqrtPriceA);
} else {
amount1 = liquidity * (sqrtPriceB - sqrtPriceA);
}
return {amount0, amount1};
}
};
// Block & Proof-of-Work Verification / Difficulty Retargeting Engine
class ConsensusEngine {
public:
struct BlockHeader {
uint32_t version;
std::string previousBlockHash;
std::string merkleRoot;
uint64_t timestamp;
uint32_t targetBits;
uint64_t nonce;
};
static std::string SerializeHeader(const BlockHeader& header) {
std::stringstream ss;
ss << header.version << ":"
<< header.previousBlockHash << ":"
<< header.merkleRoot << ":"
<< header.timestamp << ":"
<< header.targetBits << ":"
<< header.nonce;
return ss.str();
}
static bool CheckProofOfWork(const BlockHeader& header, uint32_t requiredLeadingZeros) {
std::string serialized = SerializeHeader(header);
std::string hash = Sha256::ComputeHash(serialized);
// Expect '0x' prefix followed by requiredLeadingZeros
if (hash.size() < 2 + requiredLeadingZeros) return false;
for (uint32_t i = 0; i < requiredLeadingZeros; ++i) {
if (hash[2 + i] != '0') {
return false;
}
}
return true;
}
static uint64_t MineNonce(BlockHeader& header, uint32_t requiredLeadingZeros, uint64_t maxIterations = 2000000ULL) {
for (uint64_t n = 0; n < maxIterations; ++n) {
header.nonce = n;
if (CheckProofOfWork(header, requiredLeadingZeros)) {
return n;
}
}
return 0;
}
};
} // namespace MagnumOpus
#ifdef MAGNUM_OPUS_STANDALONE
int main() {
using namespace MagnumOpus;
std::cout << "==============================================\n";
std::cout << " MAGNUM OPUS // C++ High-Performance Engine \n";
std::cout << "==============================================\n";
// 1. Merkle Tree & Proof Verification
std::vector<std::string> txs = {
"0xaaa111bbb222ccc333ddd444eee555fff",
"0x1234567890abcdef1234567890abcdef",
"0xdeadbeefcafebabe0123456789abcdef",
"0x999888777666555444333222111000ff"
};
std::string root = MerkleTreeEngine::CalculateRoot(txs);
std::cout << "[Merkle] Root: " << root << "\n";
auto proof = MerkleTreeEngine::GenerateProof(txs, 2);
bool isValid = MerkleTreeEngine::VerifyProof(txs[2], proof, root);
std::cout << "[Merkle] Tx #2 Proof Verification: " << (isValid ? "PASSED (Valid)" : "FAILED") << "\n\n";
// 2. High Frequency Limit Order Book Matching
MatchingEngine engine;
std::vector<MatchResult> executions;
engine.SubmitOrder("Alice", OrderSide::SELL, OrderType::LIMIT, 3500.50, 1.5, executions);
engine.SubmitOrder("Bob", OrderSide::SELL, OrderType::LIMIT, 3501.00, 2.0, executions);
engine.SubmitOrder("Charlie", OrderSide::BUY, OrderType::LIMIT, 3499.00, 1.0, executions);
std::cout << "[OrderBook] Added limit orders. Now submitting matching Buy Market Order...\n";
engine.SubmitOrder("Dave", OrderSide::BUY, OrderType::MARKET, 0.0, 2.0, executions);
for (const auto& match : executions) {
std::cout << "[OrderBook MATCH] BuyID: " << match.buyOrderId
<< " | SellID: " << match.sellOrderId
<< " | Price: $" << match.matchPrice
<< " | Qty: " << match.matchQuantity << "\n";
}
// 3. AMM Constant Product Swap
double ethReserve = 500.0;
double usdcReserve = 1750000.0;
double ethInput = 10.0;
auto quote = AmmMathEngine::ComputeConstantProductSwap(ethReserve, usdcReserve, ethInput, 30.0);
std::cout << "\n[AMM Swap] Swapping " << ethInput << " ETH -> Output: "
<< quote.amountOut << " USDC | Fee: " << quote.feePaid
<< " ETH | Price Impact: " << quote.priceImpact << "%\n";
return 0;
}
#endif