forked from LS-King/SM2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
2,787 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include "miracl.h" | ||
#include "BigUtil.h" | ||
|
||
//大数异或 | ||
big Xor2(big x, big y) { | ||
big result = mirvar(0); | ||
unsigned char xString[1000]; | ||
unsigned char yString[1000]; | ||
//大数转换为二进制比特串 | ||
int lengthX = big_to_bytes(0, x, xString, FALSE); | ||
int lengthY = big_to_bytes(0, y, yString, FALSE); | ||
|
||
if (lengthX < lengthY) { | ||
for (int i = 0; i < lengthX; i++) { | ||
yString[lengthY - 1 - i] ^= xString[lengthX - 1 - i]; | ||
} | ||
//二进制比特串转换为大数 | ||
bytes_to_big(lengthY, yString, result); | ||
} | ||
else { | ||
for (int i = 0; i < lengthY; i++) { | ||
xString[lengthX - 1 - i] ^= yString[lengthY - 1 - i]; | ||
} | ||
//二进制比特串转换为大数 | ||
bytes_to_big(lengthX, xString, result); | ||
} | ||
return result; | ||
} | ||
|
||
//大数加法 | ||
big Add2(big x, big y) { | ||
big result = mirvar(0); | ||
add(x, y, result); | ||
return result; | ||
} | ||
|
||
//大数减法 | ||
big Sub2(big x, big y) { | ||
big result = mirvar(0); | ||
subtract(x, y, result); | ||
return result; | ||
} | ||
|
||
//大数乘法 | ||
big Multiply2(big x, big y) { | ||
big result = mirvar(0); | ||
multiply(x, y, result); | ||
return result; | ||
} | ||
|
||
//大数除法 | ||
big Divide2(big x, big y) { | ||
big x1 = mirvar(0); | ||
big y1 = mirvar(0); | ||
big z1 = mirvar(0); | ||
copy(x, x1); | ||
copy(y, y1); | ||
divide(x1, y1, z1); | ||
mirkill(x1); | ||
mirkill(y1); | ||
return z1; | ||
} | ||
|
||
//大数取余 | ||
big Mod2(big x, big y) { | ||
big x1 = mirvar(0); | ||
big y1 = mirvar(0); | ||
big z1 = mirvar(0); | ||
copy(x, x1); | ||
copy(y, y1); | ||
powmod(x1, mirvar(1), y1, z1); | ||
mirkill(x1); | ||
mirkill(y1); | ||
return z1; | ||
} | ||
|
||
//大数乘方 | ||
big Pow2(big x, int y) { | ||
big x1 = mirvar(0); | ||
big y1 = mirvar(0); | ||
copy(x, x1); | ||
for (int i = 0; i < y; i++) | ||
{ | ||
y1 = Multiply2(x1, x1); | ||
} | ||
mirkill(x1); | ||
return y1; | ||
} | ||
|
||
//大数转换为十六进制串 | ||
char* BigToHexChars2(big x) { | ||
mip->IOBASE = 16; | ||
char* str = (char*)malloc(sizeof(char) * maxLen); | ||
cotstr(x, str); | ||
return str; | ||
} | ||
|
||
//十六进制数串转换为大数 | ||
big HexCharsToBig(char* str) { | ||
mip->IOBASE = 16; | ||
big result = mirvar(0); | ||
cinstr(result, str); | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#ifndef BIGUTIL_H | ||
#define BIGUTIL_H | ||
|
||
#include "miracl.h" | ||
|
||
extern miracl* mip; | ||
extern int maxLen; | ||
|
||
//big类型基本运算封装 | ||
big Xor2(big x, big y); | ||
big Add2(big x, big y); | ||
big Sub2(big x, big y); | ||
big Multiply2(big x, big y); | ||
big Divide2(big x, big y); | ||
big Mod2(big x, big y); | ||
big Pow2(big x, int y); // x^y | ||
char* BigToHexChars2(big x); //大数转十六进制串 | ||
big HexCharsToBig(char* str); //十六进制串转大数 | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include "ConstValue.h" | ||
|
||
char* p = "8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3"; | ||
char* a = "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498"; | ||
char* b = "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A"; | ||
char* n = "8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7"; | ||
char* Gx = "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D"; | ||
char* Gy = "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#ifndef CONSTVALUE_H | ||
#define CONSTVALUE_H | ||
|
||
extern char* p; //大素数p | ||
extern char* a; //椭圆曲线参数a | ||
extern char* b; //椭圆曲线参数b | ||
extern char* n; //循环群的阶n | ||
extern char* Gx;//基点G横坐标 | ||
extern char* Gy;//基点G纵坐标 | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include "miracl.h" | ||
|
||
//新建一个椭圆曲线上的点 | ||
epoint* NewPoint(big x, big y) { | ||
epoint* result = epoint_init(); | ||
epoint_set(x, y, 0, result); | ||
return result; | ||
} | ||
|
||
//椭圆曲线上的点相加 | ||
epoint* AddEpoint(epoint* a, epoint* b) { | ||
epoint* result = epoint_init(); | ||
epoint_copy(b, result); | ||
ecurve_add(a, result); | ||
return result; | ||
} | ||
|
||
//椭圆曲线上的点多倍点运算 | ||
epoint* MultiplyEpoint(big a, epoint* b) { | ||
epoint* result = epoint_init(); | ||
ecurve_mult(a, b, result); | ||
return result; | ||
} | ||
|
||
//获得椭圆曲线上点的横坐标 | ||
big PointX(epoint* point) { | ||
big x = mirvar(0); | ||
big y = mirvar(0); | ||
epoint_get(point, x, y); | ||
mirkill(y); | ||
return x; | ||
} | ||
|
||
//获得椭圆曲线上点的纵坐标 | ||
big PointY(epoint* point) { | ||
big x = mirvar(0); | ||
big y = mirvar(0); | ||
epoint_get(point, x, y); | ||
mirkill(x); | ||
return y; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#ifndef ECURVEUTIL_H | ||
#define ECURVEUTIL_H | ||
|
||
#include "miracl.h" | ||
|
||
extern miracl* mip; | ||
extern int maxLen; | ||
|
||
epoint* NewPoint(big x, big y); //新建一个椭圆曲线上的点 | ||
epoint* AddEpoint(epoint*, epoint*); //椭圆曲线上的点相加 | ||
epoint* MultiplyEpoint(big, epoint*); //椭圆曲线上的点多倍点运算 | ||
big PointX(epoint*); //获得椭圆曲线上点的横坐标 | ||
big PointY(epoint*); //获得椭圆曲线上点的纵坐标 | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include<stdio.h> | ||
#include<stdlib.h> | ||
#include "MyString.h" | ||
|
||
//将字符串转换为16进制数串 | ||
char* ConvertStringAsHex(MyString* s) { | ||
char* result = (char*)malloc(sizeof(char) * (s->size * 2 + 1)); | ||
for (int i = 0; i < s->size; i++) { | ||
sprintf(&result[i * 2], "%02X", s->data[i]); | ||
} | ||
result[s->size * 2] = '\0'; | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#ifndef MYSTRING_H | ||
#define MYSTRING_H | ||
|
||
typedef struct MyString | ||
{ | ||
unsigned char* data; | ||
int size; | ||
}MyString; | ||
|
||
char* ConvertStringAsHex(MyString*); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 16 | ||
VisualStudioVersion = 16.0.30804.86 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SM2", "SM2.vcxproj", "{4DA8191E-6DE3-4895-B75E-F7B385FCE437}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|x64 = Debug|x64 | ||
Debug|x86 = Debug|x86 | ||
Release|x64 = Release|x64 | ||
Release|x86 = Release|x86 | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{4DA8191E-6DE3-4895-B75E-F7B385FCE437}.Debug|x64.ActiveCfg = Debug|x64 | ||
{4DA8191E-6DE3-4895-B75E-F7B385FCE437}.Debug|x64.Build.0 = Debug|x64 | ||
{4DA8191E-6DE3-4895-B75E-F7B385FCE437}.Debug|x86.ActiveCfg = Debug|Win32 | ||
{4DA8191E-6DE3-4895-B75E-F7B385FCE437}.Debug|x86.Build.0 = Debug|Win32 | ||
{4DA8191E-6DE3-4895-B75E-F7B385FCE437}.Release|x64.ActiveCfg = Release|x64 | ||
{4DA8191E-6DE3-4895-B75E-F7B385FCE437}.Release|x64.Build.0 = Release|x64 | ||
{4DA8191E-6DE3-4895-B75E-F7B385FCE437}.Release|x86.ActiveCfg = Release|Win32 | ||
{4DA8191E-6DE3-4895-B75E-F7B385FCE437}.Release|x86.Build.0 = Release|Win32 | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {76EB429A-3D96-4A62-A2CF-81D3F3C31436} | ||
EndGlobalSection | ||
EndGlobal |
Oops, something went wrong.