-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add function to shuffle vector with a seed.
- Loading branch information
Showing
10 changed files
with
306 additions
and
9 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,65 @@ | ||
// Copyright 2024 The Cross-Media Measurement Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "any_sketch/crypto/shuffle.h" | ||
|
||
#include "absl/status/status.h" | ||
#include "common_cpp/macros/macros.h" | ||
#include "math/open_ssl_uniform_random_generator.h" | ||
|
||
namespace wfa::measurement::common::crypto { | ||
|
||
using math::CreatePrngFromSeed; | ||
using math::OpenSslUniformPseudorandomGenerator; | ||
using math::UniformPseudorandomGenerator; | ||
|
||
// Shuffles the vector data using Fisher-Yates approach. Let n be the size of | ||
// data, the Fisher-Yates shuffle is as below. | ||
// For i = (n-1) to 1: | ||
// Draws a random value in the range [0; i] | ||
// Swaps data[i] and data[j] | ||
absl::Status ShuffleWithSeed(std::vector<uint32_t>& data, | ||
const PrngSeed& seed) { | ||
// Does nothing if the input is empty or has size 1. | ||
if (data.size() <= 1) { | ||
return absl::OkStatus(); | ||
} | ||
|
||
// Initializes the pseudorandom generator using the provided seed. | ||
ASSIGN_OR_RETURN(std::unique_ptr<UniformPseudorandomGenerator> prng, | ||
CreatePrngFromSeed(seed)); | ||
|
||
// Samples random values. | ||
ASSIGN_OR_RETURN( | ||
std::vector<unsigned char> arr, | ||
prng->GeneratePseudorandomBytes(data.size() * sizeof(unsigned __int128))); | ||
|
||
unsigned __int128* rand = (unsigned __int128*)arr.data(); | ||
for (uint64_t i = data.size() - 1; i >= 1; i--) { | ||
// Ideally, to make sure that the sampled permutation is not biased, rand[i] | ||
// needs to be re-sampled if rand[i] >= 2^128 - (2^128 % (i+1)). However, | ||
// the probability that this happens with any i in [1; data.size() - 1] is | ||
// less than (data.size())^2/2^{128}, which is less than 2^{-40} for any | ||
// input vector of size less than 2^{43}. | ||
int index = rand[i] % (i + 1); | ||
// Swaps the element at current position with the one at position index. | ||
uint64_t temp = data[i]; | ||
data[i] = data[index]; | ||
data[index] = temp; | ||
} | ||
|
||
return absl::OkStatus(); | ||
} | ||
|
||
} // namespace wfa::measurement::common::crypto |
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,34 @@ | ||
// Copyright 2024 The Cross-Media Measurement Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef SRC_MAIN_CC_ANY_SKETCH_CRYPTO_SHUFFLE_H_ | ||
#define SRC_MAIN_CC_ANY_SKETCH_CRYPTO_SHUFFLE_H_ | ||
|
||
#include <string> | ||
#include <utility> | ||
|
||
#include "absl/status/status.h" | ||
#include "math/open_ssl_uniform_random_generator.h" | ||
#include "wfa/any_sketch/secret_share.pb.h" | ||
|
||
namespace wfa::measurement::common::crypto { | ||
|
||
using any_sketch::PrngSeed; | ||
using wfa::math::UniformPseudorandomGenerator; | ||
|
||
absl::Status ShuffleWithSeed(std::vector<uint32_t>& data, const PrngSeed& seed); | ||
|
||
} // namespace wfa::measurement::common::crypto | ||
|
||
#endif // SRC_MAIN_CC_ANY_SKETCH_CRYPTO_SHUFFLE_H_ |
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
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
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,93 @@ | ||
// Copyright 2024 The Cross-Media Measurement Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "any_sketch/crypto/shuffle.h" | ||
|
||
#include "absl/status/status.h" | ||
#include "absl/status/statusor.h" | ||
#include "common_cpp/testing/status_macros.h" | ||
#include "common_cpp/testing/status_matchers.h" | ||
#include "gtest/gtest.h" | ||
|
||
namespace wfa::any_sketch::crypto { | ||
namespace { | ||
|
||
using measurement::common::crypto::PrngSeed; | ||
using measurement::common::crypto::ShuffleWithSeed; | ||
using ::wfa::StatusIs; | ||
using ::wfa::math::kBytesPerAes256Iv; | ||
using ::wfa::math::kBytesPerAes256Key; | ||
|
||
TEST(ShuffleWithSeed, EmptyInputSucceeds) { | ||
PrngSeed seed; | ||
std::vector<uint32_t> data; | ||
absl::Status ret = ShuffleWithSeed(data, seed); | ||
ASSERT_EQ(ret, absl::OkStatus()); | ||
ASSERT_EQ(data.size(), 0); | ||
} | ||
|
||
TEST(ShuffleWithSeed, InputHasOneElementSucceeds) { | ||
PrngSeed seed; | ||
std::vector<uint32_t> data = {1}; | ||
absl::Status ret = ShuffleWithSeed(data, seed); | ||
ASSERT_EQ(ret, absl::OkStatus()); | ||
ASSERT_EQ(data.size(), 1); | ||
EXPECT_EQ(data[0], 1); | ||
} | ||
|
||
TEST(ShuffleWithSeed, InputSizeAtLeastTwoInvalidSeedFails) { | ||
PrngSeed seed; | ||
*seed.mutable_key() = std::string(kBytesPerAes256Key - 1, 'a'); | ||
*seed.mutable_iv() = std::string(kBytesPerAes256Iv, 'b'); | ||
|
||
std::vector<uint32_t> data = {1, 2}; | ||
absl::Status ret = ShuffleWithSeed(data, seed); | ||
EXPECT_THAT(ret, StatusIs(absl::StatusCode::kInvalidArgument, | ||
absl::Substitute( | ||
"The uniform pseudorandom generator key has " | ||
"length of $0 bytes but $1 bytes are required.", | ||
seed.key().size(), kBytesPerAes256Key))); | ||
} | ||
|
||
TEST(ShuffleWithSeed, ShufflingSucceeds) { | ||
PrngSeed seed; | ||
*seed.mutable_key() = std::string(kBytesPerAes256Key, 'a'); | ||
*seed.mutable_iv() = std::string(kBytesPerAes256Iv, 'b'); | ||
|
||
int kInputSize = 100; | ||
std::vector<uint32_t> data(kInputSize); | ||
|
||
for (int i = 0; i < kInputSize; i++) { | ||
data[i] = i; | ||
} | ||
std::vector<uint32_t> input = data; | ||
absl::Status ret = ShuffleWithSeed(data, seed); | ||
ASSERT_EQ(ret, absl::OkStatus()); | ||
ASSERT_EQ(data.size(), kInputSize); | ||
|
||
// Verifies that the output array is different from the input array. | ||
// With a random seed, there is a negligible chance of 1/(kInputsize!) that | ||
// the permutation does not modify the original array and causes this check to | ||
// fail. | ||
EXPECT_NE(input, data); | ||
|
||
// Verifies that the input array and the output array have the same elements. | ||
std::sort(data.begin(), data.end()); | ||
for (int i = 0; i < kInputSize; i++) { | ||
EXPECT_EQ(data[i], i); | ||
} | ||
} | ||
|
||
} // namespace | ||
} // namespace wfa::any_sketch::crypto |
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