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

upd bugfind

parent 698d6acc
......@@ -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;
}
......@@ -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;
}
......
......@@ -77,8 +77,8 @@ typedef native_int::NativeVector<NativeInteger> NativeVector;
//uncommented line (and breaking the documentation of the line)
#ifndef MATHBACKEND
#define MATHBACKEND 2
//#define MATHBACKEND 4
//#define MATHBACKEND 2
#define MATHBACKEND 4
//#define MATHBACKEND 6
#endif
......
......@@ -1587,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);
}
......@@ -1611,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);
}
......@@ -1691,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);
......@@ -1709,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);
}
......@@ -1754,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);
......@@ -1797,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);
......@@ -1819,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);
......
......@@ -40,7 +40,7 @@
#include <thread>
#include "backend.h"
//#define FIXED_SEED // if defined, then uses a fixed seed number for reproducable results during debug. Use only one OMP thread to ensure reproducability
#define FIXED_SEED // if defined, then uses a fixed seed number for reproducible results during debug. Use only one OMP thread to ensure reproducibility
namespace lbcrypto {
......
......@@ -485,27 +485,36 @@ namespace exp_int {
throw std::runtime_error(errMsg);
}
mubintvec ans(*this);
ans.m_data[i] = ans.m_data[i].ModAdd(b, this->m_modulus);
ans.m_data[i].ModAddEq(b, this->m_modulus);
return std::move(ans);
}
// method to add scalar to vector
template<class ubint_el_t>
template<class ubint_el_t>
mubintvec<ubint_el_t> mubintvec<ubint_el_t>::ModAdd(const ubint_el_t &b) const{
mubintvec ans(*this);
for(usint i=0;i<this->m_data.size();i++){
ans.m_data[i] = ans.m_data[i].ModAdd(b, ans.m_modulus);
}
return std::move(ans);
}
mubintvec ans(*this);
for(usint i=0;i<this->m_data.size();i++){
ans.m_data[i].ModAddEq(b, ans.m_modulus);
}
return std::move(ans);
}
// method to add scalar to vector
template<class ubint_el_t>
const mubintvec<ubint_el_t>& mubintvec<ubint_el_t>::ModAddEq(const ubint_el_t &b) {
for(usint i=0;i<this->m_data.size();i++){
this->m_data[i].ModAddEq(b, this->m_modulus);
}
return *this;
}
// needs to match BE 2 using signed modulus for result
// method to subtract scalar from vector
template<class ubint_el_t>
mubintvec<ubint_el_t> mubintvec<ubint_el_t>::ModSub(const ubint_el_t &b) const{
mubintvec ans(*this);
for(usint i=0;i<this->m_data.size();i++){
ans.m_data[i] = ans.m_data[i].ModSub(b, ans.m_modulus);
ans.m_data[i].ModSubEq(b, ans.m_modulus);
}
return std::move(ans);
}
......@@ -524,7 +533,7 @@ namespace exp_int {
#ifdef NO_BARRETT //non barrett way
mubintvec ans(*this);
for(usint i=0;i<this->m_data.size();i++){
ans.m_data[i] = ans.m_data[i].ModMul(b, ans.m_modulus);
ans.m_data[i].ModMulEq(b, ans.m_modulus);
}
return std::move(ans);
#else
......@@ -675,7 +684,7 @@ template<class ubint_el_t>
throw std::logic_error("mubintvec multiplying vectors of different lengths");
} else {
for(usint i=0;i<ans.m_data.size();i++){
ans.m_data[i] = ans.m_data[i].ModMul(b.m_data[i],ans.m_modulus);
ans.m_data[i].ModMulEq(b.m_data[i],ans.m_modulus);
}
return std::move(ans);
}
......
......@@ -350,28 +350,25 @@ return result;
//copy allocator
template<typename limb_t>
const ubint<limb_t>& ubint<limb_t>::operator=(const ubint &rhs){
//std::cout<<"Ca";
if(this!=&rhs){
this->m_MSB=rhs.m_MSB;
this->m_state = rhs.m_state;
//copy vector
this->m_value=rhs.m_value;
}
return *this;
}
#if 1
// move allocator
template<typename limb_t>
const ubint<limb_t>& ubint<limb_t>::operator=(ubint &&rhs){
//std::cout<<"Ma";
if(this!=&rhs){
this->m_MSB = rhs.m_MSB;
this->m_state = rhs.m_state;
this->m_value = std::move(rhs.m_value);
this->m_value = rhs.m_value;
}
return *this;
}
#endif
// // move allocator
// template<typename limb_t>
// const ubint<limb_t>& ubint<limb_t>::operator=(ubint &&rhs){
// if(this!=&rhs){
// this->m_MSB = rhs.m_MSB;
// this->m_state = rhs.m_state;
// this->m_value = std::move(rhs.m_value);
// }
// return *this;
// }
/**
* Left Shift is done by splitting the number of shifts into
*1. Multiple of the bit length of limb data type.
......@@ -2087,7 +2084,7 @@ return result;
template<typename limb_t>
void ubint<limb_t>::ModBarrettInPlace(const ubint& modulus, const ubint& mu) {
*this %= modulus;
this->ModEq( modulus );
return;
}
......@@ -2218,11 +2215,11 @@ return result;
ubint b_op(b);
//reduce this to a value lower than modulus
if(*this>modulus){
if(*this >= modulus){
a.ModEq(modulus);
}
//reduce b to a value lower than modulus
if(b>modulus){
if(b >= modulus){
b_op.ModEq(modulus);
}
......@@ -2243,11 +2240,11 @@ return result;
ubint 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);
}
......@@ -2347,6 +2344,7 @@ return result;
return ans;
}
// FIXME make this in-place!
template<typename limb_t>
const ubint<limb_t>& ubint<limb_t>::ModMulEq(const ubint& b, const ubint& modulus) {
*this = this->ModMul(b, modulus);
......@@ -2435,7 +2433,7 @@ return result;
ubint<limb_t> ubint<limb_t>::ModBarrett(const ubint& modulus, const ubint mu_arr[BARRETT_LEVELS+1]) const{
#ifdef NO_BARRETT
ubint ans(*this);
ans%=modulus;
ans.ModEq(modulus);
return(ans);
#else
if(*this<modulus){
......
......@@ -327,13 +327,21 @@ namespace exp_int{
~ubint();
/**
* Assignment operator (move copy)
* Assignment operator (copy)
*
* @param &rhs is the ubint to be assigned from.
* @return assigned ubint ref.
*/
const ubint& operator=(const ubint &rhs);
// /**
// * Move assignment
// *
// * @param &&rhs is the ubint to move.
// * @return object of type ubint.
// */
// const ubint& operator=(ubint &&rhs);
/**
* Assignment operator from unsigned integer
*
......@@ -341,7 +349,7 @@ namespace exp_int{
* @return the assigned ubint ref.
*/
const ubint& operator=(const uint64_t val) {
*this = ubint(val);
*this = ubint(val);
return *this;
}
......@@ -356,47 +364,6 @@ namespace exp_int{
return *this;
}
/**
* Move copy constructor
*
* @param &&rhs is the ubint to move.
* @return object of type ubint.
*/
const ubint& operator=(ubint &&rhs);
//Shift Operators
// /**
// * Left shift operator of ubint
// * @param shift is the amount to shift of type usint.
// * @return the object of type ubint
// */
// ubint operator<<(const usint shift) const;
//
// /**
// * Left shift operator uses in-place algorithm and operates on the same variable. It is used to reduce the copy constructor call.
// *
// * @param shift is the amount to shift of type usint.
// * @return the object of type ubint
// */
// ubint& operator<<=(usint shift);
//
// /**
// * Right shift operator of ubint
// * @param shift is the amount to shift of type usint.
// * @return the object of type ubint
// */
// ubint operator>>(usint shift) const;
//
// /**
// * Right shift operator uses in-place algorithm and operates on the same variable. It is used to reduce the copy constructor call.
// *
// * @param shift is the amount to shift of type usint.
// * @return the object of type ubint
// */
// ubint& operator>>=(usint shift);
//Auxillary Functions
/**
......@@ -999,7 +966,7 @@ namespace exp_int{
int divr_vect(ubint& r, const ubint& u, const ubint& v) const;
int divq_vect(ubint& q, const ubint& u, const ubint& v) const;
private: //todo make private again
private:
//vector storing the native integers. stored little endian
vector<limb_t> m_value;
......@@ -1036,9 +1003,6 @@ namespace exp_int{
static const ubint *m_modChain;
//public:
private:
/**
* function to return the MSB of number.
......
......@@ -454,7 +454,7 @@ public:
NativeInteger ModAdd(const NativeInteger& b, const NativeInteger& modulus) const {
Duint_type modsum = (Duint_type)m_value;
modsum += b.m_value;
if (modsum > modulus.m_value)
if (modsum >= modulus.m_value)
modsum %= modulus.m_value;
return (uint_type)modsum;
}
......@@ -469,7 +469,7 @@ public:
const NativeInteger& ModAddEq(const NativeInteger& b, const NativeInteger& modulus) {
Duint_type modsum = (Duint_type)m_value;
modsum += b.m_value;
if (modsum > modulus.m_value)
if (modsum >= modulus.m_value)
modsum %= modulus.m_value;
this->m_value = (uint_type)modsum;
return *this;
......@@ -531,11 +531,11 @@ public:
Duint_type mod = modulus.m_value;
//reduce this to a value lower than modulus
if(av > mod) {
if(av >= mod) {
av %= mod;
}
//reduce b to a value lower than modulus
if(bv > mod){
if(bv >= mod){
bv %= mod;
}
......@@ -559,11 +559,11 @@ public:
Duint_type mod = modulus.m_value;
//reduce this to a value lower than modulus
if(m_value > mod) {
if(m_value >= mod) {
m_value %= mod;
}
//reduce b to a value lower than modulus
if(bv > mod){
if(bv >= mod){
bv %= mod;
}
......@@ -635,8 +635,8 @@ public:
Duint_type av = m_value;
Duint_type bv = b.m_value;
if( av > modulus.m_value ) av = av%modulus.m_value;
if( bv > modulus.m_value ) bv = bv%modulus.m_value;
if( av >= modulus.m_value ) av = av%modulus.m_value;
if( bv >= modulus.m_value ) bv = bv%modulus.m_value;
return uint_type((av*bv)%modulus.m_value);
}
......@@ -652,8 +652,8 @@ public:
Duint_type av = m_value;
Duint_type bv = b.m_value;
if( av > modulus.m_value ) av = av%modulus.m_value;
if( bv > modulus.m_value ) bv = bv%modulus.m_value;
if( av >= modulus.m_value ) av = av%modulus.m_value;
if( bv >= modulus.m_value ) bv = bv%modulus.m_value;
this->m_value = uint_type((av*=bv)%=modulus.m_value);
......@@ -748,7 +748,7 @@ public:
product = product * mid;
//running product is calculated
if(product>modulus){
if(product >= modulus){
product = product % modulus;
}
......
......@@ -59,7 +59,7 @@ const uint64_t HashUtil::k_512[80] = { 0x428a2f98d728ae22, 0x7137449123ef65cd, 0
0x431d67c49c100d4c, 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, 0x5fcb6fab3ad6faec, 0x6c44198c4a475817 };
void HashUtil::SHA256(string message, vector<uint8_t>& digest) {
void HashUtil::SHA256(string message, vector<int64_t>& digest) {
uint32_t h_256[8] = { 0x6a09e667,0xbb67ae85,0x3c6ef372,0xa54ff53a,0x510e527f,0x9b05688c,0x1f83d9ab,0x5be0cd19 };
......@@ -68,7 +68,7 @@ void HashUtil::SHA256(string message, vector<uint8_t>& digest) {
while ((m_len + pad_len) % 512 != 448) {
pad_len++;
}
message.push_back(char());
message.push_back(0);
for (int a = 0;a < (pad_len) / 8 - 1;a++) {
message.push_back(0);
}
......
......@@ -40,7 +40,7 @@ enum HashAlgorithm { SHA_256 = 0, SHA_512 = 1 };
class HashUtil {
public:
static void Hash(string message, HashAlgorithm algo, vector<uint8_t>& digest) {
static void Hash(string message, HashAlgorithm algo, vector<int64_t>& digest) {
switch (algo) {
case SHA_256:
SHA256(message, digest);
......@@ -59,8 +59,8 @@ public:
static std::string HashString(std::string message);
private:
static void SHA256(string message, vector<uint8_t>& digest);
static void SHA512(string message, vector<uint8_t>& digest);
static void SHA256(string message, vector<int64_t>& digest);
static void SHA512(string message, vector<int64_t>& digest);
static const uint32_t k_256[64];
static const uint64_t k_512[80];
};
......
......@@ -709,9 +709,6 @@ TEST(UTBinInt,mod_arithmetic){
// = {(m mod q) + (n mod q)}mod q
// ConvertToInt converts BigInteger calculatedResult to integer
// TEST CASE WHEN THE FIRST NUMBER IS GREATER THAN MOD
{
BigInteger m("58059595");
......@@ -772,11 +769,7 @@ TEST(UTBinInt,mod_arithmetic){
// = 0 when m=n
// = {(m mod q)+q-(n mod q)}mod q when m<n
// ConvertToInt converts BigInteger calculatedResult to
// integer
//MEMORY ALLOCATION ERROR IN MODSUB METHOD (due to copying value to null pointer)
// ConvertToInt converts BigInteger calculatedResult to integer
// TEST CASE WHEN THE FIRST NUMBER IS GREATER THAN MOD
{
......@@ -822,21 +815,114 @@ TEST(UTBinInt,mod_arithmetic){
// The method "Mod Mul" operates on BigIntegers m,n,q
// Returns: (m*n)mod q
// = {(m mod q)*(n mod q)}
// = {(m mod q)*(n mod q)}mod q
// ConvertToInt converts BigInteger calculatedResult to integer
// FIRST > MOD
{
BigInteger m("39960");
BigInteger n("7959");
BigInteger q("406756");
BigInteger m("38");
BigInteger n("4");
BigInteger q("32");
BigInteger calculatedResult = m.ModMul(n,q);
uint64_t expectedResult = 24;
EXPECT_EQ(expectedResult, calculatedResult.ConvertToInt())
<< "Failure testing ModMul first > mod";
}
// FIRST == MOD
{
BigInteger m("32");