123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481 |
- #ifndef __DUORAM_HPP__
- #define __DUORAM_HPP__
- #include "types.hpp"
- #include "mpcio.hpp"
- #include "coroutine.hpp"
- template <typename T>
- class Duoram {
-
-
-
-
-
-
- int player;
- size_t oram_size;
-
-
- std::vector<T> database;
- std::vector<T> blind;
- std::vector<T> &p0_blind;
- std::vector<T> peer_blinded_db;
- std::vector<T> &p1_blind;
- public:
-
- using type = T;
-
- class Shape;
-
- class Flat;
-
- Duoram(int player, size_t size);
-
- inline size_t size() { return oram_size; }
-
- Flat flat(MPCTIO &tio, yield_t &yield, size_t start = 0,
- size_t len = 0) {
- return Flat(*this, tio, yield, start, len);
- }
-
- void dump() const;
- };
- template <typename T>
- class Duoram<T>::Shape {
-
- friend class Flat;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- template <typename U, typename FT, typename FST, typename Sh>
- class MemRefS;
-
- template <typename FT, typename FST>
- class MemRefExpl;
-
-
-
-
- template <typename U, typename Sh>
- class MemRefInd;
- protected:
-
-
-
- const Shape &parent;
-
- Duoram &duoram;
-
- size_t shape_size;
-
-
- nbits_t addr_size;
-
- address_t addr_mask;
-
- MPCTIO &tio;
- yield_t &yield;
-
-
-
-
-
-
-
- bool explicitmode;
-
-
- void set_shape_size(size_t sz);
-
-
- Shape(const Shape &parent, Duoram &duoram, MPCTIO &tio,
- yield_t &yield) : parent(parent), duoram(duoram), shape_size(0),
- tio(tio), yield(yield), explicitmode(false) {}
-
- Shape(const Shape ©_from, MPCTIO &tio, yield_t &yield) :
- parent(copy_from.parent), duoram(copy_from.duoram),
- shape_size(copy_from.shape_size),
- addr_size(copy_from.addr_size), addr_mask(copy_from.addr_mask),
- tio(tio), yield(yield),
- explicitmode(copy_from.explicitmode) {}
-
-
-
-
-
-
-
-
-
-
-
- virtual size_t indexmap(size_t idx) const = 0;
-
-
-
- inline std::tuple<T&,T&> get_server(size_t idx,
- std::nullopt_t null = std::nullopt) const {
- size_t physaddr = indexmap(idx);
- return std::tie(
- duoram.p0_blind[physaddr],
- duoram.p1_blind[physaddr]);
- }
-
-
-
- inline std::tuple<T&,T&,T&> get_comp(size_t idx,
- std::nullopt_t null = std::nullopt) const {
- size_t physaddr = indexmap(idx);
- return std::tie(
- duoram.database[physaddr],
- duoram.blind[physaddr],
- duoram.peer_blinded_db[physaddr]);
- }
-
-
-
- template <typename FT>
- inline std::tuple<FT&,FT&> get_server(size_t idx, FT T::*field) const {
- size_t physaddr = indexmap(idx);
- return std::tie(
- duoram.p0_blind[physaddr].*field,
- duoram.p1_blind[physaddr].*field);
- }
-
-
-
-
- template <typename FT>
- inline std::tuple<FT&,FT&,FT&> get_comp(size_t idx, FT T::*field) const {
- size_t physaddr = indexmap(idx);
- return std::tie(
- duoram.database[physaddr].*field,
- duoram.blind[physaddr].*field,
- duoram.peer_blinded_db[physaddr].*field);
- }
- public:
-
- inline size_t size() { return shape_size; }
-
-
-
-
-
-
-
-
-
-
- void explicitonly(bool enable);
-
-
-
- std::vector<T> reconstruct() const;
-
- T reconstruct(const T& share) const;
- };
- template <typename T>
- class Duoram<T>::Flat : public Duoram<T>::Shape {
-
- size_t start;
- size_t len;
- inline size_t indexmap(size_t idx) const {
- size_t paridx = idx + start;
- if (&(this->parent) == this) {
- return paridx;
- } else {
- return this->parent.indexmap(paridx);
- }
- }
-
- void butterfly(address_t start, nbits_t depth, bool dir);
- public:
-
-
- Flat(Duoram &duoram, MPCTIO &tio, yield_t &yield, size_t start = 0,
- size_t len = 0);
-
- Flat(const Flat ©_from, MPCTIO &tio, yield_t &yield) :
- Shape(copy_from, tio, yield), start(copy_from.start),
- len(copy_from.len) {}
-
-
-
- Flat context(MPCTIO &new_tio, yield_t &new_yield) const {
- return Flat(*this, new_tio, new_yield);
- }
- Flat context(yield_t &new_yield) const {
- return Flat(*this, this->tio, new_yield);
- }
-
- typename Duoram::Shape::template MemRefS<RegAS,T,std::nullopt_t,Flat>
- operator[](const RegAS &idx) {
- typename Duoram<T>::Shape::
- template MemRefS<RegAS,T,std::nullopt_t,Flat>
- res(*this, idx, std::nullopt);
- return res;
- }
- typename Duoram::Shape::template MemRefS<RegXS,T,std::nullopt_t,Flat>
- operator[](const RegXS &idx) {
- typename Duoram<T>::Shape::
- template MemRefS<RegXS,T,std::nullopt_t,Flat>
- res(*this, idx, std::nullopt);
- return res;
- }
- typename Duoram::Shape::template MemRefExpl<T,std::nullopt_t>
- operator[](address_t idx) {
- typename Duoram<T>::Shape::
- template MemRefExpl<T,std::nullopt_t>
- res(*this, idx, std::nullopt);
- return res;
- }
- template <typename U>
- Duoram::Shape::MemRefInd<U, Flat>
- operator[](const std::vector<U> &indcs) {
- typename Duoram<T>::Shape::
- template MemRefInd<U,Flat>
- res(*this, indcs);
- return res;
- }
- template <typename U, size_t N>
- Duoram::Shape::MemRefInd<U, Flat>
- operator[](const std::array<U,N> &indcs) {
- typename Duoram<T>::Shape::
- template MemRefInd<U,Flat>
- res(*this, indcs);
- return res;
- }
-
-
-
-
-
-
-
- template<typename U,typename V>
- void osort(const U &idx1, const V &idx2, bool dir=0);
-
-
-
-
- void bitonic_sort(address_t start, nbits_t depth, bool dir=0);
-
-
-
-
-
- RegAS obliv_binary_search(RegAS &target);
- };
- template <typename T>
- template <typename U, typename FT, typename FST, typename Sh>
- class Duoram<T>::Shape::MemRefS {
- Sh &shape;
- U idx;
- FST fieldsel;
- private:
-
-
- MemRefS<U,FT,FST,Sh> &oram_update(const FT& M, const prac_template_true&);
-
-
- MemRefS<U,FT,FST,Sh> &oram_update(const FT& M, const prac_template_false&);
- public:
- MemRefS<U,FT,FST,Sh>(Sh &shape, const U &idx, FST fieldsel) :
- shape(shape), idx(idx), fieldsel(fieldsel) {}
-
- template <typename SFT>
- MemRefS<U,SFT,SFT T::*,Sh> field(SFT T::*subfieldsel) {
- auto res = MemRefS<U,SFT,SFT T::*,Sh>(this->shape, idx, subfieldsel);
- return res;
- }
-
- operator FT();
-
- MemRefS<U,FT,FST,Sh> &operator+=(const FT& M);
-
- MemRefS<U,FT,FST,Sh> &operator=(const FT& M);
- };
- template <typename T> template <typename FT, typename FST>
- class Duoram<T>::Shape::MemRefExpl {
- Shape &shape;
- address_t idx;
- FST fieldsel;
- public:
- MemRefExpl(Shape &shape, address_t idx, FST fieldsel) :
- shape(shape), idx(idx), fieldsel(fieldsel) {}
-
- template <typename SFT>
- MemRefExpl<SFT,SFT T::*> field(SFT T::*subfieldsel) {
- auto res = MemRefExpl<SFT,SFT T::*>(this->shape, idx, subfieldsel);
- return res;
- }
-
- operator FT();
-
- MemRefExpl &operator+=(const FT& M);
-
- MemRefExpl &operator=(const FT& M);
-
- MemRefExpl &operator-=(const FT& M) { *this += (-M); return *this; }
- };
- template <typename T> template <typename U, typename Sh>
- class Duoram<T>::Shape::MemRefInd {
- Sh &shape;
- std::vector<U> indcs;
- public:
- MemRefInd(Sh &shape, std::vector<U> indcs) :
- shape(shape), indcs(indcs) {}
- template <size_t N>
- MemRefInd(Sh &shape, std::array<U,N> aindcs) :
- shape(shape) { for ( auto &i : aindcs ) { indcs.push_back(i); } }
-
- operator std::vector<T>();
-
- MemRefInd &operator+=(const std::vector<T>& M);
- template <size_t N>
- MemRefInd &operator+=(const std::array<T,N>& M);
-
- MemRefInd &operator=(const std::vector<T>& M);
- template <size_t N>
- MemRefInd &operator=(const std::array<T,N>& M);
-
- MemRefInd &operator-=(const std::vector<T>& M) { *this += (-M); return *this; }
- template <size_t N>
- MemRefInd &operator-=(const std::array<T,N>& M) { *this += (-M); return *this; }
- };
- #include "duoram.tcc"
- #endif
|