-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix potential overflow in Intp type (#197)
Summary: Pull Request resolved: #197 We use int32_t to store 32 bit signed integers. This might cause undesired behavior. To overcome this issue, we add some extra special treatment for these edge cases. Reviewed By: chualynn Differential Revision: D36106694 fbshipit-source-id: 978a021d46382857f59f43b454adeb4d7bdcbb40
- Loading branch information
1 parent
96966c1
commit ea4765c
Showing
2 changed files
with
88 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#include <gmock/gmock.h> | ||
#include <gtest/gtest.h> | ||
#include <memory> | ||
#include <random> | ||
|
||
#include "fbpcf/mpc_std_lib/util/util.h" | ||
|
||
namespace fbpcf::mpc_std_lib::util { | ||
|
||
TEST(IntpTypeTest, testAdd) { | ||
const int8_t width = 32; | ||
int64_t largestSigned = std::numeric_limits<int32_t>().max(); | ||
int64_t smallestSigned = std::numeric_limits<int32_t>().min(); | ||
std::random_device rd; | ||
std::mt19937_64 e(rd()); | ||
std::uniform_int_distribution<int32_t> dist(smallestSigned, largestSigned); | ||
for (int i = 0; i < 1000; i++) { | ||
auto v1 = dist(e); | ||
auto v2 = dist(e); | ||
int32_t v = Intp<true, width>(v1) + Intp<true, width>(v2); | ||
int32_t expectedV = (uint64_t)v1 + (uint64_t)v2; | ||
EXPECT_EQ(v, expectedV); | ||
} | ||
} | ||
|
||
TEST(IntpTypeTest, testSubtract) { | ||
const int8_t width = 32; | ||
int64_t largestSigned = std::numeric_limits<int32_t>().max(); | ||
int64_t smallestSigned = std::numeric_limits<int32_t>().min(); | ||
std::random_device rd; | ||
std::mt19937_64 e(rd()); | ||
std::uniform_int_distribution<int32_t> dist(smallestSigned, largestSigned); | ||
for (int i = 0; i < 1000; i++) { | ||
auto v1 = dist(e); | ||
auto v2 = dist(e); | ||
int32_t v = Intp<true, width>(v1) - Intp<true, width>(v2); | ||
int32_t expectedV = (uint64_t)v1 - (uint64_t)v2; | ||
EXPECT_EQ(v, expectedV); | ||
} | ||
} | ||
|
||
} // namespace fbpcf::mpc_std_lib::util |