Skip to content

Commit

Permalink
feat: version 0.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
PETAce committed Jan 23, 2025
1 parent d08e3d7 commit 069ae98
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@
### Features

- Added a GMP-based generic implementation of the Paillier cryptosystem.

## Version 0.4.0

- No update.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ if(NOT CMAKE_BUILD_TYPE)
endif()
message(STATUS "Build type (CMAKE_BUILD_TYPE): ${CMAKE_BUILD_TYPE}")

project(SOLO VERSION 0.3.0 LANGUAGES CXX C)
project(SOLO VERSION 0.4.0 LANGUAGES CXX C)

########################
# Global configuration #
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ This project is licensed under the [Apache-2.0 License](LICENSE).

To cite PETAce in academic papers, please use the following BibTeX entries.

### Version 0.3.0
### Version 0.4.0

```tex
@misc{petace,
title = {PETAce (release 0.3.0)},
title = {PETAce (release 0.4.0)},
howpublished = {\url{https://github.com/tiktok-privacy-innovation/PETAce}},
month = Jun,
year = 2024,
month = Jan,
year = 2025,
note = {TikTok Pte. Ltd.},
key = {PETAce}
}
Expand Down
4 changes: 2 additions & 2 deletions bench/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@

cmake_minimum_required(VERSION 3.14)

project(SOLOBench VERSION 0.3.0 LANGUAGES CXX)
project(SOLOBench VERSION 0.4.0 LANGUAGES CXX)

# If not called from root CMakeLists.txt
if(NOT DEFINED SOLO_BUILD_BENCH)
set(SOLO_BUILD_BENCH ON)

find_package(PETAce-Solo 0.3.0 EXACT REQUIRED)
find_package(PETAce-Solo 0.4.0 EXACT REQUIRED)

# Must define these variables and include macros
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib)
Expand Down
4 changes: 2 additions & 2 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@

cmake_minimum_required(VERSION 3.14)

project(SOLOExample VERSION 0.3.0 LANGUAGES CXX)
project(SOLOExample VERSION 0.4.0 LANGUAGES CXX)

# If not called from root CMakeLists.txt
if(NOT DEFINED SOLO_BUILD_EXAMPLE)
set(SOLO_BUILD_EXAMPLE ON)

# Import PETAce-Solo
find_package(PETAce-Solo 0.3.0 EXACT REQUIRED)
find_package(PETAce-Solo 0.4.0 EXACT REQUIRED)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin)
endif()
Expand Down
4 changes: 2 additions & 2 deletions src/solo/ahe_paillier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ namespace petace {
namespace solo {
namespace ahepaillier {

constexpr std::size_t kAHEMaxRandomBits = 8192;
constexpr std::size_t kAHEMaxRandomByteCount = 1024;
constexpr std::size_t kAHEMaxRandomBits = 16384;
constexpr std::size_t kAHEMaxRandomByteCount = 2048;

PublicKey::PublicKey(std::size_t key_length, bool enable_djn)
: key_length_(key_length), n_byte_count_((key_length_ + 7) / 8), enable_djn_(enable_djn), pk_set_(false) {
Expand Down
29 changes: 29 additions & 0 deletions src/solo/ec_openssl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,5 +427,34 @@ void ECOpenSSL::point_from_bytes(const Byte* in, std::size_t in_byte_count, Poin
}
}

void ECOpenSSL::secret_key_from_bytes(const Byte* in, std::size_t in_byte_count, SecretKey& out) const {
if (in_byte_count == 0) {
throw std::invalid_argument("in_byte_count is zero");
}
if (in == nullptr && in_byte_count != 0) {
throw std::invalid_argument("in is nullptr");
}
if (out.data() == nullptr) {
throw std::invalid_argument("out is nullptr");
}
if (BN_bin2bn(reinterpret_cast<const unsigned char*>(in), static_cast<int>(in_byte_count), out.data()) == nullptr) {
throw std::runtime_error("openssl error: " + std::to_string(ERR_get_error()));
}
}

std::size_t ECOpenSSL::secret_key_to_bytes(const SecretKey& in, std::size_t out_byte_count, Byte* out) const {
if (in.data() == nullptr) {
throw std::invalid_argument("in is nullptr");
}
if (out == nullptr) {
return static_cast<std::size_t>(BN_num_bytes(in.data()));
}
std::size_t res = static_cast<std::size_t>(BN_bn2bin(in.data(), reinterpret_cast<unsigned char*>(out)));
if (res > out_byte_count) {
throw std::runtime_error("the buffer is not large enough.");
}
return res;
}

} // namespace solo
} // namespace petace
23 changes: 23 additions & 0 deletions src/solo/ec_openssl.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,29 @@ class ECOpenSSL {
*/
void point_from_bytes(const Byte* in, std::size_t in_byte_count, Point& out) const;

/**
* @brief Reads a secret key from a byte buffer.
*
* @param[in] in The byte buffer to read from.
* @param[in] in_byte_count The size of the input byte buffer.
* @param[out] The secret key to write to.
* @throws std::invalid_argument if the input buffer's byte size is zero or either input or output is nullptr.
* @throws std::runtime_error if an OpenSSL command fails or if the buffer is not large enough.
*/
void secret_key_from_bytes(const Byte* in, std::size_t in_byte_count, SecretKey& out) const;

/**
* @brief Writes a secret key into a byte buffer and returns the number of bytes written (if the byte buffer is
* empty).
*
* @param[in] in The secret key to be written.
* @param[in] out_byte_count The size of the output byte buffer.
* @param[out] The byte buffer to write to.
* @throws std::invalid_argument if the input is nullptr.
* @throws std::runtime_error if an OpenSSL command fails or if the buffer is not large enough.
*/
std::size_t secret_key_to_bytes(const SecretKey& in, std::size_t out_byte_count, Byte* out = nullptr) const;

private:
void hash_to_field(
std::shared_ptr<Hash> hash, const Byte* in, std::size_t in_byte_count, BIGNUM* out, BN_CTX* ctx) const;
Expand Down
4 changes: 2 additions & 2 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@

cmake_minimum_required(VERSION 3.14)

project(SOLOTest VERSION 0.3.0 LANGUAGES CXX C)
project(SOLOTest VERSION 0.4.0 LANGUAGES CXX C)

# If not called from root CMakeLists.txt
if(NOT DEFINED SOLO_BUILD_TEST)
set(SOLO_BUILD_TEST ON)

find_package(PETAce-Solo 0.3.0 EXACT REQUIRED)
find_package(PETAce-Solo 0.4.0 EXACT REQUIRED)

# Must define these variables and include macros
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib)
Expand Down
8 changes: 4 additions & 4 deletions test/ahe_paillier_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ TEST(AHEPaillierTest, Serialization) {
{
petace::solo::PRNGFactory prng_factory = petace::solo::PRNGFactory(petace::solo::PRNGScheme::BLAKE2Xb);
auto prng = prng_factory.create();
std::size_t illegal_bits = 8200;
std::size_t illegal_byte_count = 1025;
std::size_t illegal_bits = 16385;
std::size_t illegal_byte_count = 2049;
std::vector<petace::solo::Byte> bn_bytes(illegal_byte_count);
prng->generate(illegal_byte_count, bn_bytes.data());
#ifdef SOLO_USE_IPCL
Expand Down Expand Up @@ -319,7 +319,7 @@ TEST(AHEPaillierTest, Serialization) {
{
petace::solo::PRNGFactory prng_factory = petace::solo::PRNGFactory(petace::solo::PRNGScheme::BLAKE2Xb);
auto prng = prng_factory.create();
std::size_t illegal_byte_count = 1025;
std::size_t illegal_byte_count = 2049;
std::vector<petace::solo::Byte> bn_bytes(illegal_byte_count);
prng->generate(illegal_byte_count, bn_bytes.data());
petace::solo::ahepaillier::Plaintext pt;
Expand All @@ -330,7 +330,7 @@ TEST(AHEPaillierTest, Serialization) {
{
petace::solo::PRNGFactory prng_factory = petace::solo::PRNGFactory(petace::solo::PRNGScheme::BLAKE2Xb);
auto prng = prng_factory.create();
std::size_t illegal_byte_count = 1025;
std::size_t illegal_byte_count = 2049;
std::vector<petace::solo::Byte> bn_bytes(illegal_byte_count);
prng->generate(illegal_byte_count, bn_bytes.data());
std::shared_ptr<petace::solo::ahepaillier::SecretKey> sk = nullptr;
Expand Down
9 changes: 9 additions & 0 deletions test/ec_openssl_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,18 @@ TEST(ECOpenSSLTest, ECScheme) {
ASSERT_THROW(ec.create_secret_key(nullptr, sk), std::invalid_argument);
ec.create_secret_key(prng, sk);
ec.create_secret_key(prng, sk_new);

std::size_t sk_byte_count = ec.secret_key_to_bytes(sk, 0, nullptr);
std::vector<petace::solo::Byte> sk_byte_array(sk_byte_count);
ASSERT_EQ(ec.secret_key_to_bytes(sk, sk_byte_count, sk_byte_array.data()), sk_byte_count);
EC::SecretKey sk_deserialized;
ec.secret_key_from_bytes(sk_byte_array.data(), sk_byte_count, sk_deserialized);

ASSERT_THROW(ec.hash_to_curve(nullptr, 64, plaintext), std::invalid_argument);
ec.hash_to_curve(input, 64, plaintext);
ec.encrypt(plaintext, sk, ciphertext);
ec.encrypt(plaintext, sk_deserialized, ciphertext_new);
ASSERT_TRUE(ec.are_equal(ciphertext, ciphertext_new));
ec.switch_key(ciphertext, sk, sk_new, ciphertext_new);
ec.decrypt(ciphertext_new, sk_new, plaintext_new);
ASSERT_TRUE(ec.are_equal(plaintext_new, plaintext));
Expand Down

0 comments on commit 069ae98

Please sign in to comment.