JuliaCrypto / SEAL.jl

SEAL.jl is an easy-to-use wrapper for the original SEAL library and supports homomorphic encryption with the BFV and CKKS schemes.
https://juliacrypto.github.io/SEAL.jl/stable
MIT License
15 stars 3 forks source link

Cannot generate public key #9

Closed narger-ef closed 3 years ago

narger-ef commented 3 years ago

Hello everyone, I'm new with this library so I'm trying the examples but Julia gives me an error when I try to generate a public key, in this line:

public_key_ = public_key(keygen);

Saying

ERROR: could not load symbol KeyGenerator_PublicKey

I noticed that in the header keygenerator.h of the original SEAL library the way to generate a public key is to call _generate_publickey(), could the problem be there?

I also noticed that there used to be a _publickey() generation function in old SEAL versions (<3.6), so probably it's just a renaming procedure, but I'm not very practical with call function =(

Anyone can help? Thank you!

narger-ef commented 3 years ago

Little update: I tried to convert the C++ function in keygenerator.h:

inline void create_public_key(PublicKey &destination) const {
       destination = generate_pk(false);
}

to this function in keygenerator.jl

function create_public_key(destination::PublicKey, keygen::KeyGenerator)
  retval = ccall((:KeyGenerator_CreatePublicKey, libsealc), Clong,
                 (Ptr{Cvoid}, {Ptr{Cvoid}}),
                 keygen, destination)
  @check_return_value retval
  return destination
end

But Julia crashes with a segmentation fault whenever I try to run that function... I would like to update this library to support SEAL 3.6, does anyone know what's going on? Am I writing something wrong?

Thank you

sloede commented 3 years ago

Hej @narger-ef, thanks for posting this issue. You have caught me in a particularly bad moment, I have to admit: the JLL file with the SEAL binaries was recently updated to SEAL 3.6.2, while this library is still supporting the SEAL API as of v3.5.4. I cannot immediately give you an answer to your question, since I first need to have a closer look into what has changed on their side, but I will try to get back to you within the next few days.

At the moment I don't see immediately why your snippet for create_public_key should not work, but it could be related to the fact that you pass destination to ccall, which only returns a C pointer as a scalar, not as a Ref. Thus maybe try something along the following lines:

function create_public_key(destination::PublicKey, keygen::KeyGenerator)
  handleref = Ref{Ptr{Cvoid}}(C_NULL)
  retval = ccall((:KeyGenerator_CreatePublicKey, libsealc), Clong,
                 (Ptr{Cvoid}, {Ptr{Cvoid}}),
                 keygen, keyptr)
  @check_return_value retval
  destination.handle = handleref[]
  return destination
end

This is similar to what we do in https://github.com/JuliaCrypto/SEAL.jl/blob/fd1002ff57d0853ab5070aea4d3362fb834a7efb/src/keygenerator.jl#L33-L40 However, the question remains: Why do you want to pass an already created public key to the function that is supposed to create one for you, instead of just modifying the lines I just referenced into calling KeyGenerator_CreatePublicKey instead of KeyGenerator_PublicKey?

narger-ef commented 3 years ago

Hello, I managed to update the function following your example, I just had to add another null pointer since the method in 3.6 is defined like this:

SEAL_C_FUNC KeyGenerator_CreatePublicKey(void *thisptr, bool save_seed, void **public_key);

By the way your question makes sense and I agree with you, but in (3.6 changelog), they say that:

Changed the names of the public key generation functions to clearly express that a new key is created each time, e.g., KeyGenerator::create_public_key.

And even in (1_bfv_basics.cpp) now the "workflow" is to create an empty PublicKey object and then pass it to create_public_key():

KeyGenerator keygen(context);
SecretKey secret_key = keygen.secret_key();
PublicKey public_key;
keygen.create_public_key(public_key);

So I'm trying to update sticking to the original library.

Anyway I hope that your bad moment will pass as soon as possible! I will let you know if I will eventually be able to update the whole package

Thank you

sloede commented 3 years ago

OK, thanks for letting me know. I have not have worked with this package that much lately, but seeing that there's interest also from other people, I will have a look at it in the next week and let you know once I know how much time it will take me to update everything.

sloede commented 3 years ago

@narger-ef Just to give you an update: in #13 I have fixed all tests and updated the API such that everything works again with SEAL v3.6.2. It will take me a little more time (1-2 days would be my guess), but then I'll merge and create a new release of SEAL.jl (probably v0.4.0). I'll let you know here once this has happened.

narger-ef commented 3 years ago

Wow, thank you so much! Really appreciated!

I'm gonna use this library for some months so this update will absolutely be good for me

sloede commented 3 years ago

@narger-ef I managed to finish everything I wanted to do before the next release, and I just registered v0.4.0 as a new version of SEAL.jl. Please verify that everything works and if yes, feel free to close this issue.

I'm happy to hear that someone else is also using this library, thus in case of questions, please do not hesitate to create new issues!

narger-ef commented 3 years ago

Thank you, I'm going to test it tomorrow.

I am trying to study feasibility of traditional ML algorithms using FHE in Julia, of course I'm going to update if the work has potential.

Thank you again! Take care