-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
329 lines (285 loc) · 11.7 KB
/
Copy pathtest.cpp
File metadata and controls
329 lines (285 loc) · 11.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
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <array>
#include <sstream>
#include <iomanip>
using namespace std;
// Define a struct to represent a product
struct Product {
string id; // Product ID
string description; // Product description
string owner; // Current owner
// Constructor
Product(const string& _id, const string& _description, const string& _owner)
: id(_id), description(_description), owner(_owner) {}
};
// Define a struct to represent a token
struct Token {
string id; // Token ID
string owner; // Owner of the token
int balance; // Token balance
// Constructor
Token(const string& _id, const string& _owner, int _balance) : id(_id), owner(_owner), balance(_balance) {}
// Transfer tokens to another owner
bool transfer(const string& newOwner, int amount) {
if (amount > balance) {
cout << "Insufficient balance for transfer." << endl;
return false;
}
owner = newOwner;
balance -= amount;
cout << amount << " tokens transferred to " << newOwner << endl;
return true;
}
// Get token balance
int getBalance() const {
return balance;
}
};
// Right rotate function
uint32_t rightRotate(uint32_t value, uint32_t count) {
return (value >> count) | (value << (32 - count));
}
// Function to compute SHA-256 hash of a string
string sha256(const string& input) {
array<uint8_t, 32> hash;
uint8_t buffer[input.size()];
copy(input.begin(), input.end(), buffer);
// Initialize SHA-256 context
uint32_t state[8];
state[0] = 0x6a09e667;
state[1] = 0xbb67ae85;
state[2] = 0x3c6ef372;
state[3] = 0xa54ff53a;
state[4] = 0x510e527f;
state[5] = 0x9b05688c;
state[6] = 0x1f83d9ab;
state[7] = 0x5be0cd19;
// Constants
const uint32_t k[64] = {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
};
uint64_t ml = input.size() * 8; // Message length in bits
// Pre-processing
buffer[input.size()] = 0x80; // Append single '1' bit
for (int i = input.size() + 1; i < 64; ++i) {
buffer[i] = 0x00; // Pad with zeros
}
// Append original message length in bits
for (int i = 56; i < 64; ++i) {
buffer[i] = ml >> ((63 - i) * 8) & 0xff;
}
// Process message in 512-bit chunks
for (int i = 0; i < input.size() + 1; i += 64) {
uint32_t w[64];
// Prepare message schedule
for (int t = 0; t < 16; ++t) {
w[t] = (buffer[i + (t * 4) + 0] << 24) |
(buffer[i + (t * 4) + 1] << 16) |
(buffer[i + (t * 4) + 2] << 8) |
(buffer[i + (t * 4) + 3]);
}
for (int t = 16; t < 64; ++t) {
uint32_t s0 = (rightRotate(w[t - 15], 7) ^ rightRotate(w[t - 15], 18) ^ (w[t - 15] >> 3));
uint32_t s1 = (rightRotate(w[t - 2], 17) ^ rightRotate(w[t - 2], 19) ^ (w[t - 2] >> 10));
w[t] = w[t - 16] + s0 + w[t - 7] + s1;
}
// Initialize working variables
uint32_t a = state[0];
uint32_t b = state[1];
uint32_t c = state[2];
uint32_t d = state[3];
uint32_t e = state[4];
uint32_t f = state[5];
uint32_t g = state[6];
uint32_t h = state[7];
// Compression function main loop
for (int t = 0; t < 64; ++t) {
uint32_t S1 = (rightRotate(e, 6) ^ rightRotate(e, 11) ^ rightRotate(e, 25));
uint32_t ch = ((e & f) ^ ((~e) & g));
uint32_t temp1 = h + S1 + ch + k[t] + w[t];
uint32_t S0 = (rightRotate(a, 2) ^ rightRotate(a, 13) ^ rightRotate(a, 22));
uint32_t maj = ((a & b) ^ (a & c) ^ (b & c));
uint32_t temp2 = S0 + maj;
h = g;
g = f;
f = e;
e = d + temp1;
d = c;
c = b;
b = a;
a = temp1 + temp2;
}
// Update state
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
state[4] += e;
state[5] += f;
state[6] += g;
state[7] += h;
}
// Final hash value
stringstream ss;
for (int i = 0; i < 8; ++i) {
ss << hex << setw(8) << setfill('0') << state[i];
}
return ss.str();
}
// Define a class for managing tokens and products in a supply chain
class SupplyChainManager {
private:
map<string, unique_ptr<Token>> tokens; // Map to store tokens by ID
map<string, unique_ptr<Product>> products; // Map to store products by ID
// Merkle Tree Node
struct MerkleNode {
string hash;
unique_ptr<MerkleNode> left;
unique_ptr<MerkleNode> right;
MerkleNode(const string& _hash) : hash(_hash), left(nullptr), right(nullptr) {}
};
// Utility function to create Merkle Tree
unique_ptr<MerkleNode> createMerkleTree(const vector<string>& hashes) {
if (hashes.empty())
return nullptr;
if (hashes.size() == 1)
return make_unique<MerkleNode>(hashes[0]);
vector<string> nextLevel;
for (size_t i = 0; i < hashes.size(); i += 2) {
string combinedHash = hashes[i];
if (i + 1 < hashes.size())
combinedHash += hashes[i + 1];
string hash = sha256(combinedHash);
nextLevel.push_back(hash);
}
return createMerkleTree(nextLevel);
}
// Utility function to print Merkle Tree
void printMerkleTree(const unique_ptr<MerkleNode>& root, int depth = 0) {
if (!root)
return;
cout << string(depth, '-') << root->hash << endl;
printMerkleTree(root->left, depth + 1);
printMerkleTree(root->right, depth + 1);
}
public:
// Issue a new token
void issueToken(const string& id, const string& owner, int initialBalance) {
unique_ptr<Token> newToken = make_unique<Token>(id, owner, initialBalance);
tokens[id] = move(newToken);
cout << "Token " << id << " issued to " << owner << " with initial balance " << initialBalance << endl;
}
// Issue a new product
void issueProduct(const string& id, const string& description, const string& owner) {
unique_ptr<Product> newProduct = make_unique<Product>(id, description, owner);
products[id] = move(newProduct);
cout << "Product " << id << " issued: " << description << ", owned by " << owner << endl;
}
// Transfer tokens
void transferToken(const string& tokenId, const string& sender, const string& receiver, int amount) {
if (tokens.find(tokenId) == tokens.end()) {
cout << "Token with ID " << tokenId << " not found." << endl;
return;
}
Token& token = *tokens[tokenId];
if (token.owner != sender) {
cout << "Sender does not own the token." << endl;
return;
}
if (!token.transfer(receiver, amount)) {
cout << "Token transfer failed." << endl;
return;
}
}
// Transfer product ownership
void transferProduct(const string& productId, const string& sender, const string& receiver) {
if (products.find(productId) == products.end()) {
cout << "Product with ID " << productId << " not found." << endl;
return;
}
Product& product = *products[productId];
if (product.owner != sender) {
cout << "Sender does not own the product." << endl;
return;
}
product.owner = receiver;
cout << "Product " << productId << " transferred to " << receiver << endl;
}
// Get token balance for a specific owner
int getTokenBalance(const string& tokenId, const string& owner) const {
if (tokens.find(tokenId) == tokens.end()) {
cout << "Token with ID " << tokenId << " not found." << endl;
return -1;
}
const Token& token = *tokens.at(tokenId);
if (token.owner != owner) {
cout << "Owner does not own the token." << endl;
return -1;
}
return token.getBalance();
}
// Build and print Merkle Tree of token transactions
void printTokenTransactionsMerkleTree() {
vector<string> transactionHashes;
for (const auto& pair : tokens) {
const Token& token = *pair.second;
stringstream ss;
ss << token.id << token.owner << token.balance;
string hash = sha256(ss.str());
transactionHashes.push_back(hash);
}
unique_ptr<MerkleNode> root = createMerkleTree(transactionHashes);
cout << "Token Transactions Merkle Tree:" << endl;
printMerkleTree(root);
}
};
int main() {
// Create a supply chain manager instance
SupplyChainManager scm;
// Issue some tokens
scm.issueToken("ABC", "Manufacturer", 1000);
scm.issueToken("MNO", "Retailer", 3000);
scm.issueToken("DEF", "Manufacturer", 2000);
scm.issueToken("PQR", "Retailer", 4000);
scm.issueToken("GHI", "Manufacturer", 1000);
scm.issueToken("STU", "Retailer", 4500);
scm.issueToken("JKL", "Manufacturer", 4280);
scm.issueToken("VWX", "Retailer", 8400);
// Issue some products
scm.issueProduct("P001", "Noradrenaline", "Manufacturer");
scm.issueProduct("P002", "INN", "Regulatory Bodies");
scm.issueProduct("P003", "Lidocaine", "Consumer");
scm.issueProduct("P004", "INN", "Distributors");
scm.issueProduct("P005", "paracetamol", "Distributors");
scm.issueProduct("P006", "Adrenaline", "Regulatory Bodies");
scm.issueProduct("P007", "paracetamol", "Manufacturer");
scm.issueProduct("P008", "paracetamol", "Regulatory Bodies");
scm.issueProduct("P009", "INN", "Distributors");
scm.issueProduct("P010", "paracetamol", "Manufacturer");
scm.issueProduct("P011", "Atropine", "Manufacturer");
// Transfer tokens
scm.transferToken("ABC", "Manufacturer", "Supplier", 300);
scm.transferToken("DEF", "Manufacturer", "Supplier", 800);
scm.transferToken("GHI", "Manufacturer", "Supplier", 700);
scm.transferToken("JKL", "Manufacturer", "Supplier", 550);
scm.transferToken("MNO", "Manufacturer", "Supplier", 650);
scm.transferToken("PQR", "Manufacturer", "Supplier", 400);
// Transfer products
scm.transferProduct("P001", "Manufacturer", "Supplier");
scm.transferProduct("P002", "Regulatory Bodies", "Manufacturer");
scm.transferProduct("P005", "Consumers", "Manufacturer");
scm.transferProduct("P008", "Distributors", "Supplier");
// Print token transactions Merkle Tree
scm.printTokenTransactionsMerkleTree();
return 0;
}