secretflow / heu

A high-performance homomorphic encryption algorithm library.
https://www.secretflow.org.cn/docs/heu/en/
Apache License 2.0
83 stars 39 forks source link

[Feature]: How to call the HEU framework in C++ #152

Open yongboy opened 1 month ago

yongboy commented 1 month ago

Feature Request Type

Language binding

Describe the context

Hello,

I have a distributed scenario where one end needs to use C++ to call HEU for encryption and serialization, while the other end needs to use Python to call HEU for decryption. The two ends collaborate through an MQ queue to complete the computation.

I would like to know if there is an existing example in the current project for this use case, or could you please provide guidance on how to achieve this functionality with HEU? Thank you very much!


您好,我有一个分布式的场景,一端需要C++调用HEU完成加密并序列化,另一端需要Python调用HEU完成解密,两端通过MQ队列协作完成运算。

请问,当前的项目有没有这样的示范,或者怎么使用HEU完成这一功能,还请指点一下,多谢!

shaojian-ant commented 3 weeks ago

Hello, You can refer to the C++ test cases available at: GitHub - heu/library/phe/encryptor_test.cc.

// Create the HeKit with the desired schema type
SchemaType schema = SchemaType::Paillier;
HeKit he_kit(schema);

// Obtain the PlainEncoder from HeKit and encode your cleartext
PlainEncoder edr = he_kit.GetEncoder<PlainEncoder>(1);
Plaintext pt = edr.Encode(100);

// Obtain the Encryptor from HeKit and encrypt your plaintext
auto encryptor = he_kit->GetEncryptor();
auto ct = encryptor->Encrypt(pt);

// Serialize the ciphertext
auto buffer = ct.Serialize();

For Python, you can refer to the documentation at: SecretFlow - Getting Started.

To deserialize the ciphertext, you can use the load_from method as shown below:

from heu import phe
buffer = ct.serialize()
ct = phe.Ciphertext.load_from(buffer)