Home > Cryptography, thep > Guide to HElib (#3)

Guide to HElib (#3)

To demonstrate HElib and to be able to better play with some of the settings, I’ve written a simple application which implements a RPN calculator. You can grab a copy of it on the thep downloads page. You’ll have to modify Makefile to point the HElib variable to the proper directory. Compile by typing make and run it by typing ./HEcalc. You’ll then be able to do things like:

3

4

*

q

at which point it should return 12.

This is a very preliminary version of HEcalc. I haven’t done anything with ciphertext packing, etc. The purpose of this code is to demonstrate how to add, subtract, and multiply encrypted numbers (along with how to encrypt, decrypt, etc). Play with it some. You’ll notice that by changing the value of L you’ll be able to do more multiplications without the error becoming too great to properly decrypt. You can do as many additions as you want (basically) as the error grows very slowly with additions. Another thing you’ll see when playing with this is that when you decrypt a value, you actually get a number of copies of that value. This is due to ciphertext packing (i.e., storing many values in the same ciphertext). This allows you to do SIMD operations. I hope to detail ciphertext packing and SIMD in the next post. Until then, feel free to use the comments to ask questions about HEcalc and I’ll answer as best as I can.

About these ads
  1. ram
    June 27, 2013 at 2:14 am

    Dear Pwnhome,
    A lot of thanks for this program.I am working with this.But my doubt is that ,will this program works for negative numbers also or not. Because when I give 2 and 3 for subtraction (2-3) I am not getting correct results.
    Thanks in advance

  2. GNZL
    June 28, 2013 at 4:53 am

    Hi ram,

    This program is working with plaintext space Z_101, so the result will be in the range [0,100]. If you do the subtraction (2 – 3) the result will be 100 = -1 mod 101. You can modify the program to give you results in the range [-51,50], instead of p.print(cout) you can do the following:

    vector vl;
    p.decode(vl);
    long result = vl[0];
    if (result > 50) result -= 101;
    cout << result << endl;

    Hope it helps.

  3. October 24, 2013 at 7:28 am

    Dear Pwnhome,

    I’m working on homomorphic encryption and your code is being a great help. Anyway, I need some explanations on SIMD operations and why you get a number of copies of the result, as I would like to store just one copy. When do you plan to write your next post?

    Kind regards

    • pwnhome
      October 24, 2013 at 7:46 am

      Glad you are finding the code useful. I’ve been pretty busy which is why I haven’t done any additional posts. I hope to eventually. SIMD basically means you can hold say 5 different plaintexts in a single ciphertext then operate on all of the plaintexts at the same time. Thus if I want to compute a function f on 5 different values, I pack the 5 values into a single ciphertext and run f once. I then unpack to get the 5 answers. HElib I believe always does SIMD. I don’t think there is a way to disable that (at least not one I am aware of ATM).

  4. December 4, 2013 at 11:10 pm

    Dear Mike, is it possible to talk to you about some crypto via email or Skype or IM. I was looking at your Stack Exchange solution and I wanted to ask why are you proposing to divide mi / 2 or any other number to increase the probability of encryption.

  5. January 10, 2014 at 6:18 am

    Hi again,

    Is there any way to send the ciphertext via socket? I’d like to try serialization, but I’m not sure how it works.

    Thank you!

  6. GNZL
    January 10, 2014 at 6:31 am

    Hello Serch,

    Ctxt class implements operators for outputting the ciphertext in text format and for reading it from text format. You could use the operator << to output the content of a ciphertext to an ostringstring, and the obtain a char* using the methods .str().c_str(), then write this to the socket, in the other side you could rebuild the Ctxt doing the reverse procedure. This surely will have some issues with encodings so another approach would be to implements methods toBinary() and fromBinary that let you write/read the content from/to the Ctxt to/from a char* or ostream/istream.

  7. January 10, 2014 at 7:37 am

    Hi GNZL,

    Thank you for your quick answer. I’m quite new in C++, so I haven’t fully understood your comment. Could you put an example code, please?

    Thank you!

  8. GNZL
    January 10, 2014 at 9:46 am

    You could do something like this.

    Ctxt myctxt(pubKey);
    //Do something wi myctxt, e.g. encrypt some value
    ostringstring oss;
    oss <> receivedctxt;

    //now you should be able to operate with the ctxt

    You will need to have the same pubKey in both ends of the socket in order to make this work.

    Ask me if you need anything else.

  9. January 13, 2014 at 2:37 am

    Hi again, GNZL:

    I can’t exactly understand what you mean. For the moment the client sends the data, but I’m not capable of receiving it. Is this code more or less what you suggest?

    This is the client code:

    EncryptedArray ea(*context, G);
    Ctxt& c = *(new Ctxt(*publicKey));
    PlaintextArray p_encrypt(ea);
    p_encrypt.encode(myValue);
    ea.encrypt(c, *publicKey, p_encrypt);

    std::ostringstream oss;
    oss <> cipher; //This doesn’t work

    Once again, thank you for your help!

  10. GNZL
    January 13, 2014 at 4:26 am

    Hi Serch,

    There was a typo in my code, it should be:

    std::ostringstream oss;
    oss << cipher;

    • January 13, 2014 at 4:42 am

      Ok, it seems that my message was not posted completedly. What I was saying is that I have the client part working (oss <>cipher didn’t work for me). I attach my code so far:

      This is the client part:

      EncryptedArray ea(*context, G);
      Ctxt& c = *(new Ctxt(*publicKey));
      PlaintextArray p_encrypt(ea);
      cout<<"Encriptando valor."<<endl;
      p_encrypt.encode(valor);
      ea.encrypt(c, *publicKey, p_encrypt);

      std::ostringstream oss;
      oss<<c;
      if(send(sock, oss.str().c_str(), oss.str().size(), 0) >cipher; //This line doesn’t work

      Is this code correct?

      Thank you again

      • January 13, 2014 at 4:43 am

        This thing is not posting my comments properly. Another try:

        Client part:

        EncryptedArray ea(*context, G);
        Ctxt& c = *(new Ctxt(*publicKey));
        PlaintextArray p_encrypt(ea);
        cout<<"Encriptando valor."<<endl;
        p_encrypt.encode(valor);
        ea.encrypt(c, *publicKey, p_encrypt);

        std::ostringstream oss;
        oss<<c;
        if(send(sock, oss.str().c_str(), oss.str().size(), 0) < 0){
        puts("\nSend failed");
        return 1;
        }
        close(sock);
        return 0;

  11. January 13, 2014 at 4:44 am

    And server part:

    puts(“Connection accepted”);
    std::ostringstream oss;
    recv(client_sock , oss, oss.srt().size(), 0);
    Ctxt& cipher = *(new Ctxt(*publicKey)); //I’m not sure how to initialize Ctxt
    oss>>cipher; //This line doesn’t work

    Is this code correct?

    Thank you again and sorry for all the messages

  12. GNZL
    January 13, 2014 at 5:04 am

    In the server code i would do something like this:

    int buffer_size = 100000;//Maybe a higher or lower value
    char* buffer = new char[buffer_size];
    int bytes_read = recv(client_sock, buffer, buffer_size. 0);
    string sBuffer((const char*) buffer, bytes_read);
    delete buffer;
    std::istringstream iss;
    iss.str(sBuffer);
    Ctxt cipher(*publicKey);//If you don’t send the public key to the server you have to initialize it there with the same parameters of the client.
    iss >> cipher;

    Try this and tell if it works.

    • January 13, 2014 at 7:49 am

      I think that code should work, but I’m having a “bad vector input” error in the line “iss >> cipher”. However, I’m not sure if it’s because of the server code or the client code. I’m comparing the size in both sides and it’s different; I don’t know if this is relevant.

      • January 13, 2014 at 9:54 am

        Ok, I correct myself. The code worked, I just changed the Ctxt declaration to Ctxt& cipher = *(new Ctxt(*publicKey)); and I put the socket reading clause in a while. The problem was that I wasn’t reading all the data.

        Thank you very much!!!

  13. GNZL
    January 13, 2014 at 10:28 am

    Hi Serch,

    just remember that if you create the Ctxt object with the operator new, you will have to remove it later with “delete” or you will have memory leaks.

    • Urvi
      March 12, 2014 at 4:45 am

      Hi GNZL, I am working on a client server implemetation using the HELib library.
      Its a distributed environment wherein a server would set up the HElib( generating the public private keys) Now i wish to send the public key to all the participating parties so that they can encrypt there data using the public keys and perform some homomorphic operations over it.. I am unable to figure out how can i share/send the public keys to the parties.
      Any input would be a great help..

      Thanks,
      Urvi

  14. waat
    January 16, 2014 at 3:17 am

    Hi everyone, I am not very familiar with c++ in Linux Ubuntu, however I was able to install the library HElib and also to run the HEcalc example. Now, I am using eclipse as IDE to play with the code, but I am not able to build or run from eclipse. Can anyone give any ideas or advice if I am using the correct IDE or I missed steps to compile and run from eclipse?

    • January 16, 2014 at 4:00 am

      I think I can reply this. I am also running HElib with Eclipse (in Windows, though). This are the steps I have followed: New C++ Project / Empty project / CygwinGCC (That’s my case, I suppose it won’t make big difference if you choose a different compiler). Then, I have included all the NTL and HElib header files into the project folder as well as the libraries (libntl.a and libfhe.a). Then, right click in the project properties, C/C++ Build / Settings / Cygwin C++ Compiler / Includes, and add the path to the header files above, and the header files below (this is pretty intuitive). After this, Cygwin C++ Linker, and do the same for the libraries (when adding the libraries above, remember that libntl.a becomes ntl). I think that’s all, in my case, and as I’m also new to Eclipse I also changed the settings in C/C++ General, Paths and Symbols, but I’m not sure if this is needed.

      If you need further assistance, I’ll try to help as far as I’m able.

      • waat
        January 17, 2014 at 8:25 am

        Hi Serch,

        I added the path of the folders (HElib and NTL) into the then ew project as you mentioned; just for testing, I was trying to build the example “HEcalc” from eclipse, but it showed me many errors. Also, I installed eclipse and Cygwin GCC in a Windows machine, I included the same folders, but still I got errors. I was looking for the files (libntl.a and libfhe.a) and I did not find them, how can I get those files? I apreciate any help.

        Thanks in advance.

  15. January 20, 2014 at 2:01 am

    Hi waat,

    To play with HElib, you need to compile the libraries by yourself in order to get the *.a files. I did it with some help, so I’m not sure how it’s exactly done, but I think that you have an example in this guide to HElib (I think it was in the second part). Anyway, I think it’s as easy as to change some paths in the Makefiles.

    First you need to compile NTL in order to get libntl.a, and then you have to use that library to get fhe.a, which you’ll have to rename to libfhe.a

    Anyway, I can recall exactly how I did it, so I hope anyone here can help you better than me. Hope my information helps.

  16. February 5, 2014 at 5:39 am

    Hi, it’s me again,

    Does anybody know if HElib can import secret keys? I’d like to work with my own Certification Authority and all this stuff, and I’ve seen that HElib includes an option to import a secret key, but I don’t know if it’s compatible.

    Thank you for your help.

  17. waat
    February 6, 2014 at 1:29 am

    Hi everyone, I just wanted to know if someone has tried to encrypt a file (eg. txt, doc, xls) by using the HElib, that is because in the examples, there are just working with numbers. Thanks for your attention.

    • pwnhome
      February 11, 2014 at 6:42 pm

      waat, sure you can encrypt a file with HElib, but remember. The entire point of HElib is to compute on encrypted data. Do you want to compute on the encrypted text?

  18. February 11, 2014 at 8:17 am

    I modify my last question: Can I create a X509 Certificate with the keys HElib generates? If so, how can I create it?

    By the way, I haven’t tried encrypting a file, for the moment only numbers are important for me.

    • pwnhome
      February 11, 2014 at 6:41 pm

      Serch, no you cannot create an X509 cert with HElib. Homomorphic encryption has not been standardized.

      • Serch
        February 12, 2014 at 7:12 am

        But can I create a certificate even if it’s not standard? Or import a private key? I’d like to work with certificates but I’m quite new in this.

        Thank you.

  19. pwnhome
    February 12, 2014 at 7:14 am

    Serch :

    But can I create a certificate even if it’s not standard? Or import a private key? I’d like to work with certificates but I’m quite new in this.

    Thank you.

    I’m not familiar with X509 enough to know. If it will let you store an arbitrary key, then probably.

  20. eleftheria
    February 18, 2014 at 7:50 am

    Hey all,

    FYI, I am a newbie in HElib and haven’t coded in C++ for quite some time.

    Anyway, what I am experiencing is a segmentation fault, a couple of seconds after running ./HEcalc (regardless of whether I type anything or not, after getting the greeding messages). From my understanding, there must be something wrong with the setupHELib(), but I haven’t changed anything in the source. Any ideas on what it could be and/or how could I solve it?

    Thank you in advance!

    • pwnhome
      February 20, 2014 at 10:15 am

      I haven’t noticed that. What kind of system are you on (OS, compiler, etc)? Can you attach a debugger and point out what line it crashes on?

      • eleftheria
        March 6, 2014 at 4:43 pm

        I was late in responding, as I was trying to debug it myself and report properly. gdb was pointing me (eventually) to a c++ file (hashtable.h) and thus driving me crazy… I didn’t manage to debug it, but I updated to the latest HElib version and now everything works.

  21. Serch
    February 26, 2014 at 2:53 am

    Hi everyone:

    Does HElib support divisions? Can I divide two numbers? Or can I operate with float variables?

    • pwnhome
      February 26, 2014 at 6:20 am

      I don’t think it supports division or floats. Using the functionality it provides, you could build that capability, but it does not support it out of the box.

  22. Urvi
    March 11, 2014 at 12:59 am

    Dear Sir/Mam,
    I am trying to use the HElib library in my implemntation.. I had a look at the HEcalc code provided by you.. Thanks for posting it, it’s a great help… I had some queries in it:

    1. While setting up Helib , you have assigned the secret key to the public key.
    secretKey = new FHESecKey(*context);
    publicKey = secretKey;
    I could not at all understand this step.

    2. We have to encode our plaintext , so that it replicates the same value in all the n slots.
    what should we do if we want different values in each slot so that we can perform the same operation simultaneoulsy on multiple values…

    Thanks in Advance,
    Waiting for your reply,

    Urvi N

    • XYZ
      April 10, 2014 at 5:31 am

      Hello EveryOne,
      I am big time stuck in my code with a very basic doubt… I tried to print the public key and each time it turned out to be same… Is it that Helib generates the same public key every time for a set of parameters???? Please it’s really urgent.. Can someone please throw some light on this… B’coz if this is true, then the security of Helib is in question…
      Thanks in advance…

      • pwnhome
        April 10, 2014 at 5:34 am

        Can you post your code or a link to it?

      • XYZ
        April 10, 2014 at 5:37 am

        void setUpHelib()
        {
        cout<<"Inside helib setup"<<endl;
        p=101;
        long r=1;
        long L=8;
        long c=2;
        long k=80;
        long s=0;
        long d=0;
        long w=64;
        long m = FindM(k, L, c, p, d, s, 0);

        cout<<"Setting up the HElib…"<alMod.getFactorsOverZZ()[0];

        secretKey = new FHESecKey(*context);
        publicKey = secretKey;

        secretKey->GenSecKey(w);
        addSome1DMatrices(*secretKey);
        ostringstream oss;
        writeContextBase(oss, *context);
        oss<<*context;
        contextStr=oss.str();
        ostringstream oss_pubKey;
        oss_pubKey << *publicKey;
        publicKeyStr=oss_pubKey.str().c_str();
        pubKeyLen=publicKeyStr.length();
        cout<<"pub key="<<publicKeyStr<<endl<<flush; /// it always turns out to be the same public key…
        cout<<"HElib set-up completed successfully"<<endl;
        }

        Let me know if you need anything more….

      • GNZL
        April 10, 2014 at 5:41 am

        You have to seed de PRNG of C and NTL with srand48() and SetSeed() functions.

      • XYZ
        April 10, 2014 at 5:54 am

        Hello GNZL,
        Can you please elaborate a bit. I am unable to figure out where do i need to use the srand48() function. We need just the m, p, r params to generate the Context and this context is used to generate the public key rit?? Now since m,p,r stay constant, the same public key gets generated every time…

        Thanks

      • GNZL
        April 10, 2014 at 7:55 am

        XYZ :
        Hello GNZL,
        Can you please elaborate a bit. I am unable to figure out where do i need to use the srand48() function. We need just the m, p, r params to generate the Context and this context is used to generate the public key rit?? Now since m,p,r stay constant, the same public key gets generated every time…
        Thanks

        Dear XYZ,

        before the line:

        secretKey->GenSecKey(w);

        do something like this:

        srand48(time(0));
        ZZ zzSeed;
        zzSeed = conv(time(0));
        SetSeed(zzSeed);

        You can use any value different from time(0) for the seed.

      • XYZ
        April 11, 2014 at 4:04 am

        Hello GNZL,
        Thanks a lot for your input…. it worked….
        Thanks again….

  23. fed
    April 5, 2014 at 5:50 pm

    Hello thank you for the post it is truly helpful for people who wants to implements HE schem, but i need to ask one question.i implement in steps the helib library and then compiled and executed ./hcacl as it is shown below, but the problem is that i don”t know yet how to encrypt or decrypt the data? the only thing that i can do is do operations on numbers?

  24. fed
    April 12, 2014 at 5:44 pm

    hello how can i make a debugging in order to point in all the modules on the HELIB library?

  25. Serch
    May 21, 2014 at 7:22 am

    Hi again!

    I keep working with HElib. Now I have a new problem: I am trying to write and read the public key from a file. Everything fine when I write it, I just convert it to ostringstream and it works fine. But I cannot convert it back to its own format (FHEPubKey). Can anybody help me? This is my code:

    std::ifstream file(“myFile.txt”);

    std::stringstream aux;
    aux <>(*publicKey);

    Thanks in advance.

    • fed
      June 6, 2014 at 5:38 am

      Hello,

      How can we do function such as ( search in database, count function, and other queries) with only the and and xor gates of fully homomorphic encryption?

      I would really appreciate if you explain to me this i need to understand it.

      Thank you

  26. Abhi007
    May 22, 2014 at 8:35 pm

    I am doing a Finger print authentication process. Th Feature extraction using minutiae has been done and i get an N x 4 matrix, where the 4 columns are {x,y, theta, crossing number}

    The first two are (x,y) co-ordinates of the ith minutiae point detected. The theta determines orientation of the ith minutiae and crossing number detects the type of minutiae(ridge=1,bifurcation=3).

    I want to encrypt this matrix. Perform matching in the encrypted domain and send the result back in encrypted form.(using HElib). Is there idea for doing this. Please help.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: