mirror of
git://git.gniibe.org/gnuk/gnuk.git
synced 2026-11-20 19:11:31 +00:00
@@ -1,3 +1,11 @@
|
||||
2023-12-05 NIIBE Yutaka <[email protected]>
|
||||
|
||||
* src/bn.c (bn256_add_cond, bn256_sub_cond): New.
|
||||
* src/ecc-ed25519.c (mod_reduce_M): Use bn256_add_cond.
|
||||
(eddsa_sign_25519): Likewise.
|
||||
* src/modp256k1.c (modp256k1_reduce): Use bn256_set_cond.
|
||||
* src/ecc.c (ecdsa): Use bn256_set_cond and bn256_sub_cond.
|
||||
|
||||
2023-12-01 NIIBE Yutaka <[email protected]>
|
||||
|
||||
* src/bn.c (bn256_swap_cond): New.
|
||||
|
||||
@@ -457,3 +457,49 @@ bn256_set_cond (bn256 *A, const bn256 *B, uint32_t b)
|
||||
p++;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
bn256_add_cond (bn256 *A, const bn256 *B, uint32_t b)
|
||||
{
|
||||
uint32_t mask = 0UL - b;
|
||||
uint32_t *p = A->word;
|
||||
const uint32_t *q = B->word;
|
||||
uint32_t carry = 0;
|
||||
int i;
|
||||
uint32_t v;
|
||||
|
||||
for (i = 0; i < BN256_WORDS; i++)
|
||||
{
|
||||
v = *q & mask;
|
||||
*p += carry;
|
||||
carry = (*p < carry);
|
||||
*p += v;
|
||||
carry = (*p < v);
|
||||
p++;
|
||||
q++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
bn256_sub_cond (bn256 *A, const bn256 *B, uint32_t b)
|
||||
{
|
||||
uint32_t mask = 0UL - b;
|
||||
uint32_t *p = A->word;
|
||||
const uint32_t *q = B->word;
|
||||
uint32_t borrow = 0;
|
||||
int i;
|
||||
uint32_t v;
|
||||
|
||||
for (i = 0; i < BN256_WORDS; i++)
|
||||
{
|
||||
uint32_t borrow0 = (*p < borrow);
|
||||
|
||||
v = *q & mask;
|
||||
*p -= borrow;
|
||||
borrow = (*p < v) + borrow0;
|
||||
*p -= v;
|
||||
p++;
|
||||
q++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,3 +23,5 @@ int bn256_cmp (const bn256 *A, const bn256 *B);
|
||||
void bn256_random (bn256 *X);
|
||||
void bn256_swap_cond (bn256 *A, bn256 *B, uint32_t b);
|
||||
void bn256_set_cond (bn256 *A, const bn256 *B, uint32_t b);
|
||||
void bn256_add_cond (bn256 *A, const bn256 *B, uint32_t b);
|
||||
void bn256_sub_cond (bn256 *A, const bn256 *B, uint32_t b);
|
||||
|
||||
+6
-19
@@ -3,7 +3,7 @@
|
||||
* the twisted Edwards curve: -x^2 + y^2 = 1 + d*x^2*y^2
|
||||
* d = -121665/121666
|
||||
*
|
||||
* Copyright (C) 2014, 2017 Free Software Initiative of Japan
|
||||
* Copyright (C) 2014, 2017, 2023 Free Software Initiative of Japan
|
||||
* Author: NIIBE Yutaka <[email protected]>
|
||||
*
|
||||
* This file is a part of Gnuk, a GnuPG USB Token implementation.
|
||||
@@ -19,7 +19,7 @@
|
||||
* License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
@@ -625,10 +625,7 @@ mod_reduce_M (bn256 *R, const bn512 *A)
|
||||
r->word[7] &= 0x0fffffff;
|
||||
/* R -= R' */
|
||||
borrow = bn256_sub (R, R, r);
|
||||
if (borrow)
|
||||
bn256_add (R, R, M);
|
||||
else
|
||||
bn256_add ((bn256 *)tmp, R, M);
|
||||
bn256_add_cond (R, M, borrow);
|
||||
|
||||
/* Q_size: 5 */
|
||||
bnX_mul_C (tmp, q, 5); /* TMP = Q*C */
|
||||
@@ -640,10 +637,7 @@ mod_reduce_M (bn256 *R, const bn512 *A)
|
||||
/* R += R' */
|
||||
bn256_add (R, R, r);
|
||||
borrow = bn256_sub (R, R, M);
|
||||
if (borrow)
|
||||
bn256_add (R, R, M);
|
||||
else
|
||||
bn256_add ((bn256 *)tmp, R, M);
|
||||
bn256_add_cond (R, M, borrow);
|
||||
|
||||
/* Q_size: 1 */
|
||||
bnX_mul_C (tmp, q, 1); /* TMP = Q*C */
|
||||
@@ -652,10 +646,7 @@ mod_reduce_M (bn256 *R, const bn512 *A)
|
||||
memcpy (r, tmp, sizeof (uint32_t)*5);
|
||||
/* R -= R' */
|
||||
borrow = bn256_sub (R, R, r);
|
||||
if (borrow)
|
||||
bn256_add (R, R, M);
|
||||
else
|
||||
bn256_add ((bn256 *)tmp, R, M);
|
||||
bn256_add_cond (R, M, borrow);
|
||||
#undef borrow
|
||||
}
|
||||
|
||||
@@ -700,11 +691,7 @@ eddsa_sign_25519 (const uint8_t *input, size_t ilen, uint32_t *out,
|
||||
|
||||
memcpy (r, tmp, sizeof (bn256));
|
||||
|
||||
if ((borrow && !carry))
|
||||
bn256_add (s, s, M);
|
||||
else
|
||||
bn256_add (tmp, s, M);
|
||||
|
||||
bn256_add_cond (s, M, (borrow && (carry ^ 1)));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/* -*- coding: utf-8 -*-
|
||||
* ecc.c - Elliptic curve over GF(prime)
|
||||
*
|
||||
* Copyright (C) 2011, 2013, 2014, 2015
|
||||
* Copyright (C) 2011, 2013, 2014, 2015, 2023
|
||||
* Free Software Initiative of Japan
|
||||
* Author: NIIBE Yutaka <[email protected]>
|
||||
*
|
||||
@@ -18,7 +18,7 @@
|
||||
* License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
@@ -344,10 +344,7 @@ FUNC(ecdsa) (bn256 *r, bn256 *s, const bn256 *z, const bn256 *d)
|
||||
/* 1 <= k <= N - 1 */
|
||||
FUNC(compute_kG) (KG, k);
|
||||
borrow = bn256_sub (r, KG->x, N);
|
||||
if (borrow)
|
||||
memcpy (r, KG->x, sizeof (bn256));
|
||||
else
|
||||
memcpy (KG->x, r, sizeof (bn256));
|
||||
bn256_set_cond (r, KG->x, borrow);
|
||||
}
|
||||
while (bn256_is_zero (r));
|
||||
|
||||
@@ -355,10 +352,7 @@ FUNC(ecdsa) (bn256 *r, bn256 *s, const bn256 *z, const bn256 *d)
|
||||
bn256_mul (tmp, r, d);
|
||||
mod_reduce (s, tmp, N, MU_lower);
|
||||
carry = bn256_add (s, s, z);
|
||||
if (carry)
|
||||
bn256_sub (s, s, N);
|
||||
else
|
||||
bn256_sub ((bn256 *)tmp, s, N);
|
||||
bn256_sub_cond (s, N, carry);
|
||||
bn256_mul (tmp, s, k_inv);
|
||||
mod_reduce (s, tmp, N, MU_lower);
|
||||
}
|
||||
|
||||
+4
-6
@@ -1,7 +1,8 @@
|
||||
/*
|
||||
* modp256k1.c -- modulo arithmetic for p256k1
|
||||
*
|
||||
* Copyright (C) 2014, 2016, 2020 Free Software Initiative of Japan
|
||||
* Copyright (C) 2014, 2016, 2020, 2023
|
||||
* Free Software Initiative of Japan
|
||||
* Author: NIIBE Yutaka <[email protected]>
|
||||
*
|
||||
* This file is a part of Gnuk, a GnuPG USB Token implementation.
|
||||
@@ -17,7 +18,7 @@
|
||||
* License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
@@ -240,10 +241,7 @@ modp256k1_reduce (bn256 *X, const bn512 *A)
|
||||
modp256k1_add (W0, W0, S);
|
||||
|
||||
borrow = bn256_sub (tmp, W0, P256K1);
|
||||
if (borrow)
|
||||
memcpy (tmp, W0, sizeof (bn256));
|
||||
else
|
||||
memcpy (W0, tmp, sizeof (bn256));
|
||||
bn256_set_cond (W0, tmp, (borrow ^ 1));
|
||||
|
||||
#undef W0
|
||||
#undef W1
|
||||
|
||||
Reference in New Issue
Block a user