Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
modular_arithmetic.cpp
Go to the documentation of this file.
1#include <gmp.h>
3#include <algorithm>
4
5namespace fc {
6
7 std::variant<modular_arithmetic_error, bytes> modexp(const bytes& _base, const bytes& _exponent, const bytes& _modulus)
8 {
9 if (_modulus.size() == 0) {
11 }
12
13 auto output = bytes(_modulus.size(), '\0');
14
15 mpz_t base, exponent, modulus;
16 mpz_inits(base, exponent, modulus, nullptr);
17
18 if (_base.size()) {
19 mpz_import(base, _base.size(), 1, 1, 0, 0, _base.data());
20 }
21
22 if (_exponent.size()) {
23 mpz_import(exponent, _exponent.size(), 1, 1, 0, 0, _exponent.data());
24 }
25
26 mpz_import(modulus, _modulus.size(), 1, 1, 0, 0, _modulus.data());
27
28 if (mpz_sgn(modulus) == 0) {
29 mpz_clears(base, exponent, modulus, nullptr);
30 return output;
31 }
32
33 mpz_t result;
34 mpz_init(result);
35
36 mpz_powm(result, base, exponent, modulus);
37 // export as little-endian
38 mpz_export(output.data(), nullptr, -1, 1, 0, 0, result);
39 // and convert to big-endian
40 std::reverse(output.begin(), output.end());
41
42 mpz_clears(base, exponent, modulus, result, nullptr);
43
44 return output;
45 }
46
47}
namespace sysio::chain
Definition authority.cpp:3
std::variant< modular_arithmetic_error, bytes > modexp(const bytes &_base, const bytes &_exponent, const bytes &_modulus)
std::vector< char > bytes
Definition alt_bn128.hpp:10