Skip to content

Commit b2d0672

Browse files
rodiazetchfast
andauthored
evmmax: Make ModArith header-only and constexpr (ethereum#964)
Move all ModArith's methods to the class definition and make them constexpr. Change evmmax library type to INTERFACE. Co-authored-by: Paweł Bylica <[email protected]>
1 parent 391bd64 commit b2d0672

File tree

6 files changed

+116
-139
lines changed

6 files changed

+116
-139
lines changed

include/evmmax/evmmax.hpp

+84-6
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,112 @@ class ModArith
2121
/// The modulus inversion, i.e. the number N' such that mod⋅N' = 2⁶⁴-1.
2222
const uint64_t m_mod_inv;
2323

24+
/// Compute the modulus inverse for Montgomery multiplication, i.e. N': mod⋅N' = 2⁶⁴-1.
25+
///
26+
/// @param mod0 The least significant word of the modulus.
27+
static constexpr uint64_t compute_mod_inv(uint64_t mod0) noexcept
28+
{
29+
// TODO: Find what is this algorithm and why it works.
30+
uint64_t base = 0 - mod0;
31+
uint64_t result = 1;
32+
for (auto i = 0; i < 64; ++i)
33+
{
34+
result *= base;
35+
base *= base;
36+
}
37+
return result;
38+
}
39+
40+
/// Compute R² % mod.
41+
static constexpr UintT compute_r_squared(const UintT& mod) noexcept
42+
{
43+
// R is 2^num_bits, R² is 2^(2*num_bits) and needs 2*num_bits+1 bits to represent,
44+
// rounded to 2*num_bits+64) for intx requirements.
45+
constexpr auto r2 = intx::uint<UintT::num_bits * 2 + 64>{1} << (UintT::num_bits * 2);
46+
return intx::udivrem(r2, mod).rem;
47+
}
48+
49+
static constexpr std::pair<uint64_t, uint64_t> addmul(
50+
uint64_t t, uint64_t a, uint64_t b, uint64_t c) noexcept
51+
{
52+
const auto p = intx::umul(a, b) + t + c;
53+
return {p[1], p[0]};
54+
}
55+
2456
public:
25-
explicit ModArith(const UintT& modulus) noexcept;
57+
constexpr explicit ModArith(const UintT& modulus) noexcept
58+
: mod{modulus},
59+
m_r_squared{compute_r_squared(modulus)},
60+
m_mod_inv{compute_mod_inv(modulus[0])}
61+
{}
2662

2763
/// Converts a value to Montgomery form.
2864
///
2965
/// This is done by using Montgomery multiplication mul(x, R²)
3066
/// what gives aR²R⁻¹ % mod = aR % mod.
31-
UintT to_mont(const UintT& x) const noexcept;
67+
constexpr UintT to_mont(const UintT& x) const noexcept { return mul(x, m_r_squared); }
3268

3369
/// Converts a value in Montgomery form back to normal value.
3470
///
3571
/// Given the x is the Montgomery form x = aR, the conversion is done by using
3672
/// Montgomery multiplication mul(x, 1) what gives aRR⁻¹ % mod = a % mod.
37-
UintT from_mont(const UintT& x) const noexcept;
73+
constexpr UintT from_mont(const UintT& x) const noexcept { return mul(x, 1); }
3874

3975
/// Performs a Montgomery modular multiplication.
4076
///
4177
/// Inputs must be in Montgomery form: x = aR, y = bR.
4278
/// This computes Montgomery multiplication xyR⁻¹ % mod what gives aRbRR⁻¹ % mod = abR % mod.
4379
/// The result (abR) is in Montgomery form.
44-
UintT mul(const UintT& x, const UintT& y) const noexcept;
80+
constexpr UintT mul(const UintT& x, const UintT& y) const noexcept
81+
{
82+
// Coarsely Integrated Operand Scanning (CIOS) Method
83+
// Based on 2.3.2 from
84+
// High-Speed Algorithms & Architectures For Number-Theoretic Cryptosystems
85+
// https://www.microsoft.com/en-us/research/wp-content/uploads/1998/06/97Acar.pdf
86+
87+
constexpr auto S = UintT::num_words; // TODO(C++23): Make it static
88+
89+
intx::uint<UintT::num_bits + 64> t;
90+
for (size_t i = 0; i != S; ++i)
91+
{
92+
uint64_t c = 0;
93+
for (size_t j = 0; j != S; ++j)
94+
std::tie(c, t[j]) = addmul(t[j], x[j], y[i], c);
95+
auto tmp = intx::addc(t[S], c);
96+
t[S] = tmp.value;
97+
const auto d = tmp.carry; // TODO: Carry is 0 for sparse modulus.
98+
99+
const auto m = t[0] * m_mod_inv;
100+
std::tie(c, std::ignore) = addmul(t[0], m, mod[0], 0);
101+
for (size_t j = 1; j != S; ++j)
102+
std::tie(c, t[j - 1]) = addmul(t[j], m, mod[j], c);
103+
tmp = intx::addc(t[S], c);
104+
t[S - 1] = tmp.value;
105+
t[S] = d + tmp.carry; // TODO: Carry is 0 for sparse modulus.
106+
}
107+
108+
if (t >= mod)
109+
t -= mod;
110+
111+
return static_cast<UintT>(t);
112+
}
45113

46114
/// Performs a modular addition. It is required that x < mod and y < mod, but x and y may be
47115
/// but are not required to be in Montgomery form.
48-
UintT add(const UintT& x, const UintT& y) const noexcept;
116+
constexpr UintT add(const UintT& x, const UintT& y) const noexcept
117+
{
118+
const auto s = addc(x, y); // TODO: cannot overflow if modulus is sparse (e.g. 255 bits).
119+
const auto d = subc(s.value, mod);
120+
return (!s.carry && d.carry) ? s.value : d.value;
121+
}
49122

50123
/// Performs a modular subtraction. It is required that x < mod and y < mod, but x and y may be
51124
/// but are not required to be in Montgomery form.
52-
UintT sub(const UintT& x, const UintT& y) const noexcept;
125+
constexpr UintT sub(const UintT& x, const UintT& y) const noexcept
126+
{
127+
const auto d = subc(x, y);
128+
const auto s = d.value + mod;
129+
return (d.carry) ? s : d.value;
130+
}
53131
};
54132
} // namespace evmmax

