CC++实现区块链(中)之算法实现

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;
}
};
2、加密算法实现
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 << “\t\t”;
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;
}
};
不过我觉得以上代码中的POINT(代表动点M)可能目前来看没有什么太大的意义,但是在后面身份认证的时候可能会用到,所以先留着。
区块结构定义
struct block {
unsigned long long this_hash;
unsigned long long pre_hash;
unsigned long long data;
};
作者:程序小黑
来源:CSDN
原文:https://blog.csdn.net/qq_27180763/article/details/82588194
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/weixin_43822361/article/details/84716844