C++实现区块链的源码

 更新时间:2021年01月20日 09:48:19   作者:程序小黑  
这篇文章主要介绍了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,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,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;
}
}
}
}
}
};

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++区块链内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C语言中求余运算符的使用解读

    C语言中求余运算符的使用解读

    这篇文章主要介绍了C语言中求余运算符的使用,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-02-02
  • 使用C语言实现三子棋游戏

    使用C语言实现三子棋游戏

    这篇文章主要为大家详细介绍了使用C语言实现三子棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-07-07
  • c++虚函数与虚函数表原理

    c++虚函数与虚函数表原理

    这篇文章主要介绍了c++虚函数与虚函数表原理,用virtual 修饰的成员函数叫虚函数,下面围绕c++虚函数与虚函数得相关资料展开内容,需要的朋友可以参考一下
    2021-12-12
  • C++实现扫雷游戏(控制台不闪屏版)

    C++实现扫雷游戏(控制台不闪屏版)

    这篇文章主要为大家详细介绍了C++实现扫雷游戏,控制台不闪屏版,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-03-03
  • C语言由浅入深讲解文件的操作上篇

    C语言由浅入深讲解文件的操作上篇

    C语言具有操作文件的能力,比如打开文件、读取和追加数据、插入和删除数据、关闭文件、删除文件等。与其他编程语言相比,C语言文件操作的接口相当简单和易学
    2022-04-04
  • C++写Linux框架示例解析

    C++写Linux框架示例解析

    这篇文章主要为大家介绍了C++实现Linux框架示例代码详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-12-12
  • C++实现十六进制字符串转换成int整形值的示例

    C++实现十六进制字符串转换成int整形值的示例

    今天小编就为大家分享一篇关于C++实现十六进制字符串转换成int整形值的示例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • C++重载输入和输出运算符详解

    C++重载输入和输出运算符详解

    在C++中,标准库本身已经对左移运算符<<和右移运算符>>分别进行了重载,使其能够用于不同数据的输入输出,本节以前面的 complex 类为例来演示输入输出运算符的重载,需要的朋友可以参考下
    2023-09-09
  • C++ LeetCode1780判断数字是否可以表示成三的幂的和

    C++ LeetCode1780判断数字是否可以表示成三的幂的和

    这篇文章主要为大家介绍了C++ LeetCode1780判断数字是否可以表示成三的幂的和题解示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-12-12
  • C++中构造函数的参数缺省的详解

    C++中构造函数的参数缺省的详解

    这篇文章主要介绍了C++中构造函数的参数缺省的详解的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
    2017-10-10

最新评论