Skip to content

Commit

Permalink
Fixed mupliple issues: general and for Emscripten
Browse files Browse the repository at this point in the history
  • Loading branch information
dsuponitskiy-duality committed Nov 2, 2023
1 parent a705999 commit 40a89d8
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 56 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,9 @@ if( "${NATIVE_SIZE}" EQUAL 128 )
message(SEND_ERROR "Cannot support NATIVE_SIZE == 128")
endif()
elseif( "${NATIVE_SIZE}" EQUAL 64 )
if ( EMSCRIPTEN )
set( HAVE_INT128 FALSE)
endif()
if( ${HAVE_INT64} )
set( NATIVEINT 64 )
message (STATUS "NATIVEINT is set to " ${NATIVEINT})
Expand Down
8 changes: 8 additions & 0 deletions configure/config_core.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
//==================================================
// DO NOT TOUCH THIS FILE! It is generated by cmake
//==================================================
#ifndef __CMAKE_GENERATED_CONFIG_CORE_H__
#define __CMAKE_GENERATED_CONFIG_CORE_H__

#cmakedefine WITH_BE2
#cmakedefine WITH_BE4
#cmakedefine WITH_NOISE_DEBUG
Expand All @@ -9,3 +15,5 @@
#cmakedefine HAVE_INT64 @HAVE_INT64@
#cmakedefine MATHBACKEND @MATHBACKEND@
#cmakedefine NATIVEINT @NATIVEINT@

#endif // __CMAKE_GENERATED_CONFIG_CORE_H__
62 changes: 35 additions & 27 deletions src/core/include/math/hal/basicint.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,46 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//==================================================================================

#ifndef __BASICINT_H__
#define __BASICINT_H__

#include "config_core.h"
#include <cstdint>

#ifndef __BASICINT_H__
#define __BASICINT_H__

#if NATIVEINT == 128 && !defined(__EMSCRIPTEN__)
#define MAX_MODULUS_SIZE 121
using BasicInteger = unsigned __int128;
using DoubleNativeInt = unsigned __int128;
using uint128_t = unsigned __int128;
using int128_t = __int128;
#elif NATIVEINT == 64 && defined(HAVE_INT128)
// clang-format off
#if NATIVEINT == 128
#define MAX_MODULUS_SIZE 121
using BasicInteger = unsigned __int128;
using DoubleNativeInt = unsigned __int128;
using uint128_t = unsigned __int128;
using int128_t = __int128;
#elif NATIVEINT == 64
#if defined(HAVE_INT128)
#define MAX_MODULUS_SIZE 60
using BasicInteger = uint64_t;
using DoubleNativeInt = unsigned __int128;
using uint128_t = unsigned __int128;
using int128_t = __int128;
#elif NATIVEINT == 64 && !defined(HAVE_INT128)
#define MAX_MODULUS_SIZE 58
using BasicInteger = uint64_t;
using DoubleNativeInt = uint64_t;
using uint128_t = uint64_t;
using int128_t = int64_t;
#elif NATIVEINT == 32
#define MAX_MODULUS_SIZE 28
using BasicInteger = uint32_t;
using DoubleNativeInt = uint64_t;
using uint128_t = uint64_t;
using int128_t = int64_t;
using BasicInteger = uint64_t;
using DoubleNativeInt = unsigned __int128;
using uint128_t = unsigned __int128;
using int128_t = __int128;
#else
#error "Configuration Error: basicint.h"
#if defined EMSCRIPTEN
#define MAX_MODULUS_SIZE 57
#else
#define MAX_MODULUS_SIZE 58
#endif
using BasicInteger = uint64_t;
using DoubleNativeInt = uint64_t;
using uint128_t = uint64_t;
using int128_t = int64_t;
#endif
#elif NATIVEINT == 32
#define MAX_MODULUS_SIZE 28
using BasicInteger = uint32_t;
using DoubleNativeInt = uint64_t;
using uint128_t = uint64_t;
using int128_t = int64_t;
#else
#error "Configuration Error: basicint.h"
#endif
// clang-format on

#endif // __BASICINT_H__
13 changes: 7 additions & 6 deletions src/core/include/math/hal/bigintdyn/ubintdyn.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,14 @@
#include <utility>
#include <vector>

