Skip to content

Commit

Permalink
Put plaintext classes into separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
Bogdan Kulynych committed May 29, 2015
1 parent 27648ca commit 1e72600
Show file tree
Hide file tree
Showing 8 changed files with 675 additions and 615 deletions.
1 change: 1 addition & 0 deletions include/she.hpp
@@ -1,4 +1,5 @@
#pragma once

#include "she/plaintext.hpp"
#include "she/ciphertext.hpp"
#include "she/key.hpp"
93 changes: 16 additions & 77 deletions include/she/ciphertext.hpp
Expand Up @@ -6,86 +6,23 @@
#include <memory>

#include <boost/operators.hpp>
#include <boost/variant/variant.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/access.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/split_free.hpp>

#include <gmpxx.h>

#include "key.hpp"
#include "random.hpp"
#include "serializations.hpp"
#include "key.hpp"


namespace she
{

class PrivateKey;
class PlaintextArray;
class EncryptedArray;


class PlaintextArray : boost::equality_comparable<PlaintextArray
, boost::xorable<PlaintextArray
, boost::andable<PlaintextArray
> > >
{
friend class EncryptedArray;
public:
// Construct from plaintext
PlaintextArray(const std::vector<bool> & plaintext) noexcept : _elements(plaintext) {}

// Empty ctor for deserialization purposes
PlaintextArray() noexcept {};

// Convert to bits
operator const std::vector<bool>() const { return _elements; };
operator std::vector<bool>() { return _elements; };

// Element-wise addition (XOR)
PlaintextArray & operator^=(const PlaintextArray &) noexcept;

// Element-wise multiplication (AND)
PlaintextArray & operator&=(const PlaintextArray &) noexcept;

// Homomorphic equality comparison
const PlaintextArray equal(const std::vector<PlaintextArray> &) const noexcept;
const EncryptedArray equal(const std::vector<EncryptedArray> &) const noexcept;

// Homomorphic select function
const PlaintextArray select(const std::vector<PlaintextArray> &) const noexcept;
const EncryptedArray select(const std::vector<EncryptedArray> &) const noexcept;

// Extend array
PlaintextArray & extend(const PlaintextArray & other) noexcept;

// EncryptedArray compatibility
unsigned int degree() const noexcept { return 0; }
unsigned int max_degree() const noexcept { return 0; }

// Ciphertext size
size_t size() const noexcept { return _elements.size(); }

// Encrypted bits
const std::vector<bool>& elements() const noexcept { return _elements; }
std::vector<bool>& elements() noexcept { return _elements; }

// Representation comparison
bool operator==(const PlaintextArray &) const noexcept;

private:
friend class boost::serialization::access;

std::vector<bool> _elements;

template<class Archive>
void serialize(Archive & ar, unsigned int const version) const
{
ar & BOOST_SERIALIZATION_NVP(_elements);
}
};


class EncryptedArray : boost::equality_comparable<EncryptedArray
, boost::xorable<EncryptedArray
Expand Down Expand Up @@ -181,18 +118,6 @@ class EncryptedArray : boost::equality_comparable<EncryptedArray
BOOST_SERIALIZATION_SPLIT_MEMBER()
};

// Homomorphic addition (XOR)
PlaintextArray sum(const std::vector<PlaintextArray> &) noexcept;
EncryptedArray sum(const std::vector<EncryptedArray> &) noexcept;

// Homomorphic multiplication (AND)
PlaintextArray product(const std::vector<PlaintextArray> &) noexcept;
EncryptedArray product(const std::vector<EncryptedArray> &) noexcept;

// Arrays concatenation
PlaintextArray concat(const std::vector<PlaintextArray> &) noexcept;
EncryptedArray concat(const std::vector<EncryptedArray> &) noexcept;


class CompressedCiphertext : boost::equality_comparable<CompressedCiphertext>
{
Expand Down Expand Up @@ -249,4 +174,18 @@ class CompressedCiphertext : boost::equality_comparable<CompressedCiphertext>
BOOST_SERIALIZATION_SPLIT_MEMBER()
};


// Homomorphic addition (XOR)
PlaintextArray sum(const std::vector<PlaintextArray> &) noexcept;
EncryptedArray sum(const std::vector<EncryptedArray> &) noexcept;

// Homomorphic multiplication (AND)
PlaintextArray product(const std::vector<PlaintextArray> &) noexcept;
EncryptedArray product(const std::vector<EncryptedArray> &) noexcept;

// Arrays concatenation
PlaintextArray concat(const std::vector<PlaintextArray> &) noexcept;
EncryptedArray concat(const std::vector<EncryptedArray> &) noexcept;


} // namespace she
80 changes: 80 additions & 0 deletions include/she/plaintext.hpp
@@ -0,0 +1,80 @@
#pragma once

#include <cstddef>
#include <memory>

#include <boost/operators.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/access.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/split_free.hpp>

#include "serializations.hpp"


