#C/C++区块链实现代码优化
###你有多少电力,算完这道哈希,
###比特币的共识太给力,随处可藏,无问东西。
###因你的梦太美,我已不想再退,
###算法来的信任太纯粹,好想带你全世界都走一回
###分布式的记帐,多简单多抽象,
###可中本聪他呀,却是下落不详。
###谁接青黄,少年郎,造以太坊。
###ICO太疯狂,乱相惹怒央行,
###那庄家太嚣张,这韭菜在摇晃,
###先熄灭妄想,才能成长。
###几千几万通证,几言几语新政,
###谁在走进 悬崖边的乱阵,
###一步迈错 你交出了谁的灵魂。
###多明显的泡沫,明显得好寂寞,
###抽象掩盖欲望,却又欲盖弥彰,
###谁在嚣张,嚣张到失去形状。
###监管一记耳光,逼谁露出真相,
###这梦想太张扬,这现实太慌张,
###先落地生长,才好推广。
###既然已在路上,慢一点又何妨,
###就像硅谷那样,代码多写几行,
###这征途太远,要勇敢且倔强。
#include <iostream>
#include <stdio.h>
#include <windows.h>
#include <string>
#include <cmath>
#include <climits>
#include <list>
#include <time.h>
using namespace std;
struct Moving_Point {
long x;
long y;
};
int circle = 1;
class Martix {
public:
int circle_s; //假定向量环路为1;
int KEY;
long _publickey;
private:
long **martix_4_2 = new long*[KEY / 2]; //存储向量矩阵
long **martix_8_8 = new long*[KEY]; //存储由向量矩阵得到的转置矩阵
long *martix_complete = new long[KEY * 2]; //存储操作完成后的矩阵(一维)
public:
Martix(string a) {};
Martix(int a, int b, int circle):KEY(circle*8),circle_s(circle)
{
int key = 8;
int cir = circle_s;
for (int i = 0; i < KEY; i++) {
martix_8_8[i] = new long[KEY];
}
while (cir--)
{
martix_4_2[key / 2 - 4] = new long[2];
martix_4_2[key / 2 - 4][0] = (-1)*b; martix_4_2[key / 2 - 4][1] = (-1)*a;
martix_4_2[key / 2 - 3] = new long[2];
martix_4_2[key / 2 - 3][0] = b; martix_4_2[key / 2 - 3][1] = (-1)*a;
martix_4_2[key / 2 - 2] = new long[2];
martix_4_2[key / 2 - 2][0] = b; martix_4_2[key / 2 - 2][1] = a;
martix_4_2[key / 2 - 1] = new long[2];
martix_4_2[key / 2 - 1][0] = (-1)*b; martix_4_2[key / 2 - 1][1] = a;
key += 8;
}
Change_New_Martix();
Save_Martix();
_publickey = GetPublicKey();
}
void Change_New_Martix() {
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
martix_8_8[i][j] = 0;
}
}
for (int j = 2; j < KEY / 2 + 2; j++) {
martix_8_8[0][j] = martix_4_2[j - 2][0] * (-1);
martix_8_8[1][j] = martix_4_2[j - 2][1] * (-1);
}
for (int i = 2; i < KEY / 2 + 2; i++) {
martix_8_8[i][0] = martix_4_2[i - 2][0] * (-1);
martix_8_8[i][1] = martix_4_2[i - 2][1] * (-1);
}
for (int i = 2; i < KEY / 2 + 2; i++)
{
for (int j = 2; j < KEY / 2 + 2; j++)
{
martix_8_8[i][j] = 0;
}
}
for (int i = 0; i < KEY / 2; i++) {
delete[] martix_4_2[i];
}
delete []martix_4_2;
}
public:
void Save_Martix()
{
int key = 0;
for (int i = 0; i < KEY / 2 + 2; i++)
{
for (int j = 0; j < KEY / 2 + 2; j++)
{
if (martix_8_8[i][j] != 0)
{
martix_complete[key++] = martix_8_8[i][j];
}
}
}
for (int i = 0; i < KEY; i++) {
delete[] martix_8_8[i];
}
delete[]martix_8_8;
}
long GetPublicKey()
{
long public_key = martix_complete[0];
for (int i = 1; i < KEY * 2; i++)
{
if (i % 2 == 0)
{
public_key = public_key + martix_complete[i];
}
else {
public_key = public_key * martix_complete[i];
}
}
delete []martix_complete;
return public_key;
}
};
class Cryptography :public Martix
{
public:
/*作为私钥,发送方保存内容*/
long a; //椭圆长轴的半轴长度
long b; //椭圆短轴的半轴长度
/*作为公钥,接收方接受公钥*/
long public_Key; //通过椭圆矩阵算法得到的公钥G
Moving_Point p; //随机选定的在椭圆上的点
public:
Cryptography(string a) :Martix("OK") {};
Cryptography( long in_a, long in_b, int diffcult) :Martix(in_a, in_b, diffcult)
{
this->a = in_a;
this->b = in_b;
p.x = 0;
p.y = 0;
public_Key = _publickey;
}
};
struct block {
long this_hash;
long pre_hash;
long data;
};
block blo;
block pre_blo = { 0,0,0 };
class Create_Block :public Cryptography {
public:
list<block> Block;
long public_key_key;
int numerous = 1;
public:
Create_Block(string a) :Cryptography("OK") {};
Create_Block(int x = rand()%2048, int y = rand()%1024, int diffcult = rand()%1024):Cryptography(rand()%2048,rand()%1024,rand()%1024) {
public_key_key = public_Key;
}
void Make_First_Block()
{
//Cryptography one(2,1,1);
Cryptography one(rand() % 2048, rand() % 1024, rand() % 1024);
public_key_key = one.public_Key;
blo.data = numerous;
blo.pre_hash = 0;
blo.this_hash = (blo.pre_hash + public_key_key) * (a + b);
Block.push_back(blo);
}
void Append_Block()
{
numerous++;
pre_blo = blo;
bool flag = true;
auto temp = public_Key;
//int cir = circle_s + 1;
int a = rand() % 2048;
int b = rand() % 1024;
int c = rand() % 1024;
Cryptography one(a,b,c);
public_key_key = one.public_Key;
blo.data = numerous;
blo.pre_hash = blo.this_hash;
blo.this_hash = (blo.pre_hash + public_key_key) * (a + b);
Block.push_back(blo);
}
};
class Get_Block :Create_Block{
public:
int number = 1;
long __public_key;
Get_Block(int num=1):Create_Block("null") {
number = num;
}
void calc()
{
double start = clock();
while (true) {
for (long long z = 1; z < 1024; z++) {
for (long long j = 1; j < 1024; j = j + 1) {
for (long long i = 1; i < 2048; i = i + 1) {
Cryptography* person = new Cryptography(i, j, z);
__public_key = person->public_Key;
block bloc;
bloc.data = number;
bloc.pre_hash = pre_blo.this_hash;
bloc.this_hash = (blo.pre_hash + __public_key) * (i + j);
if (blo.data == bloc.data &&blo.pre_hash == bloc.pre_hash && blo.this_hash == bloc.this_hash)
{
double end = clock();
cout << "历时" << end - start << "毫秒获得的第" << number++ << "个区块信息为:" << endl;
cout << "data:" << bloc.data << endl;
cout << "this_hash:" << bloc.this_hash << endl;
cout << "pre_hash:" << bloc.pre_hash << endl << "============================" << endl << endl;
this->Append_Block();
start = clock();
}
delete []person;
}
}
}
}
}
};
int main()
{
Create_Block one;
one.Make_First_Block();
int flag = 10;
Get_Block two(1);
two.calc();
system("pause");
return 0;
}