Rename precompile.sol and add more tests

This commit is contained in:
Nikola Matic 2022-09-22 14:52:36 +02:00
parent 5f6e606e4d
commit 1bde61c0be
7 changed files with 71 additions and 9 deletions

View File

@ -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)

View File

@ -3,13 +3,13 @@
#include <map>
#include <string>
#include "solstdlib/precompiles.h"
#include "solstdlib/cryptography.h"
namespace solidity::solstdlib
{
static std::map<std::string, std::string> sources = {
{ "std/precompiles.sol", precompiles },
{ "std/cryptography.sol", cryptography },
};
} // namespace solidity::solstdlib

View File

@ -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))
}
}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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));
}
}
// ====