purify
C++ Purify implementation with native circuit and BPP support
Loading...
Searching...
No Matches
purify::FieldElement Class Reference

Field element modulo the backend scalar field used by this implementation. More...

#include <numeric.hpp>

Public Member Functions

 FieldElement ()
 
UInt256 to_uint256 () const
 Exports the field element as a canonical 256-bit unsigned integer.
 
std::array< unsigned char, 32 > to_bytes_be () const
 Serializes the field element in big-endian form.
 
std::array< unsigned char, 32 > to_bytes_le () const
 Serializes the field element in little-endian form.
 
std::string to_hex () const
 Formats the field element as lowercase hexadecimal.
 
std::string to_decimal () const
 Formats the field element as an unsigned decimal string.
 
bool is_zero () const
 Returns true when the element is zero.
 
bool is_one () const
 Returns true when the element is one.
 
bool is_odd () const
 Returns true when the canonical representative is odd.
 
bool is_square () const
 Returns true when the element is a quadratic residue in the field.
 
FieldElement negate () const
 Returns the additive inverse modulo the field prime.
 
void conditional_assign (const FieldElement &other, bool flag)
 Conditionally assigns other into *this when flag is true.
 
FieldElement inverse_consttime () const
 Returns the multiplicative inverse modulo the field prime in constant time.
 
FieldElement inverse () const
 Returns the multiplicative inverse modulo the field prime using the faster variable-time backend.
 
std::optional< FieldElementsqrt () const
 Computes a square root when one exists, otherwise returns std::nullopt.
 
FieldElement pow (const UInt256 &exponent) const
 Raises the element to an unsigned exponent via square-and-multiply.
 

Static Public Member Functions

static FieldElement zero ()
 Returns the additive identity of the scalar field.
 
static FieldElement one ()
 Returns the multiplicative identity of the scalar field.
 
static FieldElement from_u64 (std::uint64_t value)
 Constructs a field element from an unsigned 64-bit integer.
 
static FieldElement from_int (std::int64_t value)
 Constructs a field element from a signed integer, reducing negatives modulo the field.
 
static Result< FieldElementtry_from_bytes32 (const std::array< unsigned char, 32 > &bytes)
 Decodes a canonical 32-byte big-endian field element.
 
static FieldElement from_bytes32 (const std::array< unsigned char, 32 > &bytes)
 Decodes a 32-byte big-endian field element.
 
static Result< FieldElementtry_from_uint256 (const UInt256 &value)
 Converts a canonical 256-bit unsigned integer into the scalar field representation.
 
static FieldElement from_uint256 (const UInt256 &value)
 Converts a 256-bit unsigned integer into the scalar field representation.
 

Friends

struct detail::FieldElementAccess
 
bool operator== (const FieldElement &lhs, const FieldElement &rhs)
 Compares two field elements for exact equality.
 
bool operator!= (const FieldElement &lhs, const FieldElement &rhs)
 Compares two field elements for inequality.
 
FieldElement operator+ (const FieldElement &lhs, const FieldElement &rhs)
 Adds two field elements modulo the field prime.
 
FieldElement operator- (const FieldElement &lhs, const FieldElement &rhs)
 Subtracts two field elements modulo the field prime.
 
FieldElement operator* (const FieldElement &lhs, const FieldElement &rhs)
 Multiplies two field elements modulo the field prime.
 

Detailed Description

The implementation delegates arithmetic to secp256k1-zkp scalar routines through a thin C bridge so the C++ layer stays header-only.

Definition at line 815 of file numeric.hpp.

Constructor & Destructor Documentation

◆ FieldElement()

purify::FieldElement::FieldElement ( )

Definition at line 28 of file numeric.cpp.

References purify_scalar_set_int().

Referenced by zero().

Member Function Documentation

◆ conditional_assign()

void purify::FieldElement::conditional_assign ( const FieldElement other,
bool  flag 
)

Definition at line 127 of file numeric.cpp.

References purify_scalar_cmov().

◆ from_bytes32()

FieldElement purify::FieldElement::from_bytes32 ( const std::array< unsigned char, 32 > &  bytes)
static

Precondition: the input is canonical and strictly below the field modulus.

Definition at line 63 of file numeric.cpp.

