blob: a6535842734056a667eda9f30c81bea3ea9fa907 [file] [log] [blame]
Armando Montanez47008e82020-08-04 11:04:45 -07001// 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
23namespace pw::random {
24namespace {
25
26constexpr uint64_t seed1 = 5;
27constexpr uint64_t result1[] = {
28 0x423212e85fb37474u,
29 0x96051f25a1aadc74u,
30 0x8ac1f520f5595a79u,
31 0x7587fe57095b7c11u,
32};
33constexpr int result1_count = sizeof(result1) / sizeof(result1[0]);
34
35constexpr uint64_t seed2 = 0x21feabcd5fb37474u;
36constexpr uint64_t result2[] = {
37 0x568ea260a4f3e793u,
38 0x5ea87d669ab04d36u,
39 0x77a8675eec48ae8bu,
40};
41constexpr int result2_count = sizeof(result2) / sizeof(result2[0]);
42
43TEST(XorShiftStarRng64, ValidateSeries1) {
44 XorShiftStarRng64 rng(seed1);
45 for (size_t i = 0; i < result1_count; ++i) {
46 uint64_t val = 0;
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -080047 EXPECT_EQ(rng.GetInt(val).status(), OkStatus());
Armando Montanez47008e82020-08-04 11:04:45 -070048 EXPECT_EQ(val, result1[i]);
49 }
50}
51
52TEST(XorShiftStarRng64, ValidateSeries2) {
53 XorShiftStarRng64 rng(seed2);
54 for (size_t i = 0; i < result2_count; ++i) {
55 uint64_t val = 0;
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -080056 EXPECT_EQ(rng.GetInt(val).status(), OkStatus());
Armando Montanez47008e82020-08-04 11:04:45 -070057 EXPECT_EQ(val, result2[i]);
58 }
59}
60
61TEST(XorShiftStarRng64, InjectEntropyBits) {
62 XorShiftStarRng64 rng(seed1);
63 uint64_t val = 0;
64 rng.InjectEntropyBits(0x1, 1);
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -080065 EXPECT_EQ(rng.GetInt(val).status(), OkStatus());
Armando Montanez47008e82020-08-04 11:04:45 -070066 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.
71TEST(XorShiftStarRng64, EntropyBitCount) {
72 XorShiftStarRng64 rng_1(seed1);
73 uint64_t first_val = 0;
74 rng_1.InjectEntropyBits(0x1, 1);
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -080075 EXPECT_EQ(rng_1.GetInt(first_val).status(), OkStatus());
Armando Montanez47008e82020-08-04 11:04:45 -070076
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 Hepler1b3da3a2021-01-07 13:26:57 -080082 EXPECT_EQ(rng_2.GetInt(second_val).status(), OkStatus());
Armando Montanez47008e82020-08-04 11:04:45 -070083
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.
90TEST(XorShiftStarRng64, IncrementalEntropy) {
91 XorShiftStarRng64 rng_1(seed1);
92 uint64_t first_val = 0;
93 rng_1.InjectEntropyBits(0x6, 3);
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -080094 EXPECT_EQ(rng_1.GetInt(first_val).status(), OkStatus());
Armando Montanez47008e82020-08-04 11:04:45 -070095
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 Hepler1b3da3a2021-01-07 13:26:57 -0800103 EXPECT_EQ(rng_2.GetInt(second_val).status(), OkStatus());
Armando Montanez47008e82020-08-04 11:04:45 -0700104
105 EXPECT_EQ(first_val, second_val);
106}
107
108TEST(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 Hepler1b3da3a2021-01-07 13:26:57 -0800117 EXPECT_EQ(rng.GetInt(val).status(), OkStatus());
Armando Montanez47008e82020-08-04 11:04:45 -0700118 EXPECT_NE(val, result1[0]);
119}
120
121} // namespace
122} // namespace pw::random