Skip to content

Commit 8cc8009

Browse files
authored
crypto: Hide FieldElement implementation details (#1416)
Refactors the `FieldElement` class to improve encapsulation by converting it from a `struct` to a `class` and hiding implementation details. The internal state (`value_`) and the helper method (`wrap()`) are now properly encapsulated as private members, while maintaining the public API.
1 parent 89a378d commit 8cc8009

1 file changed

Lines changed: 15 additions & 14 deletions

File tree

lib/evmone_precompiles/ecc.hpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,24 @@ concept FieldSpec = requires { T::ORDER; };
2929
///
3030
/// TODO: Combine with BaseFieldElem.
3131
template <FieldSpec Spec>
32-
struct FieldElement
32+
class FieldElement
3333
{
3434
using uint_type = std::remove_const_t<decltype(Spec::ORDER)>;
35-
static constexpr auto& ORDER = Spec::ORDER;
36-
static constexpr ModArith<uint_type> Fp{ORDER};
35+
static constexpr ModArith<uint_type> Fp{Spec::ORDER};
3736

38-
// TODO: Make this private.
39-
uint_type value_{};
37+
uint_type value_;
38+
39+
/// Wraps a value into the Element type assuming it is already in the internal ModArith form.
40+
[[gnu::always_inline]] static constexpr FieldElement wrap(const uint_type& v) noexcept
41+
{
42+
FieldElement element;
43+
element.value_ = v;
44+
return element;
45+
}
46+
47+
public:
48+
/// The alias to the finite field's order.
49+
static constexpr auto& ORDER = Spec::ORDER;
4050

4151
FieldElement() = default;
4252

@@ -102,15 +112,6 @@ struct FieldElement
102112
{
103113
return wrap(Fp.mul(a.value_, Fp.inv(b.value_)));
104114
}
105-
106-
/// Wraps a raw value into the Element type assuming it is already in Montgomery form.
107-
/// TODO: Make this private.
108-
[[gnu::always_inline]] static constexpr FieldElement wrap(const uint_type& v) noexcept
109-
{
110-
FieldElement element;
111-
element.value_ = v;
112-
return element;
113-
}
114115
};
115116

116117
/// The affine (two coordinates) point on an Elliptic Curve over a prime field.

0 commit comments

Comments
 (0)