Skip to content

Commit

Permalink
Bug 1317924 - resurrect mpi tests, r=ttaubert
Browse files Browse the repository at this point in the history
Differential Revision: https://nss-review.dev.mozaws.net/D68

--HG--
rename : lib/freebl/mpi/mpi-test.c => cmd/mpitests/mpi-test.c
rename : gtests/util_gtest/util_gtest.gyp => cmd/mpitests/mpitests.gyp
rename : lib/freebl/mpi/test-info.c => cmd/mpitests/test-info.c
rename : gtests/util_gtest/util_gtest.gyp => gtests/freebl_gtest/freebl_gtest.gyp
extra : rebase_source : 163b288b5fac99b04e7de8376628a5a6b0e3f1da
extra : amend_source : 5850535f6d2114601a4bed890315b130245f28c5
  • Loading branch information
franziskuskiefer committed Nov 16, 2016
1 parent d1a94bd commit 4c3da8d
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 8 deletions.
File renamed without changes.
30 changes: 30 additions & 0 deletions cmd/mpitests/mpitests.gyp
@@ -0,0 +1,30 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
{
'includes': [
'../../coreconf/config.gypi',
'../../cmd/platlibs.gypi'
],
'targets': [
{
'target_name': 'mpi_tests',
'type': 'executable',
'sources': [
'mpi-test.c',
],
'dependencies': [
'<(DEPTH)/lib/freebl/freebl.gyp:<(freebl_name)',
]
}
],
'target_defaults': {
'include_dirs': [
'<(DEPTH)/lib/freebl/mpi',
'<(DEPTH)/lib/util',
]
},
'variables': {
'module': 'nss'
}
}
7 changes: 2 additions & 5 deletions lib/freebl/mpi/test-info.c → cmd/mpitests/test-info.c
Expand Up @@ -8,7 +8,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/* Table mapping test suite names to index numbers */
const int g_count = 42;
const int g_count = 41;
const char *g_names[] = {
"list", /* print out a list of the available test suites */
"copy", /* test assignment of mp-int structures */
Expand All @@ -31,7 +31,6 @@ const char *g_names[] = {
"expt-digit", /* test digit exponentiation */
"expt", /* test full exponentiation */
"expt-2", /* test power-of-two exponentiation */
"square-root", /* test integer square root function */
"modulo-digit", /* test digit modular reduction */
"modulo", /* test full modular reduction */
"mod-add", /* test modular addition */
Expand Down Expand Up @@ -76,7 +75,6 @@ int test_div(void);
int test_expt_d(void);
int test_expt(void);
int test_2expt(void);
int test_sqrt(void);
int test_mod_d(void);
int test_mod(void);
int test_addmod(void);
Expand Down Expand Up @@ -105,7 +103,7 @@ int (*g_tests[])(void) = {
test_add, test_sub_d, test_sub, test_mul_d,
test_mul, test_sqr, test_div_d, test_div_2,
test_div_2d, test_div, test_expt_d, test_expt,
test_2expt, test_sqrt, test_mod_d, test_mod,
test_2expt, test_mod_d, test_mod,
test_addmod, test_submod, test_mulmod, test_sqrmod,
test_exptmod, test_exptmod_d, test_invmod, test_cmp_d,
test_cmp_z, test_cmp, test_cmp_mag, test_parity,
Expand Down Expand Up @@ -136,7 +134,6 @@ const char *g_descs[] = {
"test digit exponentiation",
"test full exponentiation",
"test power-of-two exponentiation",
"test integer square root function",
"test digit modular reduction",
"test full modular reduction",
"test modular addition",
Expand Down
37 changes: 37 additions & 0 deletions gtests/freebl_gtest/freebl_gtest.gyp
@@ -0,0 +1,37 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
{
'includes': [
'../../coreconf/config.gypi',
'../common/gtest.gypi',
],
'targets': [
{
'target_name': 'freebl_gtest',
'type': 'executable',
'sources': [
'mpi_unittest.cc',
'<(DEPTH)/gtests/common/gtests.cc'
],
'dependencies': [
'<(DEPTH)/exports.gyp:nss_exports',
'<(DEPTH)/lib/freebl/freebl.gyp:<(freebl_name)',
'<(DEPTH)/gtests/google_test/google_test.gyp:gtest',
],
'defines': [
'CT_VERIF',
],
}
],
'target_defaults': {
'include_dirs': [
'<(DEPTH)/gtests/google_test/gtest/include',
'<(DEPTH)/gtests/common',
'<(DEPTH)/lib/freebl/mpi',
]
},
'variables': {
'module': 'nss'
}
}
83 changes: 83 additions & 0 deletions gtests/freebl_gtest/mpi_unittest.cc
@@ -0,0 +1,83 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.

#include "secdert.h"
#include "secitem.h"
#include "secport.h"

#include "gtest/gtest.h"

#include <stdint.h>
#include <string.h>
#include <string>

#include "mpi.h"
namespace nss_test {

class MPITest : public ::testing::Test {
protected:
void TestCmp(const std::string a_string, const std::string b_string,
int result) {
mp_int a, b, c;
MP_DIGITS(&a) = 0;
MP_DIGITS(&b) = 0;
MP_DIGITS(&c) = 0;
ASSERT_EQ(MP_OKAY, mp_init(&a));
ASSERT_EQ(MP_OKAY, mp_init(&b));

mp_read_radix(&a, a_string.c_str(), 16);
mp_read_radix(&b, b_string.c_str(), 16);
EXPECT_EQ(result, mp_cmp(&a, &b));
}
};

TEST_F(MPITest, MpiCmp01Test) { TestCmp("0", "1", -1); }
TEST_F(MPITest, MpiCmp10Test) { TestCmp("1", "0", 1); }
TEST_F(MPITest, MpiCmp00Test) { TestCmp("0", "0", 0); }
TEST_F(MPITest, MpiCmp11Test) { TestCmp("1", "1", 0); }

TEST_F(MPITest, MpiCmpConstTest) {
mp_int a, b, c;
MP_DIGITS(&a) = 0;
MP_DIGITS(&b) = 0;
MP_DIGITS(&c) = 0;
ASSERT_EQ(MP_OKAY, mp_init(&a));
ASSERT_EQ(MP_OKAY, mp_init(&b));
ASSERT_EQ(MP_OKAY, mp_init(&c));

mp_read_radix(&a, const_cast<char *>("FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551"), 16);
mp_read_radix(&b, const_cast<char *>("FF0FFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551"), 16);
mp_read_radix(&c, const_cast<char *>("FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632550"), 16);

mp_taint(&b);
mp_taint(&c);

uint32_t runs = 5000000;
uint32_t time_b = 0, time_c = 0;
for (uint32_t i = 0; i < runs; ++i) {
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
int r = mp_cmp(&a, &b);
clock_gettime(CLOCK_MONOTONIC, &end);
unsigned long long used = end.tv_sec * 1000000000L + end.tv_nsec;
used -= (unsigned long long)start.tv_sec * 1000000000L + start.tv_nsec;
time_b += used;
ASSERT_EQ(1, r);
}
printf("time b: %u\n", time_b / runs);

for (uint32_t i = 0; i < runs; ++i) {
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
int r = mp_cmp(&a, &c);
clock_gettime(CLOCK_MONOTONIC, &end);
unsigned long long used = end.tv_sec * 1000000000L + end.tv_nsec;
used -= (unsigned long long)start.tv_sec * 1000000000L + start.tv_nsec;
time_c += used;
ASSERT_EQ(1, r);
}
printf("time c: %u\n", time_c / runs);
}

} // nss_test
3 changes: 3 additions & 0 deletions lib/freebl/freebl.gyp
Expand Up @@ -103,6 +103,9 @@
'dependencies': [
'<(DEPTH)/lib/util/util.gyp:nssutil3',
],
'defines': [
'CT_VERIF',
],
}],
[ 'target_arch=="x64"', {
'sources': [
Expand Down
24 changes: 24 additions & 0 deletions lib/freebl/mpi/mpi.c
Expand Up @@ -37,6 +37,10 @@
#include "logtab.h"
#endif

#ifdef CT_VERIF
#include <valgrind/memcheck.h>
#endif

/* {{{ Constant strings */

/* Constant strings returned by mp_strerror() */
Expand Down Expand Up @@ -82,6 +86,26 @@ mp_set_prec(mp_size prec)

/* }}} */

#ifdef CT_VERIF
void
mp_taint(mp_int *mp)
{
size_t i;
for (i = 0; i < mp->used; ++i) {
VALGRIND_MAKE_MEM_UNDEFINED(&(mp->dp[i]), sizeof(mp_digit));
}
}

void
mp_untaint(mp_int *mp)
{
size_t i;
for (i = 0; i < mp->used; ++i) {
VALGRIND_MAKE_MEM_DEFINED(&(mp->dp[i]), sizeof(mp_digit));
}
}
#endif

/*------------------------------------------------------------------------*/
/* {{{ mp_init(mp) */

Expand Down
12 changes: 10 additions & 2 deletions lib/freebl/mpi/mpi.h
Expand Up @@ -12,6 +12,9 @@

#include "mpi-config.h"

#include "seccomon.h"
SEC_BEGIN_PROTOS

#if MP_DEBUG
#undef MP_IOFUNC
#define MP_IOFUNC 1
Expand Down Expand Up @@ -273,7 +276,6 @@ void freebl_cpuid(unsigned long op, unsigned long *eax,
if (MP_OKAY > (res = (x))) \
goto CLEANUP

#if defined(MP_API_COMPATIBLE)
#define NEG MP_NEG
#define ZPOS MP_ZPOS
#define DIGIT_MAX MP_DIGIT_MAX
Expand All @@ -300,6 +302,12 @@ void freebl_cpuid(unsigned long op, unsigned long *eax,
#else
#define ARGCHK(X, Y) /* */
#endif
#endif /* defined MP_API_COMPATIBLE */

#ifdef CT_VERIF
void mp_taint(mp_int *mp);
void mp_untaint(mp_int *mp);
#endif

SEC_END_PROTOS

#endif /* end _H_MPI_ */
1 change: 0 additions & 1 deletion lib/freebl/mpi/test-arrays.txt
Expand Up @@ -33,7 +33,6 @@ divide:test_div:test full division
expt-digit:test_expt_d:test digit exponentiation
expt:test_expt:test full exponentiation
expt-2:test_2expt:test power-of-two exponentiation
square-root:test_sqrt:test integer square root function
modulo-digit:test_mod_d:test digit modular reduction
modulo:test_mod:test full modular reduction
mod-add:test_addmod:test modular addition
Expand Down
6 changes: 6 additions & 0 deletions nss.gyp
Expand Up @@ -195,6 +195,12 @@
'cmd/pkix-errcodes/pkix-errcodes.gyp:pkix-errcodes',
],
}],
[ 'test_build==1', {
'dependencies': [
'cmd/mpitests/mpitests.gyp:mpi_tests',
'gtests/freebl_gtest/freebl_gtest.gyp:freebl_gtest',
],
}],
],
},
],
Expand Down

0 comments on commit 4c3da8d

Please sign in to comment.