From 68beee255a272f0224f12e936bf446ccf033c572 Mon Sep 17 00:00:00 2001 From: bxq2011hust Date: Thu, 7 Dec 2023 16:50:05 +0800 Subject: [PATCH] add paillier paillierAdd(bytes,bytes) --- .../extension/PaillierPrecompiled.cpp | 25 +++++++++++++++++++ .../solidity/PaillierPrecompiled.sol | 7 +++++- .../bcos-framework/ledger/Features.h | 1 + .../unittests/interfaces/FeaturesTest.cpp | 3 ++- cmake/ProjectPaillier.cmake | 11 +++++--- libinitializer/StorageInitializer.h | 9 ++++++- 6 files changed, 49 insertions(+), 7 deletions(-) diff --git a/bcos-executor/src/precompiled/extension/PaillierPrecompiled.cpp b/bcos-executor/src/precompiled/extension/PaillierPrecompiled.cpp index 9501d37855..089cd61b2c 100644 --- a/bcos-executor/src/precompiled/extension/PaillierPrecompiled.cpp +++ b/bcos-executor/src/precompiled/extension/PaillierPrecompiled.cpp @@ -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()) { 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( @@ -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") diff --git a/bcos-executor/src/precompiled/solidity/PaillierPrecompiled.sol b/bcos-executor/src/precompiled/solidity/PaillierPrecompiled.sol index 0b853da8a3..4769f1951a 100644 --- a/bcos-executor/src/precompiled/solidity/PaillierPrecompiled.sol +++ b/bcos-executor/src/precompiled/solidity/PaillierPrecompiled.sol @@ -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); } diff --git a/bcos-framework/bcos-framework/ledger/Features.h b/bcos-framework/bcos-framework/ledger/Features.h index b743c78efa..7a7be54ff9 100644 --- a/bcos-framework/bcos-framework/ledger/Features.h +++ b/bcos-framework/bcos-framework/ledger/Features.h @@ -43,6 +43,7 @@ class Features feature_balance, feature_balance_precompiled, feature_balance_policy1, + feature_paillier_add_raw, }; private: diff --git a/bcos-framework/test/unittests/interfaces/FeaturesTest.cpp b/bcos-framework/test/unittests/interfaces/FeaturesTest.cpp index e1c83a9083..1726fbf6a9 100644 --- a/bcos-framework/test/unittests/interfaces/FeaturesTest.cpp +++ b/bcos-framework/test/unittests/interfaces/FeaturesTest.cpp @@ -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"); @@ -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() diff --git a/cmake/ProjectPaillier.cmake b/cmake/ProjectPaillier.cmake index 863d564571..4f9adf2723 100644 --- a/cmake/ProjectPaillier.cmake +++ b/cmake/ProjectPaillier.cmake @@ -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= -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_BUILD_TYPE=Release diff --git a/libinitializer/StorageInitializer.h b/libinitializer/StorageInitializer.h index 99ae13a43b..daec086f39 100644 --- a/libinitializer/StorageInitializer.h +++ b/libinitializer/StorageInitializer.h @@ -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 @@ -54,9 +55,10 @@ 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 @@ -64,6 +66,10 @@ class StorageInitializer 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; @@ -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));