1. 程式人生 > 程式設計 >C++實現區塊鏈的原始碼

C++實現區塊鏈的原始碼

看了上面的演算法,相信大家基本可以猜到,相對於比特幣的限量的性質,對於本演算法來說,難解程度的根本原因即為向量環路的迭代次數。迭代次數越多,則演算法越難解,從而導致解題需要花費更多的時候,再基於這點,在數學上,當解題次數足夠大時,效率會無限小,從而導致瞭解題時間無限長最後導致加密貨幣的發放無限小。
創世區塊建立(部分大媽在前面有實現,而區塊這一部分將會詳細解答)

void Make_First_Block()
{
Getpublickey();
blo.data = circle;
blo.pre_hash = 0;
blo.this_hash = (blo.pre_hash+public_Key) * (a+b);
Block.push_back(blo);
}

由於在區塊鏈中,本區快的數字簽名是基於上一區塊的數字簽名和區塊本身的DATA決定, 所以,在這裡我們採用了上一區塊的數字簽名加上難解的PublicKey乘上長軸和短軸的和實現本區塊的數字簽名的演算法。
新增區塊(噹噹前區塊被算出時,新增新區塊,檢查簽名正確性。)

void Append_Block()
{
pre_blo = blo;
bool flag = true;
auto temp = public_Key;
circle = circle + 1;
Getpublickey();
blo.data = circle;
blo.pre_hash = blo.this_hash;
blo.this_hash = (blo.pre_hash + public_Key) * (a + b);
for(list::iterator itor = Block.begin(); itor != Block.end(); itor++)
{
if ((*itor).this_hash != (*itor).pre_hash + temp * (a + b))
{
flag = false;
break;
}
}
if (flag) { Block.push_back(blo); };
}

這個迭代其實可以不用的,因為我在外部還定義了一個block型別的全域性變數Pre_block和blo。Pre_block儲存了上一個區塊的資訊。而本區塊的資訊則儲存在Blo中。只有當用戶解出當前區塊後,才可以得到新區塊。而data引數,為了方便僅儲存了當前區塊所在的位置。
區塊的計算(用類實現)

class Get_Block :Create_Block {
public:
int diffcult;
int number = 1;
Get_Block():Create_Block(“OK”){
}
void calc()
{
double start = clock();
while (true){
for (unsigned long long z = 1; z < ULLONG_MAX; z++){
for (unsigned long long j = 1; j < 65535; j = j + 1) {
for (unsigned long long i = 1; i < 65535; i = i + 1) {
Cryptography *person = new Cryptography(i,j,z);
person->Getpublickey();
block bloc;
bloc.data = circle;
bloc.pre_hash = pre_blo.this_hash;
bloc.this_hash = (blo.pre_hash + person->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;
}
}
}
}
}
};

完整程式碼:

#include 
#include <stdio.h>
#include <windows.h>
#include 
#include 
#include 
#include 
#include <time.h>
using namespace std;

struct Moving_Point {
unsigned long long x;
unsigned long long y;
};

int circle = 1;

class Martix {
public:
static const int circle_s = 1; //假定向量環路為1;
static const int KEY =Martix::circle_s * 8;
private:
unsigned long long martix_4_2[Martix::KEY / 2][2]; //儲存向量矩陣
unsigned long long martix_8_8[Martix::KEY][Martix::KEY]; //儲存由向量矩陣得到的轉置矩陣
unsigned long long martix_complete[KEY * 2]; //儲存操作完成後的矩陣(一維)

public:
Martix(string a) {};
Martix(int a,int b,int circle)
{
int key = 8;
int cir = circle;
while (cir–)
{
martix_4_2[key / 2 - 4][0] = (-1)*b; martix_4_2[key / 2 - 4][1] = (-1)*a;
martix_4_2[key / 2 - 3][0] = b; martix_4_2[key / 2 - 3][1] = (-1)*a;
martix_4_2[key / 2 - 2][0] = b; martix_4_2[key / 2 - 2][1] = a;
martix_4_2[key / 2 - 1][0] = (-1)*b; martix_4_2[key / 2 - 1][1] = a;
key += 8;
}
}
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;
}
}
}
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];
}
}
}
}
unsigned long long GetPublicKey()
{
unsigned long 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];
}
}
return public_key;
}
};
class Cryptography :Martix
{
public:
/作為私鑰,傳送方儲存內容/
unsigned long long a; //橢圓長軸的半軸長度
unsigned long long b; //橢圓短軸的半軸長度
/作為公鑰,接收方接受公鑰/
unsigned long long public_Key; //通過橢圓矩陣演算法得到的公鑰G
Moving_Point p; //隨機選定的在橢圓上的點

public:
Cryptography(string a) :Martix(“OK”) {};
Cryptography(unsigned long long in_a,unsigned long 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 = Getpublickey();
}
unsigned long long Getpublickey()
{
Get_Public_Key();
return public_Key;
}
Moving_Point GetPoint()
{
Get_Point();
return p;
}
public:
void PrintPrivateKey() {
cout << “#############私鑰:#############” << endl;
cout << “長軸:” << 2this->a << “\t\t”;
cout << “短軸:” << 2this->b << endl;
}
private:
void Get_Point()
{
if (p.x == 0 && p.y == 0)
{
while (!Is_Moving_Point())
{
Get_Moving_Point_P();
}
}
}
void Get_Public_Key()
{
this->Change_New_Martix();
this->Save_Martix();
this->public_Key = this->GetPublicKey();
}
void Get_Moving_Point_P() //得到一個隨機的在橢圓上的點的座標
{
for (int i = 0; i < this->a; i++)
{
for (int j = 0; j < this->b; j++)
{
p.x = i;
p.y = j;
}
}
}
bool Is_Moving_Point() {
if (pow(b,2)*pow(p.y,2) + pow(a,2)*pow(p.x,2) == pow(a,2)*pow(b,2) && p.y <= a && p.x <= b)
return true;
else
return false;
}
};
struct block {
unsigned long long this_hash;
unsigned long long pre_hash;
unsigned long long data;
};

