amraboelkher | 04a46b4 | 2022-02-15 13:07:31 +0000 | [diff] [blame^] | 1 | diff --git a/BUILD.gn b/BUILD.gn |
| 2 | index 2f929064ff924..55152038d3c19 100644 |
| 3 | --- a/BUILD.gn |
| 4 | +++ b/BUILD.gn |
| 5 | @@ -106,7 +106,6 @@ if (is_chromeos_ash) { |
| 6 | "src/prng/chacha_prng_util.cc", |
| 7 | "src/prng/single_thread_chacha_prng.cc", |
| 8 | "src/relinearization_key.cc", |
| 9 | - "src/statusor.cc", |
| 10 | ] |
| 11 | public_deps = [ |
| 12 | ":serialization_proto", |
| 13 | @@ -163,7 +162,6 @@ if (is_chromeos_ash) { |
| 14 | "src/relinearization_key_test.cc", |
| 15 | "src/sample_error_test.cc", |
| 16 | "src/status_macros_test.cc", |
| 17 | - "src/statusor_test.cc", |
| 18 | "src/symmetric_encryption_test.cc", |
| 19 | "src/symmetric_encryption_with_prng_test.cc", |
| 20 | "src/testing/coefficient_polynomial_ciphertext_test.cc", |
| 21 | diff --git a/patches/0006-Quit-using-ValueOrDie.patch b/patches/0006-Quit-using-ValueOrDie.patch |
| 22 | new file mode 100644 |
| 23 | index 0000000000000..e69de29bb2d1d |
| 24 | diff --git a/src/montgomery_test.cc b/src/montgomery_test.cc |
| 25 | index 2d952ddfbdad4..97444fc6c15c2 100644 |
| 26 | --- a/src/montgomery_test.cc |
| 27 | +++ b/src/montgomery_test.cc |
| 28 | @@ -768,7 +768,7 @@ TYPED_TEST(MontgomeryTest, BatchOperations) { |
| 29 | std::vector<TypeParam> expected_add, expected_sub, expected_mul; |
| 30 | TypeParam scalar = |
| 31 | TypeParam::ImportRandom(prng.get(), modulus_params.get()) |
| 32 | - .ValueOrDie(); |
| 33 | + .value(); |
| 34 | auto scalar_constants_tuple = scalar.GetConstant(modulus_params.get()); |
| 35 | auto scalar_constant = std::get<0>(scalar_constants_tuple); |
| 36 | auto scalar_constant_barrett = std::get<1>(scalar_constants_tuple); |
| 37 | @@ -776,9 +776,9 @@ TYPED_TEST(MontgomeryTest, BatchOperations) { |
| 38 | expected_mul_scalar; |
| 39 | for (size_t i = 0; i < length; i++) { |
| 40 | a.push_back(TypeParam::ImportRandom(prng.get(), modulus_params.get()) |
| 41 | - .ValueOrDie()); |
| 42 | + .value()); |
| 43 | b.push_back(TypeParam::ImportRandom(prng.get(), modulus_params.get()) |
| 44 | - .ValueOrDie()); |
| 45 | + .value()); |
| 46 | auto constants_tuple = b[i].GetConstant(modulus_params.get()); |
| 47 | auto constant = std::get<0>(constants_tuple); |
| 48 | auto constant_barrett = std::get<1>(constants_tuple); |
| 49 | diff --git a/src/polynomial_test.cc b/src/polynomial_test.cc |
| 50 | index 6d5112e67c6b0..e246788d09bcc 100644 |
| 51 | --- a/src/polynomial_test.cc |
| 52 | +++ b/src/polynomial_test.cc |
| 53 | @@ -55,7 +55,7 @@ template <typename Prng> |
| 54 | class PolynomialTest : public ::testing::Test { |
| 55 | protected: |
| 56 | PolynomialTest() |
| 57 | - : params14_(uint_m::Params::Create(rlwe::kNewhopeModulus).ValueOrDie()), |
| 58 | + : params14_(uint_m::Params::Create(rlwe::kNewhopeModulus).value()), |
| 59 | zero_(uint_m::ImportZero(params14_.get())) {} |
| 60 | |
| 61 | void SetUp() override { srand(0); } |
| 62 | @@ -98,7 +98,7 @@ class PolynomialTest : public ::testing::Test { |
| 63 | } |
| 64 | |
| 65 | std::unique_ptr<Prng> MakePrng(absl::string_view seed) { |
| 66 | - auto prng = Prng::Create(seed.substr(0, Prng::SeedLength())).ValueOrDie(); |
| 67 | + auto prng = Prng::Create(seed.substr(0, Prng::SeedLength())).value(); |
| 68 | return prng; |
| 69 | } |
| 70 | |
| 71 | diff --git a/src/status_macros.h b/src/status_macros.h |
| 72 | index d297bfe0ed682..2622f6e544b8e 100644 |
| 73 | --- a/src/status_macros.h |
| 74 | +++ b/src/status_macros.h |
| 75 | @@ -34,7 +34,7 @@ |
| 76 | if (ABSL_PREDICT_FALSE(!statusor.ok())) { \ |
| 77 | return std::move(statusor).status(); \ |
| 78 | } \ |
| 79 | - lhs = std::move(statusor).ValueOrDie() |
| 80 | + lhs = std::move(statusor).value() |
| 81 | |
| 82 | // Internal helper for concatenating macro values. |
| 83 | #define RLWE_STATUS_MACROS_IMPL_CONCAT_INNER_(x, y) x##y |
| 84 | diff --git a/src/status_macros_test.cc b/src/status_macros_test.cc |
| 85 | index d43b55c9bb800..1f96d59e5d3b7 100644 |
| 86 | --- a/src/status_macros_test.cc |
| 87 | +++ b/src/status_macros_test.cc |
| 88 | @@ -27,8 +27,8 @@ namespace { |
| 89 | TEST(StatusMacrosTest, TestAssignOrReturn) { |
| 90 | StatusOr<StatusOr<int>> a(StatusOr<int>(2)); |
| 91 | auto f = [&]() -> absl::Status { |
| 92 | - RLWE_ASSIGN_OR_RETURN(StatusOr<int> status_or_a, a.ValueOrDie()); |
| 93 | - EXPECT_EQ(2, status_or_a.ValueOrDie()); |
| 94 | + RLWE_ASSIGN_OR_RETURN(StatusOr<int> status_or_a, a.value()); |
| 95 | + EXPECT_EQ(2, status_or_a.value()); |
| 96 | return absl::OkStatus(); |
| 97 | }; |
| 98 | auto status = f(); |
| 99 | diff --git a/src/statusor.h b/src/statusor.h |
| 100 | index b7ada09372c9f..2d3ecd810c49c 100644 |
| 101 | --- a/src/statusor.h |
| 102 | +++ b/src/statusor.h |
| 103 | @@ -17,239 +17,13 @@ |
| 104 | #ifndef RLWE_STATUSOR_H_ |
| 105 | #define RLWE_STATUSOR_H_ |
| 106 | |
| 107 | -#include <cassert> |
| 108 | - |
| 109 | -#include "absl/base/attributes.h" |
| 110 | -#include "absl/status/status.h" |
| 111 | -#include "absl/types/optional.h" |
| 112 | +#include "absl/status/statusor.h" |
| 113 | #include "third_party/shell-encryption/base/shell_encryption_export.h" |
| 114 | |
| 115 | namespace rlwe { |
| 116 | |
| 117 | template <typename T> |
| 118 | -class SHELL_ENCRYPTION_EXPORT StatusOr { |
| 119 | - public: |
| 120 | - // Construct a new StatusOr with Status::UNKNOWN status |
| 121 | - StatusOr(); |
| 122 | - |
| 123 | - // Construct a new StatusOr with the given non-ok status. After calling |
| 124 | - // this constructor, calls to value() will CHECK-fail. |
| 125 | - // |
| 126 | - // NOTE: Not explicit - we want to use StatusOr<T> as a return |
| 127 | - // value, so it is convenient and sensible to be able to do 'return |
| 128 | - // Status()' when the return type is StatusOr<T>. |
| 129 | - // |
| 130 | - // REQUIRES: status != Status::OK. This requirement is DCHECKed. |
| 131 | - // In optimized builds, passing Status::OK here will have the effect |
| 132 | - // of passing PosixErrorSpace::EINVAL as a fallback. |
| 133 | - StatusOr(const absl::Status& status); |
| 134 | - |
| 135 | - // Construct a new StatusOr with the given value. If T is a plain pointer, |
| 136 | - // value must not be NULL. After calling this constructor, calls to |
| 137 | - // value() will succeed, and calls to status() will return OK. |
| 138 | - // |
| 139 | - // NOTE: Not explicit - we want to use StatusOr<T> as a return type |
| 140 | - // so it is convenient and sensible to be able to do 'return T()' |
| 141 | - // when the return type is StatusOr<T>. |
| 142 | - // |
| 143 | - // REQUIRES: if T is a plain pointer, value != NULL. This requirement is |
| 144 | - // DCHECKed. In optimized builds, passing a NULL pointer here will have |
| 145 | - // the effect of passing absl::StatusCode::kInternal as a fallback. |
| 146 | - StatusOr(const T& value); |
| 147 | - |
| 148 | - // Copy constructor. |
| 149 | - StatusOr(const StatusOr& other); |
| 150 | - |
| 151 | - // Assignment operator. |
| 152 | - StatusOr& operator=(const StatusOr& other); |
| 153 | - |
| 154 | - // Move constructor and move-assignment operator. |
| 155 | - StatusOr(StatusOr&& other) = default; |
| 156 | - StatusOr& operator=(StatusOr&& other) = default; |
| 157 | - |
| 158 | - // Rvalue-reference overloads of the other constructors and assignment |
| 159 | - // operators, to support move-only types and avoid unnecessary copying. |
| 160 | - StatusOr(T&& value); |
| 161 | - |
| 162 | - // Returns a reference to our status. If this contains a T, then |
| 163 | - // returns Status::OK. |
| 164 | - const absl::Status& status() const; |
| 165 | - |
| 166 | - // Returns this->status().ok() |
| 167 | - bool ok() const; |
| 168 | - |
| 169 | - // Returns a reference to our current value, or CHECK-fails if !this->ok(). |
| 170 | - const T& ValueOrDie() const&; |
| 171 | - T& ValueOrDie() &; |
| 172 | - const T&& ValueOrDie() const&&; |
| 173 | - T&& ValueOrDie() &&; |
| 174 | - |
| 175 | - // Returns a reference to our current value, or CHECK-fails if !this->ok(). |
| 176 | - const T& value() const&; |
| 177 | - T& value() &; |
| 178 | - const T&& value() const&&; |
| 179 | - T&& value() &&; |
| 180 | - |
| 181 | - // Ignores any errors. This method does nothing except potentially suppress |
| 182 | - // complaints from any tools that are checking that errors are not dropped on |
| 183 | - // the floor. |
| 184 | - void IgnoreError() const {} |
| 185 | - |
| 186 | - operator absl::Status() const { return status(); } |
| 187 | - |
| 188 | - template <template <typename> class OtherStatusOrType> |
| 189 | - operator OtherStatusOrType<T>() { |
| 190 | - if (value_) { |
| 191 | - return OtherStatusOrType<T>(std::move(value_.value())); |
| 192 | - } else { |
| 193 | - return OtherStatusOrType<T>(status()); |
| 194 | - } |
| 195 | - } |
| 196 | - |
| 197 | - private: |
| 198 | - absl::Status status_; |
| 199 | - absl::optional<T> value_; |
| 200 | -}; |
| 201 | - |
| 202 | -namespace internal { |
| 203 | - |
| 204 | -class SHELL_ENCRYPTION_EXPORT StatusOrHelper { |
| 205 | - public: |
| 206 | - // Move type-agnostic error handling to the .cc. |
| 207 | - static SHELL_ENCRYPTION_EXPORT absl::Status HandleInvalidStatusCtorArg(); |
| 208 | - static SHELL_ENCRYPTION_EXPORT absl::Status HandleNullObjectCtorArg(); |
| 209 | - static SHELL_ENCRYPTION_EXPORT void Crash(const absl::Status& status); |
| 210 | - |
| 211 | - // Customized behavior for StatusOr<T> vs. StatusOr<T*> |
| 212 | - template <typename T> |
| 213 | - struct Specialize; |
| 214 | -}; |
| 215 | - |
| 216 | -template <typename T> |
| 217 | -struct SHELL_ENCRYPTION_EXPORT StatusOrHelper::Specialize { |
| 218 | - // For non-pointer T, a reference can never be NULL. |
| 219 | - static inline bool IsValueNull(const T& t) { return false; } |
| 220 | -}; |
| 221 | - |
| 222 | -template <typename T> |
| 223 | -struct SHELL_ENCRYPTION_EXPORT StatusOrHelper::Specialize<T*> { |
| 224 | - static inline bool IsValueNull(const T* t) { return t == nullptr; } |
| 225 | -}; |
| 226 | - |
| 227 | -} // namespace internal |
| 228 | - |
| 229 | -template <typename T> |
| 230 | -inline StatusOr<T>::StatusOr() |
| 231 | - : status_(absl::UnknownError("")), value_(absl::nullopt) {} |
| 232 | - |
| 233 | -template <typename T> |
| 234 | -inline StatusOr<T>::StatusOr(const absl::Status& status) |
| 235 | - : status_(status), value_(absl::nullopt) { |
| 236 | - if (status.ok()) { |
| 237 | - status_ = internal::StatusOrHelper::HandleInvalidStatusCtorArg(); |
| 238 | - } |
| 239 | -} |
| 240 | - |
| 241 | -template <typename T> |
| 242 | -inline StatusOr<T>::StatusOr(const T& value) |
| 243 | - : status_(absl::OkStatus()), value_(value) { |
| 244 | - if (internal::StatusOrHelper::Specialize<T>::IsValueNull(value)) { |
| 245 | - status_ = internal::StatusOrHelper::HandleNullObjectCtorArg(); |
| 246 | - } |
| 247 | -} |
| 248 | - |
| 249 | -template <typename T> |
| 250 | -inline StatusOr<T>::StatusOr(const StatusOr& other) |
| 251 | - : status_(other.status_), value_(other.value_) {} |
| 252 | - |
| 253 | -template <typename T> |
| 254 | -inline StatusOr<T>& StatusOr<T>::operator=(const StatusOr<T>& other) { |
| 255 | - status_ = other.status_; |
| 256 | - value_.reset(other.value_); |
| 257 | - return *this; |
| 258 | -} |
| 259 | - |
| 260 | -template <typename T> |
| 261 | -inline StatusOr<T>::StatusOr(T&& value) |
| 262 | - : status_(absl::OkStatus()), value_(std::forward<T>(value)) { |
| 263 | - if (internal::StatusOrHelper::Specialize<T>::IsValueNull(value_.value())) { |
| 264 | - status_ = internal::StatusOrHelper::HandleNullObjectCtorArg(); |
| 265 | - } |
| 266 | -} |
| 267 | - |
| 268 | -template <typename T> |
| 269 | -inline const absl::Status& StatusOr<T>::status() const { |
| 270 | - return status_; |
| 271 | -} |
| 272 | - |
| 273 | -template <typename T> |
| 274 | -inline bool StatusOr<T>::ok() const { |
| 275 | - return status_.ok(); |
| 276 | -} |
| 277 | - |
| 278 | -template <typename T> |
| 279 | -inline const T& StatusOr<T>::ValueOrDie() const& { |
| 280 | - if (!value_) { |
| 281 | - internal::StatusOrHelper::Crash(status()); |
| 282 | - } |
| 283 | - return value_.value(); |
| 284 | -} |
| 285 | - |
| 286 | -template <typename T> |
| 287 | -inline T& StatusOr<T>::ValueOrDie() & { |
| 288 | - if (!value_) { |
| 289 | - internal::StatusOrHelper::Crash(status()); |
| 290 | - } |
| 291 | - return value_.value(); |
| 292 | -} |
| 293 | - |
| 294 | -template <typename T> |
| 295 | -inline const T&& StatusOr<T>::ValueOrDie() const&& { |
| 296 | - if (!value_) { |
| 297 | - internal::StatusOrHelper::Crash(status()); |
| 298 | - } |
| 299 | - return std::move(value_.value()); |
| 300 | -} |
| 301 | - |
| 302 | -template <typename T> |
| 303 | -inline T&& StatusOr<T>::ValueOrDie() && { |
| 304 | - if (!value_) { |
| 305 | - internal::StatusOrHelper::Crash(status()); |
| 306 | - } |
| 307 | - return std::move(value_.value()); |
| 308 | -} |
| 309 | - |
| 310 | -template <typename T> |
| 311 | -inline const T& StatusOr<T>::value() const& { |
| 312 | - if (!value_) { |
| 313 | - internal::StatusOrHelper::Crash(status()); |
| 314 | - } |
| 315 | - return value_.value(); |
| 316 | -} |
| 317 | - |
| 318 | -template <typename T> |
| 319 | -inline T& StatusOr<T>::value() & { |
| 320 | - if (!value_) { |
| 321 | - internal::StatusOrHelper::Crash(status()); |
| 322 | - } |
| 323 | - return value_.value(); |
| 324 | -} |
| 325 | - |
| 326 | -template <typename T> |
| 327 | -inline const T&& StatusOr<T>::value() const&& { |
| 328 | - if (!value_) { |
| 329 | - internal::StatusOrHelper::Crash(status()); |
| 330 | - } |
| 331 | - return std::move(value_.value()); |
| 332 | -} |
| 333 | - |
| 334 | -template <typename T> |
| 335 | -inline T&& StatusOr<T>::value() && { |
| 336 | - if (!value_) { |
| 337 | - internal::StatusOrHelper::Crash(status()); |
| 338 | - } |
| 339 | - return std::move(value_.value()); |
| 340 | -} |
| 341 | +using StatusOr = absl::StatusOr<T>; |
| 342 | |
| 343 | } // namespace rlwe |
| 344 | |
| 345 | diff --git a/src/testing/coefficient_polynomial_test.cc b/src/testing/coefficient_polynomial_test.cc |
| 346 | index bf43ee6f4ae02..e2a36c3d7d6bf 100644 |
| 347 | --- a/src/testing/coefficient_polynomial_test.cc |
| 348 | +++ b/src/testing/coefficient_polynomial_test.cc |
| 349 | @@ -43,7 +43,7 @@ unsigned int seed = 0; |
| 350 | class PolynomialTest : public ::testing::Test { |
| 351 | protected: |
| 352 | PolynomialTest() |
| 353 | - : params14_(uint_m::Params::Create(rlwe::kNewhopeModulus).ValueOrDie()), |
| 354 | + : params14_(uint_m::Params::Create(rlwe::kNewhopeModulus).value()), |
| 355 | one_(uint_m::ImportOne(params14_.get())), |
| 356 | zero_(uint_m::ImportZero(params14_.get())) {} |
| 357 | |
| 358 | diff --git a/src/testing/status_testing.h b/src/testing/status_testing.h |
| 359 | index 27b8c0ed63073..6adc0278c98d2 100644 |
| 360 | --- a/src/testing/status_testing.h |
| 361 | +++ b/src/testing/status_testing.h |
| 362 | @@ -43,6 +43,6 @@ |
| 363 | #define RLWE_ASSERT_OK_AND_ASSIGN_IMPL_(statusor, lhs, rexpr) \ |
| 364 | auto statusor = (rexpr); \ |
| 365 | ASSERT_THAT(statusor.ok(), ::testing::Eq(true)); \ |
| 366 | - lhs = std::move(statusor).ValueOrDie() |
| 367 | + lhs = std::move(statusor).value() |
| 368 | |
| 369 | #endif // RLWE_TESTING_STATUS_TESTING_H_ |