github.com/ledgerwatch/erigon-lib@v1.0.0/pedersen_hash/fraction_field_element.inl (about)

     1  #include "fraction_field_element.h"
     2  
     3  #include "error_handling.h"
     4  
     5  namespace starkware {
     6  
     7  template <typename FieldElementT>
     8  FractionFieldElement<FieldElementT> FractionFieldElement<FieldElementT>::operator+(
     9      const FractionFieldElement<FieldElementT>& rhs) const {
    10    const auto num_value = this->numerator_ * rhs.denominator_ + this->denominator_ * rhs.numerator_;
    11    const auto denom_value = this->denominator_ * rhs.denominator_;
    12    return FractionFieldElement(num_value, denom_value);
    13  }
    14  
    15  template <typename FieldElementT>
    16  FractionFieldElement<FieldElementT> FractionFieldElement<FieldElementT>::operator-(
    17      const FractionFieldElement<FieldElementT>& rhs) const {
    18    const auto num_value = this->numerator_ * rhs.denominator_ - this->denominator_ * rhs.numerator_;
    19    const auto denom_value = this->denominator_ * rhs.denominator_;
    20    return FractionFieldElement(num_value, denom_value);
    21  }
    22  
    23  template <typename FieldElementT>
    24  FractionFieldElement<FieldElementT> FractionFieldElement<FieldElementT>::operator*(
    25      const FractionFieldElement<FieldElementT>& rhs) const {
    26    return FractionFieldElement(
    27        this->numerator_ * rhs.numerator_, this->denominator_ * rhs.denominator_);
    28  }
    29  
    30  template <typename FieldElementT>
    31  bool FractionFieldElement<FieldElementT>::operator==(
    32      const FractionFieldElement<FieldElementT>& rhs) const {
    33    return this->numerator_ * rhs.denominator_ == this->denominator_ * rhs.numerator_;
    34  }
    35  
    36  template <typename FieldElementT>
    37  FractionFieldElement<FieldElementT> FractionFieldElement<FieldElementT>::Inverse() const {
    38    ASSERT(this->numerator_ != FieldElementT::Zero(), "Zero does not have an inverse");
    39    return FractionFieldElement(denominator_, numerator_);
    40  }
    41  
    42  }  // namespace starkware