Skip to content

Commit

Permalink
Optimize passing std::span & std::string_view objects to function
Browse files Browse the repository at this point in the history
By passing std::span and std::string_view by value, the compiler can potentially eliminate pointer indirection in the callee and eliminate aliasing.
  • Loading branch information
smlu committed Dec 14, 2023
1 parent f1a8ace commit 4a9e828
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 68 deletions.
28 changes: 14 additions & 14 deletions include/ack/bigint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1286,11 +1286,11 @@ namespace ack {

void print() const
{
const auto data = to_bytes();
if (is_negative()) {
const auto d = to_bytes();
if ( is_negative() ) {
eosio::print("-");
}
eosio::printhex(data.data(), data.size());
eosio::printhex( d.data(), d.size() );
}

constexpr void swap(bigint& rhs) noexcept
Expand Down Expand Up @@ -1374,7 +1374,7 @@ namespace ack {
* @param data - byte data in big-endian byte orientation. If data.size() is 0, this is set to 0.
* @return true if successful i.e. can allocate data.size(), false otherwise.
*/
constexpr bool set_bytes(bytes_view data)
constexpr bool set_bytes(const bytes_view data)
{
is_neg_ = false;
if (data.size() == 0) {
Expand Down Expand Up @@ -1444,9 +1444,9 @@ namespace ack {
*/
bytes to_bytes() const
{
bytes data(byte_length());
get_bytes(data); // TODO: add check fro error. Should probably never return false
return data;
bytes d( byte_length() );
get_bytes( d ); // TODO: add check for error. Should probably never return false
return d;
}

/**
Expand Down Expand Up @@ -1812,11 +1812,11 @@ namespace ack {
static constexpr int div_mods1(bigint* q, const bigint& x, int y)
{
assert(y != invalid_var);
bool xNeg = x.is_neg_;
bool yNeg = y < 0;
word_t absY = detail::cabs(y);
size_t xn = x.size();
int r = 0;
const bool xNeg = x.is_neg_;
const bool yNeg = y < 0;
const word_t absY = detail::cabs(y);
const size_t xn = x.size();
int r = 0;

if (q) {
q->is_neg_ = xNeg ^ yNeg;
Expand Down Expand Up @@ -1846,8 +1846,8 @@ namespace ack {
// TODO: make constexpr when udiv is constexpr
static bool div_mod(bigint* q, bigint& r, const bigint& x, const bigint& y)
{
bool xNeg = x.is_neg_;
bool qsign = xNeg ^ y.is_neg_;
const bool xNeg = x.is_neg_;
const bool qsign = xNeg ^ y.is_neg_;
if (!udiv(q, r, x.buf_, x.size(), y.buf_, y.size())){
return false;
}
Expand Down
14 changes: 7 additions & 7 deletions include/ack/keccak.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ namespace ack {
* @param data - data to hash
* @return 256-bit hash
*/
[[nodiscard]] inline hash256 sha3_256(const bytes_view& data)
[[nodiscard]] inline hash256 sha3_256(const bytes_view data)
{
std::array<byte_t, 32> h;
internal_do_not_use::keccak<17 * 8, 6>(
Expand All @@ -140,7 +140,7 @@ namespace ack {
* @param data - data to hash
* @return 384-bit hash
*/
[[nodiscard]] inline hash384 sha3_384(const bytes_view& data)
[[nodiscard]] inline hash384 sha3_384(const bytes_view data)
{
std::array<byte_t, 48> h;
internal_do_not_use::keccak<13 * 8, 6>(
Expand All @@ -160,7 +160,7 @@ namespace ack {
* @param data - data to hash
* @return 512-bit hash
*/
[[nodiscard]] inline hash512 sha3_512(const bytes_view& data)
[[nodiscard]] inline hash512 sha3_512(const bytes_view data)
{
std::array<byte_t, 64> h;
internal_do_not_use::keccak<9 * 8, 6>(
Expand All @@ -179,7 +179,7 @@ namespace ack {
* @return Size long hash
*/
template<std::size_t Size>
[[nodiscard]] constexpr inline hash_t<Size> shake128_fixed(const bytes_view& data)
[[nodiscard]] constexpr inline hash_t<Size> shake128_fixed(const bytes_view data)
{
using hash = hash_t<Size>;
using word_t = typename hash::word_t;
Expand All @@ -203,7 +203,7 @@ namespace ack {
* @param hash_len - output hash length
* @return hash_len long hash
*/
[[nodiscard]] inline bytes shake128(const bytes_view& data, std::size_t hash_len)
[[nodiscard]] inline bytes shake128(const bytes_view data, std::size_t hash_len)
{
bytes h( hash_len );
internal_do_not_use::keccak<21 * 8, 31>(
Expand All @@ -222,7 +222,7 @@ namespace ack {
* @return Size long hash
*/
template<std::size_t Size>
[[nodiscard]] constexpr inline hash_t<Size> shake256_fixed(const bytes_view& data)
[[nodiscard]] constexpr inline hash_t<Size> shake256_fixed(const bytes_view data)
{
using hash = hash_t<Size>;
using word_t = typename hash::word_t;
Expand All @@ -247,7 +247,7 @@ namespace ack {
* @param hash_len - output hash length
* @return hash_len long hash
*/
[[nodiscard]] inline bytes shake256(const bytes_view& data, std::size_t out_len)
[[nodiscard]] inline bytes shake256(const bytes_view data, std::size_t out_len)
{
bytes h( out_len );
internal_do_not_use::keccak<17 * 8, 31>(
Expand Down
10 changes: 5 additions & 5 deletions include/ack/public_key.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ namespace ack {
bytes_view exponent;

constexpr rsa_public_key_view() {}
constexpr rsa_public_key_view( const bytes_view& mod, const bytes_view& exp ) :
constexpr rsa_public_key_view( const bytes_view mod, const bytes_view exp ) :
modulus( mod ),
exponent( exp )
{}
Expand All @@ -74,24 +74,24 @@ namespace ack {
std::optional<eosio::unsigned_int> pss_salt_len;

constexpr rsa_pss_public_key_view() {}
constexpr rsa_pss_public_key_view( const bytes_view& mod, const bytes_view& exp ) :
constexpr rsa_pss_public_key_view( const bytes_view mod, const bytes_view exp ) :
rsa_public_key_view{ mod, exp }
{}

constexpr rsa_pss_public_key_view( const bytes_view& mod, const bytes_view& exp, std::optional<eosio::unsigned_int> salt_len ) :
constexpr rsa_pss_public_key_view( const bytes_view mod, const bytes_view exp, std::optional<eosio::unsigned_int> salt_len ) :
rsa_public_key_view{ mod, exp },
pss_salt_len( std::move(salt_len) )
{}

constexpr rsa_pss_public_key_view( const bytes_view& mod, const bytes_view& exp, std::optional<uint32_t> salt_len ) :
constexpr rsa_pss_public_key_view( const bytes_view mod, const bytes_view exp, std::optional<uint32_t> salt_len ) :
rsa_public_key_view{ mod, exp },
pss_salt_len( salt_len.has_value()
? std::optional<eosio::unsigned_int>{ salt_len.value() }
: std::nullopt
)
{}

constexpr rsa_pss_public_key_view( const bytes_view& mod, const bytes_view& exp, uint32_t salt_len ) :
constexpr rsa_pss_public_key_view( const bytes_view mod, const bytes_view exp, uint32_t salt_len ) :
rsa_public_key_view{ mod, exp },
pss_salt_len( salt_len )
{}
Expand Down
56 changes: 28 additions & 28 deletions include/ack/rsa.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,19 @@ namespace ack {
static_assert( pkcs1_v1_5_t_sha512_size == 83 );

template<typename HashF>
inline auto eosiohash(const bytes_view& msg, HashF&& hashfunc) {
inline auto eosiohash(const bytes_view msg, HashF&& hashfunc) {
return hashfunc( reinterpret_cast<const char*>( msg.data() ), msg.size() );
}

inline hash160 eosiosha1(const bytes_view& msg) {
inline hash160 eosiosha1(const bytes_view msg) {
return eosiohash( msg, eosio::sha1 );
}

inline hash256 eosiosha256(const bytes_view& msg) {
inline hash256 eosiosha256(const bytes_view msg) {
return eosiohash( msg, eosio::sha256 );
}

inline hash512 eosiosha512(const bytes_view& msg) {
inline hash512 eosiosha512(const bytes_view msg) {
return eosiohash( msg, eosio::sha512 );
}

Expand All @@ -76,7 +76,7 @@ namespace ack {
* @param n - public key modulus
* @return true if s < n else false.
*/
inline bool is_s_valid(const bytes_view& s, std::span<const uint8_t> n) {
inline bool is_s_valid(const bytes_view s, const bytes_view n) {
// Skip leading zeros of s
size_t sofs = 0;
while (sofs < s.size() && s[sofs] == 0x00) {
Expand Down Expand Up @@ -177,7 +177,7 @@ namespace ack {
*
* @return result, the size of vector is the same as modulus
*/
[[nodiscard]] inline bytes powm( const bytes_view& base, const bytes_view& exponent, const bytes_view& modulus ) {
[[nodiscard]] inline bytes powm( const bytes_view base, const bytes_view exponent, const bytes_view modulus ) {
if ( exponent.size() > sizeof(uint64_t) ) {
for (size_t i = 0; i < exponent.size() - sizeof(uint64_t); i++) {
eosio::check( exponent[i] == 0x00, "exponent too big" );
Expand Down Expand Up @@ -247,7 +247,7 @@ namespace ack {
// T generator - https://tools.ietf.org/html/rfc3447#section-9.2
// @param put_hash - function should calculate hash and put the calculated digest to the buffer pointed to by it's argument
template<std::size_t S, typename std::size_t HS>
inline void rsa_1_5_t_generator(std::span<byte_t>& t, const std::array<byte_t, S> digest_info_prefix, const hash_t<HS>& digest)
inline void rsa_1_5_t_generator(std::span<byte_t> t, const std::array<byte_t, S> digest_info_prefix, const hash_t<HS>& digest)
{
memcpy( t.data(), digest_info_prefix.data(), digest_info_prefix.size() );
auto hash = digest.extract_as_byte_array();
Expand All @@ -273,7 +273,7 @@ namespace ack {
[[nodiscard]] static bool rsassa_pss_mgf1_verify(const rsa_pss_public_key_view& pub_key, const hash_t<HLen>& digest, const bytes_view signature, HashF&& hashfunc)
{
// using HDT = typename std::invoke_result_t<HashT, const char*, size_t>;
using HDT = typename std::invoke_result_t<HashF, const bytes_view&>;
using HDT = typename std::invoke_result_t<HashF, const bytes_view>;
static_assert( std::is_same_v<hash_t<HLen>, HDT> );

// 1. Length check
Expand Down Expand Up @@ -353,8 +353,8 @@ namespace ack {
*
* @return false if verification has failed, true if succeeds
*/
[[nodiscard]] inline bool verify_rsa_sha1(const rsa_public_key_view& pub_key, const hash160& digest, const bytes_view& signature) {
return rsassa_pkcs1_v1_5_verify<detail::pkcs1_v1_5_t_sha1_size>( pub_key, signature, [&](std::span<byte_t>&& t) {
[[nodiscard]] inline bool verify_rsa_sha1(const rsa_public_key_view& pub_key, const hash160& digest, const bytes_view signature) {
return rsassa_pkcs1_v1_5_verify<detail::pkcs1_v1_5_t_sha1_size>( pub_key, signature, [&](std::span<byte_t> t) {
rsa_1_5_t_generator( t, detail::sha1_digest_info_prefix, digest );
});
}
Expand All @@ -368,7 +368,7 @@ namespace ack {
* @param signature - RSA PKCS1 v1.5 signature
* @param error - error message to use when verification fails
*/
inline void assert_rsa_sha1(const rsa_public_key_view& pub_key, const hash160& digest, const bytes_view& signature, const char* error) {
inline void assert_rsa_sha1(const rsa_public_key_view& pub_key, const hash160& digest, const bytes_view signature, const char* error) {
eosio::check( verify_rsa_sha1( pub_key, digest, signature ), error );
}

Expand All @@ -383,8 +383,8 @@ namespace ack {
*
* @return false if verification has failed, true if succeeds
*/
[[nodiscard]] inline bool verify_rsa_sha256(const rsa_public_key_view& pub_key, const hash256& digest, const bytes_view& signature) {
return rsassa_pkcs1_v1_5_verify<detail::pkcs1_v1_5_t_sha256_size>( pub_key, signature, [&](std::span<byte_t>&& t) {
[[nodiscard]] inline bool verify_rsa_sha256(const rsa_public_key_view& pub_key, const hash256& digest, const bytes_view signature) {
return rsassa_pkcs1_v1_5_verify<detail::pkcs1_v1_5_t_sha256_size>( pub_key, signature, [&](std::span<byte_t> t) {
rsa_1_5_t_generator( t, detail::sha256_digest_info_prefix, digest );
});
}
Expand All @@ -398,7 +398,7 @@ namespace ack {
* @param signature - RSA PKCS1 v1.5 signature
* @param error - error message to use when verification fails
*/
inline void assert_rsa_sha256(const rsa_public_key_view& pub_key, const hash256& digest, const bytes_view& signature, const char* error) {
inline void assert_rsa_sha256(const rsa_public_key_view& pub_key, const hash256& digest, const bytes_view signature, const char* error) {
eosio::check( verify_rsa_sha256( pub_key, digest, signature ), error );
}

Expand All @@ -413,8 +413,8 @@ namespace ack {
*
* @return false if verification has failed, true if succeeds
*/
[[nodiscard]] inline bool verify_rsa_sha384(const rsa_public_key_view& pub_key, const hash384& digest, const bytes_view& signature) {
return rsassa_pkcs1_v1_5_verify<detail::pkcs1_v1_5_t_sha384_size>( pub_key, signature, [&](std::span<byte_t>&& t) {
[[nodiscard]] inline bool verify_rsa_sha384(const rsa_public_key_view& pub_key, const hash384& digest, const bytes_view signature) {
return rsassa_pkcs1_v1_5_verify<detail::pkcs1_v1_5_t_sha384_size>( pub_key, signature, [&](std::span<byte_t> t) {
rsa_1_5_t_generator( t, detail::sha384_digest_info_prefix, digest );
});
}
Expand All @@ -428,7 +428,7 @@ namespace ack {
* @param signature - RSA PKCS1 v1.5 signature
* @param error - error message to use when verification fails
*/
inline void assert_rsa_sha384(const rsa_public_key_view& pub_key, const hash384& digest, const bytes_view& signature, const char* error) {
inline void assert_rsa_sha384(const rsa_public_key_view& pub_key, const hash384& digest, const bytes_view signature, const char* error) {
eosio::check( verify_rsa_sha384( pub_key, digest, signature ), error );
}

Expand All @@ -443,8 +443,8 @@ namespace ack {
*
* @return false if verification has failed, true if succeeds
*/
[[nodiscard]] inline bool verify_rsa_sha512(const rsa_public_key_view& pub_key, const hash512& digest, const bytes_view& signature) {
return rsassa_pkcs1_v1_5_verify<detail::pkcs1_v1_5_t_sha512_size>( pub_key, signature, [&](std::span<byte_t>&& t) {
[[nodiscard]] inline bool verify_rsa_sha512(const rsa_public_key_view& pub_key, const hash512& digest, const bytes_view signature) {
return rsassa_pkcs1_v1_5_verify<detail::pkcs1_v1_5_t_sha512_size>( pub_key, signature, [&](std::span<byte_t> t) {
rsa_1_5_t_generator( t, detail::sha512_digest_info_prefix, digest );
});
}
Expand All @@ -458,7 +458,7 @@ namespace ack {
* @param signature - RSA PKCS1 v1.5 signature
* @param error - error message to use when verification fails
*/
inline void assert_rsa_sha512(const rsa_public_key_view& pub_key, const hash512& digest, const bytes_view& signature, const char* error) {
inline void assert_rsa_sha512(const rsa_public_key_view& pub_key, const hash512& digest, const bytes_view signature, const char* error) {
eosio::check( verify_rsa_sha512( pub_key, digest, signature ), error );
}

Expand All @@ -473,7 +473,7 @@ namespace ack {
*
* @return false if verification has failed, true if succeeds
*/
[[nodiscard]] inline bool verify_rsa_pss_sha1(const rsa_pss_public_key_view& pub_key, const hash160& digest, const bytes_view& signature) {
[[nodiscard]] inline bool verify_rsa_pss_sha1(const rsa_pss_public_key_view& pub_key, const hash160& digest, const bytes_view signature) {
return rsassa_pss_mgf1_verify( pub_key, digest, signature, detail::eosiosha1 );
}

Expand All @@ -486,7 +486,7 @@ namespace ack {
* @param signature - RSASSA-PSS MGF1 SHA-1 signature
* @param error - error message to use when verification fails
*/
inline void assert_rsa_pss_sha1(const rsa_pss_public_key_view& pub_key, const hash160& digest, const bytes_view& signature, const char* error) {
inline void assert_rsa_pss_sha1(const rsa_pss_public_key_view& pub_key, const hash160& digest, const bytes_view signature, const char* error) {
eosio::check( verify_rsa_pss_sha1( pub_key, digest, signature ), error );
}

Expand All @@ -501,7 +501,7 @@ namespace ack {
*
* @return false if verification has failed, true if succeeds
*/
[[nodiscard]] inline bool verify_rsa_pss_sha256(const rsa_pss_public_key_view& pub_key, const hash256& digest, const bytes_view& signature) {
[[nodiscard]] inline bool verify_rsa_pss_sha256(const rsa_pss_public_key_view& pub_key, const hash256& digest, const bytes_view signature) {
return rsassa_pss_mgf1_verify( pub_key, digest, signature, detail::eosiosha256 );
}

Expand All @@ -514,7 +514,7 @@ namespace ack {
* @param signature - RSASSA-PSS MGF1 SHA-256 signature
* @param error - error message to use when verification fails
*/
inline void assert_rsa_pss_sha256(const rsa_pss_public_key_view& pub_key, const hash256& digest, const bytes_view& signature, const char* error) {
inline void assert_rsa_pss_sha256(const rsa_pss_public_key_view& pub_key, const hash256& digest, const bytes_view signature, const char* error) {
eosio::check( verify_rsa_pss_sha256( pub_key, digest, signature ), error );
}

Expand All @@ -529,7 +529,7 @@ namespace ack {
*
* @return false if verification has failed, true if succeeds
*/
[[nodiscard]] inline bool verify_rsa_pss_sha384(const rsa_pss_public_key_view& pub_key, const hash384& digest, const bytes_view& signature) {
[[nodiscard]] inline bool verify_rsa_pss_sha384(const rsa_pss_public_key_view& pub_key, const hash384& digest, const bytes_view signature) {
return rsassa_pss_mgf1_verify( pub_key, digest, signature, sha384 );
}

Expand All @@ -542,7 +542,7 @@ namespace ack {
* @param signature - RSASSA-PSS MGF1 SHA-384 signature
* @param error - error message to use when verification fails
*/
inline void assert_rsa_pss_sha384(const rsa_pss_public_key_view& pub_key, const hash384& digest, const bytes_view& signature, const char* error) {
inline void assert_rsa_pss_sha384(const rsa_pss_public_key_view& pub_key, const hash384& digest, const bytes_view signature, const char* error) {
eosio::check( verify_rsa_pss_sha384( pub_key, digest, signature ), error );
}

Expand All @@ -557,7 +557,7 @@ namespace ack {
*
* @return false if verification has failed, true if succeeds
*/
[[nodiscard]] inline bool verify_rsa_pss_sha512(const rsa_pss_public_key_view& pub_key, const hash512& digest, const bytes_view& signature) {
[[nodiscard]] inline bool verify_rsa_pss_sha512(const rsa_pss_public_key_view& pub_key, const hash512& digest, const bytes_view signature) {
return rsassa_pss_mgf1_verify( pub_key, digest, signature, detail::eosiosha512 );
}

Expand All @@ -570,7 +570,7 @@ namespace ack {
* @param signature - RSASSA-PSS MGF1 SHA-512 signature
* @param error - error message to use when verification fails
*/
inline void assert_rsa_pss_sha512(const rsa_pss_public_key_view& pub_key, const hash512& digest, const bytes_view& signature, const char* error) {
inline void assert_rsa_pss_sha512(const rsa_pss_public_key_view& pub_key, const hash512& digest, const bytes_view signature, const char* error) {
eosio::check( verify_rsa_pss_sha512( pub_key, digest, signature ), error );
}
}
2 changes: 1 addition & 1 deletion include/ack/sha.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ namespace ack {
* @param data - data to hash
* @return 384-bit hash
*/
[[nodiscard]] inline hash384 sha384(const bytes_view& data)
[[nodiscard]] inline hash384 sha384(const bytes_view data)
{
using namespace internal_do_not_use;
return truncate<sha384_hash_size>(
Expand Down
Loading

0 comments on commit 4a9e828

Please sign in to comment.