References purify::Expected< T, E >::has_value(), and try_from_bytes32().

◆ from_int()

◆ from_u64()

FieldElement purify::FieldElement::from_u64 ( std::uint64_t  value)
static

Definition at line 40 of file numeric.cpp.

References purify_scalar_set_u64().

Referenced by from_int(), and one().

◆ from_uint256()

FieldElement purify::FieldElement::from_uint256 ( const UInt256 value)
static

Precondition: the integer is strictly below the field modulus.

Definition at line 73 of file numeric.cpp.

References purify::Expected< T, E >::has_value(), and try_from_uint256().

◆ inverse()

FieldElement purify::FieldElement::inverse ( ) const

Definition at line 137 of file numeric.cpp.

References purify_scalar_inverse_var().

◆ inverse_consttime()

FieldElement purify::FieldElement::inverse_consttime ( ) const

Definition at line 131 of file numeric.cpp.

References purify_scalar_inverse().

◆ is_odd()

bool purify::FieldElement::is_odd ( ) const

Definition at line 112 of file numeric.cpp.

References purify_scalar_is_even().

◆ is_one()

bool purify::FieldElement::is_one ( ) const

Definition at line 108 of file numeric.cpp.

References purify_scalar_is_one().

Referenced by purify::ExprBuilder::add_scaled().

◆ is_square()

bool purify::FieldElement::is_square ( ) const

Definition at line 116 of file numeric.cpp.

References purify_fe_is_square().

◆ is_zero()

◆ negate()

◆ one()

FieldElement purify::FieldElement::one ( )
static

◆ pow()

FieldElement purify::FieldElement::pow ( const UInt256 exponent) const

Definition at line 152 of file numeric.cpp.

References purify::BigUInt< Words >::limbs, and purify_fe_pow().

◆ sqrt()

std::optional< FieldElement > purify::FieldElement::sqrt ( ) const

Definition at line 143 of file numeric.cpp.

References purify_fe_sqrt().

◆ to_bytes_be()

std::array< unsigned char, 32 > purify::FieldElement::to_bytes_be ( ) const

◆ to_bytes_le()

std::array< unsigned char, 32 > purify::FieldElement::to_bytes_le ( ) const

Definition at line 90 of file numeric.cpp.

References to_bytes_be().

◆ to_decimal()

std::string purify::FieldElement::to_decimal ( ) const

Definition at line 100 of file numeric.cpp.

References purify::BigUInt< Words >::to_decimal(), and to_uint256().

Referenced by purify::Expr::to_string().

◆ to_hex()

std::string purify::FieldElement::to_hex ( ) const

Definition at line 96 of file numeric.cpp.

References purify::BigUInt< Words >::to_hex(), and to_uint256().

◆ to_uint256()

UInt256 purify::FieldElement::to_uint256 ( ) const

Definition at line 79 of file numeric.cpp.

References purify::BigUInt< 4 >::from_bytes_be(), and to_bytes_be().

Referenced by to_decimal(), and to_hex().

◆ try_from_bytes32()

Result< FieldElement > purify::FieldElement::try_from_bytes32 ( const std::array< unsigned char, 32 > &  bytes)
static

◆ try_from_uint256()

◆ zero()

Friends And Related Symbol Documentation

◆ detail::FieldElementAccess

friend struct detail::FieldElementAccess
friend

Definition at line 912 of file numeric.hpp.

◆ operator!=

bool operator!= ( const FieldElement lhs,
const FieldElement rhs 
)
friend

Definition at line 163 of file numeric.cpp.

◆ operator*

FieldElement operator* ( const FieldElement lhs,
const FieldElement rhs 
)
friend

Definition at line 177 of file numeric.cpp.

◆ operator+

FieldElement operator+ ( const FieldElement lhs,
const FieldElement rhs 
)
friend

Definition at line 167 of file numeric.cpp.

◆ operator-

FieldElement operator- ( const FieldElement lhs,
const FieldElement rhs 
)
friend

Definition at line 173 of file numeric.cpp.

◆ operator==

bool operator== ( const FieldElement lhs,
const FieldElement rhs 
)
friend

Definition at line 159 of file numeric.cpp.


The documentation for this class was generated from the following files: