bug fixes for secp256k1

This commit is contained in:
NIIBE Yutaka
2014-02-21 17:24:19 +09:00
parent 2accc339e2
commit ede98fbe7e
7 changed files with 26 additions and 3 deletions

View File

@@ -1,5 +1,15 @@
2014-02-21 Niibe Yutaka <gniibe@fsij.org> 2014-02-21 Niibe Yutaka <gniibe@fsij.org>
* src/ecc.c (compute_kG): Compute higer index at first.
(point_is_on_the_curve): Don't use coefficient_a if it's zero.
* src/jpc.c (jpc_double): Care coefficient A.
* src/ec_p256r1.c (COEFFICIENT_A_IS_MINUS_3): New.
* src/ec_p256k1.c (COEFFICIENT_A_IS_ZERO): New.
* src/jpc_p256r1.c (COEFFICIENT_A_IS_MINUS_3): Likewise.
* src/jpc_p256k1.c (COEFFICIENT_A_IS_MINUS_3): Likewise.
* src/modp256k1.c (modp256k1_shift): Bug fix. * src/modp256k1.c (modp256k1_shift): Bug fix.
2014-02-20 Niibe Yutaka <gniibe@fsij.org> 2014-02-20 Niibe Yutaka <gniibe@fsij.org>

View File

@@ -30,6 +30,7 @@
#include "ec_p256k1.h" #include "ec_p256k1.h"
#define FIELD p256k1 #define FIELD p256k1
#define COEFFICIENT_A_IS_ZERO 1
/* /*
* a = 0, b = 7 * a = 0, b = 7

View File

@@ -30,6 +30,7 @@
#include "ec_p256r1.h" #include "ec_p256r1.h"
#define FIELD p256r1 #define FIELD p256r1
#define COEFFICIENT_A_IS_MINUS_3 1
/* /*
* a = -3 mod p256r1 * a = -3 mod p256r1

View File

@@ -140,11 +140,10 @@ FUNC(compute_kG) (ac *X, const bn256 *K)
for (i = 31; i >= 0; i--) for (i = 31; i >= 0; i--)
{ {
FUNC(jpc_double) (Q, Q); FUNC(jpc_double) (Q, Q);
FUNC(jpc_add_ac_signed) (Q, Q, &precomputed_KG[index[i]&0x0f],
index[i] >> 7);
FUNC(jpc_add_ac_signed) (Q, Q, &precomputed_2E_KG[index[i+32]&0x0f], FUNC(jpc_add_ac_signed) (Q, Q, &precomputed_2E_KG[index[i+32]&0x0f],
index[i+32] >> 7); index[i+32] >> 7);
FUNC(jpc_add_ac_signed) (Q, Q, &precomputed_KG[index[i]&0x0f],
index[i] >> 7);
} }
dst = k_is_even ? Q : tmp; dst = k_is_even ? Q : tmp;
@@ -170,8 +169,10 @@ point_is_on_the_curve (const ac *P)
MFNC(sqr) (s, P->x); MFNC(sqr) (s, P->x);
MFNC(mul) (s, s, P->x); MFNC(mul) (s, s, P->x);
#ifdef COEFFICIENT_A_IS_ZERO
MFNC(mul) (t, coefficient_a, P->x); MFNC(mul) (t, coefficient_a, P->x);
MFNC(add) (s, s, t); MFNC(add) (s, s, t);
#endif
MFNC(add) (s, s, coefficient_b); MFNC(add) (s, s, coefficient_b);
MFNC(sqr) (t, P->y); MFNC(sqr) (t, P->y);

View File

@@ -47,12 +47,20 @@ FUNC(jpc_double) (jpc *X, const jpc *A)
MFNC(sqr) (b, b); MFNC(sqr) (b, b);
MFNC(shift) (b, b, 3); MFNC(shift) (b, b, 3);
#if defined(COEFFICIENT_A_IS_MINUS_3)
MFNC(sqr) (tmp0, A->z); MFNC(sqr) (tmp0, A->z);
MFNC(sub) (c, A->x, tmp0); MFNC(sub) (c, A->x, tmp0);
MFNC(add) (tmp0, tmp0, A->x); MFNC(add) (tmp0, tmp0, A->x);
MFNC(mul) (tmp0, tmp0, c); MFNC(mul) (tmp0, tmp0, c);
MFNC(shift) (c, tmp0, 1); MFNC(shift) (c, tmp0, 1);
MFNC(add) (c, c, tmp0); MFNC(add) (c, c, tmp0);
#elif defined (COEFFICIENT_A_IS_ZERO)
MFNC(sqr) (tmp0, A->x);
MFNC(shift) (c, tmp0, 1);
MFNC(add) (c, c, tmp0);
#else
#error "not supported."
#endif
MFNC(sqr) (d, c); MFNC(sqr) (d, c);
MFNC(shift) (tmp0, a, 1); MFNC(shift) (tmp0, a, 1);

View File

@@ -29,5 +29,6 @@
#define FIELD p256k1 #define FIELD p256k1
#define CONST_P256 P256K1 #define CONST_P256 P256K1
#define COEFFICIENT_A_IS_ZERO 1
#include "jpc.c" #include "jpc.c"

View File

@@ -29,5 +29,6 @@
#define FIELD p256r1 #define FIELD p256r1
#define CONST_P256 P256R1 #define CONST_P256 P256R1
#define COEFFICIENT_A_IS_MINUS_3 1
#include "jpc.c" #include "jpc.c"