From e8b170e9ebc73ea4b8b823c82e215f9f500fa070 Mon Sep 17 00:00:00 2001 From: "zhu.junling" Date: Mon, 28 Aug 2023 16:47:10 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E6=8F=90=E5=8D=87SM2=E5=9F=BA=E7=82=B9=E6=A0=87=E9=87=8F?= =?UTF-8?q?=E4=B9=98=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sm2_alg.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 76 insertions(+), 2 deletions(-) diff --git a/src/sm2_alg.c b/src/sm2_alg.c index 4eb24fad4..ee724fa87 100644 --- a/src/sm2_alg.c +++ b/src/sm2_alg.c @@ -22,7 +22,11 @@ #define sm2_print_bn(label,a) sm2_bn_print(stderr,0,0,label,a) // 这个不应该放在这里,应该放在测试文件中 - +#define USE_SM2_GCACHE +#ifdef USE_SM2_GCACHE +static SM2_JACOBIAN_POINT SM2_JPOINT_GCACHE[32][256]; +static int SM2_GCACHE_INITED = 0; +#endif const SM2_BN SM2_P = { 0xffffffff, 0xffffffff, 0x00000000, 0xffffffff, @@ -1039,9 +1043,75 @@ void sm2_jacobian_point_from_bytes(SM2_JACOBIAN_POINT *P, const uint8_t in[64]) /* should we check if sm2_jacobian_point_is_on_curve */ } +#ifdef USE_SM2_GCACHE +void sm2_init_cache(SM2_JACOBIAN_POINT cache[][256], const SM2_JACOBIAN_POINT *G) +{ + int i, j, k, m; + SM2_JACOBIAN_POINT cache2[8], *P; + SM2_JACOBIAN_POINT _Q, *Q = &_Q; + SM2_JACOBIAN_POINT _T, *T = &_T; + sm2_jacobian_point_copy(T, G); + + for (i = 0; i < 32; i++) { + for (k = 0; k < 8; k++) { + sm2_jacobian_point_copy(cache2 + k, T); + sm2_jacobian_point_dbl(T, T); + } + for (j = 0; j < 256; j++) { + sm2_jacobian_point_set_infinity(Q); + for (m = j, k = 0; m && k < 8; k++) { + if (m & 0x1) { + P = cache2 + k; + if (sm2_jacobian_point_is_at_infinity(Q) && !sm2_bn_is_one(P->Z)) { + SM2_BN x, y; + sm2_jacobian_point_get_xy(P, x, y); + sm2_jacobian_point_set_xy(P, x, y); + } + sm2_jacobian_point_add(Q, Q, P); + } + m >>= 1; + } + if (!sm2_jacobian_point_is_at_infinity(Q) && !sm2_bn_is_one(Q->Z)) { + SM2_BN x, y; + sm2_jacobian_point_get_xy(Q, x, y); + sm2_jacobian_point_set_xy(Q, x, y); + } + sm2_jacobian_point_copy(&SM2_JPOINT_GCACHE[i][j], Q); + } + } +} + +void sm2_jacobian_point_fastmul_generator(SM2_JACOBIAN_POINT *R, const uint8_t k[32]) +{ + int i; + SM2_JACOBIAN_POINT *P; + SM2_JACOBIAN_POINT _Q, *Q = &_Q; + + // Init the cache, size: 32 * 256 + if (SM2_GCACHE_INITED == 0) { + sm2_init_cache(SM2_JPOINT_GCACHE, SM2_G); + SM2_GCACHE_INITED = 1; + } + + sm2_jacobian_point_set_infinity(Q); + for (i = 31; i >= 0; i--) { + P = SM2_JPOINT_GCACHE[31 - i] + k[i]; + sm2_jacobian_point_add(Q, Q, P); + } + + sm2_jacobian_point_copy(R, Q); +} +#endif + void sm2_jacobian_point_mul_generator(SM2_JACOBIAN_POINT *R, const SM2_BN k) { +#ifdef USE_SM2_GCACHE + uint8_t _k[32]; + sm2_bn_to_bytes(k, _k); + sm2_jacobian_point_fastmul_generator(R, _k); +#else sm2_jacobian_point_mul(R, k, SM2_G); +#endif } /* R = t * P + s * G */ @@ -1208,11 +1278,15 @@ int sm2_point_mul_generator(SM2_POINT *R, const uint8_t k[32]) SM2_BN _k; SM2_JACOBIAN_POINT _R; +#ifdef USE_SM2_GCACHE + sm2_jacobian_point_fastmul_generator(&_R, k); +#else sm2_bn_from_bytes(_k, k); sm2_jacobian_point_mul_generator(&_R, _k); + sm2_bn_clean(_k); +#endif sm2_jacobian_point_to_bytes(&_R, (uint8_t *)R); - sm2_bn_clean(_k); return 1; } From 3d04d3cc7158fb7e6ddbe0f24adb4c86c4bd4170 Mon Sep 17 00:00:00 2001 From: "zhu.junling" Date: Wed, 30 Aug 2023 11:04:24 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=B0=86SM2=E7=BC=93=E5=AD=98=E4=B8=AD?= =?UTF-8?q?=E9=97=B4=E5=80=8D=E4=B9=98=E7=BB=93=E6=9E=9C=E8=BD=AC=E6=8D=A2?= =?UTF-8?q?=E4=B8=BA=E4=BB=BF=E5=B0=84=E5=9D=90=E6=A0=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sm2_alg.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/sm2_alg.c b/src/sm2_alg.c index ee724fa87..978dca0a6 100644 --- a/src/sm2_alg.c +++ b/src/sm2_alg.c @@ -1056,17 +1056,17 @@ void sm2_init_cache(SM2_JACOBIAN_POINT cache[][256], const SM2_JACOBIAN_POINT *G for (k = 0; k < 8; k++) { sm2_jacobian_point_copy(cache2 + k, T); sm2_jacobian_point_dbl(T, T); + if (!sm2_bn_is_one(T->Z)) { + SM2_BN x, y; + sm2_jacobian_point_get_xy(T, x, y); + sm2_jacobian_point_set_xy(T, x, y); + } } for (j = 0; j < 256; j++) { sm2_jacobian_point_set_infinity(Q); for (m = j, k = 0; m && k < 8; k++) { if (m & 0x1) { P = cache2 + k; - if (sm2_jacobian_point_is_at_infinity(Q) && !sm2_bn_is_one(P->Z)) { - SM2_BN x, y; - sm2_jacobian_point_get_xy(P, x, y); - sm2_jacobian_point_set_xy(P, x, y); - } sm2_jacobian_point_add(Q, Q, P); } m >>= 1; From f6e9df37ba94af11c7602fe040e01942944a99a5 Mon Sep 17 00:00:00 2001 From: "zhu.junling" Date: Mon, 11 Sep 2023 15:38:37 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=B0=86=E7=BC=93=E5=AD=98=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=E5=B1=80=E9=83=A8=E9=9D=99=E6=80=81=E5=8F=98?= =?UTF-8?q?=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sm2_alg.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/sm2_alg.c b/src/sm2_alg.c index 978dca0a6..9579cfc03 100644 --- a/src/sm2_alg.c +++ b/src/sm2_alg.c @@ -21,12 +21,7 @@ #define sm2_print_bn(label,a) sm2_bn_print(stderr,0,0,label,a) // 这个不应该放在这里,应该放在测试文件中 - #define USE_SM2_GCACHE -#ifdef USE_SM2_GCACHE -static SM2_JACOBIAN_POINT SM2_JPOINT_GCACHE[32][256]; -static int SM2_GCACHE_INITED = 0; -#endif const SM2_BN SM2_P = { 0xffffffff, 0xffffffff, 0x00000000, 0xffffffff, @@ -1076,13 +1071,15 @@ void sm2_init_cache(SM2_JACOBIAN_POINT cache[][256], const SM2_JACOBIAN_POINT *G sm2_jacobian_point_get_xy(Q, x, y); sm2_jacobian_point_set_xy(Q, x, y); } - sm2_jacobian_point_copy(&SM2_JPOINT_GCACHE[i][j], Q); + sm2_jacobian_point_copy(&cache[i][j], Q); } } } void sm2_jacobian_point_fastmul_generator(SM2_JACOBIAN_POINT *R, const uint8_t k[32]) { + static SM2_JACOBIAN_POINT SM2_JPOINT_GCACHE[32][256]; + static int SM2_GCACHE_INITED = 0; int i; SM2_JACOBIAN_POINT *P; SM2_JACOBIAN_POINT _Q, *Q = &_Q;