circuit_context.h 8.8 KB
Newer Older
J
jingqinghe 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once

#include <algorithm>
#include <memory>
J
jhjiangcs 已提交
18
#include <algorithm>
J
jingqinghe 已提交
19

J
update  
jingqinghe 已提交
20
#include "core/paddlefl_mpc/mpc_protocol/abstract_network.h"
J
jingqinghe 已提交
21 22 23 24 25 26 27 28
#include "prng_utils.h"

namespace aby3 {

using AbstractNetwork = paddle::mpc::AbstractNetwork;

class CircuitContext {
public:
J
jhjiangcs 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    CircuitContext(size_t party,
                   std::shared_ptr<AbstractNetwork> network,
                   const block& seed = g_zero_block,
                   const block& seed2 = g_zero_block) {
        init(party, network, seed, seed2);
    }

    CircuitContext(const CircuitContext& other) = delete;

    CircuitContext& operator=(const CircuitContext& other) = delete;

    void init(size_t party,
              std::shared_ptr<AbstractNetwork> network,
              block seed,
              block seed2) {
        set_party(party);
        set_network(network);

        if (equals(seed, g_zero_block)) {
            seed = block_from_dev_urandom();
        }

        if (equals(seed2, g_zero_block)) {
            seed2 = block_from_dev_urandom();
        }
        set_random_seed(seed, 0);
        // seed2 is private
        set_random_seed(seed2, 2);

        // 3 for 3-party computation
        size_t party_pre = (this->party() - 1 + 3) % 3;
        size_t party_next = (this->party() + 1) % 3;

        if (party == 1) {
            block recv_seed = this->network()->template recv<block>(party_next);
            this->network()->template send(party_pre, seed);
            seed = recv_seed;
        } else {
            this->network()->template send(party_pre, seed);
            seed = this->network()->template recv<block>(party_next);
        }

        set_random_seed(seed, 1);
    }

    void set_party(size_t party) {
        if (party >= 3) {
            // exception handling
        }
        _party = party;
    }

    void set_network(std::shared_ptr<AbstractNetwork> network) {
        _network = network;
    }
<<<<<<< HEAD

    AbstractNetwork* network() {
        return _network.get();
    }

    void set_random_seed(const block& seed, size_t idx) {
        if (idx >= 3) {
            // exception handling
        }
        _prng[idx].set_seed(seed);
    }

    size_t party() const {
        return _party;
    }
J
jingqinghe 已提交
100

J
jhjiangcs 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    size_t pre_party() const {
        return (_party + 3 - 1) % 3;
    }

    size_t next_party() const {
        return (_party + 1) % 3;
    }

    template <typename T>
    T gen_random(bool next) {
        return _prng[next].get<T>();
    }

    template<typename T, template <typename> class Tensor>
    void gen_random(Tensor<T>& tensor, bool next) {
        std::for_each(tensor.data(), tensor.data() + tensor.numel(),
                      [this, next](T& val) {
                          val = this->template gen_random<T>(next);
                      });
    }
J
jingqinghe 已提交
121

J
jhjiangcs 已提交
122 123 124 125
    template <typename T>
    T gen_random_private() {
        return _prng[2].get<T>();
    }
J
jingqinghe 已提交
126

J
jhjiangcs 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
    template<typename T, template <typename> class Tensor>
    void gen_random_private(Tensor<T>& tensor) {
        std::for_each(tensor.data(), tensor.data() + tensor.numel(),
                      [this](T& val) {
                          val = this->template gen_random_private<T>();
                      });
    }

    template <typename T>
    T gen_zero_sharing_arithmetic() {
        return _prng[0].get<T>() - _prng[1].get<T>();
    }

    template<typename T, template <typename> class Tensor>
    void gen_zero_sharing_arithmetic(Tensor<T>& tensor) {
        std::for_each(tensor.data(), tensor.data() + tensor.numel(),
                      [this](T& val) {
                          val = this->template gen_zero_sharing_arithmetic<T>();
                      });
    }

    template <typename T>
    T gen_zero_sharing_boolean() {
        return _prng[0].get<T>() ^ _prng[1].get<T>();
    }
J
jingqinghe 已提交
152

J
jhjiangcs 已提交
153 154 155 156 157 158
    template<typename T, template <typename> class Tensor>
    void gen_zero_sharing_boolean(Tensor<T>& tensor) {
        std::for_each(tensor.data(), tensor.data() + tensor.numel(),
                      [this](T& val) {
                          val = this->template gen_zero_sharing_boolean<T>();
                      });
J
jingqinghe 已提交
159 160
    }

J
jhjiangcs 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
    template<typename T, template <typename> class Tensor>
=======

