Skip to content

Commit

Permalink
Give a proper name to a PRF stream
Browse files Browse the repository at this point in the history
  • Loading branch information
Bogdan Kulynych committed Aug 8, 2017
1 parent 3caa208 commit a97edfe
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 50 deletions.
6 changes: 3 additions & 3 deletions include/she/ciphertext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ class CompressedCiphertext : boost::equality_comparable<CompressedCiphertext>

ParameterSet _parameter_set;

void initialize_oracle() const noexcept;
mutable std::unique_ptr<RandomOracle> _oracle;
void initialize_prf_stream() const noexcept;
mutable std::unique_ptr<PseudoRandomStream> _prf_stream;

std::vector<mpz_class> _elements_deltas;
mpz_class _public_element_delta;
Expand All @@ -168,7 +168,7 @@ class CompressedCiphertext : boost::equality_comparable<CompressedCiphertext>
ar & BOOST_SERIALIZATION_NVP(_elements_deltas);
ar & BOOST_SERIALIZATION_NVP(_public_element_delta);

initialize_oracle();
initialize_prf_stream();
}

BOOST_SERIALIZATION_SPLIT_MEMBER()
Expand Down
12 changes: 6 additions & 6 deletions include/she/key.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ class ParameterSet : boost::equality_comparable<ParameterSet>
unsigned int noise_size_bits,
unsigned int private_key_size_bits,
unsigned int ciphertext_size_bits,
unsigned int oracle_seed);
unsigned int prf_seed);

ParameterSet() noexcept;

unsigned int security;
unsigned int noise_size_bits;
unsigned int private_key_size_bits;
unsigned int ciphertext_size_bits;
unsigned int oracle_seed;
unsigned int prf_seed;

// Generate parameter set for given `security`, random oracle `seed`, that allows to perform at least
// Generate parameter set for given `security`, random prf `seed`, that allows to perform at least
// `circuit_mult_size homomorphic multiplications on ciphertexts
static const ParameterSet
generate_parameter_set(unsigned int security, unsigned int circuit_mult_size, unsigned int seed);
Expand All @@ -55,7 +55,7 @@ class ParameterSet : boost::equality_comparable<ParameterSet>
ar & BOOST_SERIALIZATION_NVP(noise_size_bits);
ar & BOOST_SERIALIZATION_NVP(private_key_size_bits);
ar & BOOST_SERIALIZATION_NVP(ciphertext_size_bits);
ar & BOOST_SERIALIZATION_NVP(oracle_seed);
ar & BOOST_SERIALIZATION_NVP(prf_seed);
}
};

Expand Down Expand Up @@ -88,7 +88,7 @@ class PrivateKey : boost::equality_comparable<PrivateKey>

void initialize_random_generators() const noexcept;
mutable std::unique_ptr<CSPRNG> _generator;
mutable std::unique_ptr<RandomOracle> _oracle;
mutable std::unique_ptr<PseudoRandomStream> _prf_stream;

PrivateKey& generate_values() noexcept;
mpz_class _private_element;
Expand Down Expand Up @@ -116,4 +116,4 @@ class PrivateKey : boost::equality_comparable<PrivateKey>
};


} // namespace she
} // namespace she
4 changes: 2 additions & 2 deletions include/she/random.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ class CSPRNG
};


