|
| 1 | +// evmone: Fast Ethereum Virtual Machine implementation |
| 2 | +// Copyright 2025 The evmone Authors. |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +#include "precompiles_gmp.hpp" |
| 6 | +#include <gmp.h> |
| 7 | +#include <cassert> |
| 8 | + |
| 9 | +namespace evmone::state |
| 10 | +{ |
| 11 | +void expmod_gmp(bytes_view base, bytes_view exp, bytes_view mod, uint8_t* output) noexcept |
| 12 | +{ |
| 13 | + mpz_t b, e, m, r; // NOLINT(*-isolate-declaration) |
| 14 | + mpz_inits(b, e, m, r, nullptr); |
| 15 | + mpz_import(b, base.size(), 1, 1, 0, 0, base.data()); |
| 16 | + mpz_import(e, exp.size(), 1, 1, 0, 0, exp.data()); |
| 17 | + mpz_import(m, mod.size(), 1, 1, 0, 0, mod.data()); |
| 18 | + assert(mpz_sgn(m) != 0); |
| 19 | + |
| 20 | + mpz_powm(r, b, e, m); |
| 21 | + |
| 22 | + size_t export_size = 0; |
| 23 | + mpz_export(output, &export_size, 1, 1, 0, 0, r); |
| 24 | + assert(export_size <= mod.size()); |
| 25 | + mpz_clears(b, e, m, r, nullptr); |
| 26 | + |
| 27 | + std::copy_backward(output, output + export_size, output + mod.size()); |
| 28 | + std::fill_n(output, mod.size() - export_size, 0); |
| 29 | +} |
| 30 | +} // namespace evmone::state |
0 commit comments