#if NATIVEINT >= 64
// TODO: fix shifting issues with expdtype = uint128_t
// using expdtype = BasicInteger;
using expdtype = uint64_t;
#elif NATIVEINT == 32
using expdtype = BasicInteger;
// clang-format off
// TODO: fix shifting issue when limb_t == Dlimb_t
#if (NATIVEINT >= 64 && defined(HAVE_INT128))
using expdtype = uint64_t;
#else
using expdtype = uint32_t;
#endif
// clang-format on

#define _SECURE_SCL 0 // to speed up VS
#define NO_BARRETT // currently barrett is slower than mod
Expand Down
8 changes: 5 additions & 3 deletions src/core/include/math/hal/intnat/mubintvecnat.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,11 @@ class NativeVectorT final : public lbcrypto::BigVectorInterface<NativeVectorT<In
* @param value is the modulus value to set.
*/
void SetModulus(const IntegerType& value) {
if (value.GetMSB() > MAX_MODULUS_SIZE)
OPENFHE_THROW(lbcrypto::not_available_error,
"NativeVectorT supports only modulus size <= " + std::to_string(MAX_MODULUS_SIZE) + " bits");
if (value.GetMSB() > MAX_MODULUS_SIZE) {
std::string errMsg{"Requested modulus' size " + std::to_string(value.GetMSB()) + " is not supported."};
errMsg += " NativeVectorT supports only modulus size <= " + std::to_string(MAX_MODULUS_SIZE);
OPENFHE_THROW(lbcrypto::not_available_error, errMsg);
}
m_modulus.m_value = value.m_value;
}

Expand Down
12 changes: 12 additions & 0 deletions src/core/include/math/nbtheory-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,18 @@ IntType FirstPrime(uint64_t nBits, uint64_t m) {
}
}

template <typename IntType>
IntType GetMaxPrime(uint64_t nBits, uint64_t m) {
IntType q = FirstPrime<IntType>(nBits, m);
IntType ret = PreviousPrime<IntType>(q, m);
if (ret.GetMSB() != nBits) {
std::string errMsg{"Can not find a prime for an integer with " + std::to_string(nBits) + " bits"};
errMsg += " and cyclotomic order of " + std::to_string(m) + ". Please adjust parameters";
OPENFHE_THROW(lbcrypto::not_available_error, errMsg);
}
return ret;
}

