C/C++实现区块链(下)之区块链实现(原创代码,转载必究)

资讯 2024-07-09 阅读:157 评论:0
美化布局示例

欧易(OKX)最新版本

【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   全球官网 大陆官网

币安(Binance)最新版本

币安交易所app【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   官网地址

火币HTX最新版本

火币老牌交易所【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   官网地址
#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 {
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 << "长轴:" << 2*this->a << " ";
cout << "短轴:" << 2*this->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> 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<block>::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;
}
美化布局示例

欧易(OKX)最新版本

【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   全球官网 大陆官网

币安(Binance)最新版本

币安交易所app【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   官网地址

火币HTX最新版本

火币老牌交易所【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   官网地址
文字格式和图片示例

注册有任何问题请添加 微信:MVIP619 拉你进入群

弹窗与图片大小一致 文章转载注明

分享:

扫一扫在手机阅读、分享本文

发表评论
平台列表
美化布局示例

欧易(OKX)

  全球官网 大陆官网

币安(Binance)

  官网

火币(HTX)

  官网

Gate.io

  官网

Bitget

  官网

deepcoin

  官网
热门文章
  • 孟洪涛谈威科夫交易法

    孟洪涛谈威科夫交易法
     大咖看市 | 判断趋势的工具(一)  原创2016-04-30孟洪涛期货日报 在讨论判断工具之前,我们先说下判断趋势。趋势包括以下几个阶段:趋势的开始,趋势在运行中,以及趋势的结束。我们判断趋势就是能够找出当前市场处于趋势的哪个阶段,以便调整交易。趋势的不同阶段伴随着交易者不同的行为,起始阶段是进场时机,结束阶段是出场时机,同时也是准备反转的进场时机。但是在交易中遇到的最头疼的问题是以上几个趋势阶段并不会白纸黑字表现出来。 ...
  • 0.00015693个比特币等于多少人民币/美金

    0.00015693个比特币等于多少人民币/美金
    0.000 15693比特币等于多少人民币?根据比特币对人民币的最新汇率,0.000 15693比特币等于10.6 1678529美元/76.86554996人民币。比特币(BTC)【比特币价格翻倍】美元(USDT)人民币(CNY)0.000/克洛克-0/5693【数字货币矿机】10.6 167852976.8655254996比特币对人民币的最新汇率为:489,807.72 CNY(1比特币= 489,807.72人民币)(1美元=7.24人民币)(0.00015693 U...
  • 0.00003374个比特币等于多少人民币/美金

    0.00003374个比特币等于多少人民币/美金
    0.00003374比特币等于多少人民币?根据比特币对人民币的最新汇率,0.00003374比特币等于2.2826 1222美元/16.5261124728人民币。比特币(BTC)美元(USDT)人民币(CNY)0.00003374克洛克-0/22216.5261124728比特币对人民币的最新汇率为:489807.72 CNY(1比特币=489807.72人民币)(1美元=7.24人民币)(0.00003374USDT=0.0002442776 CNY)。汇率更新于2024...
  • 0.00006694个比特币等于多少人民币/美金

    0.00006694个比特币等于多少人民币/美金
    0.00006694比特币等于多少人民币?根据比特币对人民币的最新汇率,0.00006694比特币等于4.53424784美元/32.5436 16人民币。比特币(BTC)美元(USDT)人民币(CNY)0.000066944.53424784【比特币密码】32.82795436 16比特币对人民币的最新汇率为:490408.64 CNY(1比特币=490408.64人民币)(1美元=7.24人民币)(0.00006694USDT=0.0004846456 CNY)汇率更新时...
  • ??今日BTC和ETH行情分析以及对BICO的看法

    ??今日BTC和ETH行情分析以及对BICO的看法
    ? 如果你刚认识我那么此刻开始你的幸福? 幸运之路正式开启!? ? 历史记录皆可追溯,往期的记录依然可查,山水相逢,皆是缘!?以后也会经常分享一些看好现货给大家! ? 本周热点 ? ? ? ? ? TON 启动公...
标签列表