17 return 32 - __builtin_clz(n - 1);
21namespace sysio {
namespace chain {
namespace webassembly {
35 "Unactivated signature type used during assert_recover_key");
37 "Unactivated key type used when creating assert_recover_key");
39 if(
context.control.is_producing_block())
40 SYS_ASSERT(
s.variable_size() <=
context.control.configured_subjective_signature_length_limit(),
41 sig_variable_size_limit_exception,
"signature variable length component size greater than subjective maximum");
44 SYS_ASSERT( check ==
p, crypto_api_exception,
"Error expected key different than recovered key" );
55 "Unactivated signature type used during recover_key");
57 if(
context.control.is_producing_block())
58 SYS_ASSERT(
s.variable_size() <=
context.control.configured_subjective_signature_length_limit(),
59 sig_variable_size_limit_exception,
"signature variable length component size greater than subjective maximum");
65 if (
s.which() >= config::genesis_num_supported_key_types ) {
67 "destination buffer must at least be able to hold an ECC public key");
69 auto copy_size = std::min<size_t>(
pub.size(), packed_pubkey.size());
70 std::memcpy(
pub.data(), packed_pubkey.data(), copy_size);
71 return packed_pubkey.size();
79 return out_ds.tellp();
84 auto result =
context.trx_context.hash_with_checktime<
fc::sha256>( data.data(), data.size() );
85 SYS_ASSERT( result == *hash_val, crypto_api_exception,
"hash mismatch" );
89 auto result =
context.trx_context.hash_with_checktime<
fc::sha1>( data.data(), data.size() );
90 SYS_ASSERT( result == *hash_val, crypto_api_exception,
"hash mismatch" );
94 auto result =
context.trx_context.hash_with_checktime<
fc::sha512>( data.data(), data.size() );
95 SYS_ASSERT( result == *hash_val, crypto_api_exception,
"hash mismatch" );
99 auto result =
context.trx_context.hash_with_checktime<
fc::ripemd160>( data.data(), data.size() );
100 SYS_ASSERT( result == *hash_val, crypto_api_exception,
"hash mismatch" );
104 *hash_val =
context.trx_context.hash_with_checktime<
fc::sha1>( data.data(), data.size() );
108 *hash_val =
context.trx_context.hash_with_checktime<
fc::sha256>( data.data(), data.size() );
112 *hash_val =
context.trx_context.hash_with_checktime<
fc::sha512>( data.data(), data.size() );
124 if(std::holds_alternative<fc::alt_bn128_error>(maybe_err)) {
128 const auto& res = std::get<bytes>(maybe_err);
130 if( result.size() < res.size() )
133 std::memcpy( result.data(), res.data(), res.size() );
142 if(std::holds_alternative<fc::alt_bn128_error>(maybe_err)) {
146 const auto& res = std::get<bytes>(maybe_err);
148 if( result.size() < res.size() )
151 std::memcpy( result.data(), res.data(), res.size() );
158 auto checktime = [
this]() {
context.trx_context.checktime(); };
160 if(std::holds_alternative<fc::alt_bn128_error>(res)) {
164 return !std::get<bool>(res);
171 if (
context.control.is_producing_block()) {
172 unsigned int base_modulus_size = std::max(base.size(), modulus.
size());
174 if (base_modulus_size < exp.
size()) {
175 SYS_THROW(subjective_block_production_exception,
176 "mod_exp restriction: exponent bit size cannot exceed bit size of either base or modulus");
179 static constexpr uint64_t bit_calc_limit = 106;
181 uint64_t bit_calc = 5 * ceil_log2(exp.
size()) + 8 * ceil_log2(base_modulus_size);
183 if (bit_calc_limit < bit_calc) {
184 SYS_THROW(subjective_block_production_exception,
185 "mod_exp restriction: bit size too large for input arguments");
189 bytes bbase(base.data(), base.data() + base.size());
193 auto maybe_err =
fc::modexp(bbase, bexp, bmod);
194 if(std::holds_alternative<fc::modular_arithmetic_error>(maybe_err)) {
198 const auto& res = std::get<bytes>(maybe_err);
200 if( out.size() < res.size() )
203 std::memcpy( out.data(), res.data(), res.size() );
215 bool _final =
final == 1;
217 bytes bmessage(message.data(), message.data() + message.size());
221 auto checktime = [
this]() {
context.trx_context.checktime(); };
223 auto maybe_err =
fc::blake2b(rounds, bstate, bmessage, bt0_offset, bt1_offset, _final, checktime);
224 if(std::holds_alternative<fc::blake2b_error>(maybe_err)) {
228 const auto& res = std::get<bytes>(maybe_err);
230 if( out.size() < res.size() )
233 std::memcpy( out.data(), res.data(), res.size() );
238 bool _keccak = keccak == 1;
239 const size_t bs = sysio::chain::config::hashing_checktime_block_size;
240 const char* data = input.
data();
244 enc.
write( data, bs);
247 context.trx_context.checktime();
250 auto res = enc.
result(!_keccak);
252 auto copy_size = std::min( output.size(), res.data_size() );
253 std::memcpy( output.data(), res.data(), copy_size );
261 if( std::holds_alternative<fc::k1_recover_error>(maybe_err)) {
265 const auto& res = std::get<bytes>(maybe_err);
267 if(
pub.size() < res.size() )
270 std::memcpy(
pub.data(), res.data(), res.size() );
#define SYS_THROW(exc_type, FORMAT,...)
#define SYS_ASSERT(expr, exc_type, FORMAT,...)
const char * data() const
sha3 result(bool is_nist=true)
void write(const char *d, uint32_t dlen)
Maintains global state information about consensus protocol rules.
uint32_t num_supported_key_types
void assert_recover_key(legacy_ptr< const fc::sha256 > digest, legacy_span< const char > sig, legacy_span< const char > pub) const
int32_t recover_key(legacy_ptr< const fc::sha256 > digest, legacy_span< const char > sig, legacy_span< char > pub) const
void sha3(span< const char > data, span< char > hash_val, int32_t keccak) const
void sha1(legacy_span< const char > data, legacy_ptr< fc::sha1 > hash_val) const
void sha256(legacy_span< const char > data, legacy_ptr< fc::sha256 > hash_val) const
int32_t alt_bn128_mul(span< const char > g1_point, span< const char > scalar, span< char > result) const
int32_t alt_bn128_add(span< const char > op1, span< const char > op2, span< char > result) const
void sha512(legacy_span< const char > data, legacy_ptr< fc::sha512 > hash_val) const
int32_t blake2_f(uint32_t rounds, span< const char > state, span< const char > message, span< const char > t0_offset, span< const char > t1_offset, int32_t final, span< char > result) const
void assert_sha512(legacy_span< const char > data, legacy_ptr< const fc::sha512 > hash_val) const
int32_t k1_recover(span< const char > signature, span< const char > digest, span< char > pub) const
void assert_sha1(legacy_span< const char > data, legacy_ptr< const fc::sha1 > hash_val) const
void assert_ripemd160(legacy_span< const char > data, legacy_ptr< const fc::ripemd160 > hash_val) const
void ripemd160(legacy_span< const char > data, legacy_ptr< fc::ripemd160 > hash_val) const
int32_t mod_exp(span< const char > base, span< const char > exp, span< const char > modulus, span< char > out) const
int32_t alt_bn128_pair(span< const char > g1_g2_pairs) const
void assert_sha256(legacy_span< const char > data, legacy_ptr< const fc::sha256 > hash_val) const
constexpr std::size_t size() const noexcept
constexpr T * data() const noexcept
void unpack(Stream &s, std::deque< T > &value)
void pack(Stream &s, const std::deque< T > &value)
fc::sha256 digest(const T &value)
std::variant< modular_arithmetic_error, bytes > modexp(const bytes &_base, const bytes &_exponent, const bytes &_modulus)
std::variant< blake2b_error, bytes > blake2b(uint32_t _rounds, const bytes &_h, const bytes &_m, const bytes &_t0_offset, const bytes &_t1_offset, bool _f, const yield_function_t &yield)
std::variant< alt_bn128_error, bytes > alt_bn128_add(const bytes &op1, const bytes &op2)
std::variant< k1_recover_error, bytes > k1_recover(const bytes &signature, const bytes &digest)
std::variant< alt_bn128_error, bytes > alt_bn128_mul(const bytes &g1_point, const bytes &scalar)
std::variant< alt_bn128_error, bool > alt_bn128_pair(const bytes &g1_g2_pairs, const yield_function_t &yield)
unsigned __int64 uint64_t