Commit ebdaaab6 authored by Gerard Ryan's avatar Gerard Ryan

Release of 1.2

parent 94b0adf6
......@@ -12,7 +12,8 @@ third-party/distros/gmp-6.1.1
third-party/distros/gmp-6.1.2
third-party/distros/ntl-10.3.0
third-party/distros/ntl-10.5.0
third-party/include
third-party/include/NTL
third-party/include/gmp.h
third-party/lib
third-party/share
timing/
......
......@@ -76,7 +76,7 @@ BMLIB_LIBSRCDIR := google/benchmark-master/include
########### PALISADE BENCHMARKS BUILD HERE
#this is is the Include directive for the palisade code being benchmarked
BM_IFLAG := -Isrc/core/lib -Isrc/pke/lib $(BMLIB_IFLAG) $(NTLINCLUDE) $(GMPINCLUDE)
BM_IFLAG := -Isrc -Isrc/core/lib -Isrc/pke/lib $(BMLIB_IFLAG) $(THIRDPARTYINCLUDE)
PLIBS := $(EXTLIBDIR)/$(PALISADEPKELIB) $(EXTLIBDIR)/$(PALISADECORELIB) $(NTLLIB) $(GMPLIB)
......@@ -97,7 +97,7 @@ $(BMARK_BUILD)/%.o: $(BMARK_SRC)/%.cpp
$(CC) $(CPPFLAGS) $(BM_IFLAG) $(LIBBENCHMARK_FLAGS) $(BM_IFLAG) -c -o $@ $(patsubst $(BMARK_BUILD)/%,$(BMARK_SRC)/%,$(patsubst %.o,%.cpp,$@))
#this target is usesd to cleanup, it is called from the top Makefile
.PHONEY: cleanbenchmark
.PHONY: cleanbenchmark
cleanbenchmark:
@echo " Cleaning Benchmarks...";
$(RM) -r $(BMARK_BIN) $(BMARK_BUILD) $(BMLIB_OBJ) $(BMLIB_LIB)
......
#
#Copyright (c) 2015, New Jersey Institute of Technology (NJIT)
#All rights reserved.
#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.
#
################
# Makefile.circuit-disable ; no actual build
################
circuitstring:
allcircuit:
allcircuitdemos:
cleancircuit:
testcircuit:
......@@ -92,20 +92,15 @@ HDREXT := h
$(objects) : %.o : %.cpp
NTLINCLUDE := -I third-party/include
GMPINCLUDE := -I third-party/include
THIRDPARTYINCLUDE := -I third-party/include -I third-party/include/rapidjson
EXTLIB := -L$(EXTLIBDIR) $(TEST_LIB) #no profiling
INC := -I src/core/lib -I src/pke/lib -I src/trapdoor/lib -I src/circuit/lib -I test $(OMPINCLUDE) $(NTLINCLUDE) $(GMPINCLUDE)
INC := -I src -I src/core/lib -I src/pke/lib -I test $(OMPINCLUDE) $(THIRDPARTYINCLUDE)
#the name of the shared object library for palisade objects.
PALISADECORELIB := libPALISADEcore$(LIBSUFFIX)
PALISADEPKELIB := libPALISADEpke$(LIBSUFFIX)
PALISADETRAPDOORLIB := libPALISADEtrapdoor$(LIBSUFFIX)
PALISADECIRCUITLIB := libPALISADEcircuit$(LIBSUFFIX)
PALISADEPYLIB := pycrypto$(LIBSUFFIX)
# run make for all components. you can run any individual component separately
# by invoking "make allxxx" for example, make allcore makes everything in core only
......@@ -113,24 +108,20 @@ PALISADEPYLIB := pycrypto$(LIBSUFFIX)
all:
$(MAKE) gmp_all
$(MAKE) ntl_all
$(MAKE) allcore allpke alltrapdoor allcircuit utall
$(MAKE) allcore allpke utall
docs: apidocs
alljava: allcore allpke jwrapper
allpython: allcore allpke alltrapdoor pywrapper
alldemos: allcoredemos allpkedemos alltrapdoordemos allcircuitdemos
alldemos: allcoredemos allpkedemos
testall: $(CORE_TEST_TARGET) $(PKE_TEST_TARGET) $(TRAPDOOR_TEST_TARGET) $(CIRCUIT_TEST_TARGET) utall
testall: $(CORE_TEST_TARGET) $(PKE_TEST_TARGET) utall
$(BINDIR)/unittest/tests$(EXESUFFIX) -t
# clean up all components. you can clean any individual compoenent separately
# by invoking "make cleantargets" for example
# each corresponding makefile will make the cleanxxxx target
.PHONEY: clean clobber
clean: cleancore cleanpke cleantrapdoor cleancircuit cleandocs cleanbenchmark
.PHONY: clean clobber
clean: cleancore cleanpke cleandocs cleanbenchmark
@echo 'Cleaning top level autogenerated directories'
$(RM) -rf $(BINDIR)
$(RM) -rf src/*/bin
......@@ -147,13 +138,10 @@ utall: $(BINDIR)/unittest/tests$(EXESUFFIX)
include Makefile.core
include Makefile.pke
include Makefile.trapdoor
include Makefile.circuit
include Makefile.gmp
include Makefile.ntl
include Makefile.wrapper
include Makefile.benchmark #builds the benchmark framework
include Makefile.docs #populates doxgen documentation
......
......@@ -8,7 +8,7 @@ TAR ?= tar
#note change the following directory to the correct one on your install
PALISADE_DIR := $(PWD)
PALISADE_DIR := $(CURDIR)
GMP_VER :=6.1.2
#check if the palisade directory is correct
......@@ -83,4 +83,4 @@ clean_gmp:
cd $(DISTROS_DIR);\
$(RM) -rf gmp-$(GMP_VER)
cd $(THIRD_PARTY_DIR); \
$(RM) -rf include lib share
$(RM) -rf include/gmp.h lib share
......@@ -4,7 +4,7 @@ TAR ?= tar
#note change the following directory to the correct one on your install
PALISADE_DIR := $(PWD)
PALISADE_DIR := $(CURDIR)
NTL_VER:= 10.5.0
#check if the palisade directory is correct
......@@ -87,4 +87,4 @@ clean_ntl:
cd $(DISTROS_DIR);\
$(RM) -rf ntl-$(NTL_VER)
cd $(THIRD_PARTY_DIR); \
$(RM) -rf include lib share includels
$(RM) -rf include/NTL lib share includels
#
#Copyright (c) 2015, New Jersey Institute of Technology (NJIT)
#All rights reserved.
#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.
#
################
# Makefile.trapdoor-disable
################
alltrapdoor:
alltrapdoordemos:
cleantrapdoor:
testtrapdoor:
#
#Copyright (c) 2015, New Jersey Institute of Technology (NJIT)
#All rights reserved.
#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.
#
pywrapper:
jwrapper:
cleanwrappers:
6/15/2018: PALISADE v1.2 is released
PALISADE v1.2 provides several important advancements and improvements to the library. Most notably, we provide:
* The Bajard-Eynard-Hasan-Zucca RNS variant of the BFV scheme is added to the library
* The implementation of the Halevi-Polyakov-Shoup RNS variant of the BFV scheme is significantly improved
* Large multiplicative depths (up to 100 and higher) for both RNS variants are now supported.
* Several low-level optimizations, e.g., in Number Theoretic Transform and NTL multiprecision math backend, are implemented.
* Multiple improvements in plaintext encodings.
* Software engineering improvements: extended batteries of unit tests, cleaner design of the matrix class, better CryptoContext wrapper, etc.
* Fixes for bugs which have been brought to our attention.
1/29/2018: PALISADE v1.1.1 is released
PALISADE v1.1.1 includes bug fixes and minor optimizations:
Fixes minor bugs in NativeInteger and multiprecision backends (BigInteger)
Deals properly with a low-probability rounding error in BFVrns
Fixes a compilation error on some CentOS systems
Improves the performance of NativeInteger
Fixes a couple of other minor bugs
* Fixes minor bugs in NativeInteger and multiprecision backends (BigInteger)
* Deals properly with a low-probability rounding error in BFVrns
* Fixes a compilation error on some CentOS systems
* Improves the performance of NativeInteger
* Fixes a couple of other minor bugs
12/29/2017: PALISADE v1.1 is released
......
......@@ -37,6 +37,8 @@ Description:
*/
#define PROFILE
#define _USE_MATH_DEFINES
#include "benchmark/benchmark_api.h"
......
......@@ -151,7 +151,7 @@ void BM_encoding_PackedIntPlaintext_SetParams(benchmark::State& state) {
}
while (state.KeepRunning()) {
PackedEncoding::SetParams(m, p);
PackedEncoding::SetParams(m, ep);
state.PauseTiming();
PackedEncoding::Destroy();
state.ResumeTiming();
......
......@@ -51,6 +51,8 @@
using namespace std;
using namespace lbcrypto;
namespace lbcrypto {
template <typename E>
static E makeElement(shared_ptr<lbcrypto::ILParamsImpl<typename E::Integer>> params) {
typename E::Vector vec = makeVector<typename E::Vector>(params->GetRingDimension(), params->GetModulus());
......@@ -182,6 +184,7 @@ PolyImpl<BE6Integer, BE6Integer, BE6Vector, BE6ILParams>::DecryptionCRTInterpola
return std::move( interp );
}
#endif
}
map<usint,shared_ptr<ILNativeParams>> Nativeparms;
map<usint,shared_ptr<BE2ILParams>> BE2parms;
......
......@@ -24,6 +24,8 @@
*
*/
#define PROFILE
#define _USE_MATH_DEFINES
#include "benchmark/benchmark_api.h"
......
# Ignore everything in this directory
apidocs/
# Except this file
!.gitignore
No preview for this file type
......@@ -38,7 +38,7 @@ PROJECT_NAME = "PALISADE Lattice Crypto Library"
# could be handy for archiving the generated documentation or if some version
# control system is used.
PROJECT_NUMBER = "v1.1.1"
PROJECT_NUMBER = "v1.2"
# Using the PROJECT_BRIEF tag one can provide an optional one line description
# for a project that appears at the top of each page and should give viewer a
......
/*
* @author TPOC: palisade@njit.edu
*
* @copyright Copyright (c) 2017, New Jersey Institute of Technology (NJIT)
* All rights reserved.
* 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.
*
*/
#include "utils/aesutil.h"
#include "utils/debug.h"
#include <iostream>
using namespace lbcrypto;
void CombineBytes(unsigned char* bytes,int64_t* numbers,const unsigned int bytelength);
void SplitBytes(int64_t* numbers, unsigned char* bytes,const unsigned int numberlength);
int main() {
double totaltime=0;
bool dbg_flag = true;
unsigned char iv[4]={1,2,3,4};
unsigned char key[32]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32};
AESUtil util(iv,key,32);
TimeVar t1;
for(int i=0;i<500;i++){
int64_t numbervector[2048];
unsigned char bytevector[2048*8];
unsigned char encrypted_bytevector[2048*8];
for(int j=0;j<2048;j++){
numbervector[j] =0;
}
SplitBytes(numbervector,bytevector,2048);
TIC(t1);
util.Encrypt(bytevector,encrypted_bytevector,2048*8,CTR);
totaltime+=TOC_US(t1);
CombineBytes(encrypted_bytevector,numbervector,2048*8);
}
DEBUG("Time for 5 vectors of 2048: " << totaltime/100<< " us");
}
void CombineBytes(unsigned char* bytes,int64_t* numbers,const unsigned int bytelength){
for(unsigned int i=0;i<bytelength;i+=8){
numbers[i/8]=((int64_t)(bytes[i]*pow(2,56))) ^ ((int64_t)(bytes[i+1]*pow(2,48))) ^ ((int64_t)(bytes[i+2]*pow(2,40))) ^ ((int64_t)(bytes[i+3]*pow(2,32))) ^ ((int64_t)(bytes[i+4]*pow(2,24))) ^ ((int64_t)(bytes[i+5]*pow(2,16))) ^ ((int64_t)(bytes[i+6]*pow(2,8))) ^ ((int64_t)(bytes[i+7]));
}
}
void SplitBytes(int64_t* numbers, unsigned char* bytes,const unsigned int numberlength){
for(unsigned int i=0;i<numberlength;i++){
bytes[8*i]=(numbers[i] >> 56) & 0xFF;
bytes[8*i+1]=(numbers[i] >> 48) & 0xFF;
bytes[8*i+2]=(numbers[i] >> 40) & 0xFF;
bytes[8*i+3]=(numbers[i] >> 32) & 0xFF;
bytes[8*i+4]=(numbers[i] >> 24) & 0xFF;
bytes[8*i+5]=(numbers[i] >> 16) & 0xFF;
bytes[8*i+6]=(numbers[i] >> 8) & 0xFF;
bytes[8*i+7]=(numbers[i]) & 0xFF;
}
}
......@@ -33,6 +33,8 @@ inline static void encodeVec(P& poly, const PlaintextModulus& mod, int64_t lb, i
poly.SetValuesToZero();
const typename P::Integer &q = poly.GetModulus();
for( size_t i=0; i < value.size() && i < poly.GetLength(); i++ ) {
if( value[i] > INT32_MAX || value[i] < INT32_MIN ) {
PALISADE_THROW( config_error, "Cannot encode a coefficient larger than 32 bits");
......@@ -43,12 +45,16 @@ inline static void encodeVec(P& poly, const PlaintextModulus& mod, int64_t lb, i
" at position " + std::to_string(i) +
" because it is out of range of plaintext modulus " + std::to_string(mod) );
uint64_t entry = value[i];
typename P::Integer entry = value[i];
if( value[i] < 0 ) {
entry += mod;
//It is more efficient to encode negative numbers using the ciphertext modulus
//no noise growth occurs
entry = q - typename P::Integer(llabs(value[i]));
}
poly[i] = entry;
}
}
......@@ -77,9 +83,15 @@ inline static void fillVec(const P& poly, const PlaintextModulus& mod, vector<in
value.clear();
int64_t half = int64_t(mod)/2;
const typename P::Integer &q = poly.GetModulus();
typename P::Integer qHalf = q>>1;
for( size_t i = 0; i < poly.GetLength(); i++ ) {
int64_t val = poly[i].ConvertToInt();
int64_t val;
if (poly[i] > qHalf)
val = (-(q-poly[i]).ConvertToInt());
else
val = poly[i].ConvertToInt();
if( val > half )
val -= mod;
value.push_back(val);
......
......@@ -41,9 +41,9 @@ namespace lbcrypto
*/
bool EncodingParamsImpl::Serialize(Serialized* serObj) const
{
if (!serObj->IsObject())
return false;
if( !serObj->IsObject() ){
serObj->SetObject();
}
SerialItem ser(rapidjson::kObjectType);
ser.AddMember("PlaintextModulus", std::to_string(this->m_plaintextModulus), serObj->GetAllocator());
......
......@@ -32,6 +32,7 @@
#include "encoding/encodingparams.h"
#include "encoding/coefpackedencoding.h"
#include "encoding/integerencoding.h"
#include "encoding/fractionalencoding.h"
#include "encoding/packedencoding.h"
#include "encoding/scalarencoding.h"
#include "encoding/stringencoding.h"
......
/**
* @file fractionalencoding.cpp EXPERIMENTAL FEATURE: Represents and defines a limited version of the fractional encoder.
* Currently it is simply an extended version of integer encoding with division supported.
* @author TPOC: palisade@njit.edu
*
* @copyright Copyright (c) 2017, New Jersey Institute of Technology (NJIT)
* All rights reserved.
* 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.
*
*/
#include "fractionalencoding.h"
namespace lbcrypto {
template<typename P>
inline static void encodePoly(P& poly, int64_t value, const PlaintextModulus& ptm, size_t divisorBits) {
uint32_t negvalue = ptm - 1;
bool isNegative = value < 0;
uint64_t entry = isNegative ? -value : value;
poly.SetValuesToZero();
if( log2((double)value) > (double)poly.GetLength() )
PALISADE_THROW( config_error, "Plaintext value " + std::to_string(value) + " will not fit in encoding of length " + std::to_string(poly.GetLength()));
size_t i = 0;
while( entry > 0 ) {
if( entry & 0x01 )
poly[i] = isNegative ? negvalue : 1;
i++;
entry >>= 1;
}
if (divisorBits!=0)
{
size_t n = poly.GetLength();
poly[n-divisorBits] = ptm-1;
}
}
bool
FractionalEncoding::Encode() {
if( this->isEncoded ) return true;
PlaintextModulus mod = this->encodingParams->GetPlaintextModulus();
if( mod < 2 ) {
PALISADE_THROW( config_error, "Plaintext modulus must be 2 or more for fractional encoding");
}
if( mod >= UINT32_MAX ) {
PALISADE_THROW( config_error, "Plaintext modulus must be less than " + std::to_string(UINT32_MAX) + " for fractional encoding");
}
if( m_integer <= LowBound() || m_integer > HighBound() )
PALISADE_THROW( config_error, "Cannot encode integer " + std::to_string(m_integer) + " because it is out of range of plaintext modulus " + std::to_string(mod) );
if( this->typeFlag == IsNativePoly ) {
encodePoly(this->encodedNativeVector, m_integer, mod, m_separator);
}
else {
encodePoly(this->encodedVector, m_integer, mod, m_separator);
}
if( this->typeFlag == IsDCRTPoly ) {
this->encodedVectorDCRT = this->encodedVector;
}
this->isEncoded = true;
return true;
}
template<typename P>
inline static int64_t decodePoly(const P& poly, const PlaintextModulus& ptm, size_t truncatedBits) {
int64_t result = 0;
int64_t powerFactor = 1;
int64_t half = ptm/2;
for (size_t i = 0; i < poly.GetLength()-truncatedBits; i++) {
int64_t val = poly[i].ConvertToInt();
if( val != 0 ) {
if( val <= half )
result += powerFactor * val;
else
result += powerFactor * (val - ptm);
}
// multiply the power factor by 2
powerFactor <<= 1;
}
return result;
}
bool
FractionalEncoding::Decode() {
auto modulus = this->encodingParams->GetPlaintextModulus();
if( this->typeFlag == IsNativePoly )
m_integer = decodePoly(this->encodedNativeVector, modulus, m_separator);
else
m_integer = decodePoly(this->encodedVector, modulus, m_separator);
return true;
}
} /* namespace lbcrypto */
/**
* @file fractionalencoding.h EXPERIMENTAL FEATURE: Defines a limited version of the fractional encoder.
* Currently it is simply an extended version of integer encoding. In addition to integer encoding operations,
* it supports integer division by another integer (with the fractional part being truncated during decryption).
* @author TPOC: palisade@njit.edu
*
* @copyright Copyright (c) 2017, New Jersey Institute of Technology (NJIT)
* All rights reserved.
* 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.
*
*/
#ifndef SRC_CORE_LIB_ENCODING_FRACTIONALENCODING_H_
#define SRC_CORE_LIB_ENCODING_FRACTIONALENCODING_H_
#include "plaintext.h"
namespace lbcrypto {
class FractionalEncoding: public PlaintextImpl {
// integer part
int64_t m_integer;
// number of bits reserved for the fractional part (index from the end)
size_t m_separator;
// m_numerator and m_denominator are not currently used
uint64_t m_numerator;
uint64_t m_denominator;
public:
// these two constructors are used inside of Decrypt
FractionalEncoding(shared_ptr<Poly::Params> vp, EncodingParams ep) :
PlaintextImpl(vp,ep), m_integer(0), m_separator(0), m_numerator(0), m_denominator(0) {}