class RandomOracle
class PseudoRandomStream
{
public:
RandomOracle(unsigned int size, unsigned int seed);
PseudoRandomStream(unsigned int size, unsigned int seed);

const mpz_class & next() const noexcept;
const void reset() const noexcept;
Expand Down
17 changes: 9 additions & 8 deletions src/ciphertext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -486,12 +486,13 @@ concat(const vector<EncryptedArray> & arrays) noexcept
CompressedCiphertext::CompressedCiphertext(const ParameterSet & params) noexcept :
_parameter_set(params)
{
initialize_oracle();
initialize_prf_stream();
}

void CompressedCiphertext::initialize_oracle() const noexcept
void CompressedCiphertext::initialize_prf_stream() const noexcept
{
_oracle.reset(new RandomOracle{_parameter_set.ciphertext_size_bits, _parameter_set.oracle_seed});
_prf_stream.reset(
new PseudoRandomStream{_parameter_set.ciphertext_size_bits, _parameter_set.prf_seed});
}

bool CompressedCiphertext::operator==(const CompressedCiphertext & other) const noexcept
Expand All @@ -503,18 +504,18 @@ bool CompressedCiphertext::operator==(const CompressedCiphertext & other) const

EncryptedArray CompressedCiphertext::expand() const noexcept
{
_oracle->reset();
_prf_stream->reset();

// Restore public element
const auto & oracle_output = _oracle->next();
const auto public_element = oracle_output - _public_element_delta;
const auto & prf_output = _prf_stream->next();
const auto public_element = prf_output - _public_element_delta;

EncryptedArray result(public_element, _parameter_set.degree());

// Restore ciphertext elements
for (const auto & delta : _elements_deltas) {
const auto & oracle_output = _oracle->next();
result._elements.push_back(oracle_output - delta);
const auto & prf_output = _prf_stream->next();
result._elements.push_back(prf_output - delta);
}

return result;
Expand Down
23 changes: 12 additions & 11 deletions src/key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace she
noise_size_bits(rho),
private_key_size_bits(eta),
ciphertext_size_bits(gamma),
oracle_seed(seed)
prf_seed(seed)
{
ASSERT((gamma >= eta) && (eta >= rho) && (rho > 0), "Bad parameters");
}
Expand All @@ -34,7 +34,7 @@ namespace she
noise_size_bits(1),
private_key_size_bits(1),
ciphertext_size_bits(1),
oracle_seed(1)
prf_seed(1)
{}

const ParameterSet
Expand All @@ -57,7 +57,7 @@ namespace she
&& (noise_size_bits == other.noise_size_bits)
&& (private_key_size_bits == other.private_key_size_bits)
&& (ciphertext_size_bits == other.ciphertext_size_bits)
&& (oracle_seed == other.oracle_seed);
&& (prf_seed == other.prf_seed);
}


Expand Down Expand Up @@ -95,37 +95,38 @@ namespace she
void PrivateKey::initialize_random_generators() const noexcept
{
_generator.reset(new CSPRNG);
_oracle.reset(new RandomOracle{_parameter_set.ciphertext_size_bits, _parameter_set.oracle_seed});
_prf_stream.reset(
new PseudoRandomStream{_parameter_set.ciphertext_size_bits, _parameter_set.prf_seed});
}

CompressedCiphertext PrivateKey::encrypt(const std::vector<bool> & bits) const noexcept
{
_oracle->reset();
_prf_stream->reset();

CompressedCiphertext result(_parameter_set);

// Generate compressed public element
const mpz_class & oracle_output = _oracle->next();
result._public_element_delta = oracle_output % _private_element;
const mpz_class & prf_output = _prf_stream->next();
result._public_element_delta = prf_output % _private_element;

for (const bool m : bits)
{
// Choose random noise
const mpz_class r = _generator->get_range_bits(_parameter_set.noise_size_bits) + 1;

// Random oracle output
const mpz_class & oracle_output = _oracle->next();
// Random PRF output
const mpz_class & prf_output = _prf_stream->next();

// Add compressed ciphertext deltas
result._elements_deltas.push_back((oracle_output - 2*r - m) % _private_element);
result._elements_deltas.push_back((prf_output - 2*r - m) % _private_element);
}

return result;
}

vector<bool> PrivateKey::decrypt(const EncryptedArray & array) const noexcept
{
_oracle->reset();
_prf_stream->reset();

vector<bool> result;
for (const mpz_class & element : array.elements())
Expand Down
10 changes: 5 additions & 5 deletions src/random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ CSPRNG::get_range(const mpz_class & upper_bound) const noexcept
}


RandomOracle::RandomOracle(unsigned int size, unsigned int seed) :
PseudoRandomStream::PseudoRandomStream(unsigned int size, unsigned int seed) :
_size(size),
_seed(seed),
_generator(gmp_randinit_default),
Expand All @@ -50,12 +50,12 @@ RandomOracle::RandomOracle(unsigned int size, unsigned int seed) :
_generator.seed(seed);
}

map<RandomOracle::keys_t, RandomOracle::values_t> RandomOracle::cached_values = {};
map<PseudoRandomStream::keys_t, PseudoRandomStream::values_t> PseudoRandomStream::cached_values = {};

