18purify_fe to_core(
const FieldElement& value) {
19 return purify_fe{detail::FieldElementAccess::raw(value)};
22FieldElement from_core(
const purify_fe& value) {
23 return detail::FieldElementAccess::from_raw(value.
value);
48 return from_u64(
static_cast<std::uint64_t
>(value));
65 assert(out.
has_value() &&
"FieldElement::from_bytes32() requires a canonical field element");
66 return std::move(*out);
75 assert(out.
has_value() &&
"FieldElement::from_uint256() requires a canonical field element");
76 return std::move(*out);
80 std::array<unsigned char, 32> bytes =
to_bytes_be();
85 std::array<unsigned char, 32> bytes{};
91 std::array<unsigned char, 32> bytes =
to_bytes_be();
92 std::reverse(bytes.begin(), bytes.end());
149 return from_core(output);
156 return from_core(output);
164 return !(lhs == rhs);
174 return lhs + rhs.
negate();
187 return from_core(output);
Purify result carrier that either holds a value or an error.
bool has_value() const noexcept
Field element modulo the backend scalar field used by this implementation.
bool is_square() const
Returns true when the element is a quadratic residue in the field.
bool is_one() const
Returns true when the element is one.
std::array< unsigned char, 32 > to_bytes_le() const
Serializes the field element in little-endian form.
static FieldElement from_bytes32(const std::array< unsigned char, 32 > &bytes)
Decodes a 32-byte big-endian field element.
static FieldElement from_uint256(const UInt256 &value)
Converts a 256-bit unsigned integer into the scalar field representation.
FieldElement inverse() const
Returns the multiplicative inverse modulo the field prime using the faster variable-time backend.
FieldElement pow(const UInt256 &exponent) const
Raises the element to an unsigned exponent via square-and-multiply.
bool is_odd() const
Returns true when the canonical representative is odd.
static FieldElement from_u64(std::uint64_t value)
Constructs a field element from an unsigned 64-bit integer.
std::string to_hex() const
Formats the field element as lowercase hexadecimal.
static Result< FieldElement > try_from_uint256(const UInt256 &value)
Converts a canonical 256-bit unsigned integer into the scalar field representation.
std::optional< FieldElement > sqrt() const
Computes a square root when one exists, otherwise returns std::nullopt.
std::string to_decimal() const
Formats the field element as an unsigned decimal string.
static FieldElement one()
Returns the multiplicative identity of the scalar field.
void conditional_assign(const FieldElement &other, bool flag)
Conditionally assigns other into *this when flag is true.
static Result< FieldElement > try_from_bytes32(const std::array< unsigned char, 32 > &bytes)
Decodes a canonical 32-byte big-endian field element.
FieldElement negate() const
Returns the additive inverse modulo the field prime.
static FieldElement from_int(std::int64_t value)
Constructs a field element from a signed integer, reducing negatives modulo the field.
UInt256 to_uint256() const
Exports the field element as a canonical 256-bit unsigned integer.
bool is_zero() const
Returns true when the element is zero.
static FieldElement zero()
Returns the additive identity of the scalar field.
std::array< unsigned char, 32 > to_bytes_be() const
Serializes the field element in big-endian form.
FieldElement inverse_consttime() const
Returns the multiplicative inverse modulo the field prime in constant time.
int purify_fe_is_square(const purify_fe *value)
int purify_fe_legendre_symbol(const purify_fe *value)
int purify_fe_sqrt(purify_fe *out, const purify_fe *value)
void purify_fe_pow(purify_fe *out, const purify_fe *value, const uint64_t exponent[4])
void purify_fe_square(purify_fe *out, const purify_fe *value)
bool operator!=(const FieldElement &lhs, const FieldElement &rhs)
constexpr Unexpected< Error > unexpected_error(ErrorCode code, const char *context=nullptr)
Constructs an unexpected Error value from a machine-readable code.
Expr operator*(const Expr &expr, const FieldElement &scalar)
FieldElement square(const FieldElement &value)
Squares a field element.
bool operator==(const Expr &lhs, const Expr &rhs)
Expr operator-(const Expr &lhs, const Expr &rhs)
int legendre_symbol(const FieldElement &value)
Returns 0 for zero, 1 for quadratic residues, and -1 for non-residues.
Bytes operator+(Bytes lhs, const Bytes &rhs)
Concatenates two byte vectors.
Fixed-width integer and field arithmetic helpers used throughout Purify.
void purify_scalar_mul(purify_scalar *out, const purify_scalar *lhs, const purify_scalar *rhs)
Multiplies two scalars modulo the backend field.
int purify_scalar_is_zero(const purify_scalar *value)
Returns nonzero when the scalar is zero.
int purify_scalar_add(purify_scalar *out, const purify_scalar *lhs, const purify_scalar *rhs)
Adds two scalars modulo the backend field.
void purify_scalar_set_int(purify_scalar *out, unsigned int value)
Initializes a scalar from an unsigned integer.
void purify_scalar_cmov(purify_scalar *dst, const purify_scalar *src, int flag)
Conditionally assigns src into dst when flag is nonzero.
int purify_scalar_is_even(const purify_scalar *value)
Returns nonzero when the scalar is even.
int purify_scalar_is_one(const purify_scalar *value)
Returns nonzero when the scalar is one.
void purify_scalar_inverse(purify_scalar *out, const purify_scalar *value)
Computes the multiplicative inverse of a scalar in constant time.
void purify_scalar_inverse_var(purify_scalar *out, const purify_scalar *value)
Computes the multiplicative inverse of a scalar.
int purify_scalar_eq(const purify_scalar *lhs, const purify_scalar *rhs)
Returns nonzero when two scalars are equal.
void purify_scalar_negate(purify_scalar *out, const purify_scalar *value)
Computes the additive inverse of a scalar.
void purify_scalar_set_u64(purify_scalar *out, uint64_t value)
Initializes a scalar from a 64-bit unsigned integer.
void purify_scalar_get_b32(unsigned char output32[32], const purify_scalar *value)
Serializes a scalar as 32 big-endian bytes.
void purify_scalar_set_b32(purify_scalar *out, const unsigned char input32[32], int *overflow)
Parses a big-endian 32-byte scalar.
static BigUInt from_bytes_be(const unsigned char *data, std::size_t size)
Parses a big-endian byte string into the fixed-width integer.
std::array< std::uint64_t, Words > limbs
std::array< unsigned char, Words *8 > to_bytes_be() const
Serializes the value to a fixed-width big-endian byte array.
std::string to_decimal() const
Formats the value as an unsigned decimal string.
std::string to_hex() const
Formats the value as lowercase hexadecimal without leading zero padding.