    AbstractNetwork* network() {
        return _network.get();
    }

    void set_random_seed(const block& seed, size_t idx) {
        if (idx >= 3) {
            // exception handling
        }
        _prng[idx].set_seed(seed);
    }

    size_t party() const {
        return _party;
    }

    size_t pre_party() const {
        return (_party + 3 - 1) % 3;
    }

    size_t next_party() const {
        return (_party + 1) % 3;
    }

    template <typename T>
    T gen_random(bool next) {
        return _prng[next].get<T>();
    }

    template<typename T, template <typename> class Tensor>
    void gen_random(Tensor<T>& tensor, bool next) {
        std::for_each(tensor.data(), tensor.data() + tensor.numel(),
                      [this, next](T& val) {
                          val = this->template gen_random<T>(next);
                      });
    }

    template <typename T>
    T gen_random_private() {
        return _prng[2].get<T>();
    }

    template<typename T, template <typename> class Tensor>
    void gen_random_private(Tensor<T>& tensor) {
        std::for_each(tensor.data(), tensor.data() + tensor.numel(),
                      [this](T& val) {
                          val = this->template gen_random_private<T>();
                      });
    }

    template <typename T>
    T gen_zero_sharing_arithmetic() {
        return _prng[0].get<T>() - _prng[1].get<T>();
    }

    template<typename T, template <typename> class Tensor>
    void gen_zero_sharing_arithmetic(Tensor<T>& tensor) {
        std::for_each(tensor.data(), tensor.data() + tensor.numel(),
                      [this](T& val) {
                          val = this->template gen_zero_sharing_arithmetic<T>();
                      });
    }

    template <typename T>
    T gen_zero_sharing_boolean() {
        return _prng[0].get<T>() ^ _prng[1].get<T>();
    }

    template<typename T, template <typename> class Tensor>
    void gen_zero_sharing_boolean(Tensor<T>& tensor) {
        std::for_each(tensor.data(), tensor.data() + tensor.numel(),
                      [this](T& val) {
                          val = this->template gen_zero_sharing_boolean<T>();
                      });
    }

    template<typename T, template <typename> class Tensor>
>>>>>>> 5a09665c36ffb7eae2288b3f837d3be18091c259
    void ot(size_t sender, size_t receiver, size_t helper,
            const Tensor<T>* choice, const Tensor<T>* m[2],
            Tensor<T>* buffer[2], Tensor<T>* ret) {
        // TODO: check tensor shape equals
        const size_t numel = buffer[0]->numel();
        if (party() == sender) {
            bool common = helper == next_party();
            this->template gen_random(*buffer[0], common);
            this->template gen_random(*buffer[1], common);
            for (size_t i = 0; i < numel; ++i) {
                buffer[0]->data()[i] ^= m[0]->data()[i];
                buffer[1]->data()[i] ^= m[1]->data()[i];
            }
            network()->template send(receiver, *buffer[0]);
            network()->template send(receiver, *buffer[1]);

        } else if (party() == helper) {
            bool common = sender == next_party();

            this->template gen_random(*buffer[0], common);
            this->template gen_random(*buffer[1], common);

            for (size_t i = 0; i < numel; ++i) {
                buffer[0]->data()[i] = choice->data()[i] & 1 ?
                    buffer[1]->data()[i] : buffer[0]->data()[i];
            }
            network()->template send(receiver, *buffer[0]);
        } else if (party() == receiver) {
            network()->template recv(sender, *buffer[0]);
            network()->template recv(sender, *buffer[1]);
            network()->template recv(helper, *ret);
            size_t i = 0;
            std::for_each(ret->data(), ret->data() + numel, [&buffer, &i, choice, ret](T& in) {
                          bool c = choice->data()[i] & 1;
                          in ^= buffer[c]->data()[i];
                          ++i;}
                          );
        }
    }
J
jingqinghe 已提交
280 281

private:
J
jhjiangcs 已提交
282 283 284
    size_t _party;

    std::shared_ptr<AbstractNetwork> _network;
J
jingqinghe 已提交
285

J
jhjiangcs 已提交
286
    PseudorandomNumberGenerator _prng[3];
J
jingqinghe 已提交
287 288 289 290

};

} // namespace aby3