lib/evmmax/CMakeLists.txt

+13-9
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
# Copyright 2023 The evmone Authors.
33
# SPDX-License-Identifier: Apache-2.0
44

5-
add_library(evmmax STATIC)
5+
add_library(evmmax INTERFACE)
66
add_library(evmone::evmmax ALIAS evmmax)
7-
target_compile_features(evmmax PUBLIC cxx_std_20)
8-
target_include_directories(evmmax PUBLIC ${PROJECT_SOURCE_DIR}/include)
9-
target_link_libraries(evmmax PUBLIC intx::intx PRIVATE evmc::evmc_cpp)
10-
target_sources(
11-
evmmax PRIVATE
12-
${PROJECT_SOURCE_DIR}/include/evmmax/evmmax.hpp
13-
evmmax.cpp
14-
)
7+
target_compile_features(evmmax INTERFACE cxx_std_20)
8+
target_include_directories(evmmax INTERFACE ${PROJECT_SOURCE_DIR}/include)
9+
target_link_libraries(evmmax INTERFACE intx::intx)
10+
11+
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.19)
12+
# We want to add the header file to the library for IDEs.
13+
# However, cmake 3.18 does not support PRIVATE scope for INTERFACE libraries.
14+
target_sources(
15+
evmmax PRIVATE
16+
${PROJECT_SOURCE_DIR}/include/evmmax/evmmax.hpp
17+
)
18+
endif()

lib/evmmax/evmmax.cpp

-117
This file was deleted.

lib/evmone_precompiles/bn254.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66

77
namespace evmmax::bn254
88
{
9-
109
namespace
1110
{
12-
const ModArith<uint256> Fp{FieldPrime};
13-
const auto B = Fp.to_mont(3);
14-
const auto B3 = Fp.to_mont(3 * 3);
11+
constexpr ModArith Fp{FieldPrime};
12+
constexpr auto B = Fp.to_mont(3);
13+
constexpr auto B3 = Fp.to_mont(3 * 3);
1514
} // namespace
1615

1716
bool validate(const Point& pt) noexcept

lib/evmone_precompiles/secp256k1.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ namespace evmmax::secp256k1
88
{
99
namespace
1010
{
11-
const ModArith<uint256> Fp{FieldPrime};
12-
const auto B = Fp.to_mont(7);
13-
const auto B3 = Fp.to_mont(7 * 3);
11+
constexpr ModArith Fp{FieldPrime};
12+
constexpr auto B = Fp.to_mont(7);
13+
constexpr auto B3 = Fp.to_mont(7 * 3);
1414

1515
constexpr Point G{0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798_u256,
1616
0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8_u256};

test/unittests/evmmax_test.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,19 @@ static auto get_test_values(const Mod& m) noexcept
7171
};
7272
}
7373

74+
[[maybe_unused]] static void constexpr_test()
75+
{
76+
// Make sure ModArith works in constexpr.
77+
static constexpr ModArith m{BN254Mod};
78+
static_assert(m.mod == BN254Mod);
79+
80+
static constexpr auto a = m.to_mont(3);
81+
static constexpr auto b = m.to_mont(11);
82+
static_assert(m.add(a, b) == m.to_mont(14));
83+
static_assert(m.sub(a, b) == m.to_mont(BN254Mod - 8));
84+
static_assert(m.mul(a, b) == m.to_mont(33));
85+
}
86+
7487
TYPED_TEST(evmmax_test, add)
7588
{
7689
const TypeParam m;

0 commit comments

Comments
 (0)