Program Listing for File integer.h

Return to documentation for file (core/include/math/hal/integer.h)

//==================================================================================
// BSD 2-Clause License
//
// Copyright (c) 2014-2023, NJIT, Duality Technologies Inc. and other contributors
//
// All rights reserved.
//
// Author TPOC: contact@openfhe.org
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
//    list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
//    this list of conditions and the following disclaimer in the documentation
//    and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//==================================================================================

/*
  This file contains the interfaces for the math integer data types
 */

#ifndef LBCRYPTO_MATH_INTEGER_INTERFACE_H
#define LBCRYPTO_MATH_INTEGER_INTERFACE_H

#include "utils/inttypes.h"

#include <string>

namespace lbcrypto {

template <typename T>
class BigIntegerInterface {
public:
    // CONSTRUCTORS

    // Constructors must be implemented in the derived classes
    // There are no base class constructors that need to be called

    // The derived classes should implement constructors from uint64_t,
    // NativeInteger, and strings There should be copy and move constructors, as
    // well as copy and move assignment

    void SetValue(const std::string& str);

    // ARITHMETIC OPERATIONS

    T Add(const T& b) const;
    T& AddEq(const T& b);

    friend T operator+(const T& a, const T& b) {
        return a.Add(b);
    }
    friend T& operator+=(T& a, const T& b) {
        return a.AddEq(b);
    }

    T Sub(const T& b) const;
    T& SubEq(const T& b);

    friend T operator-(const T& a, const T& b) {
        return a.Sub(b);
    }
    friend T& operator-=(T& a, const T& b) {
        return a.SubEq(b);
    }

    T Mul(const T& b) const;

    T& MulEq(const T& b);

    friend T operator*(const T& a, const T& b) {
        return a.Mul(b);
    }
    friend T& operator*=(T& a, const T& b) {
        return a.MulEq(b);
    }

    T DividedBy(const T& b) const;

    T& DividedByEq(const T& b);

    friend T operator/(const T& a, const T& b) {
        return a.DividedBy(b);
    }
    friend T& operator/=(T& a, const T& b) {
        return a.DividedByEq(b);
    }

    T MultiplyAndRound(const T& p, const T& q) const;
    T& MultiplyAndRoundEq(const T& p, const T& q);

    T DivideAndRound(const T& q) const;
    T& DivideAndRoundEq(const T& q);

    // MODULAR ARITHMETIC OPERATIONS

    T Mod(const T& modulus) const;
    T& ModEq(const T& modulus);

    // inline operators for the modulus operation.
    friend T operator%(const T& a, const T& b) {
        return a.Mod(b);
    }
    friend T& operator%=(T& a, const T& b) {
        return a.ModEq(b);
    }

    T ComputeMu() const;

    T Mod(const T& modulus, const T& mu) const;
    T& ModEq(const T& modulus, const T& mu);

    T ModAdd(const T& b, const T& modulus) const;
    T& ModAddEq(const T& b, const T& modulus);

    T ModAddFast(const T& b, const T& modulus) const;
    T& ModAddFastEq(const T& b, const T& modulus);

    T ModAdd(const T& b, const T& modulus, const T& mu) const;
    T& ModAddEq(const T& b, const T& modulus, const T& mu);

    T ModSub(const T& b, const T& modulus) const;
    T& ModSubEq(const T& b, const T& modulus);

    T ModSubFast(const T& b, const T& modulus) const;
    T& ModSubFastEq(const T& b, const T& modulus);

    T ModSub(const T& b, const T& modulus, const T& mu) const;
    T& ModSubEq(const T& b, const T& modulus, const T& mu);

    T ModMul(const T& b, const T& modulus) const;
    T& ModMulEq(const T& b, const T& modulus);

    T ModMul(const T& b, const T& modulus, const T& mu) const;
    T& ModMulEq(const T& b, const T& modulus, const T& mu);

    T ModMulFast(const T& b, const T& modulus) const;
    T& ModMulFastEq(const T& b, const T& modulus);

    T ModMulFast(const T& b, const T& modulus, const T& mu) const;
    T& ModMulFastEq(const T& b, const T& modulus, const T& mu);

    T ModMulFastConst(const T& b, const T& modulus, const T& bInv) const;
    T& ModMulFastConstEq(const T& b, const T& modulus, const T& bInv);

    T ModExp(const T& b, const T& modulus) const;
    T& ModExpEq(const T& b, const T& modulus);

    T ModInverse(const T& modulus) const;
    T& ModInverseEq(const T& modulus);

    // SHIFT OPERATIONS

    T LShift(usshort shift) const;
    T& LShiftEq(usshort shift);

    friend T operator<<(const T& a, usshort shift) {
        return a.LShift(shift);
    }
    friend T& operator<<=(T& a, usshort shift) {
        return a.LShiftEq(shift);
    }

    T RShift(usshort shift) const;
    T& RShiftEq(usshort shift);

    friend T operator>>(const T& a, usshort shift) {
        return a.RShift(shift);
    }
    friend T& operator>>=(T& a, usshort shift) {
        return a.RShiftEq(shift);
    }

    int Compare(const T& a) const;

    friend bool operator==(const T& a, const T& b) {
        return a.Compare(b) == 0;
    }
    friend bool operator!=(const T& a, const T& b) {
        return a.Compare(b) != 0;
    }
    friend bool operator>(const T& a, const T& b) {
        return a.Compare(b) > 0;
    }
    friend bool operator>=(const T& a, const T& b) {
        return a.Compare(b) >= 0;
    }
    friend bool operator<(const T& a, const T& b) {
        return a.Compare(b) < 0;
    }
    friend bool operator<=(const T& a, const T& b) {
        return a.Compare(b) <= 0;
    }

    uint64_t ConvertToInt() const;

    // OTHER FUNCTIONS

    usint GetMSB() const;

    usint GetLengthForBase(usint base) const;

    usint GetDigitAtIndexForBase(usint index, usint base) const;

    // STRINGS

    // The derived classes MAY implement std::ostream& operator<< but are not
    // required to

    const std::string ToString() const;

protected:
    ~BigIntegerInterface() = default;

    // SERIALIZATION
};

// TODO
class BigMatrixInterface {};
}  // namespace lbcrypto

#endif