This software implements a library for Paillier's crypto scheme.
- OpenSSL. See the OpenSSL Installation Instructions
- Protocol Buffers. See the Protocol Buffers Installation Instructions
- GoogleTest. You need it to compile and run test cases. See the GoogleTest Installation Instructions
- crypto-bn-cpp. See the crypto-bn-cpp Installation Instructions
- crypto-hash-cpp. See the crypto-hash-cpp Installation Instructions
- crypto-encode-cpp. See the crypto-encode-cpp Installation Instructions
Linux and Mac are supported now. After obtaining the Source, have a look at the installation script.
git clone https://github.com/safeheron/crypto-paillier-cpp.git
cd crypto-paillier-cpp
mkdir build && cd build
# Run "cmake .. -DOPENSSL_ROOT_DIR=Your-Root-Directory-of-OPENSSL" instead of the command below on Mac OS.
# Turn on the switcher to enable tests; by default, turn off it if you don't wanna to build the test cases.
cmake .. -DENABLE_TESTS=ON
# Add the path to the LD_LIBRARY_PATH environment variable on Mac OS; Ignore it on Linux
export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/lib/
make
make test # If you set ENABLE_TESTS ON
sudo make install
More platforms such as Windows would be supported soon.
CMake is your best option. It supports building on Linux, MacOS and Windows (soon) but also has a good chance of working on other platforms (no promises!). cmake has good support for crosscompiling and can be used for targeting the Android platform.
To build crypto-paillier-cpp from source, follow the BUILDING guide.
The canonical way to discover dependencies in CMake is the find_package command.
project(XXXX)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_BUILD_TYPE "Release")
find_package(PkgConfig REQUIRED)
pkg_search_module(PROTOBUF REQUIRED protobuf) # this looks for *.pc file
#set(OPENSSL_USE_STATIC_LIBS TRUE)
find_package(OpenSSL REQUIRED)
find_package(CryptoPaillier REQUIRED)
add_executable(${PROJECT_NAME} XXXX.cpp)
target_include_directories(${PROJECT_NAME} PUBLIC
${CryptoPaillier_INCLUDE_DIRS}
${PROTOBUF_INCLUDE_DIRS}
)
target_link_libraries(${PROJECT_NAME} PUBLIC
CryptoPaillier
OpenSSL::Crypto
${PROTOBUF_LINK_LIBRARIES}
pthread )
#include "crypto-paillier/pail.h"
using namespace std;
using namespace safeheron::bignum;
using safeheron::pail::PailPrivKey;
using safeheron::pail::PailPubKey;
int main(int argc, char **argv) {
PailPrivKey priv;
PailPubKey pub;
CreateKeyPair(priv, pub, 1024);
std::string jsonStr;
priv.ToJsonString(jsonStr);
std::cout << "priv = " << jsonStr << std::endl;
pub.ToJsonString(jsonStr);
std::cout << "pub = " << jsonStr << std::endl;
std::string s;
BN m = safeheron::rand::RandomBNLt(pub.n());
m.ToHexStr(s);
std::cout << "m = " << s << std::endl;
BN c = pub.Encrypt(m);
c.ToHexStr(s);
std::cout << "c = " << s << std::endl;
BN expect_m = priv.Decrypt(c);
expect_m.ToHexStr(s);
std::cout << "expect_m = " << s << std::endl;
EXPECT_EQ(m , expect_m);
return 0;
- Compile the example:
mkdir build && cd build
# Run "cmake .. -DOPENSSL_ROOT_DIR=Your-Root-Directory-of-OPENSSL" instead of the command below on Mac OS.
cmake ..
# Add the path to the LD_LIBRARY_PATH environment variable on Mac OS; Ignore it on Linux
export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/lib/
make
- Run:
./paillier-test
priv = {
"n": "D4A1057480041C7306FFC4B00112B68696183CBA3C3CA29BA3E2FFC92C86A95190C7801410EFBFC804CB299BA90A7098A01F12FA387A5A7A21407E037C37ABE779C754CDAB9CEC22EB323E14CDDD908BE71616D9C3E822F3FA89A1CAD428FCADAD67E6215E524E049AD45C9FE15872BD2B9C7163BA85A102A4CDB1775A0CD2FB",
"lambda": "D4A1057480041C7306FFC4B00112B68696183CBA3C3CA29BA3E2FFC92C86A95190C7801410EFBFC804CB299BA90A7098A01F12FA387A5A7A21407E037C37ABE5A6E4E181C6427C7A2C3542B277C4AA80B10D1C5462D46198A05FB850DC7A5C95CB7DD4635BB8CE5E3C6152FA6A37EA7C90A569140DE66BB5F9126D74CD823C48",
"mu": "5C9088B9715C88B850095DC54DFAE2B6F37DA89E8201DC087447CE474B00B58A48C1E1717521E60EF0F66211A502FF8C33A533FE8C906220B065ED6F5B9C5BDBDA0B1CF32B2700569F2CBB8E47223B09957D831A144514A112FA9E57EBF69A893161D92984A46F0D53C614D7F062AAB549C7661986454BC5D672B0C424D68B8D",
"p": "F155829C57C2BECF83455120BEB5CA81550B5FF064620CB4B3A51D930E92485F729D5A3DE48D0AD48F605754D212C99779337A37F0DD3D206ADAD1D64F14DF67",
"q": "E18CF0AF8D97B0D93BB7AA4197631B89E0FD9A94FCB1B4A6A684CBE6E91C57B86F4CB7801E0C74D1CF12B250A50DBEA921C38E17BBC1F82C40E0722C3D75B74D",
"pSqr": "E3821C7A5BB21B32482EEED8B5A5CEDCB4527930F4E307EC9A70432D46F3D832AE8C9A062FE8B4EDD4CE78106562BA8DC3B7864A8D5FBE31E86EAFC3921DCDAC6B540A71B301B21AFEA720A206EAB9CFA00BC150FB682FADB2733D0B01CC05F3949AC917F7F79306BC32372E850232BF8C8A5F40160088D62B8591C93D0C9B71",
"qSqr": "C6B90CACB93124A697DDB5D2EE67BAFF38F8CE5274DFCC3A988585EAB679C38E9D92D411843E897F3DD2C9B77D0CEED295D6DFE97E9A57630FB6C79AE0C4BC007374B5F86C9ADFAE84B00A242E3B7DCF731A860A48F9ECB86394AA24E25CBDF0397DF0AC803E9E81CCDB60D489A760F4BF1AF54632C80A814D38E5F3C1A12D29",
"pMinus1": "F155829C57C2BECF83455120BEB5CA81550B5FF064620CB4B3A51D930E92485F729D5A3DE48D0AD48F605754D212C99779337A37F0DD3D206ADAD1D64F14DF66",
"qMinus1": "E18CF0AF8D97B0D93BB7AA4197631B89E0FD9A94FCB1B4A6A684CBE6E91C57B86F4CB7801E0C74D1CF12B250A50DBEA921C38E17BBC1F82C40E0722C3D75B74C",
"hp": "6E7A7DCB351B8BB51CC9F87403F81D9D91439FC4EA064B31AC0BAF44272B032C506361E0627D1E59B735BCD632355E6F865CDFE28231C68D892A91465D1FD98F",
"hq": "7A4C269E9F27ED36953C64945A498C6B6EC8700F94FC700DF473ABE4C0B2DEA6FF20CF2219BF9530D382EE21D76A6BBBA44A31A6BDA23EF6473197B929E916F1",
"qInvP": "82DB04D122A7331A667B58ACBABDACE3C3C7C02B7A5BC18307996E4EE76745332239F85D820FEC7AD82A9A7E9FDD6B27F2D69A556EAB7692E1B0408FF1F505D8",
"pInvQ": "6740CA10EE6FC3A2A67B45AD3D198F1E72352A8567B54498B211200228697911702BE85E044CDFA0FB8FC42ECDA352ED7D795C70FE1FB935F9AEDA73138CA05C"
}
pub = {
"n": "D4A1057480041C7306FFC4B00112B68696183CBA3C3CA29BA3E2FFC92C86A95190C7801410EFBFC804CB299BA90A7098A01F12FA387A5A7A21407E037C37ABE779C754CDAB9CEC22EB323E14CDDD908BE71616D9C3E822F3FA89A1CAD428FCADAD67E6215E524E049AD45C9FE15872BD2B9C7163BA85A102A4CDB1775A0CD2FB",
"g": "D4A1057480041C7306FFC4B00112B68696183CBA3C3CA29BA3E2FFC92C86A95190C7801410EFBFC804CB299BA90A7098A01F12FA387A5A7A21407E037C37ABE779C754CDAB9CEC22EB323E14CDDD908BE71616D9C3E822F3FA89A1CAD428FCADAD67E6215E524E049AD45C9FE15872BD2B9C7163BA85A102A4CDB1775A0CD2FC"
}
m = AA5441B80B1F4BD5A0F9A93B95276EB1D53BF4875538DF680052018FF35BA6707E39B45A45D70767E8B6A11B14F628BEBCF047ED33AC4B4D7D828F10EE4AC9311DEF1C3050B87C4AD31E80C7F29B17AB40F20E544E99558ED9A76E9DEAFE6408DB0F41A62C0FECAB59B79B64A5FE564FACDCB70952943407A2E76CB6EE83CFFA
c = 5EBE99E8CAE39A15AD2B7B46A44B6353F871327DA26198DFF74840FB3D5724A2A2CB6889638E310F68830F4DEDF5A69F25B5E4734BD2F0A03609C3C4D5057974F3D15D5C4721A8BF7349FDC6B54EB05EE6C30317A4D91D93F9C6BEE08F2C595ADC34FA353B798488AB7F476AEF7D3EA92252D0E7F48865604E14DE19D7CF89FFFC3128B04C88232ED85EC124715622781E6733E28678DB72B1CD7237F52393D18F3738B13D79D47CF0A2F0169D4C1BD5AF981CE65840C61718B52D68A96EA45535DCE854B1806D5AB687733A6A98A23D802C4C2A1FD9CF0122EB8FEF7ED49FE01DA78B8E72272BD0D4CE2A4CC727BB3E7D90F829A544A8E239BB3AFC6491453D
expect_m = AA5441B80B1F4BD5A0F9A93B95276EB1D53BF4875538DF680052018FF35BA6707E39B45A45D70767E8B6A11B14F628BEBCF047ED33AC4B4D7D828F10EE4AC9311DEF1C3050B87C4AD31E80C7F29B17AB40F20E544E99558ED9A76E9DEAFE6408DB0F41A62C0FECAB59B79B64A5FE564FACDCB70952943407A2E76CB6EE83CFFA
This library is maintained by Safeheron. Contributions are highly welcomed! Besides GitHub issues and PRs, feel free to reach out by mail.