Alibaba-Gemini-Lab / OpenCheetah

189 stars 48 forks source link

关于AuxProtocols::multiplexer() 函数无法使用 #5

Closed zhifeng110 closed 2 years ago

zhifeng110 commented 2 years ago

尊敬的阿里大佬: 我在使用猎豹框架的AuxProtocols::multiplexer() 函数时遇到了些困难,希望能得到大佬的帮助。 具体情况是这样的,之前一直在用微软的CF2做一些应用,在移植代码到cheetah后,我的drule函数与multiplexer函数无法使用,并且报bus error (core dumped)、segmentation fault (core dumped)等错误,我把代码贴上来,在CF2上测试是没有问题的,在猎豹上就不行了,同时我也测试了OpenCheetah/SCI/tests/test_ring_aux_protocols.cpp这个文件,也是同样的错误,希望能够得到大佬们的指点。 同时,悄悄的问一下,为啥在建立一个slient OT Pack的时间要几秒钟,搞多几个OT Pack调试代码时就让人着急[dog],期待大佬早日回复,也盼望隐语框架早日开源。

图片

图片


#include "BuildingBlocks/aux-protocols.h"
#include "stdio.h"
using namespace sci;
using namespace std;
#define MAX_THREADS 4

int party, port = 31000;
int num_threads = 1;
string address = "127.0.0.1";
bool six_comparison = true;
int dim = 2ULL;
int32_t bitlength = 32;
uint64_t mask_x = (bitlength == 64 ? -1 : ((1ULL << bitlength) - 1));
uint64_t ring_mod = (1ULL << bitlength);
int64_t getSignedVal(uint64_t x) {
  int64_t sx = x%ring_mod;
  if (sx >= ring_mod/2)
    sx = sx - ring_mod;
  return sx;
}
uint64_t getRingElt(int64_t x) { return ((uint64_t)x) & mask_x; }

int main(int argc, char **argv) {
    /************* Argument Parsing  ************/
    /********************************************/
    ArgMapping amap;
    amap.arg("r", party, "Role of party: ALICE = 1; BOB = 2");
    amap.arg("p", port, "Port Number");
    amap.arg("N", dim, "Number of ReLU operations");
    amap.arg("nt", num_threads, "Number of threads");
    amap.arg("ip", address, "IP Address of server (ALICE)");
    amap.parse(argc, argv);

    sci::NetIO *ioArr[4];
    OTPack<sci::NetIO> *otpackArr[3];
    ioArr[0] = new sci::NetIO(party == 1 ? nullptr : address.c_str(), port);
    ioArr[1] = new sci::NetIO(party == 1 ? nullptr : address.c_str(), port+1);
    otpackArr[0] = new OTPack<sci::NetIO>(ioArr[0], party);

    //双方布尔共享的值
    uint8_t *sel_alice = new uint8_t[8];
    uint8_t *sel_bob = new uint8_t[8];
    //双方算数共享的值
    uint64_t *x_alice = new uint64_t[8];
    uint64_t *x_bob = new uint64_t[8];
    //密码共享的output值
    uint64_t *y_alice = new uint64_t[8];
    uint64_t *y_bob = new uint64_t[8];

    for (int i = 0; i < 8; i++) {
        sel_alice[i] = i%2;
        sel_bob[i] = 0;
        x_alice[i] = i;
        x_bob[i] = i;
    }
    for (int i = 0; i < 8; i++) {
        printf("x_alice[%d]=%ld,x_bob[%d]=%ld,sel_alice[%d]=%d,sel_bob[%d]=%d\n",
                i,x_alice[i],i,x_bob[i],i,sel_alice[i],i,sel_bob[i]);
    }

    AuxProtocols *aux = new AuxProtocols(party, ioArr[0],otpackArr[0]);

    if (party == sci::ALICE) {
        aux->multiplexer(sel_alice, x_alice, y_alice, 8, 32, 32);
        ioArr[1]->send_data(y_alice, 8 * sizeof(uint64_t));
    } else {
        aux->multiplexer(sel_bob, x_bob, y_bob, 8, 32, 32);
        //验证
        uint64_t *y_from_alice = new uint64_t[8];
        ioArr[1]->recv_data(y_from_alice, 8 * sizeof(uint64_t));
        for (size_t i = 0; i < 8; i++)  {
          printf("AilceShare = %ld\tBobShare = %ld\tbool_x = %d\tbool_y = %d\nx1+x2 = %ld\tBool = %d\tOutPut = %ld\n\n", 
                x_alice[i], x_bob[i], sel_alice[i], sel_bob[i], (x_alice[i]+x_bob[i])&mask_x,
                 !(sel_alice[i]^sel_bob[i]),getSignedVal(y_from_alice[i]+y_bob[i])); 
        }
    }
    return 0;
}
fionser commented 2 years ago

https://github.com/emp-toolkit/emp-tool/commit/d5c793376729968d8588aa0d7e7d3de46118127b#diff-d14c79a42c49e218429f58f71bb1fbb7738a24ef0ac6f90b8c649e19b812ee94L139

这里修正了 OpenSSL 的一些函数。我在新版的 emp-tool 那里没有出现问题。不过 CF2 的 StartComputation() 初始化的 AuxProtocol 确没有问题,诡异。

zhifeng110 commented 2 years ago

感谢大佬百忙之中的解答,额,我更新了最新的emp-ot、emp-tool版本,重新编译后,试了一下并没有解决【dog】。我感觉可能是我的CMakeList写错了,大佬可以帮忙看看吗?

cmake_minimum_required(VERSION 3.0)
project(cheetah_demo)

include_directories(
    /usr/include /usr/include/c++/9 
    /root/cheetah/OpenCheetah/SCI/src
    /root/cheetah/OpenCheetah/build/include/eigen3
    /root/cheetah/OpenCheetah/build/include/emp-ot
    /root/cheetah/OpenCheetah/build/include/emp-tool
    /root/cheetah/OpenCheetah/build/include/gemini
    /root/cheetah/OpenCheetah/build/include/hexl
    /root/cheetah/OpenCheetah/build/include/SEAL-3.7
    /root/cheetah/OpenCheetah/build/include
    /root/cheetah/OpenCheetah/build/SCI/src
)
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -fopenmp)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 -w")
SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O0 -w")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maes -mavx2 -mrdseed")
add_compile_definitions(USE_CHEETAH SCI_HE SCI_OT)

link_directories(/root/cheetah/OpenCheetah/build/lib)

add_executable(a.out aicl_aux.cpp)

target_link_libraries(a.out
    crypto
    SCI-Cheetah-BuildingBlocks
    SCI-BuildingBlocks
    seal-3.7
    zstd
    hexl
    gemini
    emp-tool
    SCI-HE
    SCI-Cheetah
    SCI-Math
    SCI-LinearHE 
    SCI-LinearOT
    Cheetah-Linear
)
fionser commented 2 years ago

试试用 Release 编译。 $ENV{CXXFLAGS} -O0 -w 用 -O0 很奇怪。 而且如果你的根路径下已经安装了其他的 emp-tool 可能会引用到那边的版本。

zhifeng110 commented 2 years ago

我在EzPC的docker容器中对于程序卡住的情况,在 @42yeah 老哥的帮助下,认为是socket没有及时flush数据导致的,修改了最新版的emp-tool下的net_io_channel.h,和OpenCheetah/SCI/src/utils/io_channel.h下的socket配置,对其设置TCP_NODELAY,同时对数据发送后调一次flush才得以顺利进行,唉,想问问大佬们对这事怎么看?

fionser commented 2 years ago