diff --git a/solstdlib/CMakeLists.txt b/solstdlib/CMakeLists.txt index c3bcd9cfa..287e06a46 100644 --- a/solstdlib/CMakeLists.txt +++ b/solstdlib/CMakeLists.txt @@ -1,7 +1,7 @@ # This will re-generate the headers if any file within src was modified. set_directory_properties(PROPERTY CMAKE_CONFIGURE_DEPENDS ${CMAKE_SOURCE_DIR}/solstdlib/src/) -set(STDLIB precompiles) +set(STDLIB cryptography) set(GENERATED_STDLIB_HEADERS) foreach(src IN LISTS STDLIB) set(STDLIB_FILE ${CMAKE_SOURCE_DIR}/solstdlib/src/${src}.sol) diff --git a/solstdlib/solstdlib.h.in b/solstdlib/solstdlib.h.in index aee015681..3c1352d77 100644 --- a/solstdlib/solstdlib.h.in +++ b/solstdlib/solstdlib.h.in @@ -3,13 +3,13 @@ #include #include -#include "solstdlib/precompiles.h" +#include "solstdlib/cryptography.h" namespace solidity::solstdlib { static std::map sources = { - { "std/precompiles.sol", precompiles }, + { "std/cryptography.sol", cryptography }, }; } // namespace solidity::solstdlib diff --git a/solstdlib/src/precompiles.sol b/solstdlib/src/cryptography.sol similarity index 95% rename from solstdlib/src/precompiles.sol rename to solstdlib/src/cryptography.sol index 301cd6c00..097bc6615 100644 --- a/solstdlib/src/precompiles.sol +++ b/solstdlib/src/cryptography.sol @@ -14,8 +14,7 @@ function ripemd160(bytes memory input) returns (bytes20 ret) { assembly { let success := staticcall(gas(), 3, add(input, 32), mload(input), 0, 32) if iszero(success) { revert(0, 0) } - // TODO: check byteorder - ret := mload(0) + ret := shl(96, mload(0)) } } diff --git a/test/libsolidity/semanticTests/stdlib/cryptography.sol b/test/libsolidity/semanticTests/stdlib/cryptography.sol new file mode 100644 index 000000000..1cdcc6c56 --- /dev/null +++ b/test/libsolidity/semanticTests/stdlib/cryptography.sol @@ -0,0 +1,30 @@ +pragma stdlib; + +import "std/cryptography.sol"; + +contract C { + + function f(uint256 a) external returns (bytes32) { + return sha256(abi.encodePacked(a)); + } + + function g(uint256 a) external returns (bytes20) { + return ripemd160(abi.encodePacked(a)); + } + + function h(bytes32 h, uint8 v, bytes32 r, bytes32 s) public returns (address addr) { + return ecrecover(h, v, r, s); + } +} + +// ==== +// EVMVersion: >=constantinople +// ---- +// f(uint256): 1 -> 0xec4916dd28fc4c10d78e287ca5d9cc51ee1ae73cbfde08c6b37324cbfaac8bc5 +// g(uint256): 4 -> 0x1b0f3c404d12075c68c938f9f60ebea4f74941a0000000000000000000000000 +// h(bytes32,uint8,bytes32,bytes32): +// 0x18c547e4f7b0f325ad1e56f57e26c745b09a3e503d86e00e5255ff7f715d3d1c, +// 28, +// 0x73b1693892219d736caba55bdb67216e485557ea6b6af75f37096c9aa6a5a75f, +// 0xeeb940b1d03b21e36b0e47e79769f095fe2ab855bd91e3a38756b7d75a9c4549 +// -> 0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b diff --git a/test/libsolidity/semanticTests/stdlib/ecrecover.sol b/test/libsolidity/semanticTests/stdlib/ecrecover.sol new file mode 100644 index 000000000..9547af38e --- /dev/null +++ b/test/libsolidity/semanticTests/stdlib/ecrecover.sol @@ -0,0 +1,19 @@ +pragma stdlib; + +import { ecrecover } from "std/cryptography.sol"; + +contract C { + function f(bytes32 h, uint8 v, bytes32 r, bytes32 s) public returns (address addr) { + return ecrecover(h, v, r, s); + } +} + +// ==== +// EVMVersion: >=constantinople +// ---- +// f(bytes32,uint8,bytes32,bytes32): +// 0x18c547e4f7b0f325ad1e56f57e26c745b09a3e503d86e00e5255ff7f715d3d1c, +// 28, +// 0x73b1693892219d736caba55bdb67216e485557ea6b6af75f37096c9aa6a5a75f, +// 0xeeb940b1d03b21e36b0e47e79769f095fe2ab855bd91e3a38756b7d75a9c4549 +// -> 0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b diff --git a/test/libsolidity/semanticTests/stdlib/ripemd160.sol b/test/libsolidity/semanticTests/stdlib/ripemd160.sol new file mode 100644 index 000000000..e61567d26 --- /dev/null +++ b/test/libsolidity/semanticTests/stdlib/ripemd160.sol @@ -0,0 +1,14 @@ +pragma stdlib; + +import { ripemd160 } from "std/cryptography.sol"; + +contract C { + function f(uint256 a) external returns (bytes20) { + return ripemd160(abi.encodePacked(a)); + } +} + +// ==== +// EVMVersion: >=constantinople +// ---- +// f(uint256): 4 -> 0x1b0f3c404d12075c68c938f9f60ebea4f74941a0000000000000000000000000 diff --git a/test/libsolidity/semanticTests/stdlib/sha256.sol b/test/libsolidity/semanticTests/stdlib/sha256.sol index f7a152e02..036ab9d52 100644 --- a/test/libsolidity/semanticTests/stdlib/sha256.sol +++ b/test/libsolidity/semanticTests/stdlib/sha256.sol @@ -1,11 +1,11 @@ pragma stdlib; -import "std/precompiles.sol"; +import { sha256 } from "std/cryptography.sol"; contract C { - function f(uint256 a) external returns (bytes32) { - return sha256(abi.encodePacked(a)); - } + function f(uint256 a) external returns (bytes32) { + return sha256(abi.encodePacked(a)); + } } // ====