Skip to content

Commit

Permalink
Rename SM2_Z256 to sm2_z256_t
Browse files Browse the repository at this point in the history
  • Loading branch information
guanzhi committed Apr 16, 2024
1 parent 6028d1e commit f0e70bb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 41 deletions.
2 changes: 1 addition & 1 deletion include/gmssl/sm2_z256.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
extern "C" {
#endif

typedef uint64_t SM2_Z256[4];
typedef uint64_t sm2_z256_t[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]);
Expand Down
52 changes: 26 additions & 26 deletions src/sm2_sign.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
int sm2_do_sign(const SM2_KEY *key, const uint8_t dgst[32], SM2_SIGNATURE *sig)
{
SM2_Z256_POINT _P, *P = &_P;
SM2_Z256 d;
SM2_Z256 d_inv;
SM2_Z256 e;
SM2_Z256 k;
SM2_Z256 x;
SM2_Z256 t;
SM2_Z256 r;
SM2_Z256 s;
sm2_z256_t d;
sm2_z256_t d_inv;
sm2_z256_t e;
sm2_z256_t k;
sm2_z256_t x;
sm2_z256_t t;
sm2_z256_t r;
sm2_z256_t s;

const uint64_t *one = sm2_z256_one();
const uint64_t *order = sm2_z256_order();
Expand Down Expand Up @@ -170,11 +170,11 @@ int sm2_do_sign_fast_ex(const uint64_t d[4], const uint64_t k[4], const uint64_t
int sm2_do_sign_fast(const uint64_t d[4], const uint8_t dgst[32], SM2_SIGNATURE *sig)
{
SM2_Z256_POINT R;
SM2_Z256 e;
SM2_Z256 k;
SM2_Z256 x1;
SM2_Z256 r;
SM2_Z256 s;
sm2_z256_t e;
sm2_z256_t k;
sm2_z256_t x1;
sm2_z256_t r;
sm2_z256_t s;

const uint64_t *order = sm2_z256_order();

Expand Down Expand Up @@ -224,11 +224,11 @@ int sm2_do_sign_fast(const uint64_t d[4], const uint8_t dgst[32], SM2_SIGNATURE
int sm2_do_verify_fast(const SM2_Z256_POINT *P, const uint8_t dgst[32], const SM2_SIGNATURE *sig)
{
SM2_Z256_POINT R;
SM2_Z256 r;
SM2_Z256 s;
SM2_Z256 e;
SM2_Z256 x;
SM2_Z256 t;
sm2_z256_t r;
sm2_z256_t s;
sm2_z256_t e;
sm2_z256_t x;
sm2_z256_t t;

const uint64_t *order = sm2_z256_order();

Expand Down Expand Up @@ -289,11 +289,11 @@ int sm2_do_verify(const SM2_KEY *key, const uint8_t dgst[32], const SM2_SIGNATUR
{
SM2_Z256_POINT _P, *P = &_P;
SM2_Z256_POINT _R, *R = &_R;
SM2_Z256 r;
SM2_Z256 s;
SM2_Z256 e;
SM2_Z256 x;
SM2_Z256 t;
sm2_z256_t r;
sm2_z256_t s;
sm2_z256_t e;
sm2_z256_t x;
sm2_z256_t t;

const uint64_t *order = sm2_z256_order();

Expand Down Expand Up @@ -403,7 +403,7 @@ int sm2_do_encrypt_pre_compute(uint64_t k[4], uint8_t C1[64])
// 其中k是要参与计算的,但是 (x1, y1) 不参与计算,输出为 bytes 就可以了
int sm2_do_encrypt(const SM2_KEY *key, const uint8_t *in, size_t inlen, SM2_CIPHERTEXT *out)
{
SM2_Z256 k;
sm2_z256_t k;
SM2_Z256_POINT _P, *P = &_P;
SM2_Z256_POINT _C1, *C1 = &_C1;
SM2_Z256_POINT _kP, *kP = &_kP;
Expand Down Expand Up @@ -467,7 +467,7 @@ int sm2_do_encrypt(const SM2_KEY *key, const uint8_t *in, size_t inlen, SM2_CIPH
int sm2_do_encrypt_fixlen(const SM2_KEY *key, const uint8_t *in, size_t inlen, int point_size, SM2_CIPHERTEXT *out)
{
unsigned int trys = 200;
SM2_Z256 k;
sm2_z256_t k;
SM2_Z256_POINT _P, *P = &_P;
SM2_Z256_POINT _C1, *C1 = &_C1;
SM2_Z256_POINT _kP, *kP = &_kP;
Expand Down Expand Up @@ -555,7 +555,7 @@ int sm2_do_encrypt_fixlen(const SM2_KEY *key, const uint8_t *in, size_t inlen, i
int sm2_do_decrypt(const SM2_KEY *key, const SM2_CIPHERTEXT *in, uint8_t *out, size_t *outlen)
{
int ret = -1;
SM2_Z256 d;
sm2_z256_t d;
SM2_Z256_POINT _C1, *C1 = &_C1;
uint8_t x2y2[64];
SM3_CTX sm3_ctx;
Expand Down
20 changes: 6 additions & 14 deletions tests/sm2_signtest.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,30 +102,22 @@ static int test_sm2_do_sign(void)
return 1;
}

#define SM2_U256 SM2_Z256
#define sm2_u256_one sm2_z256_one
#define sm2_u256_is_zero sm2_z256_is_zero
#define sm2_u256_from_bytes sm2_z256_from_bytes
#define sm2_u256_modn_add sm2_z256_modn_add
#define sm2_u256_modn_inv sm2_z256_modn_inv


static int test_sm2_do_sign_fast(void)
{
SM2_KEY sm2_key;
SM2_U256 d;
sm2_z256_t d;
uint8_t dgst[32];
SM2_SIGNATURE sig;
size_t i;

// d' = (d + 1)^-1 (mod n)
const uint64_t *one = sm2_u256_one();
const uint64_t *one = sm2_z256_one();
do {
sm2_key_generate(&sm2_key);
sm2_u256_from_bytes(d, sm2_key.private_key);
sm2_u256_modn_add(d, d, one);
sm2_u256_modn_inv(d, d);
} while (sm2_u256_is_zero(d));
sm2_z256_from_bytes(d, sm2_key.private_key);
sm2_z256_modn_add(d, d, one);
sm2_z256_modn_inv(d, d);
} while (sm2_z256_is_zero(d));

for (i = 0; i < TEST_COUNT; i++) {
if (sm2_do_sign_fast(d, dgst, &sig) != 1) {
Expand Down

0 comments on commit f0e70bb

Please sign in to comment.