Skip to content

Commit

Permalink
add support of sm3 hash (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
wenlinlee authored Jun 12, 2024
1 parent 8a97fa7 commit 6717143
Show file tree
Hide file tree
Showing 10 changed files with 527 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ if (NOT USE_SYSTEM_LIBRARIES)
include(fmtlib)
include(nlohmann-json)
include(range-v3)
include_directories(SYSTEM ${JSONCPP_INCLUDE_DIR})
include(ProjectBoost)
endif()

find_package(Threads)
Expand Down
2 changes: 1 addition & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -1777,7 +1777,7 @@ Compiler Features:
* Static Analyzer: Do not warn about unused variables or state mutability for functions with an empty body.
* Type Checker: Add an additional reason to be displayed when type conversion fails.
* Yul: Support object access via ``datasize``, ``dataoffset`` and ``datacopy`` in standalone assembly mode.

* Feature: Add guomi.

Bugfixes:
* Standard JSON Interface: Report specific error message for json input errors instead of internal compiler error.
Expand Down
18 changes: 9 additions & 9 deletions cmake/EthDependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ if (WIN32)
option(Boost_USE_STATIC_RUNTIME "Link Boost against static C++ runtime libraries" ON)
endif()

set(BOOST_COMPONENTS "filesystem;unit_test_framework;program_options;system")
# set(BOOST_COMPONENTS "filesystem;unit_test_framework;program_options;system")

if (WIN32)
# Boost 1.77 fixes a bug that causes crashes on Windows for some relative paths in --allow-paths.
# See https://github.com/boostorg/filesystem/issues/201
find_package(Boost 1.77.0 QUIET REQUIRED COMPONENTS ${BOOST_COMPONENTS})
else()
# Boost 1.65 is the first to also provide boost::get for rvalue-references (#5787).
find_package(Boost 1.65.0 QUIET REQUIRED COMPONENTS ${BOOST_COMPONENTS})
endif()
# if (WIN32)
# # Boost 1.77 fixes a bug that causes crashes on Windows for some relative paths in --allow-paths.
# # See https://github.com/boostorg/filesystem/issues/201
# find_package(Boost 1.77.0 QUIET REQUIRED COMPONENTS ${BOOST_COMPONENTS})
# else()
# # Boost 1.65 is the first to also provide boost::get for rvalue-references (#5787).
# find_package(Boost 1.65.0 QUIET REQUIRED COMPONENTS ${BOOST_COMPONENTS})
# endif()

# If cmake is older than boost and boost is older than 1.70,
# find_package does not define imported targets, so we have to
Expand Down
7 changes: 7 additions & 0 deletions cmake/EthOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ macro(configure_project)
eth_default_option(COVERAGE OFF)
eth_default_option(OSSFUZZ OFF)

# guomi
eth_default_option(BUILD_GM OFF)
if (BUILD_GM)
add_definitions(-DFISCO_GM)
endif()

# components
eth_default_option(TESTS ON)
eth_default_option(TOOLS ON)
Expand All @@ -33,6 +39,7 @@ macro(print_config NAME)
message("--------------------------------------------------------------- features")
message("-- COVERAGE Coverage support ${COVERAGE}")
message("------------------------------------------------------------- components")
message("-- BUILD_GM BUILD GM ${BUILD_GM}")
if (SUPPORT_TESTS)
message("-- TESTS Build tests ${TESTS}")
endif()
Expand Down
119 changes: 119 additions & 0 deletions cmake/ProjectBoost.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
include(ExternalProject)
include(GNUInstallDirs)

set(BOOST_CXXFLAGS "")
if (WIN32)
set(BOOST_BOOTSTRAP_COMMAND bootstrap.bat)
set(BOOST_BUILD_TOOL b2.exe)
set(BOOST_LIBRARY_SUFFIX -vc141-mt-x64-1_68.lib)
elseif(EMSCRIPTEN)
set(BOOST_BOOTSTRAP_COMMAND ./bootstrap.sh)
set(BOOST_BUILD_TOOL ./b2 toolset=emscripten)
set(BOOST_LIBRARY_SUFFIX .bc)
else()
set(BOOST_BOOTSTRAP_COMMAND ./bootstrap.sh)
set(BOOST_BUILD_TOOL ./b2)
set(BOOST_LIBRARY_SUFFIX .a)
if (${BUILD_SHARED_LIBS})
set(BOOST_CXXFLAGS "cxxflags=-fPIC")
endif()
endif()

#set(BOOST_CXXFLAGS "cxxflags=-Wa,-march=generic64")
if(APPLE)
set(BOOST_CXXFLAGS "cxxflags=-fPIC -std=c++14")
else()
set(BOOST_CXXFLAGS "cxxflags=-fPIC")
endif()

ExternalProject_Add(boost
PREFIX ${CMAKE_SOURCE_DIR}/deps
DOWNLOAD_NO_PROGRESS 1
URL https://osp-1257653870.cos.ap-guangzhou.myqcloud.com/FISCO-BCOS/FISCO-BCOS/deps/boost_1_76_0.tar.bz2
https://downloads.sourceforge.net/project/boost/boost/1.76.0/source/boost_1_76_0.tar.bz2
https://nchc.dl.sourceforge.net/project/boost/boost/1.76.0/boost_1_76_0.tar.bz2
URL_HASH SHA256=f0397ba6e982c4450f27bf32a2a83292aba035b827a5623a14636ea583318c41
BUILD_IN_SOURCE 1
CONFIGURE_COMMAND ${BOOST_BOOTSTRAP_COMMAND}
LOG_CONFIGURE 1
BUILD_COMMAND ${BOOST_BUILD_TOOL} stage
${BOOST_CXXFLAGS}
threading=multi
link=static
variant=release
address-model=64
--disable-icu
--with-thread
--with-date_time
--with-system
--with-regex
--with-chrono
--with-filesystem
--with-program_options
--with-random
--with-test
LOG_BUILD 1
INSTALL_COMMAND ""
)

ExternalProject_Get_Property(boost SOURCE_DIR)
set(BOOST_INCLUDE_DIR ${SOURCE_DIR})
set(BOOST_LIB_DIR ${SOURCE_DIR}/stage/lib)

add_library(Boost::system STATIC IMPORTED GLOBAL)
set_property(TARGET Boost::system PROPERTY IMPORTED_LOCATION ${BOOST_LIB_DIR}/libboost_system${BOOST_LIBRARY_SUFFIX})
set_property(TARGET Boost::system PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${BOOST_INCLUDE_DIR})
add_dependencies(Boost::system boost)

add_library(Boost::Chrono STATIC IMPORTED GLOBAL)
set_property(TARGET Boost::Chrono PROPERTY IMPORTED_LOCATION ${BOOST_LIB_DIR}/libboost_chrono${BOOST_LIBRARY_SUFFIX})
add_dependencies(Boost::Chrono boost)

add_library(Boost::DataTime STATIC IMPORTED GLOBAL)
set_property(TARGET Boost::DataTime PROPERTY IMPORTED_LOCATION ${BOOST_LIB_DIR}/libboost_date_time${BOOST_LIBRARY_SUFFIX})
set_property(TARGET Boost::DataTime PROPERTY INTERFACE_LINK_LIBRARIES Boost::system)
add_dependencies(Boost::DataTime boost)

add_library(Boost::Regex STATIC IMPORTED GLOBAL)
set_property(TARGET Boost::Regex PROPERTY IMPORTED_LOCATION ${BOOST_LIB_DIR}/libboost_regex${BOOST_LIBRARY_SUFFIX})
set_property(TARGET Boost::Regex PROPERTY INTERFACE_LINK_LIBRARIES Boost::system)
add_dependencies(Boost::Regex boost)

add_library(Boost::filesystem STATIC IMPORTED GLOBAL)
set_property(TARGET Boost::filesystem PROPERTY IMPORTED_LOCATION ${BOOST_LIB_DIR}/libboost_filesystem${BOOST_LIBRARY_SUFFIX})
set_property(TARGET Boost::filesystem PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${BOOST_INCLUDE_DIR})
set_property(TARGET Boost::filesystem PROPERTY INTERFACE_LINK_LIBRARIES Boost::system)
add_dependencies(Boost::filesystem boost)

add_library(Boost::Random STATIC IMPORTED GLOBAL)
set_property(TARGET Boost::Random PROPERTY IMPORTED_LOCATION ${BOOST_LIB_DIR}/libboost_random${BOOST_LIBRARY_SUFFIX})
add_dependencies(Boost::Random boost)

add_library(Boost::unit_test_framework STATIC IMPORTED GLOBAL)
set_property(TARGET Boost::unit_test_framework PROPERTY IMPORTED_LOCATION ${BOOST_LIB_DIR}/libboost_unit_test_framework${BOOST_LIBRARY_SUFFIX})
set_property(TARGET Boost::unit_test_framework PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${BOOST_INCLUDE_DIR})
add_dependencies(Boost::unit_test_framework boost)

add_library(Boost::Thread STATIC IMPORTED GLOBAL)
set_property(TARGET Boost::Thread PROPERTY IMPORTED_LOCATION ${BOOST_LIB_DIR}/libboost_thread${BOOST_LIBRARY_SUFFIX})
set_property(TARGET Boost::Thread PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${BOOST_INCLUDE_DIR})
set_property(TARGET Boost::Thread PROPERTY INTERFACE_LINK_LIBRARIES Boost::Chrono Boost::DataTime Boost::Regex)
add_dependencies(Boost::Thread boost)

add_library(Boost::program_options STATIC IMPORTED GLOBAL)
set_property(TARGET Boost::program_options PROPERTY IMPORTED_LOCATION ${BOOST_LIB_DIR}/libboost_program_options${BOOST_LIBRARY_SUFFIX})
set_property(TARGET Boost::program_options PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${BOOST_INCLUDE_DIR})
add_dependencies(Boost::program_options boost)

add_library(Boost::Log STATIC IMPORTED GLOBAL)
set_property(TARGET Boost::Log PROPERTY IMPORTED_LOCATION ${BOOST_LIB_DIR}/libboost_log${BOOST_LIBRARY_SUFFIX})
set_property(TARGET Boost::Log PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${BOOST_INCLUDE_DIR})
set_property(TARGET Boost::Log PROPERTY INTERFACE_LINK_LIBRARIES Boost::Filesystem Boost::Thread)
add_dependencies(Boost::Log boost)

add_library(Boost::Serialization STATIC IMPORTED GLOBAL)
set_property(TARGET Boost::Serialization PROPERTY IMPORTED_LOCATION ${BOOST_LIB_DIR}/libboost_serialization${BOOST_LIBRARY_SUFFIX})
set_property(TARGET Boost::Serialization PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${BOOST_INCLUDE_DIR})
add_dependencies(Boost::Serialization boost)

unset(SOURCE_DIR)
7 changes: 7 additions & 0 deletions libsolutil/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ set(sources
Whiskers.h
)

if(BUILD_GM)
aux_source_directory(./sm3 sources)
include_directories(./sm3)
list(REMOVE_ITEM sources Keccak256.cpp)
list(APPEND sources GmHash.cpp)
endif()

add_library(solutil ${sources})
target_link_libraries(solutil PUBLIC Boost::boost Boost::filesystem Boost::system range-v3 fmt::fmt-header-only nlohmann-json)
target_include_directories(solutil PUBLIC "${PROJECT_SOURCE_DIR}")
Expand Down
38 changes: 38 additions & 0 deletions libsolutil/GmHash.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
This file is part of FISCO-BCOS.
FISCO-BCOS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FISCO-BCOS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FISCO-BCOS. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file GmHash.cpp
* @author asherli
* @date 2018
*/

#include "Keccak256.h"

#include "sm3/sm3.h"
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

namespace solidity::util
{
h256 keccak256(bytesConstRef _input)
{
h256 ret;
SM3((unsigned char*) _input.data(), (int) _input.size(), (unsigned char*) ret.data());
return ret;
}

} // namespace dev
Loading

0 comments on commit 6717143

Please sign in to comment.