Open hui09-m opened 1 year ago
sizeof(EncryptionParameters)
means.Instead using std::stringstream
to de/serialize
std::stringstream ss;
parms.save(ss);
std::string parms_to_send = ss.str();
Then on the other side, first recv a string and use std::stringstream
to de-serialize
EncryptionParmeters parms;
parms.load(a_string);
SEALContext
is not serializable. Just exchange EncryptionParmeters
and then construct the context.
- It seems you have misunderstood what
sizeof(EncryptionParameters)
means.- Instead using
std::stringstream
to de/serializestd::stringstream ss; parms.save(ss); std::string parms_to_send = ss.str();
Then on the other side, first recv a string and use
std::stringstream
to de-serializeEncryptionParmeters parms; parms.load(a_string);
SEALContext
is not serializable. Just exchangeEncryptionParmeters
and then construct the context.
Well, I tried this method, I can get string data, however, parms.load can't load it and throw exception. For function load() just can support stream datatype, I convert string to stringstream.
For thinking about it, for get the parms, may use parms.save() and parms.load() to do this? When I use simply parms.save(ss) and parms.load(ss), it not work and throw logic_error. So I use "parms.save(reinterpret_cast<seal_byte*>(byte_buffer.data()), byte_buffer.size()); ", it works. However, when constract SEALContext, it throw std::invalid_argument error. I have no idea about it. Can you give me some advice?
The following code work fine for me
void test() {
seal::EncryptionParameters parms(seal::scheme_type::ckks);
size_t N = 8192;
std::vector<int> modulus_bits = {51, 55, 55};
auto modulus = seal::CoeffModulus::Create(N, modulus_bits);
parms.set_poly_modulus_degree(N);
parms.set_coeff_modulus(modulus);
parms.set_random_generator(
seal::UniformRandomGeneratorFactory::DefaultFactory());
seal::SEALContext conetxt(parms);
std::ostringstream outs;
parms.save(outs);
std::string parms_str = outs.str();
printf("parms bytes %zd\n", parms_str.length());
// Simulate: send string through socket
std::vector<unsigned char> recv_bytes(parms_str.length());
std::copy_n(parms_str.c_str(), recv_bytes.size(), recv_bytes.data());
// wrap the bits as string
std::string recv_string(recv_bytes.data(), recv_bytes.data() + recv_bytes.size());
std::istringstream ins(recv_string);
seal::EncryptionParameters other_parms;
other_parms.load(ins);
seal::SEALContext other_context(other_parms);
printf("Is SEAL ok = %d\n", other_context.parameters_set());
}
The following code work fine for me
void test() { seal::EncryptionParameters parms(seal::scheme_type::ckks); size_t N = 8192; std::vector<int> modulus_bits = {51, 55, 55}; auto modulus = seal::CoeffModulus::Create(N, modulus_bits); parms.set_poly_modulus_degree(N); parms.set_coeff_modulus(modulus); parms.set_random_generator( seal::UniformRandomGeneratorFactory::DefaultFactory()); seal::SEALContext conetxt(parms); std::ostringstream outs; parms.save(outs); std::string parms_str = outs.str(); printf("parms bytes %zd\n", parms_str.length()); // Simulate: send string through socket std::vector<unsigned char> recv_bytes(parms_str.length()); std::copy_n(parms_str.c_str(), recv_bytes.size(), recv_bytes.data()); // wrap the bits as string std::string recv_string(recv_bytes.data(), recv_bytes.data() + recv_bytes.size()); std::istringstream ins(recv_string); seal::EncryptionParameters other_parms; other_parms.load(ins); seal::SEALContext other_context(other_parms); printf("Is SEAL ok = %d\n", other_context.parameters_set()); }
It seems you don't use socket to transmit data, you just use stringstream to do. Socket use send() and recv() to communicate. The fomat like: send( In SOCKET s, _In_readsbytes(len) const char FAR buf, In int len, In int flags ); recv( In SOCKET s, _Out_writes_bytesto(len, return) __out_data_source(NETWORK) char FAR buf, In int len, In int flags ); Always it make client and server be two projects like following picture (project Clients and Server_for_multi_clients), you should define apart.
There is no difference between copy_n
and send/recv
pair.
To make sure you are using the right length (but not just a MaxBufSize
) and make sure your have initialized your byte vector properly.
There is no difference between
copy_n
andsend/recv
pair. To make sure you are using the right length (but not just aMaxBufSize
) and make sure your have initialized your byte vector properly.
I have try what you said. For me, I set client project and service project apart, when client.cpp use the following data to recv parms that works.
std::vector
Can SEALContext object transmit by socket? I want to use socket to imitate the comminucations between server and clients. Then I write code to acchieve the fundation. But some errors occured.
If I write parameters for SEALContext object in Client, it can print corectly. However, I can not suitable for the parameters from Server. I will show you the code. server: `#include
pragma comment(lib,"ws2_32.lib")
using namespace std; using namespace seal; const int PORT = 8000;
define MaxClient 10
define MaxBufSize 4096
define _CRT_SECURE_NO_WARINGS
//服务线程 DWORD WINAPI ServerThread(LPVOID lpParameter) { SOCKET ClientSocket = (SOCKET)lpParameter; int receByt = 0; // char RecvBuf[MaxBufSize]; char RecvBuf[1]; char SendBuf[MaxBufSize]; while (1) { //数据接收 receByt = recv(ClientSocket, RecvBuf, sizeof(RecvBuf), 0); cout << "receByt = " << receByt << endl; string str_buff = RecvBuf; cout << "str_buff = " << str_buff << endl; if (receByt > 0) { cout << "接收到的消息是:" << RecvBuf << " 来自客户端:" << *ClientSocket << endl; if (str_buff == "1") { cout << "If you are ready for FHE servive,please write 1" << endl; } else cout << "Service wronged!" << endl; } else { cout << "接收消息结束!" << endl; break; } memset(RecvBuf, 0, sizeof(RecvBuf));
/ } closesocket(ClientSocket); free(ClientSocket); return 0; }
int main() { WSAData wsd; WSAStartup(MAKEWORD(2, 2), &wsd); SOCKET ListenSocket = socket(AF_INET, SOCK_STREAM, 0); SOCKADDR_IN ListenAddr; ListenAddr.sin_family = AF_INET; ListenAddr.sin_addr.S_un.S_addr = INADDR_ANY;//表示填入本机ip ListenAddr.sin_port = htons(PORT); int n; //n = bind(ListenSocket, (LPSOCKADDR)&ListenAddr, sizeof(ListenAddr)); n = ::bind(ListenSocket, (LPSOCKADDR)&ListenAddr, sizeof(ListenAddr));
}//main`
client: `//#include "Client.h"
include "client.h"
pragma comment(lib,"ws2_32.lib")
pragma warning(disable:4996)
using namespace std; using namespace seal; const int PORT = 8000;
define MaxBufSize 4096
define _CRT_SECURE_NO_WARINGS
int main() { WSADATA wsd; WSAStartup(MAKEWORD(2, 2), &wsd); //* SOCKET SocketClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); SOCKET SocketClient = socket(AF_INET, SOCK_STREAM, 0);
//* cout << "请输入要发送的信息,按回车结束发送:" << endl; //数据处理
*/
} `