Skip to content

Commit 3c3b94a

Browse files
authored
Merge pull request #7424 from gabor-mezei-arm/7256_unit_tests_for_p192k1
Add unit tests for ecp_mod_p192k1()
2 parents 1b59e76 + cec5060 commit 3c3b94a

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed

library/ecp_curves.c

+8
Original file line numberDiff line numberDiff line change
@@ -4608,6 +4608,8 @@ static int ecp_mod_p448(mbedtls_mpi *);
46084608
#endif
46094609
#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
46104610
static int ecp_mod_p192k1(mbedtls_mpi *);
4611+
MBEDTLS_STATIC_TESTABLE
4612+
int mbedtls_ecp_mod_p192k1(mbedtls_mpi *);
46114613
#endif
46124614
#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
46134615
static int ecp_mod_p224k1(mbedtls_mpi *);
@@ -5604,6 +5606,12 @@ static inline int ecp_mod_koblitz(mbedtls_mpi *N, mbedtls_mpi_uint *Rp, size_t p
56045606
* with R = 2^32 + 2^12 + 2^8 + 2^7 + 2^6 + 2^3 + 1 = 0x0100001119
56055607
*/
56065608
static int ecp_mod_p192k1(mbedtls_mpi *N)
5609+
{
5610+
return mbedtls_ecp_mod_p192k1(N);
5611+
}
5612+
5613+
MBEDTLS_STATIC_TESTABLE
5614+
int mbedtls_ecp_mod_p192k1(mbedtls_mpi *N)
56075615
{
56085616
static mbedtls_mpi_uint Rp[] = {
56095617
MBEDTLS_BYTES_TO_T_UINT_8(0xC9, 0x11, 0x00, 0x00, 0x01, 0x00, 0x00,

library/ecp_invasive.h

+11
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,17 @@ int mbedtls_ecp_mod_p384_raw(mbedtls_mpi_uint *X, size_t X_limbs);
169169

170170
#endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
171171

172+
#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
173+
174+
/*
175+
* Fast quasi-reduction modulo p192k1 = 2^192 - R,
176+
* with R = 2^32 + 2^12 + 2^8 + 2^7 + 2^6 + 2^3 + 1 = 0x0100001119
177+
*/
178+
MBEDTLS_STATIC_TESTABLE
179+
int mbedtls_ecp_mod_p192k1(mbedtls_mpi *N);
180+
181+
#endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */
182+
172183
/** Initialise a modulus with hard-coded const curve data.
173184
*
174185
* \note The caller is responsible for the \p N modulus' memory.

scripts/mbedtls_dev/ecp.py

+64
Original file line numberDiff line numberDiff line change
@@ -488,3 +488,67 @@ def is_valid(self) -> bool:
488488
def arguments(self):
489489
args = super().arguments()
490490
return ["MBEDTLS_ECP_DP_SECP521R1"] + args
491+
492+
493+
class EcpP192K1Raw(bignum_common.ModOperationCommon,
494+
EcpTarget):
495+
"""Test cases for ECP P192K1 fast reduction."""
496+
symbol = "-"
497+
test_function = "ecp_mod_p192k1"
498+
test_name = "ecp_mod_p192k1"
499+
input_style = "fixed"
500+
arity = 1
501+
dependencies = ["MBEDTLS_ECP_DP_SECP192K1_ENABLED"]
502+
503+
moduli = ["fffffffffffffffffffffffffffffffffffffffeffffee37"] # type: List[str]
504+
505+
input_values = [
506+
"0", "1",
507+
508+
# Modulus - 1
509+
"fffffffffffffffffffffffffffffffffffffffeffffee36",
510+
511+
# Modulus + 1
512+
"fffffffffffffffffffffffffffffffffffffffeffffee38",
513+
514+
# 2^192 - 1
515+
"ffffffffffffffffffffffffffffffffffffffffffffffff",
516+
517+
# Maximum canonical P192K1 multiplication result
518+
("fffffffffffffffffffffffffffffffffffffffdffffdc6c"
519+
"0000000000000000000000000000000100002394013c7364"),
520+
521+
# First 8 number generated by random.getrandbits(384) - seed(2,2)
522+
("cf1822ffbc6887782b491044d5e341245c6e433715ba2bdd"
523+
"177219d30e7a269fd95bafc8f2a4d27bdcf4bb99f4bea973"),
524+
("ffed9235288bc781ae66267594c9c9500925e4749b575bd1"
525+
"3653f8dd9b1f282e4067c3584ee207f8da94e3e8ab73738f"),
526+
("ef8acd128b4f2fc15f3f57ebf30b94fa82523e86feac7eb7"
527+
"dc38f519b91751dacdbd47d364be8049a372db8f6e405d93"),
528+
("e8624fab5186ee32ee8d7ee9770348a05d300cb90706a045"
529+
"defc044a09325626e6b58de744ab6cce80877b6f71e1f6d2"),
530+
("2d3d854e061b90303b08c6e33c7295782d6c797f8f7d9b78"
531+
"2a1be9cd8697bbd0e2520e33e44c50556c71c4a66148a86f"),
532+
("fec3f6b32e8d4b8a8f54f8ceacaab39e83844b40ffa9b9f1"
533+
"5c14bc4a829e07b0829a48d422fe99a22c70501e533c9135"),
534+
("97eeab64ca2ce6bc5d3fd983c34c769fe89204e2e8168561"
535+
"867e5e15bc01bfce6a27e0dfcbf8754472154e76e4c11ab2"),
536+
("bd143fa9b714210c665d7435c1066932f4767f26294365b2"
537+
"721dea3bf63f23d0dbe53fcafb2147df5ca495fa5a91c89b"),
538+
539+
# Next 2 number generated by random.getrandbits(192)
540+
"47733e847d718d733ff98ff387c56473a7a83ee0761ebfd2",
541+
"cbd4d3e2d4dec9ef83f0be4e80371eb97f81375eecc1cb63"
542+
]
543+
544+
@property
545+
def arg_a(self) -> str:
546+
return super().format_arg('{:x}'.format(self.int_a)).zfill(2 * self.hex_digits)
547+
548+
def result(self) -> List[str]:
549+
result = self.int_a % self.int_n
550+
return [self.format_result(result)]
551+
552+
@property
553+
def is_valid(self) -> bool:
554+
return True

tests/suites/test_suite_ecp.function

+39
Original file line numberDiff line numberDiff line change
@@ -1355,6 +1355,45 @@ exit:
13551355
}
13561356
/* END_CASE */
13571357

1358+
/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS:MBEDTLS_ECP_DP_SECP192K1_ENABLED */
1359+
void ecp_mod_p192k1(char *input_N,
1360+
char *input_X,
1361+
char *result)
1362+
{
1363+
mbedtls_mpi X;
1364+
mbedtls_mpi N;
1365+
mbedtls_mpi res;
1366+
1367+
mbedtls_mpi_init(&X);
1368+
mbedtls_mpi_init(&N);
1369+
mbedtls_mpi_init(&res);
1370+
1371+
TEST_EQUAL(mbedtls_test_read_mpi(&X, input_X), 0);
1372+
TEST_EQUAL(mbedtls_test_read_mpi(&N, input_N), 0);
1373+
TEST_EQUAL(mbedtls_test_read_mpi(&res, result), 0);
1374+
1375+
TEST_ASSERT(mbedtls_mpi_core_uint_le_mpi(0, X.p, X.n));
1376+
TEST_ASSERT(mbedtls_mpi_core_uint_le_mpi(0, N.p, N.n));
1377+
TEST_ASSERT(mbedtls_mpi_core_uint_le_mpi(0, res.p, res.n));
1378+
1379+
size_t limbs = N.n;
1380+
size_t bytes = limbs * sizeof(mbedtls_mpi_uint);
1381+
1382+
TEST_EQUAL(X.n, 2 * limbs);
1383+
TEST_EQUAL(res.n, limbs);
1384+
1385+
TEST_EQUAL(mbedtls_ecp_mod_p192k1(&X), 0);
1386+
TEST_EQUAL(mbedtls_mpi_mod_mpi(&X, &X, &N), 0);
1387+
TEST_LE_U(mbedtls_mpi_core_bitlen(X.p, X.n), 192);
1388+
ASSERT_COMPARE(X.p, bytes, res.p, bytes);
1389+
1390+
exit:
1391+
mbedtls_mpi_free(&X);
1392+
mbedtls_mpi_free(&N);
1393+
mbedtls_mpi_free(&res);
1394+
}
1395+
/* END_CASE */
1396+
13581397
/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
13591398
void ecp_mod_setup(char *input_A, int id, int ctype, int iret)
13601399
{

0 commit comments

Comments
 (0)