Armando Montanez | 47008e8 | 2020-08-04 11:04:45 -0700 | [diff] [blame] | 1 | // Copyright 2020 The Pigweed Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 | // use this file except in compliance with the License. You may obtain a copy of |
| 5 | // the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | // License for the specific language governing permissions and limitations under |
| 13 | // the License. |
| 14 | #include "pw_random/xor_shift.h" |
| 15 | |
| 16 | #include <cinttypes> |
| 17 | #include <cstddef> |
| 18 | #include <cstdint> |
| 19 | #include <cstdio> |
| 20 | |
| 21 | #include "gtest/gtest.h" |
| 22 | |
| 23 | namespace pw::random { |
| 24 | namespace { |
| 25 | |
| 26 | constexpr uint64_t seed1 = 5; |
| 27 | constexpr uint64_t result1[] = { |
| 28 | 0x423212e85fb37474u, |
| 29 | 0x96051f25a1aadc74u, |
| 30 | 0x8ac1f520f5595a79u, |
| 31 | 0x7587fe57095b7c11u, |
| 32 | }; |
| 33 | constexpr int result1_count = sizeof(result1) / sizeof(result1[0]); |
| 34 | |
| 35 | constexpr uint64_t seed2 = 0x21feabcd5fb37474u; |
| 36 | constexpr uint64_t result2[] = { |
| 37 | 0x568ea260a4f3e793u, |
| 38 | 0x5ea87d669ab04d36u, |
| 39 | 0x77a8675eec48ae8bu, |
| 40 | }; |
| 41 | constexpr int result2_count = sizeof(result2) / sizeof(result2[0]); |
| 42 | |
| 43 | TEST(XorShiftStarRng64, ValidateSeries1) { |
| 44 | XorShiftStarRng64 rng(seed1); |
| 45 | for (size_t i = 0; i < result1_count; ++i) { |
| 46 | uint64_t val = 0; |
Wyatt Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 47 | EXPECT_EQ(rng.GetInt(val).status(), OkStatus()); |
Armando Montanez | 47008e8 | 2020-08-04 11:04:45 -0700 | [diff] [blame] | 48 | EXPECT_EQ(val, result1[i]); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | TEST(XorShiftStarRng64, ValidateSeries2) { |
| 53 | XorShiftStarRng64 rng(seed2); |
| 54 | for (size_t i = 0; i < result2_count; ++i) { |
| 55 | uint64_t val = 0; |
Wyatt Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 56 | EXPECT_EQ(rng.GetInt(val).status(), OkStatus()); |
Armando Montanez | 47008e8 | 2020-08-04 11:04:45 -0700 | [diff] [blame] | 57 | EXPECT_EQ(val, result2[i]); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | TEST(XorShiftStarRng64, InjectEntropyBits) { |
| 62 | XorShiftStarRng64 rng(seed1); |
| 63 | uint64_t val = 0; |
| 64 | rng.InjectEntropyBits(0x1, 1); |
Wyatt Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 65 | EXPECT_EQ(rng.GetInt(val).status(), OkStatus()); |
Armando Montanez | 47008e8 | 2020-08-04 11:04:45 -0700 | [diff] [blame] | 66 | EXPECT_NE(val, result1[0]); |
| 67 | } |
| 68 | |
| 69 | // Ensure injecting the same entropy integer, but different bit counts causes |
| 70 | // the randomly generated number to differ. |
| 71 | TEST(XorShiftStarRng64, EntropyBitCount) { |
| 72 | XorShiftStarRng64 rng_1(seed1); |
| 73 | uint64_t first_val = 0; |
| 74 | rng_1.InjectEntropyBits(0x1, 1); |
Wyatt Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 75 | EXPECT_EQ(rng_1.GetInt(first_val).status(), OkStatus()); |
Armando Montanez | 47008e8 | 2020-08-04 11:04:45 -0700 | [diff] [blame] | 76 | |
| 77 | // Use the same starting seed. |
| 78 | XorShiftStarRng64 rng_2(seed1); |
| 79 | uint64_t second_val = 0; |
| 80 | // Use a different number of entropy bits. |
| 81 | rng_2.InjectEntropyBits(0x1, 2); |
Wyatt Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 82 | EXPECT_EQ(rng_2.GetInt(second_val).status(), OkStatus()); |
Armando Montanez | 47008e8 | 2020-08-04 11:04:45 -0700 | [diff] [blame] | 83 | |
| 84 | EXPECT_NE(first_val, second_val); |
| 85 | } |
| 86 | |
| 87 | // Ensure injecting the same integer bit-by-bit applies the same transformation |
| 88 | // as all in one call. This lets applications decide which is more convenient |
| 89 | // without worrying about algorithmic changes. |
| 90 | TEST(XorShiftStarRng64, IncrementalEntropy) { |
| 91 | XorShiftStarRng64 rng_1(seed1); |
| 92 | uint64_t first_val = 0; |
| 93 | rng_1.InjectEntropyBits(0x6, 3); |
Wyatt Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 94 | EXPECT_EQ(rng_1.GetInt(first_val).status(), OkStatus()); |
Armando Montanez | 47008e8 | 2020-08-04 11:04:45 -0700 | [diff] [blame] | 95 | |
| 96 | // Use the same starting seed. |
| 97 | XorShiftStarRng64 rng_2(seed1); |
| 98 | uint64_t second_val = 0; |
| 99 | // Use a different number of injection calls. 6 = 0b110 |
| 100 | rng_2.InjectEntropyBits(0x1, 1); |
| 101 | rng_2.InjectEntropyBits(0x1, 1); |
| 102 | rng_2.InjectEntropyBits(0x0, 1); |
Wyatt Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 103 | EXPECT_EQ(rng_2.GetInt(second_val).status(), OkStatus()); |
Armando Montanez | 47008e8 | 2020-08-04 11:04:45 -0700 | [diff] [blame] | 104 | |
| 105 | EXPECT_EQ(first_val, second_val); |
| 106 | } |
| 107 | |
| 108 | TEST(XorShiftStarRng64, InjectEntropy) { |
| 109 | XorShiftStarRng64 rng(seed1); |
| 110 | uint64_t val = 0; |
| 111 | constexpr std::array<const std::byte, 5> entropy{std::byte(0xaf), |
| 112 | std::byte(0x9b), |
| 113 | std::byte(0x33), |
| 114 | std::byte(0x17), |
| 115 | std::byte(0x02)}; |
| 116 | rng.InjectEntropy(entropy); |
Wyatt Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 117 | EXPECT_EQ(rng.GetInt(val).status(), OkStatus()); |
Armando Montanez | 47008e8 | 2020-08-04 11:04:45 -0700 | [diff] [blame] | 118 | EXPECT_NE(val, result1[0]); |
| 119 | } |
| 120 | |
| 121 | } // namespace |
| 122 | } // namespace pw::random |