Commit ee559783 authored by Gerard Ryan's avatar Gerard Ryan
Browse files

Merge branch 'issue-794-be6-interface' into 'v1.1.1-work-branch'

Issue 794 be6 interface

Fixes #794

See merge request !366
parents 9c75e46c a7cb5e7b
......@@ -13,30 +13,26 @@ using namespace std;
#include "utils/hashutil.h"
using namespace lbcrypto;
int main()
int main(int argc, char *argv[])
{
cout << std::hex << endl;
string empty = "";
string demo = "The quick brown fox jumps over the lazy dog";
if( argc != 2 ) {
cout << "Usage is " << argv[0] << " string-to-hash" << endl;
return 0;
}
vector<uint8_t> digest;
HashUtil::Hash(empty,SHA_256,digest);
string sha = HashUtil::HashString(empty);
cout << std::hex << endl;
cout << "Empty string" << std::hex << endl;
for( size_t ii=0; ii<32; ii++ ) {
cout << std::setfill('0') << std::setw(2) << (int)digest[ii];
}
cout << endl << sha << std::dec << endl;
string demo(argv[1]);
vector<int64_t> digest;
HashUtil::Hash(demo,SHA_256,digest);
sha = HashUtil::HashString(demo);
string sha = HashUtil::HashString(demo);
cout << "The quick brown fox jumps over the lazy dog" << std::hex << endl;
cout << demo << std::hex << endl;
for( size_t ii=0; ii<32; ii++ ) {
cout << std::setfill('0') << std::setw(2) << (int) digest[ii];
}
cout << endl << sha << std::dec << endl;
return 0;
}
......@@ -33,7 +33,7 @@ inline static void encodeVec(P& poly, const PlaintextModulus& mod, int64_t lb, i
poly.SetValuesToZero();
for( size_t i=0; i < value.size(); i++ ) {
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,7 +43,7 @@ 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) );
uint32_t entry = value[i];
uint64_t entry = value[i];
if( value[i] < 0 ) {
entry += mod;
}
......
......@@ -36,7 +36,7 @@ Field2n::Field2n(const Poly & element)
} else {
// the value of element.at(i) is usually small - so a 64-bit integer is more than enough
// this approach is much faster than BigInteger::ConvertToDouble
BigInteger negativeThreshold(element.GetModulus()/ 2);
BigInteger negativeThreshold(element.GetModulus() / Poly::Integer(2));
for (size_t i = 0; i < element.GetLength(); i++) {
if (element.at(i) > negativeThreshold)
this->push_back((double)(int64_t)(-1 * (element.GetModulus() - element.at(i)).ConvertToInt()));
......
......@@ -237,9 +237,9 @@ const PolyImpl<ModType,IntType,VecType,ParmType>& PolyImpl<ModType,IntType,VecTy
for (usint j = 0; j < vectorLength; ++j) { // loops within a tower
if (j < len) {
this->at(j)= IntType(*(rhs.begin() + j));
this->operator[](j)= IntType(*(rhs.begin() + j));
} else {
this->at(j)= ZERO;
this->operator[](j)= ZERO;
}
}
......@@ -275,10 +275,10 @@ const PolyImpl<ModType, IntType, VecType, ParmType>& PolyImpl<ModType, IntType,
tempInteger = *(rhs.begin() + j);
tempBI = IntType(tempInteger);
}
at(j)= tempBI;
operator[](j)= tempBI;
}
else {
at(j)= ZERO;
operator[](j)= ZERO;
}
}
......@@ -302,10 +302,10 @@ const PolyImpl<ModType, IntType, VecType, ParmType>& PolyImpl<ModType, IntType,
tempInteger = *(rhs.begin() + j);
tempBI = IntType(tempInteger);
}
temp.at(j)= tempBI;
temp.operator[](j)= tempBI;
}
else {
temp.at(j)= ZERO;
temp.operator[](j)= ZERO;
}
}
this->SetValues(std::move(temp), m_format);
......@@ -336,10 +336,10 @@ const PolyImpl<ModType, IntType, VecType, ParmType>& PolyImpl<ModType, IntType,
tempInteger = *(rhs.begin() + j);
tempBI = IntType(tempInteger);
}
at(j)= tempBI;
operator[](j)= tempBI;
}
else {
at(j)= ZERO;
operator[](j)= ZERO;
}
}
......@@ -363,10 +363,10 @@ const PolyImpl<ModType, IntType, VecType, ParmType>& PolyImpl<ModType, IntType,
tempInteger = *(rhs.begin() + j);
tempBI = IntType(tempInteger);
}
temp.at(j)= tempBI;
temp.operator[](j)= tempBI;
}
else {
temp.at(j)= ZERO;
temp.operator[](j)= ZERO;
}
}
this->SetValues(std::move(temp), m_format);
......@@ -385,9 +385,9 @@ const PolyImpl<ModType,IntType,VecType,ParmType>& PolyImpl<ModType,IntType,VecTy
for (usint j = 0; j < vectorLength; ++j) { // loops within a tower
if (j < len) {
m_values->at(j)= *(rhs.begin() + j);
m_values->operator[](j)= *(rhs.begin() + j);
} else {
m_values->at(j)= ZERO;
m_values->operator[](j)= ZERO;
}
}
......@@ -438,7 +438,7 @@ const PolyImpl<ModType,IntType,VecType,ParmType>& PolyImpl<ModType,IntType,VecTy
m_values = make_unique<VecType>(m_params->GetRingDimension(), m_params->GetModulus());
}
for (size_t i = 0; i < m_values->GetLength(); ++i) {
this->at(i)= IntType(val);
this->operator[](i)= IntType(val);
}
return *this;
}
......@@ -499,7 +499,7 @@ usint PolyImpl<ModType,IntType,VecType,ParmType>::GetLength() const
template<typename ModType, typename IntType, typename VecType, typename ParmType>
void PolyImpl<ModType,IntType,VecType,ParmType>::SetValues(const VecType& values, Format format)
{
if (m_params->GetRootOfUnity() == 0){
if (m_params->GetRootOfUnity() == IntType(0)){
PALISADE_THROW(type_error, "Polynomial has a 0 root of unity");
}
if (m_params->GetRingDimension() != values.GetLength() || m_params->GetModulus() != values.GetModulus()) {
......@@ -518,11 +518,11 @@ void PolyImpl<ModType,IntType,VecType,ParmType>::SetValuesToZero()
template<typename ModType, typename IntType, typename VecType, typename ParmType>
void PolyImpl<ModType,IntType,VecType,ParmType>::SetValuesToMax()
{
IntType max = m_params->GetModulus() - 1;
IntType max = m_params->GetModulus() - IntType(1);
usint size = m_params->GetRingDimension();
m_values = make_unique<VecType>(m_params->GetRingDimension(), m_params->GetModulus());
for (usint i = 0; i < size; i++) {
m_values->at(i)= IntType(max);
m_values->operator[](i)= IntType(max);
}
}
......@@ -574,7 +574,7 @@ PolyImpl<ModType,IntType,VecType,ParmType> PolyImpl<ModType,IntType,VecType,Parm
// throw std::logic_error("Negate for PolyImpl is supported only in EVALUATION format.\n");
PolyImpl<ModType,IntType,VecType,ParmType> tmp( *this );
*tmp.m_values = m_values->ModMul(this->m_params->GetModulus() - 1);
*tmp.m_values = m_values->ModMul(this->m_params->GetModulus() - IntType(1));
return std::move( tmp );
}
......@@ -667,9 +667,9 @@ void PolyImpl<ModType,IntType,VecType,ParmType>::AddILElementOne()
{
IntType tempValue;
for (usint i = 0; i < m_params->GetRingDimension(); i++) {
tempValue = GetValues().at(i) + 1;
tempValue = GetValues().operator[](i) + IntType(1);
tempValue = tempValue.Mod(m_params->GetModulus());
m_values->at(i)= tempValue;
m_values->operator[](i)= tempValue;
}
}
......@@ -697,15 +697,15 @@ PolyImpl<ModType,IntType,VecType,ParmType> PolyImpl<ModType,IntType,VecType,Parm
// based on the totient index (between 0 and m - 1)
VecType expanded(m, modulus);
for (usint i = 0; i < n; i++) {
expanded.at(totientList.at(i))= m_values->at(i);
expanded.operator[](totientList.operator[](i))= m_values->operator[](i);
}
for (usint i = 0; i < n; i++) {
//determines which power of primitive root unity we should switch to
usint idx = totientList.at(i)*k % m;
usint idx = totientList.operator[](i)*k % m;
result.m_values->at(i)= expanded.at(idx);
result.m_values->operator[](i)= expanded.operator[](idx);
}
} else {
......@@ -716,7 +716,7 @@ PolyImpl<ModType,IntType,VecType,ParmType> PolyImpl<ModType,IntType,VecType,Parm
//determines which power of primitive root unity we should switch to
usint idx = (j*k) % m;
result.m_values->at((j + 1) / 2 - 1)= GetValues().at((idx + 1) / 2 - 1);
result.m_values->operator[]((j + 1) / 2 - 1)= GetValues().operator[]((idx + 1) / 2 - 1);
}
......@@ -863,7 +863,7 @@ void PolyImpl<ModType,IntType,VecType,ParmType>::MakeSparse(const uint32_t &wFac
if (m_values != 0) {
for (usint i = 0; i < m_params->GetRingDimension();i++) {
if (i%wFactor != 0) {
m_values->at(i)= IntType(0);
m_values->operator[](i)= IntType(0);
}
}
}
......@@ -893,7 +893,7 @@ void PolyImpl<ModType,IntType,VecType,ParmType>::Decompose()
//Interleaving operation.
VecType decomposeValues(GetLength() / 2, GetModulus());
for (usint i = 0; i < GetLength(); i = i + 2) {
decomposeValues.at(i / 2)= GetValues().at(i);
decomposeValues.operator[](i / 2)= GetValues().operator[](i);
}
SetValues(decomposeValues, m_format);
......@@ -912,7 +912,7 @@ template<typename ModType, typename IntType, typename VecType, typename ParmType
bool PolyImpl<ModType,IntType,VecType,ParmType>::InverseExists() const
{
for (usint i = 0; i < GetValues().GetLength(); i++) {
if (m_values->at(i) == 0)
if (m_values->operator[](i) == IntType(0))
return false;
}
return true;
......@@ -927,10 +927,10 @@ double PolyImpl<ModType,IntType,VecType,ParmType>::Norm() const
const IntType &half = m_params->GetModulus() >> 1;
for (usint i = 0; i < GetValues().GetLength(); i++) {
if (m_values->at(i) > half)
if (m_values->operator[](i) > half)
locVal = q - (*m_values)[i];
else
locVal = m_values->at(i);
locVal = m_values->operator[](i);
if (locVal > retVal)
retVal = locVal;
......
......@@ -32,10 +32,6 @@ Description:
#include "../../utils/serializable.h"
#include "../../utils/debug.h"
#if defined(_MSC_VER)
#pragma intrinsic(_BitScanReverse64)
#endif
namespace cpu_int {
//MOST REQUIRED STATIC CONSTANTS INITIALIZATION
......@@ -125,16 +121,19 @@ BigInteger<uint_type,BITLENGTH>::BigInteger(const BigInteger& bigInteger){
}
}
template<typename uint_type,usint BITLENGTH>
BigInteger<uint_type,BITLENGTH>::BigInteger(BigInteger&& bigInteger){
m_MSB = std::move(bigInteger.m_MSB);
for (size_t i=0; i < m_nSize; ++i) {
m_value[i] = std::move(bigInteger.m_value[i]);
}
}
template<typename uint_type,usint BITLENGTH>
unique_ptr<BigInteger<uint_type,BITLENGTH>> BigInteger<uint_type,BITLENGTH>::Allocator() {
return lbcrypto::make_unique<cpu_int::BigInteger<uint_type,BITLENGTH>>();
};
template<typename uint_type,usint BITLENGTH>
BigInteger<uint_type,BITLENGTH>::~BigInteger()
{
}
/*
*Converts the BigInteger to unsigned integer or returns the first 32 bits of the BigInteger.
*Splits the BigInteger into bit length of uint data type and then uses shift and add to form the 32 bit unsigned integer.
......@@ -180,6 +179,18 @@ const BigInteger<uint_type,BITLENGTH>& BigInteger<uint_type,BITLENGTH>::operato
return *this;
}
template<typename uint_type,usint BITLENGTH>
const BigInteger<uint_type,BITLENGTH>& BigInteger<uint_type,BITLENGTH>::operator=(BigInteger &&rhs){
if(this!=&rhs){
this->m_MSB = std::move(rhs.m_MSB);
for( size_t i=0; i < m_nSize; i++ )
this->m_value[i] = std::move(rhs.m_value[i]);
}
return *this;
}
/*
* Left Shift is done by splitting the number of shifts into
*1. Multiple of the bit length of uint data type.
......@@ -1576,11 +1587,11 @@ BigInteger<uint_type,BITLENGTH> BigInteger<uint_type,BITLENGTH>::ModSub(const Bi
BigInteger b_op(b);
//reduce this to a value lower than modulus
if(a > modulus){
if(a >= modulus){
a.ModEq(modulus);
}
//reduce b to a value lower than modulus
if(b > modulus){
if(b >= modulus){
b_op.ModEq(modulus);
}
......@@ -1600,11 +1611,11 @@ const BigInteger<uint_type,BITLENGTH>& BigInteger<uint_type,BITLENGTH>::ModSubEq
BigInteger b_op(b);
//reduce this to a value lower than modulus
if(*this > modulus){
if(*this >= modulus){
this->ModEq(modulus);
}
//reduce b to a value lower than modulus
if(b > modulus){
if(b >= modulus){
b_op.ModEq(modulus);
}
......@@ -1680,13 +1691,13 @@ BigInteger<uint_type,BITLENGTH> BigInteger<uint_type,BITLENGTH>::ModMul(const Bi
BigInteger bb(b);
//if a is greater than q reduce a to its mod value
if(a>modulus){
a = a.Mod(modulus);
if(a >= modulus){
a.ModEq(modulus);
}
//if b is greater than q reduce b to its mod value
if(b>modulus){
bb = bb.Mod(modulus);
if(b >= modulus){
bb.ModEq(modulus);
}
a.TimesEq(bb);
......@@ -1698,12 +1709,12 @@ const BigInteger<uint_type,BITLENGTH>& BigInteger<uint_type,BITLENGTH>::ModMulEq
BigInteger bb(b);
//if a is greater than q reduce a to its mod value
if(*this>modulus){
if(*this >= modulus){
this->ModEq(modulus);
}
//if b is greater than q reduce b to its mod value
if(b>modulus){
if(b >= modulus){
bb.ModEq(modulus);
}
......@@ -1743,11 +1754,11 @@ BigInteger<uint_type,BITLENGTH> BigInteger<uint_type,BITLENGTH>::ModBarrettMul(c
BigInteger bb(b);
//if a is greater than q reduce a to its mod value
if(*this>modulus)
if(*this >= modulus)
a.ModBarrettInPlace(modulus,mu);
//if b is greater than q reduce b to its mod value
if(b>modulus)
if(b >= modulus)
bb.ModBarrettInPlace(modulus,mu);
a.TimesEq(bb);
......@@ -1786,12 +1797,12 @@ void BigInteger<uint_type, BITLENGTH>::ModBarrettMulInPlace(const BigInteger& b,
BigInteger bb(b);
//if a is greater than q reduce a to its mod value
if (*this>modulus)
if (*this >= modulus)
this->ModBarrettInPlace(modulus, mu);
//if b is greater than q reduce b to its mod value
if (b>modulus)
if (b >= modulus)
bb.ModBarrettInPlace(modulus, mu);
this->TimesEq(bb);
......@@ -1808,11 +1819,11 @@ BigInteger<uint_type,BITLENGTH> BigInteger<uint_type,BITLENGTH>::ModBarrettMul(c
BigInteger bb(b);
//if a is greater than q reduce a to its mod value
if(*this>modulus)
if(*this >= modulus)
a.ModBarrettInPlace(modulus,mu_arr);
//if b is greater than q reduce b to its mod value
if(b>modulus)
if(b >= modulus)
bb.ModBarrettInPlace(modulus,mu_arr);
a.TimesEq(bb);
......
......@@ -240,16 +240,20 @@ namespace cpu_int{
*/
BigInteger(const BigInteger& bigInteger);
/**
* Move constructor for copying a big binary integer
*
* @param bigInteger is the big binary integer to be copied.
*/
BigInteger(BigInteger&& bigInteger);
/**
* Construct a BigInteger from a NativeInteger
* @param native
*/
BigInteger(const NativeInteger& native) : BigInteger( native.ConvertToInt() ) {}
/**
* Destructor.
*/
~BigInteger();
~BigInteger() {}
/**
* Assignment operator
......@@ -259,23 +263,39 @@ namespace cpu_int{
*/
const BigInteger& operator=(const BigInteger &rhs);
/**
* Move Assignment operator
*
* @param &rhs is the big binary integer to be assigned from.
* @return assigned BigInteger ref.
*/
const BigInteger& operator=(BigInteger &&rhs);
/**
* Assignment operator from unsigned integer
*
* @param val is the unsigned integer value that is assigned.
* @return the assigned BigInteger ref.
*/
//TODO: should this be uint_64_t?
inline const BigInteger& operator=(usint val) {
*this = intToBigInteger(val);
const BigInteger& operator=(uint64_t val) {
*this = BigInteger(val);
return *this;
}
inline const BigInteger& operator=(std::string strval) {
/**
* Assignment from string
* @param strval
* @return the assigned BigInteger ref.
*/
const BigInteger& operator=(const std::string strval) {
*this = BigInteger(strval);
return *this;
}
const BigInteger& operator=(const NativeInteger& val) {
*this = BigInteger(val);
return *this;
}
//Auxillary Functions
......
......@@ -65,7 +65,6 @@ BigVectorImpl<IntegerType>::BigVectorImpl(usint length, const IntegerType& modul
m_data[i] = 0;
}
}
}
template<class IntegerType>
......@@ -92,7 +91,6 @@ BigVectorImpl<IntegerType>::BigVectorImpl(const BigVectorImpl &bigVector){
for(usint i=0;i<m_length;i++){
m_data[i] = bigVector.m_data[i];
}
}
template<class IntegerType>
......@@ -101,6 +99,8 @@ BigVectorImpl<IntegerType>::BigVectorImpl(BigVectorImpl &&bigVector){
m_length = bigVector.m_length;
m_modulus = bigVector.m_modulus;
bigVector.m_data = NULL;
bigVector.m_length = 0;
bigVector.m_modulus = 0;
}
//ASSIGNMENT OPERATOR
......@@ -108,7 +108,7 @@ template<class IntegerType>
const BigVectorImpl<IntegerType>& BigVectorImpl<IntegerType>::operator=(const BigVectorImpl &rhs){
if(this!=&rhs){
if(this->m_length==rhs.m_length){
for (usint i = 0; i < m_length; i++){
for (size_t i = 0; i < m_length; i++){
this->m_data[i] = rhs.m_data[i];
}
}
......@@ -118,7 +118,7 @@ const BigVectorImpl<IntegerType>& BigVectorImpl<IntegerType>::operator=(const Bi
m_length = rhs.m_length;
m_modulus = rhs.m_modulus;
m_data = new IntegerType[m_length];
for (usint i = 0; i < m_length; i++){
for (size_t i = 0; i < m_length; i++){
m_data[i] = rhs.m_data[i];
}
}
......@@ -128,10 +128,24 @@ const BigVectorImpl<IntegerType>& BigVectorImpl<IntegerType>::operator=(const Bi
return *this;
}
template<class IntegerType>
const BigVectorImpl<IntegerType>& BigVectorImpl<IntegerType>::operator=(BigVectorImpl &&rhs){
if(this!=&rhs){
delete [] m_data;
m_data = rhs.m_data;
m_length = rhs.m_length;
m_modulus = rhs.m_modulus;
rhs.m_data = NULL;
}
return *this;
}
template<class IntegerType>
const BigVectorImpl<IntegerType>& BigVectorImpl<IntegerType>::operator=(std::initializer_list<uint64_t> rhs){
usint len = rhs.size();
for(usint i=0;i<m_length;i++){ // this loops over each tower
size_t len = rhs.size();
for(size_t i=0;i<m_length;i++){
if(i<len) {
if (m_modulus!=0)
m_data[i] = IntegerType(*(rhs.begin()+i))%m_modulus;
......@@ -146,10 +160,10 @@ const BigVectorImpl<IntegerType>& BigVectorImpl<IntegerType>::operator=(std::ini
}
template<class IntegerType>
const BigVectorImpl<IntegerType>& BigVectorImpl<IntegerType>::operator=(std::initializer_list<std::string> rhs){
const BigVectorImpl<IntegerType>& BigVectorImpl<IntegerType>::operator=(std::initializer_list<std::string> rhs) {
bool dbg_flag = false;
usint len = rhs.size();
for(usint i=0;i<m_length;i++){ // this loops over each tower
size_t len = rhs.size();
for(size_t i=0;i<m_length;i++){
if(i<len) {
if (m_modulus!=0)
m_data[i] = IntegerType(*(rhs.begin()+i))%m_modulus;
......@@ -164,22 +178,6 @@ const BigVectorImpl<IntegerType>& BigVectorImpl<IntegerType>::operator=(std::ini
return *this;
}
template<class IntegerType>
BigVectorImpl<IntegerType>& BigVectorImpl<IntegerType>::operator=(BigVectorImpl &&rhs){
if(this!=&rhs){
delete [] m_data;
m_data = rhs.m_data;
m_