

 

void refresh_blinds(int8_t flags[], DB_t standard_basis_vector[], DB_t finalCW, bool party, size_t db_nitems, size_t rotate_by = 0)
{
    
   DB_t tmp = 0; 

   for(size_t j = 0; j < db_nitems; ++j) 
   {
      if(flags[(j + rotate_by) % db_nitems] != 0) 
      {
         tmp = finalCW;      
      }
      else
      {
         tmp = 0;
      }

     if(party)  updated_blinds[j] = blinds[j] - standard_basis_vector[(j + rotate_by) % db_nitems]  - tmp;
     if(!party) updated_blinds[j] = blinds[j] - standard_basis_vector[(j + rotate_by) % db_nitems]  + tmp;

 
   }
 
}


void get_refresh_correction_words (tcp::socket& sb, DB_t one_shareA, DB_t one_shareB, 
                                    DB_t one_shareC, DB_t& CW0, DB_t& CW1, DB_t& CW2, DB_t value)
{
  
   DB_t tmp0 = value + one_shareA;
 
   write(sb, boost::asio::buffer(&tmp0, sizeof(tmp0)));
   read(sb, boost::asio::buffer(&CW0, sizeof(CW0)));
   CW0 = CW0 + tmp0;

   DB_t tmp1 = value + one_shareB; 

   write(sb, boost::asio::buffer(&tmp1, sizeof(tmp1)));
   read(sb, boost::asio::buffer(&CW1, sizeof(CW1)));
   CW1 = CW1 + tmp1;

   DB_t tmp2 = value + one_shareC;
 
   write(sb, boost::asio::buffer(&tmp2, sizeof(tmp2)));
   read(sb, boost::asio::buffer(&CW2, sizeof(CW2)));
 
   CW2 = CW2 + tmp2;   
}

void refresh_blinded_vector(int8_t flags0[], int8_t flags2[], DB_t b[],  DB_t d[], 
                            DB_t finalCW0, DB_t finalCW2, bool party, size_t db_nitems, size_t rotate_by = 0)
{
 
    
   DB_t tmp0 = 0;
   DB_t tmp2 = 0; 
 
   for(size_t j = 0; j < db_nitems; ++j) 
   {
       
      if(flags0[(j + rotate_by) % db_nitems] != 0) 
      {
        tmp0 =  finalCW0;
      }
      else
      {
       tmp0 = 0;
      }

      if(flags2[(j + rotate_by) % db_nitems] != 0) 
      {
       tmp2 =  finalCW2;
      }
      else
      {
        tmp2 = 0;
      }

      if(party) 
      {
       updated_blinded_DB_recv[j] = blinded_DB_recv[j] - b[(j + rotate_by) % db_nitems] + d[(j + rotate_by) % db_nitems]  - tmp0 + tmp2;
      }
      if(!party) 
      {
       updated_blinded_DB_recv[j] = blinded_DB_recv[j] - b[(j + rotate_by) % db_nitems] + d[(j + rotate_by) % db_nitems] + tmp0  - tmp2;
      }
   }
}



void refresh_blinded_vector_and_blinds(int8_t flags[], DB_t standard_basis_vector[], DB_t finalCW,int8_t flags0[], int8_t flags2[], DB_t b[],  DB_t d[], 
                             bool party, size_t db_nitems, size_t rotate_by)
{
 
  
 
   for(size_t j = 0; j < db_nitems; ++j) 
   {
      
       updated_blinded_DB_recv[j] = blinded_DB_recv[j] - b[(j + rotate_by) % db_nitems] + d[(j + rotate_by) % db_nitems]  
                     - (flags0[(j + rotate_by) % db_nitems] * finalCW) + (flags2[(j + rotate_by) % db_nitems] * finalCW);
       
       blinds[j] = blinds[j] - standard_basis_vector[(j + rotate_by) % db_nitems]  -   (flags[(j + rotate_by) % db_nitems] * finalCW);// ((flags[(j + rotate_by) % db_nitems]!=0) ? finalCW : 0 );


      // if(party) 
      // {
      //  updated_blinded_DB_recv[j] = blinded_DB_recv[j] - b[(j + rotate_by) % db_nitems] + d[(j + rotate_by) % db_nitems]  - ((flags0[(j + rotate_by) % db_nitems]!=0) ? finalCW : 0 ) + ((flags2[(j + rotate_by) % db_nitems]!=0) ? finalCW : 0 );
      //  blinds[j] = blinds[j] - standard_basis_vector[(j + rotate_by) % db_nitems]  - ((flags[(j + rotate_by) % db_nitems]!=0) ? finalCW : 0 );
      // }
      // if(!party) 
      // {
      //  updated_blinded_DB_recv[j] = blinded_DB_recv[j] - b[(j + rotate_by) % db_nitems] + d[(j + rotate_by) % db_nitems] + ((flags0[(j + rotate_by) % db_nitems]!=0) ? finalCW : 0 ) - ((flags2[(j + rotate_by) % db_nitems]!=0) ? finalCW : 0 );
      //  blinds[j] = blinds[j] - standard_basis_vector[(j + rotate_by) % db_nitems]  + ((flags[(j + rotate_by) % db_nitems]!=0) ? finalCW : 0 );
      // }
   }
}

void duoram_refresh (int8_t flags0[], int8_t flags1[], int8_t flags2[], DB_t b[], DB_t c[], DB_t d[],   DB_t CW, bool party, size_t db_nitems, size_t rotate_by_ = 0)
{ 
   refresh_blinded_vector_and_blinds(flags1, c, CW, flags0, flags2, b, d, party, db_nitems, rotate_by_);
}
