modify-sm9

This commit is contained in:
Gorachya
2019-02-02 20:24:54 -08:00
parent 58b3927232
commit 54a4d1b9aa
15 changed files with 48 additions and 20 deletions

View File

@@ -2397,7 +2397,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)
{
// (p^4-p^2+1)/n will be directly used to finish the 3rd step with k unused here.
// (p^4-p^2+1)/n is k
int i, n;
fp12_t t;
fp12_t t0;
@@ -2427,7 +2427,7 @@ static int fast_final_expo(fp12_t r, const fp12_t a, const BIGNUM *k, const BIGN
if (!fp12_mul(t, t0, t, p, ctx)) { // t = t0 * t = a ^ (p^6-1) = a1
return 0;
}
// second step: a = a ^ (p^2+1)
if (!fp12_copy(t0, t)) { // t0 = t = a1
return 0;
@@ -2461,32 +2461,24 @@ static int fast_final_expo(fp12_t r, const fp12_t a, const BIGNUM *k, const BIGN
if (!fp12_mul(t, t0, t, p, ctx)) { // t = t0 * t = a ^ (p^2+1) = a2
return 0;
}
// third step: a = a ^ [(p^4-p^2+1)/n]
BIGNUM *x = BN_new();
BN_init(x);
// this is (p^4-p^2+1)/n
const char *power_p3 = "56016940484435473570363458812714626596371"
"56263396225483794771796879929232299116963"
"85989797265808925975765890463898744492959"
"90589989684454491684765426953541105430217"
"12895268418170653274635803649243300415902"
"97941432449745271567755349";
if (!BN_dec2bn(&x, power_p3)){
if (!fp12_copy(t0, t)) {
return 0;
}
n = BN_num_bits(x);
n = BN_num_bits(k);
for (i = n - 2; i >= 0; i--) {
if (!fp12_sqr(t, t, p, ctx)) {
return 0;
}
if (BN_is_bit_set(x, i)) {
if (!fp12_mul(t, t, a, p, ctx)) {
if (BN_is_bit_set(k, i)) {
if (!fp12_mul(t, t, t0, p, ctx)) {
return 0;
}
}
}
}
fp12_copy(r, t);
return 1;
}
@@ -2571,7 +2563,7 @@ static int rate(fp12_t f, const point_t *Q, const BIGNUM *xP, const BIGNUM *yP,
point_add(&T, &T, &Q2, p, ctx);
/* f = f^((p^12 - 1)/n) */
#ifndef SM9_FAST
#ifdef NOSM9_FAST
final_expo(f, f, k, p, ctx);
#else
fast_final_expo(f, f, k, p, ctx); // (p^6-1) * (p^2+1) * [(p^4-p^2+1)/n]
@@ -2610,8 +2602,11 @@ int rate_pairing(fp12_t r, const point_t *Q, const EC_POINT *P, BN_CTX *ctx)
group = EC_GROUP_new_by_curve_name(NID_sm9bn256v1);
p = SM9_get0_prime();
a = SM9_get0_loop_count();
#ifdef NOSM9_FAST
k = SM9_get0_final_exponent();
#else
k = SM9_get0_fast_final_exponent();
#endif
xP = BN_CTX_get(ctx);
yP = BN_CTX_get(ctx);