mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-06-13 16:33:50 +08:00
remove warnings
This commit is contained in:
@@ -56,7 +56,9 @@
|
||||
#include "sm9_lcl.h"
|
||||
|
||||
|
||||
static int fp2_init(fp2_t a, BN_CTX *ctx)
|
||||
#define NOSM9_FAST
|
||||
|
||||
int fp2_init(fp2_t a, BN_CTX *ctx)
|
||||
{
|
||||
a[0] = NULL;
|
||||
a[1] = NULL;
|
||||
@@ -72,7 +74,7 @@ static int fp2_init(fp2_t a, BN_CTX *ctx)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void fp2_cleanup(fp2_t a)
|
||||
void fp2_cleanup(fp2_t a)
|
||||
{
|
||||
BN_free(a[0]);
|
||||
BN_free(a[1]);
|
||||
@@ -80,7 +82,7 @@ static void fp2_cleanup(fp2_t a)
|
||||
a[1] = NULL;
|
||||
}
|
||||
|
||||
static void fp2_clear_cleanup(fp2_t a)
|
||||
void fp2_clear_cleanup(fp2_t a)
|
||||
{
|
||||
BN_clear_free(a[0]);
|
||||
BN_clear_free(a[1]);
|
||||
@@ -88,96 +90,93 @@ static void fp2_clear_cleanup(fp2_t a)
|
||||
a[1] = NULL;
|
||||
}
|
||||
|
||||
static int fp2_is_zero(const fp2_t a)
|
||||
int fp2_is_zero(const fp2_t a)
|
||||
{
|
||||
return BN_is_zero(a[0])
|
||||
&& BN_is_zero(a[1]);
|
||||
}
|
||||
|
||||
static int fp2_print(const fp2_t a)
|
||||
int fp2_print(const fp2_t a)
|
||||
{
|
||||
printf("%s\n", BN_bn2hex(a[0]));
|
||||
printf("%s\n", BN_bn2hex(a[1]));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int fp2_is_one(const fp2_t a)
|
||||
int fp2_is_one(const fp2_t a)
|
||||
{
|
||||
return BN_is_one(a[0])
|
||||
&& BN_is_zero(a[1]);
|
||||
}
|
||||
|
||||
static void fp2_set_zero(fp2_t r)
|
||||
void fp2_set_zero(fp2_t r)
|
||||
{
|
||||
BN_zero(r[0]);
|
||||
BN_zero(r[1]);
|
||||
}
|
||||
|
||||
static int fp2_set_one(fp2_t r)
|
||||
int fp2_set_one(fp2_t r)
|
||||
{
|
||||
BN_zero(r[1]);
|
||||
return BN_one(r[0]);
|
||||
}
|
||||
|
||||
static int fp2_copy(fp2_t r, const fp2_t a)
|
||||
int fp2_copy(fp2_t r, const fp2_t a)
|
||||
{
|
||||
return BN_copy(r[0], a[0])
|
||||
&& BN_copy(r[1], a[1]);
|
||||
}
|
||||
|
||||
static int fp2_set(fp2_t r, const BIGNUM *a0, const BIGNUM *a1)
|
||||
int fp2_set(fp2_t r, const BIGNUM *a0, const BIGNUM *a1)
|
||||
{
|
||||
return BN_copy(r[0], a0)
|
||||
&& BN_copy(r[1], a1);
|
||||
}
|
||||
|
||||
static int fp2_set_hex(fp2_t r, const char *str[2])
|
||||
int fp2_set_hex(fp2_t r, const char *str[2])
|
||||
{
|
||||
return BN_hex2bn(&r[0], str[0])
|
||||
&& BN_hex2bn(&r[1], str[1]);
|
||||
}
|
||||
|
||||
static int fp2_set_u(fp2_t r)
|
||||
int fp2_set_u(fp2_t r)
|
||||
{
|
||||
BN_zero(r[0]);
|
||||
return BN_one(r[1]);
|
||||
}
|
||||
|
||||
static int fp2_set_5u(fp2_t r)
|
||||
int fp2_set_5u(fp2_t r)
|
||||
{
|
||||
BN_zero(r[0]);
|
||||
return BN_set_word(r[1], 5);
|
||||
}
|
||||
|
||||
static int fp2_set_bn(fp2_t r, const BIGNUM *a)
|
||||
int fp2_set_bn(fp2_t r, const BIGNUM *a)
|
||||
{
|
||||
BN_zero(r[1]);
|
||||
return BN_copy(r[0], a) != NULL;
|
||||
}
|
||||
|
||||
static int fp2_set_word(fp2_t r, unsigned long a)
|
||||
int fp2_set_word(fp2_t r, unsigned long a)
|
||||
{
|
||||
BN_zero(r[1]);
|
||||
return BN_set_word(r[0], a);
|
||||
}
|
||||
|
||||
static int fp2_equ(const fp2_t a, const fp2_t b)
|
||||
int fp2_equ(const fp2_t a, const fp2_t b)
|
||||
{
|
||||
return !BN_cmp(a[0], b[0]) && !BN_cmp(a[1], b[1]);
|
||||
}
|
||||
|
||||
#if SM9_TEST
|
||||
static int fp2_equ_hex(const fp2_t a, const char *str[2], BN_CTX *ctx)
|
||||
int fp2_equ_hex(const fp2_t a, const char *str[2], BN_CTX *ctx)
|
||||
{
|
||||
fp2_t t;
|
||||
fp2_init(t, ctx);
|
||||
fp2_set_hex(t, str);
|
||||
return fp2_equ(a, t);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if SM9_TEST
|
||||
static int fp2_add_word(fp2_t r, const fp2_t a, unsigned long b, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp2_add_word(fp2_t r, const fp2_t a, unsigned long b, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
BIGNUM *w = NULL;
|
||||
if (!(w = BN_CTX_get(ctx))
|
||||
@@ -190,21 +189,20 @@ static int fp2_add_word(fp2_t r, const fp2_t a, unsigned long b, const BIGNUM *p
|
||||
BN_free(w);
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
static int fp2_add(fp2_t r, const fp2_t a, const fp2_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp2_add(fp2_t r, const fp2_t a, const fp2_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
return BN_mod_add(r[0], a[0], b[0], p, ctx)
|
||||
&& BN_mod_add(r[1], a[1], b[1], p, ctx);
|
||||
}
|
||||
|
||||
static int fp2_dbl(fp2_t r, const fp2_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp2_dbl(fp2_t r, const fp2_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
return BN_mod_add(r[0], a[0], a[0], p, ctx)
|
||||
&& BN_mod_add(r[1], a[1], a[1], p, ctx);
|
||||
}
|
||||
|
||||
static int fp2_tri(fp2_t r, const fp2_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp2_tri(fp2_t r, const fp2_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
fp2_t t;
|
||||
if (!fp2_init(t, ctx)
|
||||
@@ -217,19 +215,19 @@ static int fp2_tri(fp2_t r, const fp2_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int fp2_sub(fp2_t r, const fp2_t a, const fp2_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp2_sub(fp2_t r, const fp2_t a, const fp2_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
return BN_mod_sub(r[0], a[0], b[0], p, ctx)
|
||||
&& BN_mod_sub(r[1], a[1], b[1], p, ctx);
|
||||
}
|
||||
|
||||
static int fp2_neg(fp2_t r, const fp2_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp2_neg(fp2_t r, const fp2_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
return BN_mod_sub(r[0], p, a[0], p, ctx)
|
||||
&& BN_mod_sub(r[1], p, a[1], p, ctx);
|
||||
}
|
||||
|
||||
static int fp2_mul(fp2_t r, const fp2_t a, const fp2_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp2_mul(fp2_t r, const fp2_t a, const fp2_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
BIGNUM *t = NULL;
|
||||
BIGNUM *r0 = NULL;
|
||||
@@ -262,7 +260,7 @@ static int fp2_mul(fp2_t r, const fp2_t a, const fp2_t b, const BIGNUM *p, BN_CT
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int fp2_mul_u(fp2_t r, const fp2_t a, const fp2_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp2_mul_u(fp2_t r, const fp2_t a, const fp2_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
BIGNUM *r0 = NULL;
|
||||
BIGNUM *r1 = NULL;
|
||||
@@ -297,7 +295,7 @@ static int fp2_mul_u(fp2_t r, const fp2_t a, const fp2_t b, const BIGNUM *p, BN_
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int fp2_mul_num(fp2_t r, const fp2_t a, const BIGNUM *n, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp2_mul_num(fp2_t r, const fp2_t a, const BIGNUM *n, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
BIGNUM *r0 = NULL;
|
||||
BIGNUM *r1 = NULL;
|
||||
@@ -318,7 +316,7 @@ static int fp2_mul_num(fp2_t r, const fp2_t a, const BIGNUM *n, const BIGNUM *p,
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int fp2_sqr(fp2_t r, const fp2_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp2_sqr(fp2_t r, const fp2_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
BIGNUM *r0 = NULL;
|
||||
BIGNUM *r1 = NULL;
|
||||
@@ -348,7 +346,7 @@ static int fp2_sqr(fp2_t r, const fp2_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int fp2_sqr_u(fp2_t r, const fp2_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp2_sqr_u(fp2_t r, const fp2_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
BIGNUM *r0 = NULL;
|
||||
BIGNUM *r1 = NULL;
|
||||
@@ -381,7 +379,7 @@ static int fp2_sqr_u(fp2_t r, const fp2_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int fp2_inv(fp2_t r, const fp2_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp2_inv(fp2_t r, const fp2_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
if (BN_is_zero(a[0])) {
|
||||
/* r0 = 0 */
|
||||
@@ -430,15 +428,13 @@ static int fp2_inv(fp2_t r, const fp2_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
return 1;
|
||||
}
|
||||
|
||||
#if SM9_TEST
|
||||
static int fp2_div(fp2_t r, const fp2_t a, const fp2_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp2_div(fp2_t r, const fp2_t a, const fp2_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
return fp2_inv(r, b, p, ctx)
|
||||
&& fp2_mul(r, a, r, p, ctx);
|
||||
}
|
||||
#endif
|
||||
|
||||
static int fp2_to_bin(const fp2_t a, unsigned char to[64])
|
||||
int fp2_to_bin(const fp2_t a, unsigned char to[64])
|
||||
{
|
||||
memset(to, 0, 64);
|
||||
BN_bn2bin(a[1], to + 32 - BN_num_bytes(a[1]));
|
||||
@@ -446,14 +442,13 @@ static int fp2_to_bin(const fp2_t a, unsigned char to[64])
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int fp2_from_bin(fp2_t a, const unsigned char from[64])
|
||||
int fp2_from_bin(fp2_t a, const unsigned char from[64])
|
||||
{
|
||||
return BN_bin2bn(from, 32, a[1])
|
||||
&& BN_bin2bn(from + 32, 32, a[0]);
|
||||
}
|
||||
|
||||
#if SM9_TEST
|
||||
static int fp2_test(const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp2_test(const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
const char *_a[] = {
|
||||
"5f25ce2083fc970a6b9fdcd819fb1966d300af2afd58d480c59e02b320852183",
|
||||
@@ -568,9 +563,8 @@ static int fp2_test(const BIGNUM *p, BN_CTX *ctx)
|
||||
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
static int fp4_init(fp4_t a, BN_CTX *ctx)
|
||||
int fp4_init(fp4_t a, BN_CTX *ctx)
|
||||
{
|
||||
int r;
|
||||
r = fp2_init(a[0], ctx);
|
||||
@@ -582,21 +576,19 @@ static int fp4_init(fp4_t a, BN_CTX *ctx)
|
||||
return r;
|
||||
}
|
||||
|
||||
static void fp4_cleanup(fp4_t a)
|
||||
void fp4_cleanup(fp4_t a)
|
||||
{
|
||||
fp2_cleanup(a[0]);
|
||||
fp2_cleanup(a[1]);
|
||||
}
|
||||
|
||||
#if SM9_TEST
|
||||
static void fp4_clear_cleanup(fp4_t a)
|
||||
void fp4_clear_cleanup(fp4_t a)
|
||||
{
|
||||
fp2_clear_cleanup(a[0]);
|
||||
fp2_clear_cleanup(a[1]);
|
||||
}
|
||||
#endif
|
||||
|
||||
static int fp4_print(const fp4_t a)
|
||||
int fp4_print(const fp4_t a)
|
||||
{
|
||||
fp2_print(a[0]);
|
||||
fp2_print(a[1]);
|
||||
@@ -604,131 +596,129 @@ static int fp4_print(const fp4_t a)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int fp4_is_zero(const fp4_t a)
|
||||
int fp4_is_zero(const fp4_t a)
|
||||
{
|
||||
return fp2_is_zero(a[0])
|
||||
&& fp2_is_zero(a[1]);
|
||||
}
|
||||
|
||||
static int fp4_is_one(const fp4_t a)
|
||||
int fp4_is_one(const fp4_t a)
|
||||
{
|
||||
return fp2_is_one(a[0])
|
||||
&& fp2_is_zero(a[1]);
|
||||
}
|
||||
|
||||
static void fp4_set_zero(fp4_t r)
|
||||
void fp4_set_zero(fp4_t r)
|
||||
{
|
||||
fp2_set_zero(r[0]);
|
||||
fp2_set_zero(r[1]);
|
||||
}
|
||||
|
||||
static int fp4_set_one(fp4_t r)
|
||||
int fp4_set_one(fp4_t r)
|
||||
{
|
||||
fp2_set_zero(r[1]);
|
||||
return fp2_set_one(r[0]);
|
||||
}
|
||||
|
||||
static int fp4_set_bn(fp4_t r, const BIGNUM *a)
|
||||
int fp4_set_bn(fp4_t r, const BIGNUM *a)
|
||||
{
|
||||
fp2_set_zero(r[1]);
|
||||
return fp2_set_bn(r[0], a);
|
||||
}
|
||||
|
||||
static int fp4_set_word(fp4_t r, unsigned long a)
|
||||
int fp4_set_word(fp4_t r, unsigned long a)
|
||||
{
|
||||
fp2_set_zero(r[1]);
|
||||
return fp2_set_word(r[0], a);
|
||||
}
|
||||
|
||||
static int fp4_set_fp2(fp4_t r, const fp2_t a)
|
||||
int fp4_set_fp2(fp4_t r, const fp2_t a)
|
||||
{
|
||||
fp2_set_zero(r[1]);
|
||||
return fp2_copy(r[0], a);
|
||||
}
|
||||
|
||||
static int fp4_set(fp4_t r, const fp2_t a0, const fp2_t a1)
|
||||
int fp4_set(fp4_t r, const fp2_t a0, const fp2_t a1)
|
||||
{
|
||||
return fp2_copy(r[0], a0)
|
||||
&& fp2_copy(r[1], a1);
|
||||
}
|
||||
|
||||
static int fp4_set_hex(fp4_t r, const char *str[4])
|
||||
int fp4_set_hex(fp4_t r, const char *str[4])
|
||||
{
|
||||
return fp2_set_hex(r[0], str)
|
||||
&& fp2_set_hex(r[1], str+2);
|
||||
}
|
||||
|
||||
static int fp4_copy(fp4_t r, const fp4_t a)
|
||||
int fp4_copy(fp4_t r, const fp4_t a)
|
||||
{
|
||||
return fp2_copy(r[0], a[0])
|
||||
&& fp2_copy(r[1], a[1]);
|
||||
}
|
||||
|
||||
static int fp4_set_u(fp4_t r)
|
||||
int fp4_set_u(fp4_t r)
|
||||
{
|
||||
fp2_set_zero(r[1]);
|
||||
return fp2_set_u(r[0]);
|
||||
}
|
||||
|
||||
static int fp4_set_v(fp4_t r)
|
||||
int fp4_set_v(fp4_t r)
|
||||
{
|
||||
fp2_set_zero(r[0]);
|
||||
return fp2_set_one(r[1]);
|
||||
}
|
||||
|
||||
static int fp4_equ(const fp4_t a, const fp4_t b)
|
||||
int fp4_equ(const fp4_t a, const fp4_t b)
|
||||
{
|
||||
return fp2_equ(a[0], b[0])
|
||||
&& fp2_equ(a[1], b[1]);
|
||||
}
|
||||
|
||||
#if SM9_TEST
|
||||
static int fp4_equ_hex(const fp4_t a, const char *str[4], BN_CTX *ctx)
|
||||
int fp4_equ_hex(const fp4_t a, const char *str[4], BN_CTX *ctx)
|
||||
{
|
||||
fp4_t t;
|
||||
fp4_init(t, ctx);
|
||||
fp4_set_hex(t, str);
|
||||
return fp4_equ(a, t);
|
||||
}
|
||||
#endif
|
||||
|
||||
static int fp4_to_bin(const fp4_t a, unsigned char to[128])
|
||||
int fp4_to_bin(const fp4_t a, unsigned char to[128])
|
||||
{
|
||||
return fp2_to_bin(a[1], to)
|
||||
&& fp2_to_bin(a[0], to + 64);
|
||||
}
|
||||
|
||||
static int fp4_from_bin(fp4_t a, const unsigned char from[128])
|
||||
int fp4_from_bin(fp4_t a, const unsigned char from[128])
|
||||
{
|
||||
return fp2_from_bin(a[1], from)
|
||||
&& fp2_from_bin(a[0], from + 64);
|
||||
}
|
||||
|
||||
static int fp4_add(fp4_t r, const fp4_t a, const fp4_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp4_add(fp4_t r, const fp4_t a, const fp4_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
return fp2_add(r[0], a[0], b[0], p, ctx)
|
||||
&& fp2_add(r[1], a[1], b[1], p, ctx);
|
||||
}
|
||||
|
||||
static int fp4_dbl(fp4_t r, const fp4_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp4_dbl(fp4_t r, const fp4_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
return fp2_dbl(r[0], a[0], p, ctx)
|
||||
&& fp2_dbl(r[1], a[1], p, ctx);
|
||||
}
|
||||
|
||||
static int fp4_sub(fp4_t r, const fp4_t a, const fp4_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp4_sub(fp4_t r, const fp4_t a, const fp4_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
return fp2_sub(r[0], a[0], b[0], p, ctx)
|
||||
&& fp2_sub(r[1], a[1], b[1], p, ctx);
|
||||
}
|
||||
|
||||
static int fp4_neg(fp4_t r, const fp4_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp4_neg(fp4_t r, const fp4_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
return fp2_neg(r[0], a[0], p, ctx)
|
||||
&&fp2_neg(r[1], a[1], p, ctx);
|
||||
}
|
||||
|
||||
static int fp4_mul(fp4_t r, const fp4_t a, const fp4_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp4_mul(fp4_t r, const fp4_t a, const fp4_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
fp2_t r0, r1, t;
|
||||
|
||||
@@ -759,7 +749,7 @@ static int fp4_mul(fp4_t r, const fp4_t a, const fp4_t b, const BIGNUM *p, BN_CT
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int fp4_mul_v(fp4_t r, const fp4_t a, const fp4_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp4_mul_v(fp4_t r, const fp4_t a, const fp4_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
fp2_t r0, r1, t;
|
||||
fp2_init(r0, ctx);
|
||||
@@ -788,7 +778,7 @@ static int fp4_mul_v(fp4_t r, const fp4_t a, const fp4_t b, const BIGNUM *p, BN_
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int fp4_sqr(fp4_t r, const fp4_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp4_sqr(fp4_t r, const fp4_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
fp2_t r0, r1, t;
|
||||
fp2_init(r0, ctx);
|
||||
@@ -814,7 +804,7 @@ static int fp4_sqr(fp4_t r, const fp4_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int fp4_sqr_v(fp4_t r, const fp4_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp4_sqr_v(fp4_t r, const fp4_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
fp2_t r0, r1, t;
|
||||
fp2_init(r0, ctx);
|
||||
@@ -841,7 +831,7 @@ static int fp4_sqr_v(fp4_t r, const fp4_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int fp4_inv(fp4_t r, const fp4_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp4_inv(fp4_t r, const fp4_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
fp2_t r0, r1, k;
|
||||
fp2_init(r0, ctx);
|
||||
@@ -875,8 +865,7 @@ static int fp4_inv(fp4_t r, const fp4_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
return 1;
|
||||
}
|
||||
|
||||
#if SM9_TEST
|
||||
static int fp4_test(const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp4_test(const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
const char *_a[] = {
|
||||
"bec057c34cec656c05f236d9399cd00c64319632885d200f964e4591dd7ca77",
|
||||
@@ -1012,7 +1001,6 @@ static int fp4_test(const BIGNUM *p, BN_CTX *ctx)
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
int fp12_init(fp12_t a, BN_CTX *ctx)
|
||||
{
|
||||
@@ -1035,14 +1023,12 @@ void fp12_cleanup(fp12_t a)
|
||||
fp4_cleanup(a[2]);
|
||||
}
|
||||
|
||||
#if SM9_TEST
|
||||
static void fp12_clear_cleanup(fp12_t a)
|
||||
void fp12_clear_cleanup(fp12_t a)
|
||||
{
|
||||
fp4_clear_cleanup(a[0]);
|
||||
fp4_clear_cleanup(a[1]);
|
||||
fp4_clear_cleanup(a[2]);
|
||||
}
|
||||
#endif
|
||||
|
||||
int fp12_print(const fp12_t a)
|
||||
{
|
||||
@@ -1052,137 +1038,125 @@ int fp12_print(const fp12_t a)
|
||||
return 1;
|
||||
}
|
||||
|
||||
#if SM9_TEST
|
||||
static int fp12_is_zero(const fp12_t a)
|
||||
int fp12_is_zero(const fp12_t a)
|
||||
{
|
||||
return fp4_is_zero(a[0])
|
||||
&& fp4_is_zero(a[1])
|
||||
&& fp4_is_zero(a[2]);
|
||||
}
|
||||
|
||||
static int fp12_is_one(const fp12_t a)
|
||||
int fp12_is_one(const fp12_t a)
|
||||
{
|
||||
return fp4_is_one(a[0])
|
||||
&& fp4_is_zero(a[1])
|
||||
&& fp4_is_zero(a[2]);
|
||||
}
|
||||
|
||||
static void fp12_set_zero(fp12_t r)
|
||||
void fp12_set_zero(fp12_t r)
|
||||
{
|
||||
fp4_set_zero(r[0]);
|
||||
fp4_set_zero(r[1]);
|
||||
fp4_set_zero(r[2]);
|
||||
}
|
||||
#endif
|
||||
|
||||
static int fp12_set_one(fp12_t r)
|
||||
int fp12_set_one(fp12_t r)
|
||||
{
|
||||
fp4_set_zero(r[1]);
|
||||
fp4_set_zero(r[2]);
|
||||
return fp4_set_one(r[0]);
|
||||
}
|
||||
|
||||
static int fp12_copy(fp12_t r, const fp12_t a)
|
||||
int fp12_copy(fp12_t r, const fp12_t a)
|
||||
{
|
||||
return fp4_copy(r[0], a[0])
|
||||
&& fp4_copy(r[1], a[1])
|
||||
&& fp4_copy(r[2], a[2]);
|
||||
}
|
||||
|
||||
#if SM9_TEST
|
||||
static int fp12_set(fp12_t r, const fp4_t a0, const fp4_t a1, const fp4_t a2)
|
||||
int fp12_set(fp12_t r, const fp4_t a0, const fp4_t a1, const fp4_t a2)
|
||||
{
|
||||
return fp4_copy(r[0], a0)
|
||||
&& fp4_copy(r[1], a1)
|
||||
&& fp4_copy(r[2], a2);
|
||||
}
|
||||
#endif
|
||||
|
||||
static int fp12_set_hex(fp12_t r, const char *str[12])
|
||||
int fp12_set_hex(fp12_t r, const char *str[12])
|
||||
{
|
||||
return fp4_set_hex(r[0], str)
|
||||
&& fp4_set_hex(r[1], str + 4)
|
||||
&& fp4_set_hex(r[2], str + 8);
|
||||
}
|
||||
|
||||
#if SM9_TEST
|
||||
static int fp12_set_fp4(fp12_t r, const fp4_t a)
|
||||
int fp12_set_fp4(fp12_t r, const fp4_t a)
|
||||
{
|
||||
fp4_set_zero(r[1]);
|
||||
fp4_set_zero(r[2]);
|
||||
return fp4_copy(r[0], a);
|
||||
}
|
||||
#endif
|
||||
|
||||
static int fp12_set_fp2(fp12_t r, const fp2_t a)
|
||||
int fp12_set_fp2(fp12_t r, const fp2_t a)
|
||||
{
|
||||
fp4_set_zero(r[1]);
|
||||
fp4_set_zero(r[2]);
|
||||
return fp4_set_fp2(r[0], a);
|
||||
}
|
||||
|
||||
static int fp12_set_bn(fp12_t r, const BIGNUM *a)
|
||||
int fp12_set_bn(fp12_t r, const BIGNUM *a)
|
||||
{
|
||||
fp4_set_zero(r[1]);
|
||||
fp4_set_zero(r[2]);
|
||||
return fp4_set_bn(r[0], a);
|
||||
}
|
||||
|
||||
#if SM9_TEST
|
||||
static int fp12_set_word(fp12_t r, unsigned long a)
|
||||
int fp12_set_word(fp12_t r, unsigned long a)
|
||||
{
|
||||
fp4_set_zero(r[1]);
|
||||
fp4_set_zero(r[2]);
|
||||
return fp4_set_word(r[0], a);
|
||||
}
|
||||
|
||||
static int fp12_set_u(fp12_t r)
|
||||
int fp12_set_u(fp12_t r)
|
||||
{
|
||||
fp4_set_zero(r[1]);
|
||||
fp4_set_zero(r[2]);
|
||||
return fp4_set_u(r[0]);
|
||||
}
|
||||
#endif
|
||||
|
||||
static int fp12_set_v(fp12_t r)
|
||||
int fp12_set_v(fp12_t r)
|
||||
{
|
||||
fp4_set_zero(r[1]);
|
||||
fp4_set_zero(r[2]);
|
||||
return fp4_set_v(r[0]);
|
||||
}
|
||||
|
||||
#if SM9_TEST
|
||||
static int fp12_set_w(fp12_t r)
|
||||
int fp12_set_w(fp12_t r)
|
||||
{
|
||||
fp4_set_zero(r[0]);
|
||||
fp4_set_zero(r[2]);
|
||||
return fp4_set_one(r[1]);
|
||||
}
|
||||
#endif
|
||||
|
||||
static int fp12_set_w_sqr(fp12_t r)
|
||||
int fp12_set_w_sqr(fp12_t r)
|
||||
{
|
||||
fp4_set_zero(r[0]);
|
||||
fp4_set_zero(r[1]);
|
||||
return fp4_set_one(r[2]);
|
||||
}
|
||||
|
||||
static int fp12_equ(const fp12_t a, const fp12_t b)
|
||||
int fp12_equ(const fp12_t a, const fp12_t b)
|
||||
{
|
||||
return fp4_equ(a[0], b[0])
|
||||
&& fp4_equ(a[1], b[1])
|
||||
&& fp4_equ(a[2], b[2]);
|
||||
}
|
||||
|
||||
#if SM9_TEST
|
||||
static int fp12_equ_hex(const fp12_t a, const char *str[12], BN_CTX *ctx)
|
||||
int fp12_equ_hex(const fp12_t a, const char *str[12], BN_CTX *ctx)
|
||||
{
|
||||
fp12_t t;
|
||||
fp12_init(t, ctx);
|
||||
fp12_set_hex(t, str);
|
||||
return fp12_equ(a, t);
|
||||
}
|
||||
#endif
|
||||
|
||||
int fp12_to_bin(const fp12_t a, unsigned char to[384])
|
||||
{
|
||||
@@ -1191,28 +1165,28 @@ int fp12_to_bin(const fp12_t a, unsigned char to[384])
|
||||
&& fp4_to_bin(a[0], to + 256);
|
||||
}
|
||||
|
||||
static int fp12_from_bin(fp4_t a, const unsigned char from[384])
|
||||
int fp12_from_bin(fp4_t a, const unsigned char from[384])
|
||||
{
|
||||
return fp4_from_bin(&a[2], from)
|
||||
&& fp4_from_bin(&a[1], from + 128)
|
||||
&& fp4_from_bin(&a[0], from + 256);
|
||||
}
|
||||
|
||||
static int fp12_add(fp12_t r, const fp12_t a, const fp12_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp12_add(fp12_t r, const fp12_t a, const fp12_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
return fp4_add(r[0], a[0], b[0], p, ctx)
|
||||
&& fp4_add(r[1], a[1], b[1], p, ctx)
|
||||
&& fp4_add(r[2], a[2], b[2], p, ctx);
|
||||
}
|
||||
|
||||
static int fp12_dbl(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp12_dbl(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
return fp4_dbl(r[0], a[0], p, ctx)
|
||||
&& fp4_dbl(r[1], a[1], p, ctx)
|
||||
&& fp4_dbl(r[2], a[2], p, ctx);
|
||||
}
|
||||
|
||||
static int fp12_tri(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp12_tri(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
fp12_t t;
|
||||
fp12_init(t, ctx);
|
||||
@@ -1226,14 +1200,14 @@ static int fp12_tri(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int fp12_sub(fp12_t r, const fp12_t a, const fp12_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp12_sub(fp12_t r, const fp12_t a, const fp12_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
return fp4_sub(r[0], a[0], b[0], p, ctx)
|
||||
&& fp4_sub(r[1], a[1], b[1], p, ctx)
|
||||
&& fp4_sub(r[2], a[2], b[2], p, ctx);
|
||||
}
|
||||
|
||||
static int fp12_neg(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp12_neg(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
return fp4_neg(r[0], a[0], p, ctx)
|
||||
&& fp4_neg(r[1], a[1], p, ctx)
|
||||
@@ -1286,7 +1260,7 @@ int fp12_mul(fp12_t r, const fp12_t a, const fp12_t b, const BIGNUM *p, BN_CTX *
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int fp12_sqr(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp12_sqr(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
fp4_t r0, r1, r2, t;
|
||||
fp4_init(r0, ctx);
|
||||
@@ -1328,7 +1302,7 @@ static int fp12_sqr(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int fp12_inv(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp12_inv(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
if (fp4_is_zero(a[2])) {
|
||||
fp4_t k;
|
||||
@@ -1338,7 +1312,7 @@ static int fp12_inv(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
}
|
||||
|
||||
fp4_t r0, r1, r2;
|
||||
fp4_init(r0, ctx);
|
||||
fp4_init(r0, ctx); // FIXME: r0, r1, r2 never used
|
||||
fp4_init(r1, ctx);
|
||||
fp4_init(r2, ctx);
|
||||
|
||||
@@ -1433,7 +1407,7 @@ static int fp12_inv(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
}
|
||||
|
||||
//TODO: check this!
|
||||
static int fp12_div(fp12_t r, const fp12_t a, const fp12_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp12_div(fp12_t r, const fp12_t a, const fp12_t b, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
return fp12_inv(r, b, p, ctx)
|
||||
&& fp12_mul(r, a, r, p, ctx);
|
||||
@@ -1475,7 +1449,7 @@ int fp12_pow(fp12_t r, const fp12_t a, const BIGNUM *k, const BIGNUM *p, BN_CTX
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int fp12_fast_expo_p1(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp12_fast_expo_p1(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
return fp2_copy(r[0][0], a[0][0])
|
||||
&& fp2_neg (r[0][1], a[0][1], p, ctx)
|
||||
@@ -1485,7 +1459,7 @@ static int fp12_fast_expo_p1(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *
|
||||
&& fp2_neg (r[2][1], a[2][1], p, ctx);
|
||||
}
|
||||
|
||||
static int fp12_fast_expo_p2(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp12_fast_expo_p2(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
const BIGNUM *pw20;
|
||||
const BIGNUM *pw21;
|
||||
@@ -1508,8 +1482,7 @@ static int fp12_fast_expo_p2(fp12_t r, const fp12_t a, const BIGNUM *p, BN_CTX *
|
||||
return 1;
|
||||
}
|
||||
|
||||
#if SM9_TEST
|
||||
static int fp12_test(const BIGNUM *p, BN_CTX *ctx)
|
||||
int fp12_test(const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
const char *_a[] = {
|
||||
"3a4b2fdf33cfe01aab98d17aefc8d38b0508061c3117685839bd0dfdeb5783a8",
|
||||
@@ -1792,7 +1765,6 @@ static int fp12_test(const BIGNUM *p, BN_CTX *ctx)
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
int point_init(point_t *P, BN_CTX *ctx)
|
||||
{
|
||||
@@ -1873,7 +1845,7 @@ int point_set_affine_coordinates_hex(point_t *P, const char *str[4])
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int point_equ_hex(const point_t *P, const char *str[4], BN_CTX *ctx)
|
||||
int point_equ_hex(const point_t *P, const char *str[4], BN_CTX *ctx)
|
||||
{
|
||||
point_t T;
|
||||
point_init(&T, ctx);
|
||||
@@ -2226,8 +2198,7 @@ int point_mul_generator(point_t *R, const BIGNUM *k, const BIGNUM *p, BN_CTX *ct
|
||||
return point_mul(R, k, &G, p, ctx);
|
||||
}
|
||||
|
||||
#if SM9_TEST
|
||||
static int point_test(const BIGNUM *p, BN_CTX *ctx)
|
||||
int point_test(const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
const char *_G[] = {
|
||||
"3722755292130b08d2aab97fd34ec120ee265948d19c17abf9b7213baf82d65b",
|
||||
@@ -2327,9 +2298,8 @@ static int point_test(const BIGNUM *p, BN_CTX *ctx)
|
||||
//fp12_cleanup(y);
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
static int eval_tangent(fp12_t r, const point_t *T, const BIGNUM *xP, const BIGNUM *yP,
|
||||
int eval_tangent(fp12_t r, const point_t *T, const BIGNUM *xP, const BIGNUM *yP,
|
||||
const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
int ret;
|
||||
@@ -2376,7 +2346,7 @@ end:
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int eval_line(fp12_t r, const point_t *T, const point_t *Q,
|
||||
int eval_line(fp12_t r, const point_t *T, const point_t *Q,
|
||||
const BIGNUM *xP, const BIGNUM *yP,
|
||||
const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
@@ -2426,7 +2396,7 @@ end:
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int frobenius(point_t *R, const point_t *P, const BIGNUM *p, BN_CTX *ctx)
|
||||
int frobenius(point_t *R, const point_t *P, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
fp12_t x, y;
|
||||
|
||||
@@ -2447,7 +2417,7 @@ static int frobenius(point_t *R, const point_t *P, const BIGNUM *p, BN_CTX *ctx)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int frobenius_twice(point_t *R, const point_t *P, const BIGNUM *p, BN_CTX *ctx)
|
||||
int frobenius_twice(point_t *R, const point_t *P, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
frobenius(R, P, p, ctx);
|
||||
frobenius(R, R, p, ctx);
|
||||
@@ -2455,7 +2425,7 @@ static int frobenius_twice(point_t *R, const point_t *P, const BIGNUM *p, BN_CTX
|
||||
}
|
||||
|
||||
|
||||
static int final_expo(fp12_t r, const fp12_t a, const BIGNUM *k, const BIGNUM *p, BN_CTX *ctx)
|
||||
int final_expo(fp12_t r, const fp12_t a, const BIGNUM *k, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
int i, n;
|
||||
fp12_t t;
|
||||
@@ -2484,7 +2454,7 @@ static int final_expo(fp12_t r, const fp12_t a, const BIGNUM *k, const BIGNUM *p
|
||||
}
|
||||
|
||||
|
||||
static int fast_final_expo(fp12_t r, const fp12_t a, const BIGNUM *k, const BIGNUM *p, BN_CTX *ctx)
|
||||
int fast_final_expo(fp12_t r, const fp12_t a, const BIGNUM *k, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
int i, n;
|
||||
fp12_t t;
|
||||
@@ -2544,7 +2514,7 @@ static int fast_final_expo(fp12_t r, const fp12_t a, const BIGNUM *k, const BIGN
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int rate(fp12_t f, const point_t *Q, const BIGNUM *xP, const BIGNUM *yP,
|
||||
int rate(fp12_t f, const point_t *Q, const BIGNUM *xP, const BIGNUM *yP,
|
||||
const BIGNUM *a, const BIGNUM *k, const BIGNUM *p, BN_CTX *ctx)
|
||||
{
|
||||
int ret = 0;
|
||||
@@ -2638,7 +2608,7 @@ static int rate(fp12_t f, const point_t *Q, const BIGNUM *xP, const BIGNUM *yP,
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int params_test(void)
|
||||
int params_test(void)
|
||||
{
|
||||
const BIGNUM *p = SM9_get0_prime();
|
||||
const BIGNUM *a = SM9_get0_loop_count();
|
||||
@@ -2701,8 +2671,7 @@ int rate_pairing(fp12_t r, const point_t *Q, const EC_POINT *P, BN_CTX *ctx)
|
||||
return ret;
|
||||
}
|
||||
|
||||
#if SM9_TEST
|
||||
static int rate_test(void)
|
||||
int rate_test(void)
|
||||
{
|
||||
const char *Ppubs_str[] = {
|
||||
"29DBA116152D1F786CE843ED24A3B573414D2177386A92DD8F14D65696EA5E32",
|
||||
@@ -2742,16 +2711,14 @@ static int rate_test(void)
|
||||
rate_pairing(g, &Ppubs, P1, ctx);
|
||||
|
||||
ok = fp12_equ_hex(g, g_str, ctx);
|
||||
printf("rate %d: %s\n", __LINE__, ok ? "ok" : "error");
|
||||
|
||||
fp12_cleanup(g);
|
||||
point_cleanup(&Ppubs);
|
||||
EC_GROUP_free(group);
|
||||
BN_CTX_free(ctx);
|
||||
|
||||
return 1;
|
||||
return ok;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* for SM9 sign, the (xP, yP) is the fixed generator of E(Fp)
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user