Open lzjluzijie opened 5 months ago
OK, I added a dummy values
and init ct_after
using ipcl::CipherText(pk, values)
, and the test passed. But is it the right way to use serialization?
void testSerialize() {
const uint32_t num_values = 100;
auto keys = ipcl::generateKeypair(768);
auto pk = keys.pub_key;
auto sk = keys.priv_key;
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type> dist(0, UINT_MAX);
std::vector<uint32_t> exp_value(num_values);
for (int i = 1; i < num_values; i++) {
exp_value[i] = dist(rng);
}
ipcl::PlainText pt = ipcl::PlainText(exp_value);
ipcl::CipherText ct = pk.encrypt(pt);
std::ostringstream os;
ipcl::serializer::serialize(os, ct);
std::vector<uint32_t> values(num_values);
ipcl::CipherText ct_after = ipcl::CipherText(pk, values);
std::istringstream is(os.str());
ipcl::serializer::deserialize(is, ct_after);
ipcl::PlainText pt_after = sk.decrypt(ct_after);
for (int i = 0; i < num_values; i++) {
std::vector<uint32_t> v = pt.getElementVec(i);
std::vector<uint32_t> v_after = pt_after.getElementVec(i);
std::cout << v[0] << " " << v_after[0] << std::endl;
if (v[0] != v_after[0]) {
std::cerr << "Error: " << v[0] << " != " << v_after[0] << std::endl;
exit(1);
}
}
}
OK, I added a dummy
values
and initct_after
usingipcl::CipherText(pk, values)
, and the test passed. But is it the right way to use serialization?void testSerialize() { const uint32_t num_values = 100; auto keys = ipcl::generateKeypair(768); auto pk = keys.pub_key; auto sk = keys.priv_key; std::random_device dev; std::mt19937 rng(dev()); std::uniform_int_distribution<std::mt19937::result_type> dist(0, UINT_MAX); std::vector<uint32_t> exp_value(num_values); for (int i = 1; i < num_values; i++) { exp_value[i] = dist(rng); } ipcl::PlainText pt = ipcl::PlainText(exp_value); ipcl::CipherText ct = pk.encrypt(pt); std::ostringstream os; ipcl::serializer::serialize(os, ct); std::vector<uint32_t> values(num_values); ipcl::CipherText ct_after = ipcl::CipherText(pk, values); std::istringstream is(os.str()); ipcl::serializer::deserialize(is, ct_after); ipcl::PlainText pt_after = sk.decrypt(ct_after); for (int i = 0; i < num_values; i++) { std::vector<uint32_t> v = pt.getElementVec(i); std::vector<uint32_t> v_after = pt_after.getElementVec(i); std::cout << v[0] << " " << v_after[0] << std::endl; if (v[0] != v_after[0]) { std::cerr << "Error: " << v[0] << " != " << v_after[0] << std::endl; exit(1); } } }
I think this is correct.
There is ciphertext serialization test, you can refer to it. https://github.com/intel/pailliercryptolib/blob/fdc21350302117103452968ababc2f9676f0d383/test/test_serialization.cpp#L80
OK, I added a dummy
values
and initct_after
usingipcl::CipherText(pk, values)
, and the test passed. But is it the right way to use serialization?void testSerialize() { const uint32_t num_values = 100; auto keys = ipcl::generateKeypair(768); auto pk = keys.pub_key; auto sk = keys.priv_key; std::random_device dev; std::mt19937 rng(dev()); std::uniform_int_distribution<std::mt19937::result_type> dist(0, UINT_MAX); std::vector<uint32_t> exp_value(num_values); for (int i = 1; i < num_values; i++) { exp_value[i] = dist(rng); } ipcl::PlainText pt = ipcl::PlainText(exp_value); ipcl::CipherText ct = pk.encrypt(pt); std::ostringstream os; ipcl::serializer::serialize(os, ct); std::vector<uint32_t> values(num_values); ipcl::CipherText ct_after = ipcl::CipherText(pk, values); std::istringstream is(os.str()); ipcl::serializer::deserialize(is, ct_after); ipcl::PlainText pt_after = sk.decrypt(ct_after); for (int i = 0; i < num_values; i++) { std::vector<uint32_t> v = pt.getElementVec(i); std::vector<uint32_t> v_after = pt_after.getElementVec(i); std::cout << v[0] << " " << v_after[0] << std::endl; if (v[0] != v_after[0]) { std::cerr << "Error: " << v[0] << " != " << v_after[0] << std::endl; exit(1); } } }
I think this is correct. There is ciphertext serialization test, you can refer to it.
But why ct_after
has type PlainText
? https://github.com/intel/pailliercryptolib/blob/fdc21350302117103452968ababc2f9676f0d383/test/test_serialization.cpp#L96
This is a good question. To be honest, it is possible to use types PlainText
or CipherText
in this place, because they both inherit from BaseText
. The real place for serialization is in BaseText
.
For the convenience of initialization, we chosen to use PlainText
. Of course, you can change this line to something like:
std::vector<uint32_t> zero(num_values, 0);
ipcl::CipherText ct_after(keys.pub_key, zero);
the results will be the same. If you think this is easier to understand.
Thank you. So the initialization ipcl::CipherText ct_after(keys.pub_key, zero)
is not encryption using pub_key
?
Thank you. So the initialization
ipcl::CipherText ct_after(keys.pub_key, zero)
is not encryption usingpub_key
?
If you initialize a ciphertext directly, then the 2nd parameter should be the corresponding ciphertext (encrypted).
PlainText
and CipherText
are essentially containers. Store content through a vector
. It does not care whether the stored content is encrypted or unencrypted.
https://github.com/intel/pailliercryptolib/blob/fdc21350302117103452968ababc2f9676f0d383/test/test_serialization.cpp#L96
If I change that PlainText to CipherText, there would have segmentation fault.
I think the test be something like this:
but it failed with segmentation fault
the line
test.cpp:437
isipcl::PlainText pt_after = sk.decrypt(ct_after);
. Did I make any mistake? Thank you!