Merge pull request #1209 from Gorachya/develop

add test and fix some bugs
This commit is contained in:
Zhi Guan
2022-05-08 21:38:49 +08:00
committed by GitHub
3 changed files with 368 additions and 310 deletions

View File

@@ -75,6 +75,9 @@ typedef struct {
sm9_fp2_t Z;
} sm9_twist_point_t;
extern const sm9_twist_point_t *SM9_P2;
extern const sm9_twist_point_t *SM9_Ppubs;
#define sm9_bn_init(r) memset((r),0,sizeof(sm9_bn_t))
#define sm9_bn_clean(r) memset((r),0,sizeof(sm9_bn_t))
@@ -84,19 +87,19 @@ typedef struct {
#define sm9_bn_is_zero(a) (memcmp((a),&SM9_ZERO, sizeof(sm9_bn_t)) == 0)
#define sm9_bn_is_one(a) (memcmp((a),&SM9_ONE, sizeof(sm9_bn_t)) == 0)
static void sm9_bn_to_bytes(const sm9_bn_t a, uint8_t out[32]);
static void sm9_bn_from_bytes(sm9_bn_t r, const uint8_t in[32]);
static int sm9_bn_from_hex(sm9_bn_t r, const char hex[65]);
static void sm9_bn_to_hex(const sm9_bn_t a, char hex[65]);
static void sm9_print_bn(const char *prefix, const sm9_bn_t a);
static void sm9_bn_to_bits(const sm9_bn_t a, char bits[256]);
void sm9_bn_to_bytes(const sm9_bn_t a, uint8_t out[32]);
void sm9_bn_from_bytes(sm9_bn_t r, const uint8_t in[32]);
int sm9_bn_from_hex(sm9_bn_t r, const char hex[65]);
void sm9_bn_to_hex(const sm9_bn_t a, char hex[65]);
void sm9_print_bn(const char *prefix, const sm9_bn_t a);
void sm9_bn_to_bits(const sm9_bn_t a, char bits[256]);
static int sm9_bn_cmp(const sm9_bn_t a, const sm9_bn_t b);
static int sm9_bn_equ_hex(const sm9_bn_t a, const char *hex);
static void sm9_bn_set_word(sm9_bn_t r, uint32_t a);
static void sm9_bn_add(sm9_bn_t r, const sm9_bn_t a, const sm9_bn_t b);
static void sm9_bn_sub(sm9_bn_t ret, const sm9_bn_t a, const sm9_bn_t b);
static void sm9_bn_rand_range(sm9_bn_t r, const sm9_bn_t range);
int sm9_bn_cmp(const sm9_bn_t a, const sm9_bn_t b);
int sm9_bn_equ_hex(const sm9_bn_t a, const char *hex);
void sm9_bn_set_word(sm9_bn_t r, uint32_t a);
void sm9_bn_add(sm9_bn_t r, const sm9_bn_t a, const sm9_bn_t b);
void sm9_bn_sub(sm9_bn_t ret, const sm9_bn_t a, const sm9_bn_t b);
void sm9_bn_rand_range(sm9_bn_t r, const sm9_bn_t range);
#define sm9_fp_init(a) sm9_bn_init(a)
#define sm9_fp_clean(a) sm9_bn_clean(a)
@@ -108,21 +111,21 @@ static void sm9_bn_rand_range(sm9_bn_t r, const sm9_bn_t range);
#define sm9_fp_to_hex(a,s) sm9_bn_to_hex((a),(s))
#define sm9_fp_copy(r,a) sm9_bn_copy((r),(a))
static int sm9_fp_equ(const sm9_fp_t a, const sm9_fp_t b);
static void sm9_fp_add(sm9_fp_t r, const sm9_fp_t a, const sm9_fp_t b);
static void sm9_fp_sub(sm9_fp_t r, const sm9_fp_t a, const sm9_fp_t b);
static void sm9_fp_dbl(sm9_fp_t r, const sm9_fp_t a);
static void sm9_fp_tri(sm9_fp_t r, const sm9_fp_t a);
static void sm9_fp_div2(sm9_fp_t r, const sm9_fp_t a);
static void sm9_fp_neg(sm9_fp_t r, const sm9_fp_t a);
static void sm9_fp_mul(sm9_fp_t r, const sm9_fp_t a, const sm9_fp_t b);
static void sm9_fp_sqr(sm9_fp_t r, const sm9_fp_t a);
static void sm9_fp_pow(sm9_fp_t r, const sm9_fp_t a, const sm9_bn_t e);
static void sm9_fp_inv(sm9_fp_t r, const sm9_fp_t a);
int sm9_fp_equ(const sm9_fp_t a, const sm9_fp_t b);
void sm9_fp_add(sm9_fp_t r, const sm9_fp_t a, const sm9_fp_t b);
void sm9_fp_sub(sm9_fp_t r, const sm9_fp_t a, const sm9_fp_t b);
void sm9_fp_dbl(sm9_fp_t r, const sm9_fp_t a);
void sm9_fp_tri(sm9_fp_t r, const sm9_fp_t a);
void sm9_fp_div2(sm9_fp_t r, const sm9_fp_t a);
void sm9_fp_neg(sm9_fp_t r, const sm9_fp_t a);
void sm9_fp_mul(sm9_fp_t r, const sm9_fp_t a, const sm9_fp_t b);
void sm9_fp_sqr(sm9_fp_t r, const sm9_fp_t a);
void sm9_fp_pow(sm9_fp_t r, const sm9_fp_t a, const sm9_bn_t e);
void sm9_fp_inv(sm9_fp_t r, const sm9_fp_t a);
static int sm9_barrett_bn_cmp(const sm9_barrett_bn_t a, const sm9_barrett_bn_t b);
static void sm9_barrett_bn_add(sm9_barrett_bn_t r, const sm9_barrett_bn_t a, const sm9_barrett_bn_t b);
static void sm9_barrett_bn_sub(sm9_barrett_bn_t ret, const sm9_barrett_bn_t a, const sm9_barrett_bn_t b);
int sm9_barrett_bn_cmp(const sm9_barrett_bn_t a, const sm9_barrett_bn_t b);
void sm9_barrett_bn_add(sm9_barrett_bn_t r, const sm9_barrett_bn_t a, const sm9_barrett_bn_t b);
void sm9_barrett_bn_sub(sm9_barrett_bn_t ret, const sm9_barrett_bn_t a, const sm9_barrett_bn_t b);
#define sm9_fp2_init(a) memset((a), 0, sizeof(sm9_fp2_t))
#define sm9_fp2_clean(a) memset((a), 0, sizeof(sm9_fp2_t))
@@ -131,28 +134,28 @@ static void sm9_barrett_bn_sub(sm9_barrett_bn_t ret, const sm9_barrett_bn_t a, c
#define sm9_fp2_copy(r,a) memcpy((r), (a), sizeof(sm9_fp2_t))
#define sm9_fp2_equ(a,b) (memcmp((a),(b),sizeof(sm9_fp2_t)) == 0)
static void sm9_fp2_from_hex(sm9_fp2_t r, const char hex[65 * 2]);
static void sm9_fp2_to_hex(const sm9_fp2_t a, char hex[65 * 2]);
static void sm9_fp2_print(const char *prefix, const sm9_fp2_t a);
void sm9_fp2_from_hex(sm9_fp2_t r, const char hex[65 * 2]);
void sm9_fp2_to_hex(const sm9_fp2_t a, char hex[65 * 2]);
void sm9_fp2_print(const char *prefix, const sm9_fp2_t a);
#define sm9_fp2_set_zero(a) memset((a), 0, sizeof(sm9_fp2_t))
#define sm9_fp2_set_one(a) memcpy((a), &SM9_FP2_ONE, sizeof(sm9_fp2_t))
static void sm9_fp2_set_fp(sm9_fp2_t r, const sm9_fp_t a);
void sm9_fp2_set_fp(sm9_fp2_t r, const sm9_fp_t a);
#define sm9_fp2_set_u(a) memcpy((a), &SM9_FP2_U, sizeof(sm9_fp2_t))
static void sm9_fp2_set(sm9_fp2_t r, const sm9_fp_t a0, const sm9_fp_t a1);
void sm9_fp2_set(sm9_fp2_t r, const sm9_fp_t a0, const sm9_fp_t a1);
static void sm9_fp2_add(sm9_fp2_t r, const sm9_fp2_t a, const sm9_fp2_t b);
static void sm9_fp2_dbl(sm9_fp2_t r, const sm9_fp2_t a);
static void sm9_fp2_tri(sm9_fp2_t r, const sm9_fp2_t a);
static void sm9_fp2_sub(sm9_fp2_t r, const sm9_fp2_t a, const sm9_fp2_t b);
static void sm9_fp2_neg(sm9_fp2_t r, const sm9_fp2_t a);
static void sm9_fp2_mul(sm9_fp2_t r, const sm9_fp2_t a, const sm9_fp2_t b);
static void sm9_fp2_mul_u(sm9_fp2_t r, const sm9_fp2_t a, const sm9_fp2_t b);
static void sm9_fp2_mul_fp(sm9_fp2_t r, const sm9_fp2_t a, const sm9_fp_t k);
static void sm9_fp2_sqr(sm9_fp2_t r, const sm9_fp2_t a);
static void sm9_fp2_sqr_u(sm9_fp2_t r, const sm9_fp2_t a);
static void sm9_fp2_inv(sm9_fp2_t r, const sm9_fp2_t a);
static void sm9_fp2_div(sm9_fp2_t r, const sm9_fp2_t a, const sm9_fp2_t b);
static void sm9_fp2_div2(sm9_fp2_t r, const sm9_fp2_t a);
void sm9_fp2_add(sm9_fp2_t r, const sm9_fp2_t a, const sm9_fp2_t b);
void sm9_fp2_dbl(sm9_fp2_t r, const sm9_fp2_t a);
void sm9_fp2_tri(sm9_fp2_t r, const sm9_fp2_t a);
void sm9_fp2_sub(sm9_fp2_t r, const sm9_fp2_t a, const sm9_fp2_t b);
void sm9_fp2_neg(sm9_fp2_t r, const sm9_fp2_t a);
void sm9_fp2_mul(sm9_fp2_t r, const sm9_fp2_t a, const sm9_fp2_t b);
void sm9_fp2_mul_u(sm9_fp2_t r, const sm9_fp2_t a, const sm9_fp2_t b);
void sm9_fp2_mul_fp(sm9_fp2_t r, const sm9_fp2_t a, const sm9_fp_t k);
void sm9_fp2_sqr(sm9_fp2_t r, const sm9_fp2_t a);
void sm9_fp2_sqr_u(sm9_fp2_t r, const sm9_fp2_t a);
void sm9_fp2_inv(sm9_fp2_t r, const sm9_fp2_t a);
void sm9_fp2_div(sm9_fp2_t r, const sm9_fp2_t a, const sm9_fp2_t b);
void sm9_fp2_div2(sm9_fp2_t r, const sm9_fp2_t a);
#define sm9_fp4_init(r) memcpy((r), &SM9_FP4_ZERO, sizeof(sm9_fp4_t))
#define sm9_fp4_clean(r) memcpy((r), &SM9_FP4_ZERO, sizeof(sm9_fp4_t))
@@ -163,110 +166,110 @@ static void sm9_fp2_div2(sm9_fp2_t r, const sm9_fp2_t a);
#define sm9_fp4_equ(a,b) (memcmp((a), (b), sizeof(sm9_fp4_t)) == 0)
#define sm9_fp4_copy(r,a) memcpy((r), (a), sizeof(sm9_fp4_t))
static void sm9_fp4_from_hex(sm9_fp4_t r, const char hex[65 * 4]);
static void sm9_fp4_to_hex(const sm9_fp4_t a, char hex[65 * 4]);
static void sm9_fp4_set_fp(sm9_fp4_t r, const sm9_fp_t a);
static void sm9_fp4_set_fp2(sm9_fp4_t r, const sm9_fp2_t a);
static void sm9_fp4_set(sm9_fp4_t r, const sm9_fp2_t a0, const sm9_fp2_t a1);
static void sm9_fp4_set_u(sm9_fp4_t r);
static void sm9_fp4_set_v(sm9_fp4_t r);
void sm9_fp4_from_hex(sm9_fp4_t r, const char hex[65 * 4]);
void sm9_fp4_to_hex(const sm9_fp4_t a, char hex[65 * 4]);
void sm9_fp4_set_fp(sm9_fp4_t r, const sm9_fp_t a);
void sm9_fp4_set_fp2(sm9_fp4_t r, const sm9_fp2_t a);
void sm9_fp4_set(sm9_fp4_t r, const sm9_fp2_t a0, const sm9_fp2_t a1);
void sm9_fp4_set_u(sm9_fp4_t r);
void sm9_fp4_set_v(sm9_fp4_t r);
static void sm9_fp4_add(sm9_fp4_t r, const sm9_fp4_t a, const sm9_fp4_t b);
static void sm9_fp4_dbl(sm9_fp4_t r, const sm9_fp4_t a);
static void sm9_fp4_sub(sm9_fp4_t r, const sm9_fp4_t a, const sm9_fp4_t b);
static void sm9_fp4_neg(sm9_fp4_t r, const sm9_fp4_t a);
static void sm9_fp4_mul(sm9_fp4_t r, const sm9_fp4_t a, const sm9_fp4_t b);
static void sm9_fp4_mul_fp(sm9_fp4_t r, const sm9_fp4_t a, const sm9_fp_t k);
static void sm9_fp4_mul_fp2(sm9_fp4_t r, const sm9_fp4_t a, const sm9_fp2_t b0);
static void sm9_fp4_mul_v(sm9_fp4_t r, const sm9_fp4_t a, const sm9_fp4_t b);
static void sm9_fp4_sqr(sm9_fp4_t r, const sm9_fp4_t a);
static void sm9_fp4_sqr_v(sm9_fp4_t r, const sm9_fp4_t a);
static void sm9_fp4_inv(sm9_fp4_t r, const sm9_fp4_t a);
void sm9_fp4_add(sm9_fp4_t r, const sm9_fp4_t a, const sm9_fp4_t b);
void sm9_fp4_dbl(sm9_fp4_t r, const sm9_fp4_t a);
void sm9_fp4_sub(sm9_fp4_t r, const sm9_fp4_t a, const sm9_fp4_t b);
void sm9_fp4_neg(sm9_fp4_t r, const sm9_fp4_t a);
void sm9_fp4_mul(sm9_fp4_t r, const sm9_fp4_t a, const sm9_fp4_t b);
void sm9_fp4_mul_fp(sm9_fp4_t r, const sm9_fp4_t a, const sm9_fp_t k);
void sm9_fp4_mul_fp2(sm9_fp4_t r, const sm9_fp4_t a, const sm9_fp2_t b0);
void sm9_fp4_mul_v(sm9_fp4_t r, const sm9_fp4_t a, const sm9_fp4_t b);
void sm9_fp4_sqr(sm9_fp4_t r, const sm9_fp4_t a);
void sm9_fp4_sqr_v(sm9_fp4_t r, const sm9_fp4_t a);
void sm9_fp4_inv(sm9_fp4_t r, const sm9_fp4_t a);
#define sm9_fp12_init(r) memset((r), 0, sizeof(sm9_fp12_t))
#define sm9_fp12_clean(r) memset((r), 0, sizeof(sm9_fp12_t))
#define sm9_fp12_set_zero(r) memset((r), 0, sizeof(sm9_fp12_t))
#define sm9_fp12_copy(r, a) memcpy((r), (a), sizeof(sm9_fp12_t))
static void sm9_fp12_set_one(sm9_fp12_t r);
static int sm9_fp12_is_one(const sm9_fp12_t a);
static int sm9_fp12_is_zero(const sm9_fp12_t a);
static void sm9_fp12_from_hex(sm9_fp12_t r, const char hex[65 * 12]);
static void sm9_fp12_to_hex(const sm9_fp12_t a, char hex[65 * 12]);
static void sm9_fp12_print(const char *prefix, const sm9_fp12_t a);
static void sm9_fp12_set(sm9_fp12_t r, const sm9_fp4_t a0, const sm9_fp4_t a1, const sm9_fp4_t a2);
static void sm9_fp12_set_fp(sm9_fp12_t r, const sm9_fp_t a);
static void sm9_fp12_set_fp2(sm9_fp12_t r, const sm9_fp2_t a);
static void sm9_fp12_set_fp4(sm9_fp12_t r, const sm9_fp4_t a);
static void sm9_fp12_set_u(sm9_fp12_t r);
static void sm9_fp12_set_v(sm9_fp12_t r);
static void sm9_fp12_set_w(sm9_fp12_t r);
static void sm9_fp12_set_w_sqr(sm9_fp12_t r);
void sm9_fp12_set_one(sm9_fp12_t r);
int sm9_fp12_is_one(const sm9_fp12_t a);
int sm9_fp12_is_zero(const sm9_fp12_t a);
void sm9_fp12_from_hex(sm9_fp12_t r, const char hex[65 * 12]);
void sm9_fp12_to_hex(const sm9_fp12_t a, char hex[65 * 12]);
void sm9_fp12_print(const char *prefix, const sm9_fp12_t a);
void sm9_fp12_set(sm9_fp12_t r, const sm9_fp4_t a0, const sm9_fp4_t a1, const sm9_fp4_t a2);
void sm9_fp12_set_fp(sm9_fp12_t r, const sm9_fp_t a);
void sm9_fp12_set_fp2(sm9_fp12_t r, const sm9_fp2_t a);
void sm9_fp12_set_fp4(sm9_fp12_t r, const sm9_fp4_t a);
void sm9_fp12_set_u(sm9_fp12_t r);
void sm9_fp12_set_v(sm9_fp12_t r);
void sm9_fp12_set_w(sm9_fp12_t r);
void sm9_fp12_set_w_sqr(sm9_fp12_t r);
static int sm9_fp12_equ(const sm9_fp12_t a, const sm9_fp12_t b);
static void sm9_fp12_add(sm9_fp12_t r, const sm9_fp12_t a, const sm9_fp12_t b);
static void sm9_fp12_dbl(sm9_fp12_t r, const sm9_fp12_t a);
static void sm9_fp12_tri(sm9_fp12_t r, const sm9_fp12_t a);
static void sm9_fp12_sub(sm9_fp12_t r, const sm9_fp12_t a, const sm9_fp12_t b);
static void sm9_fp12_neg(sm9_fp12_t r, const sm9_fp12_t a);
static void sm9_fp12_mul(sm9_fp12_t r, const sm9_fp12_t a, const sm9_fp12_t b);
static void sm9_fp12_sqr(sm9_fp12_t r, const sm9_fp12_t a);
static void sm9_fp12_inv(sm9_fp12_t r, const sm9_fp12_t a);
static void sm9_fp12_pow(sm9_fp12_t r, const sm9_fp12_t a, const sm9_bn_t k);
int sm9_fp12_equ(const sm9_fp12_t a, const sm9_fp12_t b);
void sm9_fp12_add(sm9_fp12_t r, const sm9_fp12_t a, const sm9_fp12_t b);
void sm9_fp12_dbl(sm9_fp12_t r, const sm9_fp12_t a);
void sm9_fp12_tri(sm9_fp12_t r, const sm9_fp12_t a);
void sm9_fp12_sub(sm9_fp12_t r, const sm9_fp12_t a, const sm9_fp12_t b);
void sm9_fp12_neg(sm9_fp12_t r, const sm9_fp12_t a);
void sm9_fp12_mul(sm9_fp12_t r, const sm9_fp12_t a, const sm9_fp12_t b);
void sm9_fp12_sqr(sm9_fp12_t r, const sm9_fp12_t a);
void sm9_fp12_inv(sm9_fp12_t r, const sm9_fp12_t a);
void sm9_fp12_pow(sm9_fp12_t r, const sm9_fp12_t a, const sm9_bn_t k);
static void sm9_fp2_conjugate(sm9_fp2_t r, const sm9_fp2_t a);
static void sm9_fp2_frobenius(sm9_fp2_t r, const sm9_fp2_t a);
static void sm9_fp4_frobenius(sm9_fp4_t r, const sm9_fp4_t a);
static void sm9_fp4_conjugate(sm9_fp4_t r, const sm9_fp4_t a);
static void sm9_fp4_frobenius2(sm9_fp4_t r, const sm9_fp4_t a);
static void sm9_fp4_frobenius3(sm9_fp4_t r, const sm9_fp4_t a);
static void sm9_fp12_frobenius(sm9_fp12_t r, const sm9_fp12_t x);
static void sm9_fp12_frobenius2(sm9_fp12_t r, const sm9_fp12_t x);
static void sm9_fp12_frobenius3(sm9_fp12_t r, const sm9_fp12_t x);
static void sm9_fp12_frobenius6(sm9_fp12_t r, const sm9_fp12_t x);
void sm9_fp2_conjugate(sm9_fp2_t r, const sm9_fp2_t a);
void sm9_fp2_frobenius(sm9_fp2_t r, const sm9_fp2_t a);
void sm9_fp4_frobenius(sm9_fp4_t r, const sm9_fp4_t a);
void sm9_fp4_conjugate(sm9_fp4_t r, const sm9_fp4_t a);
void sm9_fp4_frobenius2(sm9_fp4_t r, const sm9_fp4_t a);
void sm9_fp4_frobenius3(sm9_fp4_t r, const sm9_fp4_t a);
void sm9_fp12_frobenius(sm9_fp12_t r, const sm9_fp12_t x);
void sm9_fp12_frobenius2(sm9_fp12_t r, const sm9_fp12_t x);
void sm9_fp12_frobenius3(sm9_fp12_t r, const sm9_fp12_t x);
void sm9_fp12_frobenius6(sm9_fp12_t r, const sm9_fp12_t x);
static void sm9_point_init(sm9_point_t *R);
static void sm9_point_from_hex(sm9_point_t *R, const char hex[65 * 2]);
void sm9_point_init(sm9_point_t *R);
void sm9_point_from_hex(sm9_point_t *R, const char hex[65 * 2]);
#define sm9_point_copy(R, P) memcpy((R), (P), sizeof(sm9_point_t))
static int sm9_point_is_at_infinity(const sm9_point_t *P);
static void sm9_point_set_infinity(sm9_point_t *R);
static void sm9_point_get_xy(const sm9_point_t *P, sm9_fp_t x, sm9_fp_t y);
int sm9_point_is_at_infinity(const sm9_point_t *P);
void sm9_point_set_infinity(sm9_point_t *R);
void sm9_point_get_xy(const sm9_point_t *P, sm9_fp_t x, sm9_fp_t y);
static int sm9_point_equ(const sm9_point_t *P, const sm9_point_t *Q);
static int sm9_point_is_on_curve(const sm9_point_t *P);
static void sm9_point_dbl(sm9_point_t *R, const sm9_point_t *P);
static void sm9_point_add(sm9_point_t *R, const sm9_point_t *P, const sm9_point_t *Q);
static void sm9_point_neg(sm9_point_t *R, const sm9_point_t *P);
static void sm9_point_sub(sm9_point_t *R, const sm9_point_t *P, const sm9_point_t *Q);
static void sm9_point_mul(sm9_point_t *R, const sm9_bn_t k, const sm9_point_t *P);
static void sm9_point_mul_generator(sm9_point_t *R, const sm9_bn_t k);
int sm9_point_equ(const sm9_point_t *P, const sm9_point_t *Q);
int sm9_point_is_on_curve(const sm9_point_t *P);
void sm9_point_dbl(sm9_point_t *R, const sm9_point_t *P);
void sm9_point_add(sm9_point_t *R, const sm9_point_t *P, const sm9_point_t *Q);
void sm9_point_neg(sm9_point_t *R, const sm9_point_t *P);
void sm9_point_sub(sm9_point_t *R, const sm9_point_t *P, const sm9_point_t *Q);
void sm9_point_mul(sm9_point_t *R, const sm9_bn_t k, const sm9_point_t *P);
void sm9_point_mul_generator(sm9_point_t *R, const sm9_bn_t k);
static void sm9_twist_point_from_hex(sm9_twist_point_t *R, const char hex[65 * 4]);
void sm9_twist_point_from_hex(sm9_twist_point_t *R, const char hex[65 * 4]);
#define sm9_twist_point_copy(R, P) memcpy((R), (P), sizeof(sm9_twist_point_t))
static int sm9_twist_point_is_at_infinity(const sm9_twist_point_t *P);
static void sm9_twist_point_set_infinity(sm9_twist_point_t *R);
static void sm9_twist_point_get_xy(const sm9_twist_point_t *P, sm9_fp2_t x, sm9_fp2_t y);
int sm9_twist_point_is_at_infinity(const sm9_twist_point_t *P);
void sm9_twist_point_set_infinity(sm9_twist_point_t *R);
void sm9_twist_point_get_xy(const sm9_twist_point_t *P, sm9_fp2_t x, sm9_fp2_t y);
static int sm9_twist_point_equ(const sm9_twist_point_t *P, const sm9_twist_point_t *Q);
static int sm9_twist_point_is_on_curve(const sm9_twist_point_t *P);
static void sm9_twist_point_neg(sm9_twist_point_t *R, const sm9_twist_point_t *P);
static void sm9_twist_point_dbl(sm9_twist_point_t *R, const sm9_twist_point_t *P);
static void sm9_twist_point_add(sm9_twist_point_t *R, const sm9_twist_point_t *P, const sm9_twist_point_t *Q);
static void sm9_twist_point_sub(sm9_twist_point_t *R, const sm9_twist_point_t *P, const sm9_twist_point_t *Q);
static void sm9_twist_point_add_full(sm9_twist_point_t *R, const sm9_twist_point_t *P, const sm9_twist_point_t *Q);
static void sm9_twist_point_mul(sm9_twist_point_t *R, const sm9_bn_t k, const sm9_twist_point_t *P);
static void sm9_twist_point_mul_G(sm9_twist_point_t *R, const sm9_bn_t k);
int sm9_twist_point_equ(const sm9_twist_point_t *P, const sm9_twist_point_t *Q);
int sm9_twist_point_is_on_curve(const sm9_twist_point_t *P);
void sm9_twist_point_neg(sm9_twist_point_t *R, const sm9_twist_point_t *P);
void sm9_twist_point_dbl(sm9_twist_point_t *R, const sm9_twist_point_t *P);
void sm9_twist_point_add(sm9_twist_point_t *R, const sm9_twist_point_t *P, const sm9_twist_point_t *Q);
void sm9_twist_point_sub(sm9_twist_point_t *R, const sm9_twist_point_t *P, const sm9_twist_point_t *Q);
void sm9_twist_point_add_full(sm9_twist_point_t *R, const sm9_twist_point_t *P, const sm9_twist_point_t *Q);
void sm9_twist_point_mul(sm9_twist_point_t *R, const sm9_bn_t k, const sm9_twist_point_t *P);
void sm9_twist_point_mul_G(sm9_twist_point_t *R, const sm9_bn_t k);
static void sm9_eval_g_tangent(sm9_fp12_t num, sm9_fp12_t den, const sm9_twist_point_t *P, const sm9_point_t *Q);
static void sm9_eval_g_line(sm9_fp12_t num, sm9_fp12_t den, const sm9_twist_point_t *T, const sm9_twist_point_t *P, const sm9_point_t *Q);
void sm9_eval_g_tangent(sm9_fp12_t num, sm9_fp12_t den, const sm9_twist_point_t *P, const sm9_point_t *Q);
void sm9_eval_g_line(sm9_fp12_t num, sm9_fp12_t den, const sm9_twist_point_t *T, const sm9_twist_point_t *P, const sm9_point_t *Q);
static void sm9_twist_point_pi1(sm9_twist_point_t *R, const sm9_twist_point_t *P);
static void sm9_twist_point_pi2(sm9_twist_point_t *R, const sm9_twist_point_t *P);
static void sm9_twist_point_neg_pi2(sm9_twist_point_t *R, const sm9_twist_point_t *P);
void sm9_twist_point_pi1(sm9_twist_point_t *R, const sm9_twist_point_t *P);
void sm9_twist_point_pi2(sm9_twist_point_t *R, const sm9_twist_point_t *P);
void sm9_twist_point_neg_pi2(sm9_twist_point_t *R, const sm9_twist_point_t *P);
static void sm9_final_exponent_hard_part(sm9_fp12_t r, const sm9_fp12_t f);
static void sm9_final_exponent(sm9_fp12_t r, const sm9_fp12_t f);
static void sm9_pairing(sm9_fp12_t r, const sm9_twist_point_t *Q, const sm9_point_t *P);
void sm9_final_exponent_hard_part(sm9_fp12_t r, const sm9_fp12_t f);
void sm9_final_exponent(sm9_fp12_t r, const sm9_fp12_t f);
void sm9_pairing(sm9_fp12_t r, const sm9_twist_point_t *Q, const sm9_point_t *P);
void sm9_pairing_test();

File diff suppressed because it is too large Load Diff

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014 - 2021 The GmSSL Project. All rights reserved.
* Copyright (c) 2016 - 2021 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -53,7 +53,60 @@
#include <gmssl/error.h>
#include <gmssl/rand.h>
int main(void)
{
return 0;
#define hex_iv "123456789abcdef00fedcba987654321123456789abcdef00fedcba987654321"
#define hex_fp_add "114efe24536598809df494ff7657484edff1812d51c3955b7d869149aa123d31"
#define hex_fp_sub "43cee97c9abed9be3efe7ffffc9d30abe1d643b9b27ea351460aabb2239d3fd4"
#define hex_fp_nsub "7271168367e4cd3397052b4ff8f19699401c4f9167fc4b8a9f64ef75bfb405a9"
#define hex_fp_dbl "551de7a0ee24723edcf314ff72f478fac1c7c4e7044238acc3913cfbcdaf7d05"
#define hex_fp_tri "248cdb7163e4d7e5606ac9d731a751d591b25db4f925dd9532a20de5c2de98c9"
#define hex_fp_div2 "9df779e83d83d9c517bf85bbd4e833b289e7dfb214ecc1501cf8039cdde8d35f"
#define hex_fp_neg "30910c2f8a3f9a597c884b28414d2725301567320b1c5b1790ef2f160ad0e43c"
#define hex_fp_mul "9e4d19bb5d94a47352e6f53f4116b2a71b16a1113dc789b26528ee19f46b72e0"
#define hex_fp_sqr "46dc2a5b8853234b341d9c57f9c4ca5709e95bbfef25356812e884e4f38cd0d6"
#define hex_fp_pow "5679a8f0a46ada5b9d48008cde0b8b7a233f882c08afe8f08a36a20ac845bb1a"
#define hex_fp_inv "7d404b0027a93e3fa8f8bc7ee367a96814c42a3b69feb1845093406948a34753"
int test_sm9_fp() {
sm9_fp_t x;
sm9_fp_t y;
sm9_fp_t r;
int j = 1;
sm9_bn_copy(x, SM9_P2->X[1]);
sm9_bn_copy(y, SM9_Ppubs->Y[0]);
sm9_fp_t iv = {0x87654321, 0x0fedcba9, 0x9abcdef0, 0x12345678, 0x87654321, 0x0fedcba9, 0x9abcdef0, 0x12345678};
sm9_bn_from_hex(r, hex_iv); if (sm9_bn_cmp(r, iv) != 0) goto err; ++j;
sm9_fp_add(r, x, y); if (!sm9_bn_equ_hex(r, hex_fp_add)) goto err; ++j;
sm9_fp_sub(r, x, y); if (!sm9_bn_equ_hex(r, hex_fp_sub)) goto err; ++j;
sm9_fp_sub(r, y, x); if (!sm9_bn_equ_hex(r, hex_fp_nsub)) goto err; ++j;
sm9_fp_dbl(r, x); if (!sm9_bn_equ_hex(r, hex_fp_dbl)) goto err; ++j;
sm9_fp_tri(r, x); if (!sm9_bn_equ_hex(r, hex_fp_tri)) goto err; ++j;
sm9_fp_div2(r, x); if (!sm9_bn_equ_hex(r, hex_fp_div2)) goto err; ++j;
sm9_fp_neg(r, x); if (!sm9_bn_equ_hex(r, hex_fp_neg)) goto err; ++j;
sm9_fp_mul(r, x, y); if (!sm9_bn_equ_hex(r, hex_fp_mul)) goto err; ++j;
sm9_fp_sqr(r, x); if (!sm9_bn_equ_hex(r, hex_fp_sqr)) goto err; ++j;
sm9_fp_pow(r, x, y); if (!sm9_bn_equ_hex(r, hex_fp_pow)) goto err; ++j;
sm9_fp_inv(r, x); if (!sm9_bn_equ_hex(r, hex_fp_inv)) goto err; ++j;
printf("%s() ok\n", __FUNCTION__);
return 1;
err:
printf("sm9 test %d failed\n", j);
error_print();
return -1;
}
int main(void) {
if (test_sm9_fp() != 1) goto err;
printf("%s all tests passed\n", __FILE__);
return 0;
err:
error_print();
return -1;
}