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

everything that needs to be connected up with interface,h is

parent d2a7bb16
......@@ -246,17 +246,37 @@ BigVectorImpl<IntegerType> BigVectorImpl<IntegerType>::Mod(const IntegerType& mo
{
BigVectorImpl ans(this->GetLength(),this->GetModulus());
IntegerType halfQ(this->GetModulus() >> 1);
for (usint i = 0; i<ans.GetLength(); i++) {
if (this->at(i)>halfQ) {
ans.at(i)=this->at(i).ModSub(this->GetModulus(),modulus);
for (size_t i = 0; i<ans.GetLength(); i++) {
if (this->operator[](i)>halfQ) {
ans[i] = this->operator[](i).ModSub(this->GetModulus(),modulus);
}
else {
ans.at(i)=this->at(i).Mod(modulus);
ans[i] = this->operator[](i).Mod(modulus);
}
}
return ans;
}
}
template<class IntegerType>
const BigVectorImpl<IntegerType>& BigVectorImpl<IntegerType>::ModEq(const IntegerType& modulus) {
if (modulus==2) {
return this->ModByTwoEq();
}
else
{
IntegerType halfQ(this->GetModulus() >> 1);
for (usint i = 0; i<this->GetLength(); i++) {
if (this->operator[](i)>halfQ) {
this->operator[](i).ModSubEq(this->GetModulus(),modulus);
}
else {
this->operator[](i).ModEq(modulus);
}
}
return *this;
}
}
template<class IntegerType>
......@@ -474,55 +494,33 @@ const BigVectorImpl<IntegerType>& BigVectorImpl<IntegerType>::ModSubEq(const Big
template<class IntegerType>
BigVectorImpl<IntegerType> BigVectorImpl<IntegerType>::ModByTwo() const {
BigVectorImpl ans(*this);
ans.ModByTwoEq();
return ans;
}
template<class IntegerType>
const BigVectorImpl<IntegerType>& BigVectorImpl<IntegerType>::ModByTwoEq() {
BigVectorImpl ans(this->GetLength(),this->GetModulus());
IntegerType halfQ(this->GetModulus() >> 1);
for (usint i = 0; i<ans.GetLength(); i++) {
if (this->at(i)>halfQ) {
if (this->at(i).Mod(2) == 1)
ans.at(i)= IntegerType(0);
for (usint i = 0; i<this->GetLength(); i++) {
if (this->operator[](i)>halfQ) {
if (this->operator[](i).Mod(2) == 1)
this->operator[](i) = IntegerType(0);
else
ans.at(i)= 1;
this->operator[](i) = 1;
}
else {
if (this->at(i).Mod(2) == 1)
ans.at(i)= 1;
if (this->operator[](i).Mod(2) == 1)
this->operator[](i) = 1;
else
ans.at(i)= IntegerType(0);
this->operator[](i )= IntegerType(0);
}
}
return ans;
return *this;
}
//template<class IntegerType>
//const BigVectorImpl<IntegerType>& BigVectorImpl<IntegerType>::operator+=(const BigVectorImpl &b) {
//
// if((this->m_length!=b.m_length) || this->m_modulus!=b.m_modulus ){
// throw std::logic_error("operator+= called on BigVectorImpl's with different parameters.");
// }
//
// for(usint i=0;i<this->m_length;i++){
// this->m_data[i] = this->m_data[i].ModAdd(b.m_data[i],this->m_modulus);
// }
// return *this;
//
//}
//template<class IntegerType>
//const BigVectorImpl<IntegerType>& BigVectorImpl<IntegerType>::operator-=(const BigVectorImpl &b) {
//
// if((this->m_length!=b.m_length) || this->m_modulus!=b.m_modulus ){
// throw std::logic_error("operator-= called on BigVectorImpl's with different parameters.");
// }
//
// for(usint i=0;i<this->m_length;i++){
// this->m_data[i] = this->m_data[i].ModSub(b.m_data[i],this->m_modulus);
// }
// return *this;
//
//}
/*
Source: http://homes.esat.kuleuven.be/~fvercaut/papers/bar_mont.pdf
@article{knezevicspeeding,
......
......@@ -190,42 +190,42 @@ public:
IntegerType& at(size_t i) {
if(!this->IndexCheck(i)) {
throw std::logic_error("index out of range in BigVector");
PALISADE_THROW(lbcrypto::palisade_error, "BigVector index out of range");
}
return this->m_data[i];
}
const IntegerType& at(size_t i) const {
if(!this->IndexCheck(i)) {
throw std::logic_error("index out of range in BigVector");
PALISADE_THROW(lbcrypto::palisade_error, "BigVector index out of range");
}
return this->m_data[i];
}
void atMod(size_t i, const IntegerType &val) {
if(!this->IndexCheck(i)) {
throw std::logic_error("index out of range in BigVector");
PALISADE_THROW(lbcrypto::palisade_error, "BigVector index out of range");
}
this->m_data[i]=val%m_modulus;
return;
}
void atMod(size_t i, const std::string& val) const {
void atMod(size_t i, const std::string& val) {
if(!this->IndexCheck(i)) {
throw std::logic_error("index out of range in BigVector");
PALISADE_THROW(lbcrypto::palisade_error, "BigVector index out of range");
}
IntegerType tmp(val);
this->m_data[i]=tmp%m_modulus;
return;
}
/**
/**
* operators to get a value at an index.
* @param idx is the index to get a value at.
* @return is the value at the index. return NULL if invalid index.
* @return is the value at the index.
*/
inline IntegerType& operator[](size_t idx) { return (this->m_data[idx]); }
inline const IntegerType& operator[](size_t idx) const { return (this->m_data[idx]); }
IntegerType& operator[](size_t idx) { return (this->m_data[idx]); }
const IntegerType& operator[](size_t idx) const { return (this->m_data[idx]); }
/**
* Sets the vector modulus.
......@@ -266,6 +266,14 @@ public:
*/
BigVectorImpl Mod(const IntegerType& modulus) const;
/**
* Vector Modulus operator.
*
* @param modulus is the modulus to perform on the current vector entries.
* @return a new vector after the modulus operation on current vector.
*/
const BigVectorImpl& ModEq(const IntegerType& modulus);
//scalar operations
/**
......@@ -370,6 +378,13 @@ public:
*/
BigVectorImpl ModByTwo() const;
/**
* Perform a modulus by 2 operation. Returns the least significant bit.
*
* @return a new vector which is the return value of the modulus by 2, also the least significant bit.
*/
const BigVectorImpl& ModByTwoEq();
//component-wise subtraction
/**
......
......@@ -419,30 +419,16 @@ namespace exp_int {
template<class ubint_el_t>
mubintvec<ubint_el_t> mubintvec<ubint_el_t>::Mod(const ubint_el_t& modulus) const{
if (modulus == 2)
return this->ModByTwo();
else
{
mubintvec ans(*this);
ubint_el_t halfQ(this->GetModulus() >> 1);
for (usint i = 0; i<this->m_data.size(); i++) {
if ((*this)[i] > halfQ) {
ans.m_data[i] = ans.m_data[i].ModSub(this->GetModulus(), modulus);
}
else {
ans.m_data[i] = ans.m_data[i].Mod(modulus);
}
}
ans.ModEq(modulus);
return std::move(ans);
}
}
template<class ubint_el_t>
const mubintvec<ubint_el_t>& mubintvec<ubint_el_t>::ModEq(const ubint_el_t& modulus) {
if (modulus == 2)
return *this = this->ModByTwo();
return this->ModByTwoEq();
else
{
ubint_el_t halfQ(this->GetModulus() >> 1);
......@@ -456,7 +442,6 @@ namespace exp_int {
}
return *this;
}
}
......@@ -464,24 +449,31 @@ namespace exp_int {
template<class ubint_el_t>
mubintvec<ubint_el_t> mubintvec<ubint_el_t>::ModByTwo() const {
mubintvec ans(this->GetLength(),this->GetModulus());
ubint_el_t halfQ(this->GetModulus() >> 1);
for (usint i = 0; i<ans.GetLength(); i++) {
if (this->at(i)>halfQ) {
if (this->at(i).Mod(2) == 1)
ans.at(i)= ubint_el_t(0);
else
ans.at(i)= ubint_el_t(1);
}
else {
if (this->at(i).Mod(2) == 1)
ans.at(i)= ubint_el_t(1);
else
ans.at(i)= ubint_el_t(0);
}
}
return std::move(ans);
mubintvec ans(*this);
ans.ModByTwoEq();
return std::move(ans);
}
template<class ubint_el_t>
const mubintvec<ubint_el_t>& mubintvec<ubint_el_t>::ModByTwoEq() {
ubint_el_t halfQ(this->GetModulus() >> 1);
for (usint i = 0; i<this->GetLength(); i++) {
if (this->operator[](i)>halfQ) {
if (this->operator[](i).Mod(2) == 1)
this->operator[](i) = ubint_el_t(0);
else
this->operator[](i) = ubint_el_t(1);
}
else {
if (this->operator[](i).Mod(2) == 1)
this->operator[](i) = ubint_el_t(1);
else
this->operator[](i)= ubint_el_t(0);
}
}
return *this;
}
// method to add scalar to vector element at index i
......
......@@ -349,6 +349,7 @@ public:
* @return a new vector which is the return value of the modulus by 2, also the least significant bit.
*/
mubintvec ModByTwo() const;
const mubintvec& ModByTwoEq();
//scalar operations
......
......@@ -455,28 +455,6 @@ void myVecP<myT>::SwitchModulus(const myT& newModulus)
/// ARITHMETIC FUNCTIONS
//Math functions
// modulus
// template<class myT>
// myVecP<myT> myVecP<myT>::operator%( const myT& b) const
// {
// size_t n = this->GetLength();
// myVecP<myT> res(n);
// int rv = res.CopyModulus(*this);
// if (rv==-1) {
//#ifdef WARN_BAD_MODULUS
// std::cerr<<"in operator%(myT) Bad CopyModulus"<<std::endl;
//#endif
// }
// for (unsigned int i = 0; i < n; i++){
// res[i] = (*this)[i]%b;
// }
// return(res);
// }
template<class myT>
myVecP<myT> myVecP<myT>::Mod(const myT& modulus) const
{
......@@ -493,9 +471,9 @@ myVecP<myT> myVecP<myT>::Mod(const myT& modulus) const
for (size_t i = 0; i<this->GetLength(); i++) {
if ((*this)[i]>halfQ) {
DEBUG("negative at i="<<i);
ans[i]=(*this)[i].ModSub(thisMod, modulus);
ans[i] = (*this)[i].ModSub(thisMod, modulus);
} else {
ans[i]=(*this)[i].Mod(modulus);
ans[i] = (*this)[i].Mod(modulus);
}
}
DEBUG("ans.GetModulus() "<<ans.GetModulus());
......@@ -505,87 +483,68 @@ myVecP<myT> myVecP<myT>::Mod(const myT& modulus) const
}
return ans;
}
}
// %=
// method to vector with scalar
// template<class myT> //was inlined in .h
// const myVecP<myT>& myVecP<myT>::operator%=(const myT& modulus) {
template<class myT>
const myVecP<myT>& myVecP<myT>::ModEq(const myT& modulus)
{
bool dbg_flag = false;
DEBUG("mgmpintvec" <<*this);
DEBUG("MOD("<<modulus<<")");
if (modulus == myT(2)) {
return this->ModByTwoEq();
} else {
myT thisMod(this->GetModulus());
myT halfQ(thisMod >> 1);
DEBUG("halfQ = "<<halfQ);
for (size_t i = 0; i<this->GetLength(); i++) {
if (this->operator[](i) > halfQ) {
DEBUG("negative at i="<<i);
this->operator[](i).ModSubEq(thisMod, modulus);
} else {
this->operator[](i).ModEq(modulus);
}
}
DEBUG("ans.GetModulus() "<<this->GetModulus());
// *this = this->Mod(modulus);
// return *this;
for (size_t i = 0; i<this->GetLength(); i++) {
DEBUG("ans ["<<i<<"] = "<<this->operator[](i));
}
return *this;
}
}
// }
template<class myT>
myVecP<myT> myVecP<myT>::ModByTwo() const {
myVecP ans(*this);
ans.ModByTwoEq();
return ans;
}
//method to mod by two
template<class myT>
myVecP<myT> myVecP<myT>::ModByTwo() const {
const myVecP<myT>& myVecP<myT>::ModByTwoEq() {
myVecP ans(this->GetLength(),this->GetModulus());
myT halfQ(this->GetModulus() >> 1);
for (size_t i = 0; i<ans.GetLength(); i++) {
if ((*this)[i] > halfQ) {
if ((*this)[i].Mod(myT(2)) == myT(1))
ans[i] = 0;
for (size_t i = 0; i<this->GetLength(); i++) {
if (this->operator [](i) > halfQ) {
if (this->operator [](i).Mod(myT(2)) == myT(1))
this->operator [](i) = 0;
else
ans[i] = 1;
this->operator [](i) = 1;
}
else {
if ((*this)[i].Mod(myT(2)) == myT(1))
ans[i] = 1;
if (this->operator [](i).Mod(myT(2)) == myT(1))
this->operator [](i) = 1;
else
ans[i] = 0;
this->operator [](i) = 0;
}
}
return ans;
return *this;
}
//arithmetic.
// //addition of scalar
// template<class myT>
// myVecP<myT> myVecP<myT>::operator+(myT const& b) const
// {
// unsigned int n = this->GetLength();
// myVecP<myT> res(n);
// int rv = res.CopyModulus(*this);
// if (rv==-1) {
//#ifdef WARN_BAD_MODULUS
// std::cerr<<"in operator+(myT) Bad CopyModulus"<<std::endl;
//#endif
// }
// size_t i;
// myT bmod(b%m_modulus);
// for (i = 0; i < n; i++)
// //res[i] = (*this)[i]+b%m_modulus;
// res[i]= (*this)[i].ModAdd(bmod, m_modulus);
// return(res);
// }
////addition of vector
//template<class myT>
//myVecP<myT> myVecP<myT>::operator+(myVecP<myT> const& b) const
//{
// bool dbg_flag = false;
// DEBUG("in myVecP::operator+");
// ArgCheckVector(b, "myVecP operator+");
// myVecP<myT> res;
// int rv = res.CopyModulus(*this);
// if (rv==-1) {
//#ifdef WARN_BAD_MODULUS
// std::cerr<<"in operator+(myVecP) Bad CopyModulus"<<std::endl;
//#endif
// }
//
// //myVecP<myT>::modadd_p(res, *this, b%m_modulus);
// myVecP<myT>::modadd_p(res, *this, b);
// //NTL_OPT_RETURN(myVecP<myT>, res);
// DEBUG("myVecP::operator+ returning modulus "<<res.m_modulus);
// return(res);
//}
// method to add scalar to vector element at index i
template<class myT>
myVecP<myT> myVecP<myT>::ModAddAtIndex(size_t i, const myT &b) const{
......@@ -986,68 +945,6 @@ inline void myVecP<myT>::modmul_p(myVecP<myT>& x, myVecP<myT> const& a, myVecP<
//todo make modulus explicit.
}
//////////////////////////////////////////////////
// Set value at index with Mod
template<class myT>
void myVecP<myT>::atMod(size_t index, const myT& value){
if(!this->IndexCheck(index)){
PALISADE_THROW(lbcrypto::palisade_error, "myVecP index out of range");
}
else{
// must be set modulo
if (isModulusSet())
this->at(index) = value%m_modulus;
else //must be set directly
this->at(index) = value;
}
}
// set value at index from string with Mod
template<class myT>
void myVecP<myT>::atMod(size_t index, const std::string& str){
if(!this->IndexCheck(index)){
PALISADE_THROW(lbcrypto::palisade_error, "myVecP index out of range");
}
else{
// must be set modulo
if (isModulusSet())
this->at(index) = myT(str)%m_modulus;
else //must be set directly
this->at(index) = myT(str);
}
}
template<typename myT>
myT& myVecP<myT>::operator[](size_t index) {
return this->operator[](index);
}
template<typename myT>
const myT& myVecP<myT>::operator[](size_t index) const {
return this->operator[](index);
}
template<typename myT>
myT& myVecP<myT>::at(size_t index) {
bool dbg_flag = false;
if(!this->IndexCheck(index)){
PALISADE_THROW(lbcrypto::palisade_error, "myVecP index out of range");
}
DEBUG("in at("<<index<< ") = "<<(*this)[index]);
return this->operator[](index);
}
template<typename myT>
const myT& myVecP<myT>::at(size_t index) const {
bool dbg_flag = false;
if(!this->IndexCheck(index)){
PALISADE_THROW(lbcrypto::palisade_error, "myVecP index out of range");
}
DEBUG("in at("<<index<< ") = "<<(*this)[index]);
return this->operator[](index);
}
} // namespace NTL ends
template class NTL::myVecP<NTL::myT>; //instantiate template here
template class NTL::myVecP<NTL::myZZ>;
......@@ -130,9 +130,39 @@ public:
void clear(myVecP& x); //why isn't this inhereted?
// NOTE the underlying Vec does not have a no-bounds-checking operator[]
myT& at(size_t i) {
return this->NTL::Vec<myT>::at(i);
}
const myT& at(size_t i) const {
return this->NTL::Vec<myT>::at(i);
}
// the following are like writing to this->at(i) but with modulus implied.
void atMod(size_t index, const myT&value);
void atMod(size_t index, const std::string& str);
void atMod(size_t index, const myT&value) {
// must be set modulo
if (isModulusSet())
this->at(index) = value%m_modulus;
else //must be set directly
this->at(index) = value;
}
void atMod(size_t index, const std::string& str){
// must be set modulo
if (isModulusSet())
this->at(index) = myT(str)%m_modulus;
else //must be set directly
this->at(index) = myT(str);
}
/**
* operators to get a value at an index.
* @param idx is the index to get a value at.
* @return is the value at the index.
*/
myT& operator[](size_t idx) { return this->at(idx); }
const myT& operator[](size_t idx) const { return this->at(idx); }
/**
* Returns a vector of digit at a specific index for all entries
......@@ -164,9 +194,11 @@ public:
//arithmetic
//scalar modulus
myVecP Mod(const myT& b) const; //defined in cpp