Skip to content

Commit

Permalink
Adding tests to the primitive operations of HEAggGame (#2254)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #2254

Adding two tests for the primitive operations of Homomorphic encryption. The main operations we use in the HE Aggregation Game are:

add_with_ciphertext(c1, c2): It adds two ciphertexts c1 & c2
add_with_plaintext(c1, p1): It adds a ciphertext c1 to a plaintext  p1 and the results goes to a ciphertext

In this diff we test that these two operations return the expected sum after decryption.

These tests is also intended to act as small example of how homomorphic encryption works which can be used in talks/tutorials.

Reviewed By: xyguo, zhangpuhan

Differential Revision: D44549251

fbshipit-source-id: 8b281da75ee54254c55a504b06c37f3c475453dd
  • Loading branch information
telgamal-1 authored and facebook-github-bot committed Mar 30, 2023
1 parent ed72537 commit 9ea470c
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions fbpcs/emp_games/he_aggregation/test/HEAggGameTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
#include "fbpcs/emp_games/he_aggregation/HEAggGame.h"
#include "fbpcs/emp_games/he_aggregation/HEAggOptions.h"

#include "privacy_infra/elgamal/ElGamal.h"

namespace heschme = facebook::privacy_infra::elgamal;

namespace pcf2_he {

std::unordered_map<uint64_t, uint64_t> runGame(
Expand Down Expand Up @@ -56,6 +60,52 @@ void verifyOutput(
folly::toJson(actualOutput), folly::toJson(expectedOutput));
}

TEST(HEAggGameTest, HECiphertextAdditionTest) {
const std::string baseDir_ =
private_measurement::test_util::getBaseDirFromPath(__FILE__);

// Generate private key, public key and decryption table
auto sk = heschme::PrivateKey::generate();
auto pk = sk.toPublicKey();
heschme::initializeElGamalDecryptionTable(FLAGS_decryption_table_size);

// Encrypt values
int x = 111;
int y = 222;
heschme::Ciphertext c1 = pk.encrypt(x);
heschme::Ciphertext c2 = pk.encrypt(y);

// Perform addition
heschme::Ciphertext c3 = heschme::Ciphertext::add_with_ciphertext(c1, c2);

// Decrypt
uint64_t decrypted = sk.decrypt(c3);

EXPECT_EQ(decrypted, x + y);
}
TEST(HEAggGameTest, HEPlaintextAdditionTest) {
const std::string baseDir_ =
private_measurement::test_util::getBaseDirFromPath(__FILE__);

// Generate private key, public key and decryption table
auto sk = heschme::PrivateKey::generate();
auto pk = sk.toPublicKey();
heschme::initializeElGamalDecryptionTable(FLAGS_decryption_table_size);

// Encrypt values
int x = 111;
int y = 444;
heschme::Ciphertext c1 = pk.encrypt(x);

// Perform addition
heschme::Ciphertext c2 = heschme::Ciphertext::add_with_plaintext(c1, y);

// Decrypt
uint64_t decrypted = sk.decrypt(c2);

EXPECT_EQ(decrypted, x + y);
}

TEST(HEAggGameTest, HEAggGameCorrectnessTest) {
const std::string baseDir_ =
private_measurement::test_util::getBaseDirFromPath(__FILE__);
Expand Down

0 comments on commit 9ea470c

Please sign in to comment.