template <typename IntType>
IntType NextPrime(const IntType& q, uint64_t m) {
IntType M(m), qNew(q + M);
Expand Down
11 changes: 11 additions & 0 deletions src/core/include/math/nbtheory.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,17 @@ void PrimeFactorize(IntType n, std::set<IntType>& primeFactors);
template <typename IntType>
IntType FirstPrime(uint64_t nBits, uint64_t m);

/**
* Gets the max prime number for the given number of bits and the cyclotomic order.
*
* @param nBits the number of bits needs to be in the returned prime
* @param m cyclotomic order
*
* @return the first prime modulus
*/
template <typename IntType>
IntType GetMaxPrime(uint64_t nBits, uint64_t m);

/**
* Finds the next prime that satisfies q = 1 mod m
*
Expand Down
7 changes: 4 additions & 3 deletions src/core/include/utils/get-call-stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@
#ifndef __GET_CALL_STACK_H__
#define __GET_CALL_STACK_H__

#if defined(__clang__) || defined(__GNUG__)
#if defined(__linux__) || defined(__GNUG__)
// clang-format off
#include <string>
#include <vector>
// clang-format on

/**
* @brief get_call_stack() is a function to get the call stack
Expand All @@ -42,5 +44,4 @@
std::vector<std::string> get_call_stack();
#endif

#endif // __GET_CALL_STACK_H__

#endif // __GET_CALL_STACK_H__
28 changes: 15 additions & 13 deletions src/core/lib/utils/get-call-stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,27 @@
// 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.
//==================================================================================
#if defined(__clang__) || defined(__GNUG__)
// clang-format off
#if defined(__linux__) || defined(__GNUG__)
#include "utils/get-call-stack.h"
#include "utils/demangle.h"

#include <execinfo.h>
#include <cxxabi.h>
#include <memory>
// clang-format on

namespace {
enum { MAX_BACKTRACE_ADDRESSES = 512 };
enum { MAX_BACKTRACE_ADDRESSES = 512 };
}

static bool stringEmpty(const std::string& str) {
if(!str.length())
if (!str.length())
return true;

// str is not empty if it has any printable character
for(const char c : str) {
if(c >= 33 && c <= 126)
// str is not empty if it has any printable character
for (const char c : str) {
if (c >= 33 && c <= 126)
return false;
}

Expand All @@ -55,15 +57,15 @@ static bool stringEmpty(const std::string& str) {

std::vector<std::string> get_call_stack() {
void* bt_buffer[MAX_BACKTRACE_ADDRESSES] = {NULL};
const int n = backtrace(bt_buffer, MAX_BACKTRACE_ADDRESSES);
if(n < 1) {
const int n = backtrace(bt_buffer, MAX_BACKTRACE_ADDRESSES);
if (n < 1) {
return std::vector<std::string>();
}
const std::unique_ptr<char*> symbols(backtrace_symbols(bt_buffer, n));

const size_t numSymbols = static_cast<size_t>(n);
std::vector<std::string> ret(numSymbols);
for( size_t i = 0; i < numSymbols; ++i ) {
for (size_t i = 0; i < numSymbols; ++i) {
std::string symbol(symbols.get()[i]);
// we need to get rid of anything that doesn't belong to the name
// Mangled symbol examples:
Expand All @@ -72,16 +74,16 @@ std::vector<std::string> get_call_stack() {
// /lib/libOPENFHEpke.so.1(_ZNK8lbcrypto25ParameterGenerationBGVRNS15ParamsGenBGVRNSESt10shared_ptrINS_20CryptoParametersBaseINS_12DCRTPolyImplIN9bigintdyn9mubintvecINS4_5ubintImEEEEEEEEEjjjjjjjj+0x44a) [0x7f1b5cf6a09a]
// 1. we may have "+", so we search to find the last one to trim "symbol" from the right
size_t pos = symbol.find_last_of("+");
symbol = symbol.substr(0, pos);
symbol = symbol.substr(0, pos);
// 2. find the last "(" which indicates the beginning of the actual mangled symbol enclosed in to "()"
pos = symbol.find_last_of("(");
pos = symbol.find_last_of("(");
size_t newLen = symbol.length() - pos;
std::string mangledName(symbol.substr(pos+1, newLen));
std::string mangledName(symbol.substr(pos + 1, newLen));

ret[i] = (stringEmpty(mangledName)) ? symbols.get()[i] : demangle(mangledName.c_str());
}

return ret;
}

#endif
#endif
2 changes: 1 addition & 1 deletion src/pke/include/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ enum NOISE_FLOODING {
// number of additional moduli in NOISE_FLOODING_MULTIPARTY mode
NUM_MODULI_MULTIPARTY = 2,
// modulus size for additional moduli in NOISE_FLOODING_MULTIPARTY mode
#if NATIVEINT == 128 && !defined(__EMSCRIPTEN__)
#if NATIVEINT == 128
MULTIPARTY_MOD_SIZE = 60,
#else
MULTIPARTY_MOD_SIZE = MAX_MODULUS_SIZE,
Expand Down
6 changes: 3 additions & 3 deletions src/pke/include/scheme/ckksrns/ckksrns-fhe.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,6 @@ class FHECKKSRNS : public FHERNS {
using ParmType = typename DCRTPoly::Params;

public:
// key tuple is dim1, levelBudgetEnc, levelBudgetDec
std::map<uint32_t, std::shared_ptr<CKKSBootstrapPrecom>> m_bootPrecomMap;

virtual ~FHECKKSRNS() {}

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -266,6 +263,9 @@ class FHECKKSRNS : public FHERNS {
3; // number of double-angle iterations in CKKS bootstrapping. Must be static because it is used in a static function.
uint32_t m_correctionFactor = 0; // correction factor, which we scale the message by to improve precision

// key tuple is dim1, levelBudgetEnc, levelBudgetDec
std::map<uint32_t, std::shared_ptr<CKKSBootstrapPrecom>> m_bootPrecomMap;

// Chebyshev series coefficients for the SPARSE case
static const inline std::vector<double> g_coefficientsSparse{
-0.18646470117093214, 0.036680543700430925, -0.20323558926782626, 0.029327390306199311,
Expand Down

0 comments on commit 40a89d8

Please sign in to comment.