summaryrefslogtreecommitdiff
path: root/gtests/freebl_gtest
diff options
context:
space:
mode:
authorKevin Jacobs <kjacobs@mozilla.com>2020-02-19 22:19:11 +0000
committerKevin Jacobs <kjacobs@mozilla.com>2020-02-19 22:19:11 +0000
commitc79f4e2cb4b04862bf3a11e07cea5c53f4af5eed (patch)
treed00f125606739310593e718a2c9b8d6fcebf561d /gtests/freebl_gtest
parentd6f70e0fdc9dbc9c956906ce2917a06c6ebb44d2 (diff)
downloadnss-hg-c79f4e2cb4b04862bf3a11e07cea5c53f4af5eed.tar.gz
Bug 1609751 - Additional tests for mp_comba r=mt
Verify that when clamping, the upper 4 bytes of an `mp_digit` is checked. Differential Revision: https://phabricator.services.mozilla.com/D58576
Diffstat (limited to 'gtests/freebl_gtest')
-rw-r--r--gtests/freebl_gtest/mpi_unittest.cc49
1 files changed, 49 insertions, 0 deletions
diff --git a/gtests/freebl_gtest/mpi_unittest.cc b/gtests/freebl_gtest/mpi_unittest.cc
index 56b7454dc..eabac6ed0 100644
--- a/gtests/freebl_gtest/mpi_unittest.cc
+++ b/gtests/freebl_gtest/mpi_unittest.cc
@@ -13,6 +13,7 @@
#include <mach/mach.h>
#endif
+#include "mplogic.h"
#include "mpi.h"
namespace nss_test {
@@ -224,6 +225,54 @@ TEST_F(MPITest, MpiFixlenOctetsTooSmall) {
}
}
+TEST_F(MPITest, MpiSqrMulClamp) {
+ mp_int a, r, expect;
+ MP_DIGITS(&a) = 0;
+ MP_DIGITS(&r) = 0;
+ MP_DIGITS(&expect) = 0;
+
+ // Comba32 result is 64 mp_digits. *=2 as this is an ascii representation.
+ std::string expect_str((64 * sizeof(mp_digit)) * 2, '0');
+
+ // Set second-highest bit (0x80...^2 == 0x4000...)
+ expect_str.replace(0, 1, "4", 1);
+
+ // Test 32, 16, 8, and 4-1 mp_digit values. 32-4 (powers of two) use the comba
+ // assembly implementation, if enabled and supported. 3-1 use non-comba.
+ int n_digits = 32;
+ while (n_digits > 0) {
+ ASSERT_EQ(MP_OKAY, mp_init(&r));
+ ASSERT_EQ(MP_OKAY, mp_init(&a));
+ ASSERT_EQ(MP_OKAY, mp_init(&expect));
+ ASSERT_EQ(MP_OKAY, mp_read_radix(&expect, expect_str.c_str(), 16));
+
+ ASSERT_EQ(MP_OKAY, mp_set_int(&a, 1));
+ ASSERT_EQ(MP_OKAY, mpl_lsh(&a, &a, (n_digits * sizeof(mp_digit) * 8) - 1));
+
+ ASSERT_EQ(MP_OKAY, mp_sqr(&a, &r));
+ EXPECT_EQ(MP_USED(&expect), MP_USED(&r));
+ EXPECT_EQ(0, mp_cmp(&r, &expect));
+ mp_clear(&r);
+
+ // Take the mul path...
+ ASSERT_EQ(MP_OKAY, mp_init(&r));
+ ASSERT_EQ(MP_OKAY, mp_mul(&a, &a, &r));
+ EXPECT_EQ(MP_USED(&expect), MP_USED(&r));
+ EXPECT_EQ(0, mp_cmp(&r, &expect));
+
+ mp_clear(&a);
+ mp_clear(&r);
+ mp_clear(&expect);
+
+ // Once we're down to 4, check non-powers of two.
+ int sub = n_digits > 4 ? n_digits / 2 : 1;
+ n_digits -= sub;
+
+ // "Shift right" the string (to avoid mutating |expect_str| with MPI).
+ expect_str.resize(expect_str.size() - 2 * 2 * sizeof(mp_digit) * sub);
+ }
+}
+
// This test is slow. Disable it by default so we can run these tests on CI.
class DISABLED_MPITest : public ::testing::Test {};