Bug 1317924 - resurrect mpi tests, r=ttaubert
authorFranziskus Kiefer <franziskuskiefer@gmail.com>
Wed, 16 Nov 2016 09:18:23 +0100
changeset 12871 caeade90a0394e95b466dbfabe02195a7c40c49c
parent 12870 14ae7627120d8ce15614449edccc70935f20b143
child 12872 1801aa43ea5a85c7b833083127e3a9bb341e8fe5
push id1790
push userfranziskuskiefer@gmail.com
push dateWed, 16 Nov 2016 08:47:10 +0000
reviewersttaubert
bugs1317924
Bug 1317924 - resurrect mpi tests, r=ttaubert Differential Revision: https://nss-review.dev.mozaws.net/D68
cmd/mpitests/mpi-test.c
cmd/mpitests/mpitests.gyp
cmd/mpitests/test-info.c
gtests/freebl_gtest/freebl_gtest.gyp
gtests/freebl_gtest/mpi_unittest.cc
lib/freebl/freebl.gyp
lib/freebl/mpi/mpi-test.c
lib/freebl/mpi/mpi.c
lib/freebl/mpi/mpi.h
lib/freebl/mpi/test-arrays.txt
lib/freebl/mpi/test-info.c
nss.gyp
rename from lib/freebl/mpi/mpi-test.c
rename to cmd/mpitests/mpi-test.c
copy from gtests/util_gtest/util_gtest.gyp
copy to cmd/mpitests/mpitests.gyp
--- a/gtests/util_gtest/util_gtest.gyp
+++ b/cmd/mpitests/mpitests.gyp
@@ -1,34 +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',
-    '../common/gtest.gypi',
+    '../../cmd/platlibs.gypi'
   ],
   'targets': [
     {
-      'target_name': 'util_gtest',
+      'target_name': 'mpi_tests',
       'type': 'executable',
       'sources': [
-        'util_utf8_unittest.cc',
-        '<(DEPTH)/gtests/common/gtests.cc'
+        'mpi-test.c',
       ],
       'dependencies': [
-        '<(DEPTH)/exports.gyp:nss_exports',
-        '<(DEPTH)/gtests/google_test/google_test.gyp:gtest',
-        '<(DEPTH)/lib/util/util.gyp:nssutil',
+        '<(DEPTH)/lib/freebl/freebl.gyp:<(freebl_name)',
       ]
     }
   ],
   'target_defaults': {
     'include_dirs': [
-      '../../gtests/google_test/gtest/include',
-      '../../gtests/common',
-      '../../lib/util'
+      '<(DEPTH)/lib/freebl/mpi',
+      '<(DEPTH)/lib/util',
     ]
   },
   'variables': {
     'module': 'nss'
   }
 }
rename from lib/freebl/mpi/test-info.c
rename to cmd/mpitests/test-info.c
--- a/lib/freebl/mpi/test-info.c
+++ b/cmd/mpitests/test-info.c
@@ -3,17 +3,17 @@
  *
  *  Arbitrary precision integer arithmetic library
  *
  * 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/. */
 
 /* 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          */
     "exchange",          /* test exchange of mp-int structures            */
     "zero",              /* test zeroing of an mp-int                     */
     "set",               /* test setting an mp-int to a small constant    */
     "absolute-value",    /* test the absolute value function              */
     "negate",            /* test the arithmetic negation function         */
@@ -26,17 +26,16 @@ const char *g_names[] = {
     "square",            /* test full squaring function                   */
     "divide-digit",      /* test digit division                           */
     "divide-2",          /* test division by two                          */
     "divide-2d",         /* test division & remainder by 2^d              */
     "divide",            /* test full division                            */
     "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                         */
     "mod-subtract",      /* test modular subtraction                      */
     "mod-multiply",      /* test modular multiplication                   */
     "mod-square",        /* test modular squaring function                */
     "mod-expt",          /* test full modular exponentiation              */
     "mod-expt-digit",    /* test digit modular exponentiation             */
@@ -71,17 +70,16 @@ int test_mul(void);
 int test_sqr(void);
 int test_div_d(void);
 int test_div_2(void);
 int test_div_2d(void);
 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);
 int test_submod(void);
 int test_mulmod(void);
 int test_sqrmod(void);
 int test_exptmod(void);
 int test_exptmod_d(void);
@@ -100,17 +98,17 @@ int test_fermat(void);
 
 /* Table mapping index numbers to functions */
 int (*g_tests[])(void) = {
     test_list, test_copy, test_exch, test_zero,
     test_set, test_abs, test_neg, test_add_d,
     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,
     test_gcd, test_lcm, test_convert, test_raw,
     test_pprime, test_fermat
 };
 
 /* Table mapping index numbers to descriptions */
