Closed Ang3er closed 7 years ago
check out the Test_IO
program, you should find examples there.
Thx, I will check it out :)
After checking the code I found out that several issues are already resolved, still Iam wondering how to output and input the public key alone? Or do I have to generate it via the secret key every time?
Thanks Dominik
@Ang3er I hope the following codes can give you some hints.
void gen() {
/// Generate context, pk and sk;, and dump into files.
FHEcontext context(32, 1031, 1);
buildModChain(context, 5);
FHESecKey sk(context);
sk.GenSecKey(64);
FHEPubKey pk = sk;
std::ofstream outstream("pk.pk", std::ios::binary);
if (outstream.is_open()) {
writeContextBase(outstream, context);
outstream << context;
outstream << pk;
outstream.close();
}
std::ofstream o("sk.sk", std::ios::binary);
o << sk;
o.close();
}
void test() {
vector<long> gens, ords;
unsigned long m, p, r;
std::ifstream instream("pk.pk", std::ios::binary);
readContextBase(instream, m, p, r, gens, ords);
FHEcontext context(m, p, r, gens, ords);
instream >> context;
/// standing alone PubKey;
FHEPubKey pk(context);
instream >> pk;
/// To test the pk is exactly the right one; we test whether the sk
/// can correctly decrypt the ciphertext that generated by this pk.
Ctxt c(pk);
pk.Encrypt(c, NTL::to_ZZX(10));
c *= c;
std::ifstream insk("sk.sk", std::ios::binary);
FHESecKey sk(context);
insk >> sk;
NTL::ZZX poly;
sk.Decrypt(poly, c);
std::cout << poly << "\n"; // [100]
}
int main() {
gen();
test();
return 0;
}
Hi fionser,
ah okay you always need to have the context, and therefore genrate the public key with GenSecKey.
Thanks for your, will shortly give a feedback if I managed it :)
Hi guys,
this is a implementation of a c++ commandline interface for the HeLib. Still I have some open questions regarding the actual meaning of parameters like p,r,m,L,d ... The programm itself runs and can be used by calling functions like keyGen, add, encrypt, decrypt.
Hope you can have a look at it and maybe give me some improvements hints, regarding the correct usage of your lib and also about the open questions regarding the WIP comments in the code (see keyGen, encrypt). Thank you in advance, as I do not find a contribution guideline, Iam just posting the code as a github gist at https://gist.github.com/Ang3er/58472b6e6b1916da852caffe8be3a1fe
Hi all,
I tried fionser's example, but when I try to encrypt with imported publicKey from pk.pk and decrypt with original secretKey sk(or vice versa), it fails to decrypt and says "DoubleCRT::Op: incompatible objects".
This is my code: `int main(){
long m=2851, p=5, r=1;
long L=3;
long c=3;
long w=64;
long d=0;
long security = 64;
long ptxtSpace = power_long(p,r);
FHEcontext context(m, p, r);
buildModChain(context, L, c);
FHESecKey sk(context);
sk.GenSecKey(w,ptxtSpace);
FHEPubKey pk = sk;
std::ofstream out("pk.pk", std::ios::binary);
writeContextBase(out, context);
out << context;
out << pk;
out.close();
std::ofstream o("sk.sk", std::ios::binary);
o << sk;
o.close();
unsigned long m1, p1, r1;
vector<long> gens, ords;
std::ifstream ins("pk.pk", std::ios::binary);
readContextBase(ins, m1, p1, r1, gens, ords);
FHEcontext newContext(m1, p1, r1, gens, ords);
ins >> newContext;
FHEPubKey new_pk(newContext);
ins >> new_pk;
// Encrypt with imported pubilcKey new_pk
// and try to decrypr with original sekretKey sk;
Ctxt c1(new_pk);
new_pk.Encrypt(c1, to_ZZX(2));
c1 *= c1;
/*std::ifstream insk("sk.sk", std::ios::binary);
FHESecKey new_sk(newContext);
insk >> new_sk;*/
NTL::ZZX result;
// try to decrypt with original secretKey sk
sk.Decrypt(result, c1); //faild: "DoubleCRT::Op: incompatible objects"
std::cout<<result<<endl; //[4]
return 0;
}`
Actually I want to implement the following scenario: "Alice sends a number encrypted with her pk and wants her friends to add up their chose numbers encrypted using Alice's pk and she wants to decrypt the sum with her secret key sk."
How can I do this? Any Idea?
Thanks!!
@SayyedF , You need to use the same FHEcontext
to construct the sk and pk, because HElib compares the equality of FHEcontext through memory address.
Thanks a lot @fionser
Hello,
I am currrently working on a seperation of concerns for the Helib, meaning doing the keygen, encrypten, calculation ... in first seperated steps, and secondly on different devices. Therefore Iam currently checking on how to serialize, deserialize needed objects of Helib, to following use in and outputstreams to pipe objects in documents (as the secret key, public key, encrypted number, ...) This objects than should be able to be imported from the file base into the corrosponding object (new creation).
Can you help me with this problem, as I am not sure how to do the serialization, without changing your basic code. Other ideas are also welcome.
The bigger context shall be Homomorphic Encryption on CPS devices in a distributed system