Remove some modn functions

This commit is contained in:
Zhi Guan
2024-04-17 20:55:20 +08:00
parent 3a6bfbe03f
commit 2a5dc640bd
4 changed files with 34 additions and 30 deletions

View File

@@ -23,30 +23,28 @@ extern "C" {
typedef uint64_t sm2_z256_t[4];
int sm2_z256_rand_range(uint64_t r[4], const uint64_t range[4]);
int sm2_z256_rand_range(uint64_t r[4], const uint64_t range[4]);
void sm2_z256_copy(uint64_t r[4], const uint64_t a[4]);
void sm2_z256_copy_conditional(uint64_t dst[4], const uint64_t src[4], uint64_t move);
void sm2_z256_from_bytes(uint64_t r[4], const uint8_t in[32]);
void sm2_z256_to_bytes(const uint64_t a[4], uint8_t out[32]);
int sm2_z256_cmp(const uint64_t a[4], const uint64_t b[4]);
int sm2_z256_cmp(const uint64_t a[4], const uint64_t b[4]);
uint64_t sm2_z256_is_zero(const uint64_t a[4]);
uint64_t sm2_z256_equ(const uint64_t a[4], const uint64_t b[4]);
void sm2_z256_rshift(uint64_t r[4], const uint64_t a[4], unsigned int nbits);
uint64_t sm2_z256_add(uint64_t r[4], const uint64_t a[4], const uint64_t b[4]);
uint64_t sm2_z256_sub(uint64_t r[4], const uint64_t a[4], const uint64_t b[4]);
void sm2_z256_mul(uint64_t r[8], const uint64_t a[4], const uint64_t b[4]);
uint64_t sm2_z512_add(uint64_t r[8], const uint64_t a[8], const uint64_t b[8]);
int sm2_z256_get_booth(const uint64_t a[4], unsigned int window_size, int i);
int sm2_z256_get_booth(const uint64_t a[4], unsigned int window_size, int i);
void sm2_z256_from_hex(uint64_t r[4], const char *hex);
int sm2_z256_equ_hex(const uint64_t a[4], const char *hex);
int sm2_z256_print(FILE *fp, int ind, int fmt, const char *label, const uint64_t a[4]);
int sm2_z512_print(FILE *fp, int ind, int fmt, const char *label, const uint64_t a[8]);
int sm2_z256_equ_hex(const uint64_t a[4], const char *hex);
int sm2_z256_print(FILE *fp, int ind, int fmt, const char *label, const uint64_t a[4]);
void sm2_z256_modp_add(uint64_t r[4], const uint64_t a[4], const uint64_t b[4]);
void sm2_z256_modp_sub(uint64_t r[4], const uint64_t a[4], const uint64_t b[4]);
void sm2_z256_modp_neg(uint64_t r[4], const uint64_t a[4]);
void sm2_z256_modp_dbl(uint64_t r[4], const uint64_t a[4]);
void sm2_z256_modp_tri(uint64_t r[4], const uint64_t a[4]);
void sm2_z256_modp_sub(uint64_t r[4], const uint64_t a[4], const uint64_t b[4]);
void sm2_z256_modp_neg(uint64_t r[4], const uint64_t a[4]);
void sm2_z256_modp_haf(uint64_t r[4], const uint64_t a[4]);
void sm2_z256_modp_to_mont(const uint64_t a[4], uint64_t r[4]);
@@ -55,10 +53,8 @@ void sm2_z256_modp_mont_mul(uint64_t r[4], const uint64_t a[4], const uint64_t b
void sm2_z256_modp_mont_sqr(uint64_t r[4], const uint64_t a[4]);
void sm2_z256_modp_mont_exp(uint64_t r[4], const uint64_t a[4], const uint64_t e[4]);
void sm2_z256_modp_mont_inv(uint64_t r[4], const uint64_t a[4]);
int sm2_z256_modp_mont_sqrt(uint64_t r[4], const uint64_t a[4]);
int sm2_z256_modp_mont_print(FILE *fp, int ind, int fmt, const char *label, const uint64_t a[4]);
int sm2_z256_modp_mont_sqrt(uint64_t r[4], const uint64_t a[4]);
int sm2_z256_modn_rand(uint64_t r[4]);
void sm2_z256_modn_add(uint64_t r[4], const uint64_t a[4], const uint64_t b[4]);
void sm2_z256_modn_sub(uint64_t r[4], const uint64_t a[4], const uint64_t b[4]);
void sm2_z256_modn_neg(uint64_t r[4], const uint64_t a[4]);
@@ -73,7 +69,6 @@ void sm2_z256_modn_mont_mul(uint64_t r[4], const uint64_t a[4], const uint64_t b
void sm2_z256_modn_mont_sqr(uint64_t r[4], const uint64_t a[4]);
void sm2_z256_modn_mont_exp(uint64_t r[4], const uint64_t a[4], const uint64_t e[4]);
void sm2_z256_modn_mont_inv(uint64_t r[4], const uint64_t a[4]);
int sm2_z256_modn_mont_print(FILE *fp, int ind, int fmt, const char *label, const uint64_t a[4]);
typedef struct {
@@ -83,12 +78,12 @@ typedef struct {
} SM2_Z256_POINT;
void sm2_z256_point_set_infinity(SM2_Z256_POINT *P);
void sm2_z256_point_from_bytes(SM2_Z256_POINT *P, const uint8_t in[64]);
void sm2_z256_point_from_bytes(SM2_Z256_POINT *P, const uint8_t in[64]); // 检查is_on_curve
void sm2_z256_point_to_bytes(const SM2_Z256_POINT *P, uint8_t out[64]);
int sm2_z256_point_is_at_infinity(const SM2_Z256_POINT *P);
int sm2_z256_point_is_on_curve(const SM2_Z256_POINT *P);
int sm2_z256_point_equ(const SM2_Z256_POINT *P, const SM2_Z256_POINT *Q);
int sm2_z256_point_is_at_infinity(const SM2_Z256_POINT *P);
int sm2_z256_point_is_on_curve(const SM2_Z256_POINT *P);
int sm2_z256_point_equ(const SM2_Z256_POINT *P, const SM2_Z256_POINT *Q); // 这个要声明一下Jacobian坐标系上一个点有不同的表示
void sm2_z256_point_get_xy(const SM2_Z256_POINT *P, uint64_t x[4], uint64_t y[4]);
void sm2_z256_point_dbl(SM2_Z256_POINT *R, const SM2_Z256_POINT *A);
@@ -96,7 +91,7 @@ void sm2_z256_point_add(SM2_Z256_POINT *r, const SM2_Z256_POINT *a, const SM2_Z2
void sm2_z256_point_neg(SM2_Z256_POINT *R, const SM2_Z256_POINT *P);
void sm2_z256_point_sub(SM2_Z256_POINT *R, const SM2_Z256_POINT *A, const SM2_Z256_POINT *B);
void sm2_z256_point_get_affine(const SM2_Z256_POINT *P, uint64_t x[4], uint64_t y[4]);
int sm2_z256_point_print(FILE *fp, int fmt, int ind, const char *label, const SM2_Z256_POINT *P);
int sm2_z256_point_print(FILE *fp, int fmt, int ind, const char *label, const SM2_Z256_POINT *P);
typedef struct {
@@ -135,8 +130,8 @@ int sm2_z256_point_from_hash(SM2_Z256_POINT *R, const uint8_t *data, size_t data
int sm2_z256_point_from_octets(SM2_Z256_POINT *P, const uint8_t *in, size_t inlen);
// 这些函数的问题是依赖于sm2.h 这些接口的,最好是不要有这些依赖
// 这些接口和底层的SM2曲线实现是相关
// 这些函数还是放到sm2_sign里面好了反正这个依赖关系是处理不了
int sm2_do_sign_fast(const uint64_t d[4], const uint8_t dgst[32], SM2_SIGNATURE *sig);
int sm2_do_sign_pre_compute(uint64_t k[4], uint64_t x1[4]);
int sm2_do_sign_fast_ex(const uint64_t d[4], const uint64_t k[4], const uint64_t x1[4], const uint8_t dgst[32], SM2_SIGNATURE *sig);

View File

@@ -56,7 +56,7 @@ retry:
// rand k in [1, n - 1]
do {
if (sm2_z256_modn_rand(k) != 1) {
if (sm2_z256_rand_range(k, sm2_z256_order()) != 1) {
error_print();
return -1;
}
@@ -116,7 +116,7 @@ int sm2_do_sign_pre_compute(uint64_t k[4], uint64_t x1[4])
// rand k in [1, n - 1]
do {
if (sm2_z256_modn_rand(k) != 1) {
if (sm2_z256_rand_range(k, sm2_z256_order()) != 1) {
error_print();
return -1;
}
@@ -188,7 +188,7 @@ int sm2_do_sign_fast(const uint64_t d[4], const uint8_t dgst[32], SM2_SIGNATURE
// rand k in [1, n - 1]
do {
if (sm2_z256_modn_rand(k) != 1) {
if (sm2_z256_rand_range(k, sm2_z256_order()) != 1) {
error_print();
return -1;
}
@@ -386,7 +386,7 @@ int sm2_do_encrypt_pre_compute(uint64_t k[4], uint8_t C1[64])
// rand k in [1, n - 1]
do {
if (sm2_z256_modn_rand(k) != 1) {
if (sm2_z256_rand_range(k, sm2_z256_order()) != 1) {
error_print();
return -1;
}
@@ -425,7 +425,7 @@ retry:
// rand k in [1, n - 1]
// TODO: set rand_bytes output for testing
do {
if (sm2_z256_modn_rand(k) != 1) {
if (sm2_z256_rand_range(k, sm2_z256_order()) != 1) {
error_print();
return -1;
}
@@ -498,7 +498,7 @@ int sm2_do_encrypt_fixlen(const SM2_KEY *key, const uint8_t *in, size_t inlen, i
retry:
// rand k in [1, n - 1]
do {
if (sm2_z256_modn_rand(k) != 1) {
if (sm2_z256_rand_range(k, sm2_z256_order()) != 1) {
error_print();
return -1;
}

View File

@@ -274,7 +274,7 @@ void sm2_z256_mul(uint64_t r[8], const uint64_t a[4], const uint64_t b[4])
}
}
uint64_t sm2_z512_add(uint64_t r[8], const uint64_t a[8], const uint64_t b[8])
static uint64_t sm2_z512_add(uint64_t r[8], const uint64_t a[8], const uint64_t b[8])
{
uint64_t t, c = 0;
@@ -368,12 +368,14 @@ int sm2_z256_print(FILE *fp, int ind, int fmt, const char *label, const uint64_t
return 1;
}
int sm2_z512_print(FILE *fp, int ind, int fmt, const char *label, const uint64_t a[8])
/*
static int sm2_z512_print(FILE *fp, int ind, int fmt, const char *label, const uint64_t a[8])
{
format_print(fp, ind, fmt, "%s: %016llx%016llx%016llx%016llx%016llx%016llx%016llx%016llx\n",
label, a[7], a[6], a[5], a[4], a[3], a[2], a[1], a[0]);
return 1;
}
*/
// GF(p)
@@ -673,6 +675,7 @@ int sm2_z256_modp_mont_sqrt(uint64_t r[4], const uint64_t a[4])
return 1;
}
/*
int sm2_z256_modp_mont_print(FILE *fp, int ind, int fmt, const char *label, const uint64_t a[4])
{
uint64_t r[4];
@@ -680,6 +683,7 @@ int sm2_z256_modp_mont_print(FILE *fp, int ind, int fmt, const char *label, cons
sm2_z256_print(fp, ind, fmt, label, r);
return 1;
}
*/
// GF(n)
@@ -698,6 +702,7 @@ const uint64_t SM2_Z256_NEG_N[4] = {
0xac440bf6c62abedd, 0x8dfc2094de39fad4, 0x0000000000000000, 0x0000000100000000,
};
/*
int sm2_z256_modn_rand(uint64_t r[4])
{
if (sm2_z256_rand_range(r, SM2_Z256_N) != 1) {
@@ -706,6 +711,7 @@ int sm2_z256_modn_rand(uint64_t r[4])
}
return 1;
}
*/
#ifndef ENABLE_SM2_Z256_ARMV8
void sm2_z256_modn_add(uint64_t r[4], const uint64_t a[4], const uint64_t b[4])
@@ -938,6 +944,7 @@ void sm2_z256_modn_to_mont(const uint64_t a[4], uint64_t r[4])
}
#endif
/*
int sm2_z256_modn_mont_print(FILE *fp, int ind, int fmt, const char *label, const uint64_t a[4])
{
uint64_t r[4];
@@ -945,6 +952,7 @@ int sm2_z256_modn_mont_print(FILE *fp, int ind, int fmt, const char *label, cons
sm2_z256_print(fp, ind, fmt, label, r);
return 1;
}
*/
// Jacobian Point with Montgomery coordinates
@@ -2004,6 +2012,7 @@ int sm2_point_from_der(SM2_POINT *P, const uint8_t **in, size_t *inlen)
return 1;
}
// 这个需要保留吗?似乎也没有必要保留
int sm2_point_from_hash(SM2_POINT *R, const uint8_t *data, size_t datalen)
{
return 1;

View File

@@ -46,7 +46,7 @@ static int test_sm2_z256_rshift(void)
uint64_t b[4];
unsigned int i;
sm2_z256_modn_rand(a);
sm2_z256_rand_range(a, sm2_z256_order());
sm2_z256_rshift(r, a, 0);
sm2_z256_copy(b, a);
@@ -79,7 +79,7 @@ static int test_sm2_z256_modp_mont_sqrt(void)
int i;
for (i = 0; i < 6; i++) {
sm2_z256_modn_rand(a);
sm2_z256_rand_range(a, sm2_z256_order());
sm2_z256_modp_neg(neg_a, a);
sm2_z256_modp_to_mont(a, mont_a);