@@ -131,17 +129,16 @@ const char *g_descs[] = {
     "test full squaring function",
     "test digit division",
     "test division by two",
     "test division & remainder by 2^d",
     "test full division",
     "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",
     "test modular subtraction",
     "test modular multiplication",
     "test modular squaring function",
     "test full modular exponentiation",
     "test digit modular exponentiation",
copy from gtests/util_gtest/util_gtest.gyp
copy to gtests/freebl_gtest/freebl_gtest.gyp
--- a/gtests/util_gtest/util_gtest.gyp
+++ b/gtests/freebl_gtest/freebl_gtest.gyp
@@ -3,32 +3,35 @@
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 {
   'includes': [
     '../../coreconf/config.gypi',
     '../common/gtest.gypi',
   ],
   'targets': [
     {
-      'target_name': 'util_gtest',
+      'target_name': 'freebl_gtest',
       'type': 'executable',
       'sources': [
-        'util_utf8_unittest.cc',
+        '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',
-        '<(DEPTH)/lib/util/util.gyp:nssutil',
-      ]
+      ],
+      'defines': [
+        'CT_VERIF',
+      ],
     }
   ],
   'target_defaults': {
     'include_dirs': [
-      '../../gtests/google_test/gtest/include',
-      '../../gtests/common',
-      '../../lib/util'
+      '<(DEPTH)/gtests/google_test/gtest/include',
+      '<(DEPTH)/gtests/common',
+      '<(DEPTH)/lib/freebl/mpi',
     ]
   },
   'variables': {
     'module': 'nss'
   }
 }
new file mode 100644
--- /dev/null
+++ b/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
--- a/lib/freebl/freebl.gyp
+++ b/lib/freebl/freebl.gyp
@@ -98,16 +98,19 @@
             'nsslowhash.c',
             'stubs.c',
           ],
           'conditions': [
             [ 'test_build==1', {
               'dependencies': [
                 '<(DEPTH)/lib/util/util.gyp:nssutil3',
               ],
+              'defines': [
+                'CT_VERIF',
+              ],
             }],
             [ 'target_arch=="x64"', {
               'sources': [
                 'arcfour-amd64-gas.s',
                 'intel-aes.s',
                 'intel-gcm.s',
                 'mpi/mpi_amd64.c',
                 'mpi/mpi_amd64_gas.s',
--- a/lib/freebl/mpi/mpi.c
+++ b/lib/freebl/mpi/mpi.c
@@ -32,16 +32,20 @@
   log_r(n) = log_2(n) * log_r(2)
 
   This table, therefore, is a table of log_r(2) for 2 <= r <= 36,
   which are the output bases supported.
  */
 #include "logtab.h"
 #endif
 
+#ifdef CT_VERIF
+#include <valgrind/memcheck.h>
+#endif
+
 /* {{{ Constant strings */
 
 /* Constant strings returned by mp_strerror() */
 static const char *mp_err_string[] = {
     "unknown result code",     /* say what?            */
     "boolean true",            /* MP_OKAY, MP_YES      */
     "boolean false",           /* MP_NO                */
     "out of memory",           /* MP_MEM               */
@@ -77,16 +81,36 @@ mp_set_prec(mp_size prec)
         s_mp_defprec = MP_DEFPREC;
     else
         s_mp_defprec = prec;
 
 } /* end mp_set_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) */
 
 /*
   mp_init(mp)
 
   Initialize a new zero-valued mp_int.  Returns MP_OKAY if successful,
   MP_MEM if memory could not be allocated for the structure.
--- a/lib/freebl/mpi/mpi.h
+++ b/lib/freebl/mpi/mpi.h
@@ -7,16 +7,19 @@
  * 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/. */
 
 #ifndef _H_MPI_
 #define _H_MPI_
 
 #include "mpi-config.h"
 
+#include "seccomon.h"
+SEC_BEGIN_PROTOS
+
 #if MP_DEBUG
 #undef MP_IOFUNC
 #define MP_IOFUNC 1
 #endif
 
 #if MP_IOFUNC
 #include <stdio.h>
 #include <ctype.h>
@@ -268,17 +271,16 @@ void freebl_cpuid(unsigned long op, unsi
 
 #define MP_CHECKOK(x)          \
     if (MP_OKAY > (res = (x))) \
     goto CLEANUP
 #define MP_CHECKERR(x)         \
     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
 #define DIGIT_BIT MP_DIGIT_BIT
 #define DIGIT_FMT MP_DIGIT_FMT
 #define RADIX MP_RADIX
 #define MAX_RADIX MP_MAX_RADIX
 #define SIGN(MP) MP_SIGN(MP)
@@ -295,11 +297,17 @@ void freebl_cpuid(unsigned long op, unsi
         }               \
     }
 #elif MP_ARGCHK == 2
 #include <assert.h>
 #define ARGCHK(X, Y) assert(X)
 #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_ */
--- a/lib/freebl/mpi/test-arrays.txt
+++ b/lib/freebl/mpi/test-arrays.txt
@@ -28,17 +28,16 @@ multiply:test_mul:test full multiplicati
 square:test_sqr:test full squaring function
 divide-digit:test_div_d:test digit division
 divide-2:test_div_2:test division by two
 divide-2d:test_div_2d:test division & remainder by 2^d
 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
 mod-subtract:test_submod:test modular subtraction
 mod-multiply:test_mulmod:test modular multiplication
 mod-square:test_sqrmod:test modular squaring function
 mod-expt:test_exptmod:test full modular exponentiation
 mod-expt-digit:test_exptmod_d:test digit modular exponentiation
--- a/nss.gyp
+++ b/nss.gyp
@@ -190,16 +190,22 @@
                 'cmd/lowhashtest/lowhashtest.gyp:lowhashtest',
               ],
             }],
             [ 'disable_libpkix==0', {
               'dependencies': [
                 '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',
+              ],
+            }],
           ],
         },
       ],
     }],
     [ 'sign_libs==1', {
       'targets': [
         {
         'target_name': 'nss_sign_shared_libs',