namespace she
{

class EncryptedArray;

class PlaintextArray : boost::equality_comparable<PlaintextArray
, boost::xorable<PlaintextArray
, boost::andable<PlaintextArray
> > >
{
friend class EncryptedArray;
public:
// Construct from plaintext
PlaintextArray(const std::vector<bool> & plaintext) noexcept : _elements(plaintext) {}

// Empty ctor for deserialization purposes
PlaintextArray() noexcept {};

// Convert to bits
operator const std::vector<bool>() const { return _elements; };
operator std::vector<bool>() { return _elements; };

// Element-wise addition (XOR)
PlaintextArray & operator^=(const PlaintextArray &) noexcept;

// Element-wise multiplication (AND)
PlaintextArray & operator&=(const PlaintextArray &) noexcept;

// Homomorphic equality comparison
const PlaintextArray equal(const std::vector<PlaintextArray> &) const noexcept;
const EncryptedArray equal(const std::vector<EncryptedArray> &) const noexcept;

// Homomorphic select function
const PlaintextArray select(const std::vector<PlaintextArray> &) const noexcept;
const EncryptedArray select(const std::vector<EncryptedArray> &) const noexcept;

// Extend array
PlaintextArray & extend(const PlaintextArray & other) noexcept;

// EncryptedArray compatibility
unsigned int degree() const noexcept { return 0; }
unsigned int max_degree() const noexcept { return 0; }

// Ciphertext size
size_t size() const noexcept { return _elements.size(); }

// Encrypted bits
const std::vector<bool>& elements() const noexcept { return _elements; }
std::vector<bool>& elements() noexcept { return _elements; }

// Representation comparison
bool operator==(const PlaintextArray &) const noexcept;

private:
friend class boost::serialization::access;

std::vector<bool> _elements;

template<class Archive>
void serialize(Archive & ar, unsigned int const version) const
{
ar & BOOST_SERIALIZATION_NVP(_elements);
}
};

} // namespace she
48 changes: 0 additions & 48 deletions src/ciphertext.cpp
Expand Up @@ -12,54 +12,6 @@ using std::vector;
namespace she
{

PlaintextArray & PlaintextArray::operator^=(const PlaintextArray & other) noexcept
{
const size_t n = min(_elements.size(), other._elements.size());

// Do bit-wise XOR
for (size_t i = 0; i < n; ++i) {
_elements[i] = _elements[i] ^ other._elements[i];
}

// If sizes don't match pad with zeros from the right
for (size_t i = n; i < other._elements.size(); ++i) {
_elements.push_back(other._elements[i]);
}

return *this;
}

PlaintextArray & PlaintextArray::operator&=(const PlaintextArray & other) noexcept
{
const size_t n = min(_elements.size(), other._elements.size());

// Do bit-wise AND
for (size_t i = 0; i < n; ++i) {
_elements[i] = _elements[i] & other._elements[i];
}

// If sizes don't match pad with ones from the right
for (size_t i = n; i < other._elements.size(); ++i) {
_elements.push_back(other._elements[i]);
}

return *this;
}

PlaintextArray & PlaintextArray::extend(const PlaintextArray & other) noexcept
{
for (const auto & element : other._elements) {
_elements.push_back(element);
}

return *this;
}

bool PlaintextArray::operator==(const PlaintextArray & other) const noexcept
{
return _elements == other._elements;
}

std::set<mpz_class> EncryptedArray::public_elements = {};

EncryptedArray::EncryptedArray(const mpz_class & x, unsigned int max_degree, unsigned int degree) noexcept :
Expand Down
62 changes: 62 additions & 0 deletions src/plaintext.cpp
@@ -0,0 +1,62 @@
#include <cmath>

#include "she.hpp"
#include "she/exceptions.hpp"

using std::min;
using std::max;
using std::vector;


namespace she
{

PlaintextArray & PlaintextArray::operator^=(const PlaintextArray & other) noexcept
{
const size_t n = min(_elements.size(), other._elements.size());

// Do bit-wise XOR
for (size_t i = 0; i < n; ++i) {
_elements[i] = _elements[i] ^ other._elements[i];
}

// If sizes don't match pad with zeros from the right
for (size_t i = n; i < other._elements.size(); ++i) {
_elements.push_back(other._elements[i]);
}

return *this;
}

PlaintextArray & PlaintextArray::operator&=(const PlaintextArray & other) noexcept
{
const size_t n = min(_elements.size(), other._elements.size());

// Do bit-wise AND
for (size_t i = 0; i < n; ++i) {
_elements[i] = _elements[i] & other._elements[i];
}

// If sizes don't match pad with ones from the right
for (size_t i = n; i < other._elements.size(); ++i) {
_elements.push_back(other._elements[i]);
}

return *this;
}

PlaintextArray & PlaintextArray::extend(const PlaintextArray & other) noexcept
{
for (const auto & element : other._elements) {
_elements.push_back(element);
}

return *this;
}

bool PlaintextArray::operator==(const PlaintextArray & other) const noexcept
{
return _elements == other._elements;
}

} // namespace she

0 comments on commit 1e72600

Please sign in to comment.