const mpz_class & RandomOracle::next() const noexcept
const mpz_class & PseudoRandomStream::next() const noexcept
{
keys_t context{_size, _seed};
auto it = RandomOracle::cached_values.find(context);
auto it = PseudoRandomStream::cached_values.find(context);

if (it != cached_values.end()) {
if (it->second.size() <= _current_value) {
Expand All @@ -68,7 +68,7 @@ const mpz_class & RandomOracle::next() const noexcept
return cached_values[context][_current_value++];
}

const void RandomOracle::reset() const noexcept
const void PseudoRandomStream::reset() const noexcept
{
_current_value = 0;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/test_key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ BOOST_AUTO_TEST_CASE(parameter_set_construction)
BOOST_CHECK_EQUAL(params.noise_size_bits, 100);
BOOST_CHECK_EQUAL(params.private_key_size_bits, 1000);
BOOST_CHECK_EQUAL(params.ciphertext_size_bits, 100000);
BOOST_CHECK_EQUAL(params.oracle_seed, 5);
BOOST_CHECK_EQUAL(params.prf_seed, 5);
}
}

Expand Down
28 changes: 14 additions & 14 deletions tests/test_random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ using std::vector;
using std::abs;

using she::CSPRNG;
using she::RandomOracle;
using she::PseudoRandomStream;


BOOST_AUTO_TEST_SUITE(CSPRNG_Suite)
Expand Down Expand Up @@ -66,32 +66,32 @@ BOOST_AUTO_TEST_CASE(generator_get_range)
BOOST_AUTO_TEST_SUITE_END()


BOOST_AUTO_TEST_SUITE(RandomOracleSuite)
BOOST_AUTO_TEST_SUITE(PseudoRandomStreamSuite)

BOOST_AUTO_TEST_CASE(oracle_construction)
BOOST_AUTO_TEST_CASE(prf_stream_construction)
{
const RandomOracle oracle(100, 42);
const PseudoRandomStream prf_stream(100, 42);
}

BOOST_AUTO_TEST_CASE(oracle_output_generation)
BOOST_AUTO_TEST_CASE(prf_stream_output_generation)
{
const int bits = 100;
const unsigned int seed = 42;
const RandomOracle oracle(bits, seed);
const auto oracle_outputs = { oracle.next(), oracle.next(), oracle.next() };
const PseudoRandomStream prf_stream(bits, seed);
const auto prf_stream_outputs = { prf_stream.next(), prf_stream.next(), prf_stream.next() };

for (const auto & oracle_output : oracle_outputs)
for (const auto & prf_stream_output : prf_stream_outputs)
{
const auto output_bits = static_cast<int>(mpz_sizeinbase(oracle_output.get_mpz_t(), 2));
const auto output_bits = static_cast<int>(mpz_sizeinbase(prf_stream_output.get_mpz_t(), 2));
// BOOST_CHECK_EQUAL(output_bits, bits);
}
}

BOOST_AUTO_TEST_CASE(oracle_determinism)
BOOST_AUTO_TEST_CASE(prf_stream_determinism)
{
const int bits = 100;
const unsigned int seed = 42;
const RandomOracle nostradamus(bits, seed), pythia(bits, seed), paul_the_octopus(bits, seed + 1);
const PseudoRandomStream nostradamus(bits, seed), pythia(bits, seed), paul_the_octopus(bits, seed + 1);

const size_t iterations = 5;

Expand All @@ -102,9 +102,9 @@ BOOST_AUTO_TEST_CASE(oracle_determinism)
}
}

BOOST_AUTO_TEST_CASE(oracle_cache_reset)
BOOST_AUTO_TEST_CASE(prf_stream_cache_reset)
{
const RandomOracle nostradamus(10, 10), pythia(10, 10);
const PseudoRandomStream nostradamus(10, 10), pythia(10, 10);

const mpz_class & nostradamus_output_reference = nostradamus.next();
const mpz_class nostradamus_output_copy = mpz_class(nostradamus_output_reference);
Expand All @@ -114,7 +114,7 @@ BOOST_AUTO_TEST_CASE(oracle_cache_reset)
BOOST_CHECK(nostradamus_output_reference == pythia_output);
BOOST_CHECK(nostradamus_output_copy == pythia_output);

RandomOracle::reset_cache();
PseudoRandomStream::reset_cache();

BOOST_CHECK(nostradamus_output_reference != pythia_output);
BOOST_CHECK(nostradamus_output_copy == pythia_output);
Expand Down

0 comments on commit a97edfe

Please sign in to comment.