Closed yuesong-feng closed 2 years ago
The problem is located in Serialization.cpp, Load(), line 353
try{
// Serialization from input stream
}
catch (const ios_base::failure &){
expressive_rethrow_on_ios_base_failure(stream);
}
Why ios_base::failure occurs?
It's not clear to me what is going on based on this. Here is a small example that works, demonstrating what you are looking to do.
#include <sstream>
#include <iostream>
#include <vector>
#include <seal/seal.h>
using namespace seal;
using namespace std;
int main() {
stringstream ss;
EncryptionParameters params(scheme_type::bfv);
params.set_poly_modulus_degree(8);
params.set_coeff_modulus({ 65537 });
params.set_plain_modulus(17);
SEALContext context(params, true, sec_level_type::none);
KeyGenerator keygen(context);
auto sk = keygen.secret_key();
PublicKey pk;
keygen.create_public_key(pk);
BatchEncoder encoder(context);
Plaintext pt;
encoder.encode(vector<uint64_t>{ 1, 2, 3 }, pt);
Encryptor encryptor(context, pk);
Ciphertext ct;
encryptor.encrypt(pt, ct);
ct.save(ss);
string ss_str = ss.str();
cout << "length of ss_str: " << ss_str.length() << endl;
stringstream ss_in(ss_str);
Ciphertext ct2;
ct2.load(context, ss_in);
Decryptor decryptor(context, sk);
Plaintext out;
decryptor.decrypt(ct2, out);
vector<uint64_t> res;
encoder.decode(out, res);
for (auto i : res) {
cout << i << endl;
}
return 0;
}
I see. If I inspect the content of Ciphertext:
cout << *ct.data() << endl;
I see some numbers:
07761345085635735669741623097
However, if I save
stringstream ss;
ct.save(ss);
string ss_str = ss.str();
cout << ss_str << endl;
I got messy code:
#/?$_?s?^ge*@$%s*h#r... (messy code)
The problem is that I want to store the number (*ct.data()) as BigInt, rather than the messy code. And while decrypting, I also want to read the BigInt number, not the messy code string, to construct my Ciphertext. Can SEAL achieve this?
The ciphertext isn't a simple flat array of data. You can't just read/write the Ciphertext::data() buffer. Yes, this is the coefficient data, but there are several member variables that you need to handle as well. Moreover, the ciphertext may be in compressed form, in which case it needs to be expanded, which is what the Ciphertext::load(...) function achieves.
I can't understand why you would want to save the ciphertext as any kind of BigInt. That seems really impractical since such types would likely not have been designed to handle possibly several megabytes large objects, like the ciphertexts are. Note also that the saved ciphertext length is not necessarily a multiple of, say, 8 (bytes).
Please also note that can't do any arithmetic with a BigInt type that would translate in any meaningful way to ciphertext arithmetic. Perhaps you are misunderstanding that you could do ciphertext computations with the BigInts? However, ciphertext addition/multiplication are very different from usual integer addition and multiplication, so there isn't anything meaningful you could do with such ciphertext-BigInts.
If in any case you want to do this, I suppose you can always save the ciphertext to a stringstream and copy it byte at a time to your BigInt object.
I obtain my ciphertext and save it to a string. Now I want to construct the class Ciphertext that owns the ciphertext in the string. Here is my code:
and when at ct.load(), I get the following error:
What is the problem and how can I fix this?