Skip to content

Commit

Permalink
add bytes operation
Browse files Browse the repository at this point in the history
  • Loading branch information
ww-rm committed Jun 2, 2024
1 parent bc191d0 commit 1f71aeb
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 6 deletions.
20 changes: 16 additions & 4 deletions include/gmalglib/sm9curve.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@

#define SM9_PARAMS_LENGTH 32

#define SM9_PCMODE_RAW 0
#define SM9_PCMODE_COMPRESS 1
#define SM9_PCMODE_MIX 2
#define SM9_POINT1BYTES_INF_LENGTH 1
#define SM9_POINT1BYTES_HALF_LENGTH (1 + SM9_PARAMS_LENGTH)
#define SM9_POINT1BYTES_FULL_LENGTH (SM9_POINT1BYTES_HALF_LENGTH + SM9_PARAMS_LENGTH)
#define SM9_POINT1BYTES_MAX_LENGTH SM9_POINT1BYTES_FULL_LENGTH
#define SM9_GET_POINT1BYTES_LENGTH(pc_mode) (((pc_mode) == SM9_PCMODE_COMPRESS) ? SM9_POINT1BYTES_HALF_LENGTH : SM9_POINT1BYTES_FULL_LENGTH)

#define SM9CURVE_ERR_NOTONCURVE -1
#define SM9CURVE_ERR_INVALIDPC -2

typedef UInt256 SM9FP1;
typedef SM9FP1 SM9FP1Mont;

Expand Down Expand Up @@ -59,10 +71,10 @@ extern const UInt256* const SM9_PARAMS_N;
void SM9JacobPoint1Mont_ToPoint(const SM9JacobPoint1Mont* X, SM9Point1* Y);
void SM9JacobPoint1Mont_FromPoint(const SM9Point1* X, SM9JacobPoint1Mont* Y);

//int SM9Point1_FromBytes(const uint8_t* bytes, uint64_t bytes_len, SM9Point1* X);
//uint64_t SM9Point1_ToBytes(const SM9Point1* X, int pc_mode, uint8_t* bytes);
//uint64_t SM9JacobPoint1Mont_ToBytes(const SM9JacobPoint1Mont* X, int pc_mode, uint8_t* bytes);
//int SM9JacobPoint1Mont_FromBytes(const uint8_t* bytes, uint64_t bytes_len, SM9JacobPoint1Mont* X);
uint64_t SM9Point1_ToBytes(const SM9Point1* X, int pc_mode, uint8_t* bytes);
int SM9Point1_FromBytes(const uint8_t* bytes, uint64_t bytes_len, SM9Point1* X);
uint64_t SM9JacobPoint1Mont_ToBytes(const SM9JacobPoint1Mont* X, int pc_mode, uint8_t* bytes);
int SM9JacobPoint1Mont_FromBytes(const uint8_t* bytes, uint64_t bytes_len, SM9JacobPoint1Mont* X);

int SM9JacobPoint1Mont_IsInf(const SM9JacobPoint1Mont* X);
void SM9JacobPoint1Mont_SetInf(SM9JacobPoint1Mont* X);
Expand Down
118 changes: 116 additions & 2 deletions src/gmalglib/core/sm9curve.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#define SCALARMUL_WSIZE 4
#define SCALARMUL_TSIZE (1 << (SCALARMUL_WSIZE - 1))

// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Constants >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

// 0xB6400000_02A3A6F1_D603AB4F_F58EC745_21F2934B_1A7AEEDB_E56F9B27_E351457D
static const UInt256 _CONSTS_P = { .u32 = { 0xE351457D, 0xE56F9B27, 0x1A7AEEDB, 0x21F2934B, 0xF58EC745, 0xD603AB4F, 0x02A3A6F1, 0xB6400000 } };
static const UInt256* const CONSTS_P = &_CONSTS_P;
Expand Down Expand Up @@ -53,7 +55,6 @@ static const SM9FP1Mont* const CONSTS_FP1_MONT_FOUR = &_CONSTS_FP1_MONT_FOUR;
static const SM9FP1Mont _CONSTS_FP1_MONT_P_MINUS_ONE = { .u32 = { 0xC6A28AFA, 0xCADF364F, 0x34F5DDB7, 0x43E52696, 0xEB1D8E8A, 0xAC07569F, 0x05474DE3, 0x6C800000 } };
static const SM9FP1Mont* const CONSTS_FP1_MONT_P_MINUS_ONE = &_CONSTS_FP1_MONT_P_MINUS_ONE;


// p - 2, used for Inv
static const UInt256 _CONSTS_P_MINUS_TWO = { .u32 = { 0xE351457B, 0xE56F9B27, 0x1A7AEEDB, 0x21F2934B, 0xF58EC745, 0xD603AB4F, 0x02A3A6F1, 0xB6400000 } };
static const UInt256* const CONSTS_P_MINUS_TWO = &_CONSTS_P_MINUS_TWO;
Expand Down Expand Up @@ -87,6 +88,8 @@ static const SM9JacobPoint1Mont _CONSTS_JACOB_G1 = {
};
static const SM9JacobPoint1Mont* const CONSTS_JACOB_G1 = &_CONSTS_JACOB_G1;

// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Constants <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> FP1 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

static
Expand Down Expand Up @@ -419,7 +422,118 @@ int SM9Point1_IsOnCurve(const SM9Point1* X)
return UInt256_Cmp(&left, &right) == 0;
}

