Skip to content

Conversation

@chfast
Copy link
Member

@chfast chfast commented Mar 25, 2024

Add C++ implementation of the RIPEMD160 and use it for the precompile 0x03.

@chfast chfast requested review from gumb0 and rodiazet March 25, 2024 19:05
@codecov
Copy link

codecov bot commented Mar 25, 2024

Codecov Report

Attention: Patch coverage is 99.09910% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 98.13%. Comparing base (7f33062) to head (f82140a).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #846      +/-   ##
==========================================
- Coverage   98.13%   98.13%   -0.01%     
==========================================
  Files         123      125       +2     
  Lines       12992    13095     +103     
==========================================
+ Hits        12750    12851     +101     
- Misses        242      244       +2     
Flag Coverage Δ
statetests 62.91% <94.82%> (+0.51%) ⬆️
statetests-silkpre 21.91% <72.72%> (+0.27%) ⬆️
unittests 96.80% <87.38%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
lib/evmone_precompiles/ripemd160.cpp 100.00% <100.00%> (ø)
test/state/precompiles.cpp 90.50% <100.00%> (+0.31%) ⬆️
test/unittests/precompiles_ripemd160_test.cpp 100.00% <100.00%> (ø)
test/state/precompiles_cache.cpp 58.82% <88.88%> (+0.82%) ⬆️

... and 1 file with indirect coverage changes

@chfast chfast force-pushed the precompiles/ripemd160 branch from 085ef67 to f0b686f Compare March 26, 2024 08:41
@chfast chfast added the precompiles Related to EVM precompiles label Mar 26, 2024
@chfast chfast self-assigned this Mar 26, 2024
@chfast chfast force-pushed the precompiles/ripemd160 branch from bf3bd05 to 5187061 Compare April 4, 2024 08:52
@chfast chfast requested a review from battlmonstr April 9, 2024 17:58
{
namespace
{
// TODO(C++23): Use std::byteswap.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider this:
https://github.com/erigontech/silkworm/blob/master/silkworm/db/snapshots/seg/compressor/huffman_code.cpp#L176

I've used __cplusplus instead of TODO(C++23) and it is possible to use intrinsics if you know sizeof(T).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can even use a feature macro: https://en.cppreference.com/w/cpp/feature_test#cpp_lib_byteswap.

Here I was playing a bit with portable C++. I'd slightly uncomfortable to enable C++23 implementation if we don't test C++23 builds.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to not keep TODOs in code if possible due to https://cwe.mitre.org/data/definitions/546

If you want to avoid people trying C++23, it is better to disable it in the global makelist, and in the error message explain how to disable it if they want to still use C++23 at their own risk (maybe with an extra flag).

Using __cplusplus feels simpler to my taste, because once we update our baseline, which is likely to happen, it will be easier to clean up the code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The TODO(C++23) comments mean: after C++23 is enabled you can come here and simplify/improve the code. I actually did this for C++20 and this is not much work but can discover still missing features in compilers.

Maybe we can use different tag than TODO, but it won't solve the CWE issue.

};

/// Converts native representation to/from little-endian.
inline auto to_le(std::integral auto x) noexcept
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similar methods exist in intx::le namespace in case you are able to use intx.hpp here, see: https://github.com/erigontech/silkworm/blob/master/silkworm/core/common/endian.hpp

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On one hand I plan to improve intx's API for this. On the other hand, I wanted to stick to C++ std only for this file.

// tbl[static_cast<size_t>(PrecompileId::ecrecover)].execute = silkpre_ecrecover_execute;
tbl[static_cast<size_t>(PrecompileId::sha256)].execute = silkpre_sha256_execute;
tbl[static_cast<size_t>(PrecompileId::ripemd160)].execute = silkpre_ripemd160_execute;
// tbl[static_cast<size_t>(PrecompileId::ripemd160)].execute = silkpre_ripemd160_execute;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this disabled?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These override evmone's implementations of precompiles with silkpre. This does not make sense any more for RIPEMD160 by default as evmone just got the native implementation which is faster.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe remove it completely instead of commenting?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section gives the visualization what has been replaced so far. The goal is get rid of silkpre dependency.

@chfast chfast force-pushed the precompiles/ripemd160 branch from 5187061 to f82140a Compare April 11, 2024 08:12
@chfast chfast merged commit cd1f3f6 into master Apr 11, 2024
@chfast chfast deleted the precompiles/ripemd160 branch April 11, 2024 08:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

precompiles Related to EVM precompiles

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants