std::vector<mpair>encrypt(const MDL::Matrix<long>& data,
                          const FHEPubKey        & pk,
                          const EncryptedArray   & ea,
                          long                     from = 0,
                          long                     to = 0)
{
    to = to == 0 ? data.rows() : to;
    MDL::Timer timer;
    std::vector<mpair> ctxts(to - from, { pk, pk });
    std::vector<std::thread> workers;
    std::atomic<size_t> counter(from);

    timer.start();

    for (long wr = 0; wr < WORKER_NR; wr++) {
        workers.push_back(std::move(std::thread([&data, &ea, &to, &from,
                                                 &counter, &ctxts]() {
            size_t next;

            while ((next = counter.fetch_add(1)) < to) {
                auto vec = covariance(data[next], data[next]).vector();
                ctxts[next - from].first.pack(data[next], ea);
                ctxts[next - from].second.pack(vec, ea);
            }
        })));
    }

    for (auto && wr : workers) wr.join();
    timer.end();
    printf("Encrypt %ld data with %ld workers costed %f sec\n", to - from,
           WORKER_NR, timer.second());
    return ctxts;
}
Exemple #2
0
void act_server(int socket) {
    FHEcontext context(gM, gP, gR);
    buildModChain(context, gL);
    FHEPubKey pk(context);
    receive_pk(socket, pk);

    std::vector<Ctxt> ctxts(gC, pk);
    for (long i = 0; i < gC; i++)
        pk.Encrypt(ctxts[i], NTL::to_ZZX(i));
    send_ctxts(socket, ctxts);
    nn_close(socket);
}
Exemple #3
0
void _Tt_signature::
pretty_print(const _Tt_ostream &os) const
{
    _Tt_arg_list_cursor	argc;
    int			argn;
    int			is_osig = _otid.len() > 0;

    if (! is_osig) {
        switch (_scope) {
        case TT_FILE:
            os << "file";
            break;
        case TT_SESSION:
            os << "session";
            break;
        case TT_FILE_IN_SESSION:
            os << "file_in_session";
            break;
        }
    }
    os << " " << _op << "(";
    argc.reset(_args);
    argn = _args->count();
    while (argc.next()) {
        switch (argc->mode()) {
        case TT_IN:
            os << "in";
            break;
        case TT_OUT:
            os << "out";
            break;
        case TT_INOUT:
            os << "inout";
            break;
        }
        os << " " << argc->type() << " " << argc->name();
        if (--argn) {
            os << ", ";
        }
    }
    os << ")";
    if (_contexts->count() > 0) {
        _Tt_context_list_cursor ctxts(_contexts);
        os << " context(";
        ctxts.next();
        os << ctxts->slotName();
        while(ctxts.next()) {
            os << ", " << ctxts->slotName();
        }
        os << ")";
    }
    if (is_osig) {
        os << " => " << _ptid << " ";
        switch (_scope) {
        case TT_FILE:
            os << "file";
            break;
        case TT_SESSION:
            os << "session";
            break;
        case TT_FILE_IN_SESSION:
            os << "file_in_session";
            break;
        }
    } else {
        if (   (_reliability&TT_START) || (_reliability&TT_QUEUE)
                || (_opnum != -1))
        {
            // We are psig and have right hand side, so need =>
            os << " =>";
        }
    }
    if (_reliability & TT_START) {
        os << " start";
    }
    if (_reliability & TT_QUEUE) {
        os << " queue";
    }
    if (_opnum != -1) {
        os << " opnum=" << _opnum;
    }
    if (is_osig && _super_otid.len()) {
        os << " from " << _super_otid;
    }
    os << ";\n";
}