Skip to content

Commit

Permalink
add paillier paillierAdd(bytes,bytes)
Browse files Browse the repository at this point in the history
  • Loading branch information
bxq2011hust committed Dec 7, 2023
1 parent 367e666 commit f336af3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
25 changes: 25 additions & 0 deletions bcos-executor/src/precompiled/extension/PaillierPrecompiled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,14 @@ contract Paillier
#endif

const char* const PAILLIER_METHOD_SET_STR = "paillierAdd(string,string)";
const char* const PAILLIER_METHOD_ADD_RAW_STR = "paillierAdd(bytes,bytes)";

PaillierPrecompiled::PaillierPrecompiled(const crypto::Hash::Ptr& _hashImpl)
: Precompiled(_hashImpl), m_callPaillier(std::make_shared<CallPaillier>())
{
name2Selector[PAILLIER_METHOD_SET_STR] = getFuncSelector(PAILLIER_METHOD_SET_STR, _hashImpl);
name2Selector[PAILLIER_METHOD_ADD_RAW_STR] =
getFuncSelector(PAILLIER_METHOD_ADD_RAW_STR, _hashImpl);
}

PrecompiledExecResult::Ptr PaillierPrecompiled::call(
Expand Down Expand Up @@ -82,6 +85,28 @@ PrecompiledExecResult::Ptr PaillierPrecompiled::call(
getErrorCodeOut(_callParameters->mutableExecResult(), CODE_INVALID_CIPHERS, codec);
}
}
else if (func == name2Selector[PAILLIER_METHOD_ADD_RAW_STR] &&
blockContext.blockVersion() >= uint32_t(bcos::protocol::BlockVersion::V3_6_VERSION))
{ // paillierAdd(bytes,bytes)
bytes cipher1;
bytes cipher2;
codec.decode(data, cipher1, cipher2);
bytes result;
try
{
result = m_callPaillier->paillierAdd(cipher1, cipher2);
gasPricer->appendOperation(InterfaceOpcode::PaillierAdd);
_callParameters->setExecResult(codec.encode(result));
}
catch (CallException& e)
{
PRECOMPILED_LOG(INFO) << LOG_BADGE("PaillierPrecompiled")
<< LOG_DESC(std::string(e.what()))
<< LOG_KV("cipher1", toHex(cipher1))
<< LOG_KV("cipher2", toHex(cipher2));
getErrorCodeOut(_callParameters->mutableExecResult(), CODE_INVALID_CIPHERS, codec);
}
}
else
{
PRECOMPILED_LOG(ERROR) << LOG_BADGE("PaillierPrecompiled")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@ abstract contract PaillierPrecompiled {
function paillierAdd(
string memory cipher1,
string memory cipher2
) public virtual view returns (string memory);
) public virtual returns (string memory);

function paillierAdd(
bytes memory cipher1,
bytes memory cipher2
) public virtual returns (bytes memory);
}
11 changes: 7 additions & 4 deletions cmake/ProjectPaillier.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ endif()

ExternalProject_Add(paillier
PREFIX ${CMAKE_SOURCE_DIR}/deps
DOWNLOAD_NAME paillier-1daf3b23.tar.gz
DOWNLOAD_NAME paillier-e1944826.tar.gz
DOWNLOAD_NO_PROGRESS 1
URL https://github.com/FISCO-BCOS/paillier-lib/archive/1daf3b23b01121e8522a8b264be933f6d236fdb8.tar.gz
https://osp-1257653870.cos.ap-guangzhou.myqcloud.com/FISCO-BCOS/FISCO-BCOS/deps/paillier-1daf3b23.tar.gz
URL_HASH SHA256=574c8315961ea2ba9534a739675172a0e580ca140c9b2a6fb1008aaf608ae1c9
# URL https://github.com/FISCO-BCOS/paillier-lib/archive/1daf3b23b01121e8522a8b264be933f6d236fdb8.tar.gz
# https://osp-1257653870.cos.ap-guangzhou.myqcloud.com/FISCO-BCOS/FISCO-BCOS/deps/paillier-1daf3b23.tar.gz
# URL_HASH SHA256=574c8315961ea2ba9534a739675172a0e580ca140c9b2a6fb1008aaf608ae1c9
URL https://github.com/Shareong/paillier-lib/archive/e1944826ba291a603ff705b3ff8c0cbafa55dcd5.tar.gz
URL_HASH SHA256=bc0cd2d5391961c466651558136432134dd4012e4fdfd4df9d2bcda5fb8404dd

BUILD_IN_SOURCE 1
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
-DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_BUILD_TYPE=Release
Expand Down
9 changes: 8 additions & 1 deletion libinitializer/StorageInitializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ struct RocksDBOption
size_t writeBufferSize = 64 << 20; // 64MB
int minWriteBufferNumberToMerge = 1;
size_t blockCacheSize = 128 << 20; // 128MB
bool enable_blob_files = false;
};

class StorageInitializer
Expand All @@ -54,16 +55,21 @@ class StorageInitializer
rocksdb::DB* db = nullptr;
rocksdb::Options options;
// Note: This option will increase much memory
// options.IncreaseParallelism();
// options.IncreaseParallelism(std::thread::hardware_concurrency());
// Note: This option will increase much memory
// options.OptimizeLevelStyleCompaction();

// create the DB if it's not already present
options.create_if_missing = true;
// to mitigate write stalls
options.max_background_jobs = rocksDBOption.maxBackgroundJobs;
options.max_write_buffer_number = rocksDBOption.maxWriteBufferNumber;
// FIXME: enable blob support when space amplification is acceptable
// options.enable_blob_files = keyPageSize > 1 ? true : false;
options.enable_blob_files = rocksDBOption.enable_blob_files;
options.bytes_per_sync = 1 << 20; // 1MB
// options.level_compaction_dynamic_level_bytes = true;
// options.compaction_pri = rocksdb::kMinOverlappingRatio;
options.compression = rocksdb::kZSTD;
options.bottommost_compression = rocksdb::kZSTD; // last level compression
options.max_open_files = 256;
Expand All @@ -85,6 +91,7 @@ class StorageInitializer
// use bloom filter to optimize point lookup, i.e. get
table_options.filter_policy.reset(rocksdb::NewBloomFilterPolicy(10, false));
table_options.optimize_filters_for_memory = true;
table_options.block_size = 64 * 1024;
// table_options.cache_index_and_filter_blocks = true; // this will increase memory and
// lower performance
options.table_factory.reset(rocksdb::NewBlockBasedTableFactory(table_options));
Expand Down

0 comments on commit f336af3

Please sign in to comment.