// TODO: From and To Bytes
uint64_t SM9Point1_ToBytes(const SM9Point1* X, int pc_mode, uint8_t* bytes)
{
if (X->is_inf)
{
bytes[0] = 0x00;
return 1;
}

if (pc_mode == SM9_PCMODE_COMPRESS)
{
if (X->y.u8[0] & 0x1)
bytes[0] = 0x03;
else
bytes[0] = 0x02;
UInt256_ToBytes(&X->x, bytes + 1);
return 33;
}
else if (pc_mode == SM9_PCMODE_MIX)
{
if (X->y.u8[0] & 0x1)
bytes[0] = 0x07;
else
bytes[0] = 0x06;
UInt256_ToBytes(&X->x, bytes + 1);
UInt256_ToBytes(&X->y, bytes + 33);
return 65;
}
else
{
bytes[0] = 0x04;
UInt256_ToBytes(&X->x, bytes + 1);
UInt256_ToBytes(&X->y, bytes + 33);
return 65;
}

return 0;
}

int SM9Point1_FromBytes(const uint8_t* bytes, uint64_t bytes_len, SM9Point1* X)
{
uint8_t pc = bytes[0];
int ylsb = 0;

if (pc == 0x00)
{
if (bytes_len != 1)
return SM9CURVE_ERR_INVALIDPC;

X->is_inf = 1;
}
else if (pc == 0x04 || pc == 0x06 || pc == 0x07)
{
if (bytes_len != SM9_POINT1BYTES_FULL_LENGTH)
return SM9CURVE_ERR_INVALIDPC;

UInt256_FromBytes(bytes + 1, &X->x);
UInt256_FromBytes(bytes + 33, &X->y);
X->is_inf = 0;
if (!SM9Point1_IsOnCurve(X))
return SM9CURVE_ERR_NOTONCURVE;
}
else if (pc == 0x02 || pc == 0x03)
{
if (bytes_len != SM9_POINT1BYTES_HALF_LENGTH)
return SM9CURVE_ERR_INVALIDPC;

UInt256_FromBytes(bytes + 1, &X->x);
if (UInt256_Cmp(&X->x, CONSTS_P) >= 0)
return SM9CURVE_ERR_NOTONCURVE;

SM9FP1_ToMont(&X->x, &X->x);

// compute y, x^3 + b
SM9FP1_MontMul(&X->x, &X->x, &X->y);
SM9FP1_MontMul(&X->x, &X->y, &X->y);
SM9FP1_MontAdd(&X->y, CONSTS_FP1_MONT_B, &X->y);
if (!SM9FP1_MontHasSqrt(&X->y, &X->y))
return SM9CURVE_ERR_NOTONCURVE;

SM9FP1_FromMont(&X->x, &X->x);
SM9FP1_FromMont(&X->y, &X->y);
ylsb = X->y.u8[0] & 0x1;
if ((pc == 0x02 && ylsb) || (pc == 0x03 && !ylsb))
{
SM9FP1_Neg(&X->y, &X->y);
}
}
else
{
return SM9CURVE_ERR_INVALIDPC;
}

return 0;
}

uint64_t SM9JacobPoint1Mont_ToBytes(const SM9JacobPoint1Mont* X, int pc_mode, uint8_t* bytes)
{
SM9Point1 pt = { 0 };
SM9JacobPoint1Mont_ToPoint(X, &pt);
return SM9Point1_ToBytes(&pt, pc_mode, bytes);
}

int SM9JacobPoint1Mont_FromBytes(const uint8_t* bytes, uint64_t bytes_len, SM9JacobPoint1Mont* X)
{
SM9Point1 pt = { 0 };
int ret = SM9Point1_FromBytes(bytes, bytes_len, &pt);
if (ret != 0)
return ret;

SM9JacobPoint1Mont_FromPoint(&pt, X);
return 0;
}

static
void _SM9JacobPoint1Mont_Dbl(const SM9JacobPoint1Mont* X, SM9JacobPoint1Mont* Y)
Expand Down
23 changes: 23 additions & 0 deletions src/test/sm9curvetest.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,27 @@ void test_curve1()
printf("SM9 Curve 1 Test OK.\n");
}

void test_convert1()
{
UInt256 x = { .u32 = { 0x80A892FF, 0xABC66E3C, 0xA7137D39, 0x720B17EC, 0x3F32DA5C, 0x4E0A6600, 0xA51F3AB8, 0x787ED7B8 } };
UInt256 y = { .u32 = { 0x4C57A7B1, 0x3F220F64, 0x9A8C49DC, 0x20287127, 0x1354900B, 0xB9FF85A3, 0x91E5ADC4, 0x769DE617 } };
uint8_t pk[SM9_POINT1BYTES_MAX_LENGTH] = { 0x04 };
UInt256_ToBytes(&x, pk + 1);
UInt256_ToBytes(&y, pk + 33);

SM9JacobPoint1Mont PK = { 0 };
SM9Point1 PKK = { 0 };
assert(SM9JacobPoint1Mont_FromBytes(pk, 65, &PK) == 0);

pk[0] = 0x03;
assert(SM9JacobPoint1Mont_FromBytes(pk, 33, &PK) == 0);

SM9JacobPoint1Mont_ToPoint(&PK, &PKK);
assert(UInt256_Cmp(&y, &PKK.y) == 0);

printf("SM9 Convert 1 Test OK.\n");
}

void make_table()
{
for (int i = 0; i < 32; i++) {
Expand Down Expand Up @@ -90,6 +111,8 @@ int main()
{
test_curve1();

test_convert1();

//make_table();

return 0;
Expand Down

0 comments on commit 1f71aeb

Please sign in to comment.