Score:0

HElib: send sk and pk to another party for decryption and encryption

bb flag

This question is about the serialization of pk, sk, and context in HElib. In my scenario, there are two trusted parties (A and B), these two parties can encrypt the messages and decrypt the ciphertexts.

So, A will send the context, pk, and sk to B. Then, A encrypts the message and send ctxtA to B, B decrypts ctxtA and sends another ctxtB to A. This example is just for explanation.

But it's confusing in the implementation of HElib, get the error: context dismatch, sample code is shown below:

    Context* newContext;
    unique_ptr<SecKey> newSecKey;
    unique_ptr<PubKey> newPubKey;

void test::testStruct(){
    unsigned long p = 127;
    unsigned long m = 12800;
    unsigned long r = 1;
    unsigned long bits = 119;
    unsigned long c = 2;
    
    helib::Context context = helib::ContextBuilder<helib::BGV>()
                                .m(m)
                                .p(p)
                                .r(r)
                                .bits(bits)
                                .c(c)
                                .build();
    helib::SecKey oldSk(context);
    oldSk.GenSecKey();
    helib::addSome1DMatrices(oldSk);
    const helib::PubKey& oldPk = oldSk;
    
    stringstream ss;
    context.writeTo(ss);

    newContext = Context::readPtrFrom(ss);
    cout << "Context isEqual: " << (newContext == context) << endl; // result:1

    // new generated pk and sk
    newSecKey = make_unique<SecKey>(*newContext);
    newSecKey->GenSecKey();
    addSome1DMatrices(*newSecKey);
    newPubKey = make_unique<PubKey>(*newSecKey);

    encryptt(*newContext, *newPubKey, *newSecKey); // this works
    //  encryptt(*newContext, *newPubKey, oldSk); fail due to context dismatch
// or  encryptt(*newContext, oldPk, *newSecKey);  fail due to context dismatch
}

void test::encryptt(const Context& con, const PubKey& pk, const SecKey& sk){
    vector<long> inputtest(256);
    for (long i = 0; i< 256; i++) {
        inputtest[i] = i % 2;
    }
    cout << inputtest << endl;
    
    Ctxt ct(pk);
    vector<long> outputtest;

    //Encryption
    con.getEA().encrypt(ct, pk, inputtest);
    //Decryption
    con.getEA().decrypt(ct, sk, outputtest);

    cout << outputtest << endl;
}

Overall, (oldContext, oldSk, oldPk) and (newContext, newSk, newPk) cannot be mixed.

encryptt(*newContext, *newPubKey, oldSk); fail due to context dismatch
// or  encryptt(*newContext, oldPk, *newSecKey);  fail due to context dismatch

But, we can see that the new and old context is equal according to the code:

 cout << "Context isEqual: " << (newContext == context) << endl; // result:1

P.S. Write to and read from the file is not considered, only stringstream.

HElib

Land avatar
in flag
I think you should post this issue on `HElib repository` on GitHub.
Dylan avatar
bb flag
@Land nobody maintains the issues on the HElib repository on GitHub anymore.....many questions are not answered...In case of someone also implements this scenario and could answer me, I post here...
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.