block blo;
block pre_blo = {0,0};
class Create_Block:public Cryptography {
public:
list Block;
public:
Create_Block(string a):Cryptography(“OK”) {};
Create_Block(int x = rand()*2,int y = rand(),int diffcult = 1):Cryptography(x,y,diffcult){
}
void Make_First_Block()
{
Getpublickey();
blo.data = circle;
blo.pre_hash = 0;
blo.this_hash = (blo.pre_hash+public_Key) * (a+b);
Block.push_back(blo);
}
void Append_Block()
{
pre_blo = blo;
bool flag = true;
auto temp = public_Key;
circle = circle + 1;
Getpublickey();
blo.data = circle;
blo.pre_hash = blo.this_hash;
blo.this_hash = (blo.pre_hash + public_Key) * (a + b);
for(list::iterator itor = Block.begin(); itor != Block.end(); itor++)
{
if ((*itor).this_hash != (*itor).pre_hash + temp * (a + b))
{
flag = false;
break;
}
}
if (flag) { Block.push_back(blo); };
}
};

class Get_Block :Create_Block {
public:
int diffcult;
int number = 1;
Get_Block():Create_Block(“OK”){
}
void calc()
{
double start = clock();
while (true){
for (unsigned long long z = 1; z < ULLONG_MAX; z++){
for (unsigned long long j = 1; j < 65535; j = j + 1) {
for (unsigned long long i = 1; i < 65535; i = i + 1) {
Cryptography *person = new Cryptography(i,z);
person->Getpublickey();
block bloc;
bloc.data = circle;
bloc.pre_hash = pre_blo.this_hash;
bloc.this_hash = (blo.pre_hash + person->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 = new Create_Block();
one->Make_First_Block();
Get_Block* two = new Get_Block();
two->calc();
return 0;
}

不得不說第一個區塊的挖掘永遠是最快的。第二個區塊確實要等好久才可以得出。以上即為C/C++實現區塊鏈的全部原始碼。僅用於學習交流,不得用於商業用途,轉載必究。

作者:程式小黑
來源:CSDN
原文:https://blog.csdn.net/qq_27180763/article/details/82588305
版權宣告:本文為博主原創文章,轉載請附上博文連結!

到此這篇關於C++實現區塊鏈的原始碼的文章就介紹到這了,更多相關c++區塊鏈內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!