Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Put plaintext classes into separate files
- Loading branch information
Bogdan Kulynych
committed
May 29, 2015
1 parent
27648ca
commit 1e72600
Showing
8 changed files
with
675 additions
and
615 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
#pragma once | ||
|
||
#include "she/plaintext.hpp" | ||
#include "she/ciphertext.hpp" | ||
#include "she/key.hpp" |
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,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 |
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,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 |
Oops, something went wrong.