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 778b9c5 commit 68beee2
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 7 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.features().get(ledger::Features::Flag::feature_paillier_add_raw))
{ // 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);
}
1 change: 1 addition & 0 deletions bcos-framework/bcos-framework/ledger/Features.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Features
feature_balance,
feature_balance_precompiled,
feature_balance_policy1,
feature_paillier_add_raw,
};

private:
Expand Down
3 changes: 2 additions & 1 deletion bcos-framework/test/unittests/interfaces/FeaturesTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ BOOST_AUTO_TEST_CASE(feature)

auto keys = Features::featureKeys();

BOOST_CHECK_EQUAL(keys.size(), 13);
BOOST_CHECK_EQUAL(keys.size(), 14);
BOOST_CHECK_EQUAL(keys[0], "bugfix_revert");
BOOST_CHECK_EQUAL(keys[1], "bugfix_statestorage_hash");
BOOST_CHECK_EQUAL(keys[2], "bugfix_evm_create2_delegatecall_staticcall_codecopy");
Expand All @@ -141,6 +141,7 @@ BOOST_AUTO_TEST_CASE(feature)
BOOST_CHECK_EQUAL(keys[10], "feature_balance");
BOOST_CHECK_EQUAL(keys[11], "feature_balance_precompiled");
BOOST_CHECK_EQUAL(keys[12], "feature_balance_policy1");
BOOST_CHECK_EQUAL(keys[13], "feature_paillier_add_raw");
}

BOOST_AUTO_TEST_SUITE_END()
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 68beee2

Please sign in to comment.