sharpen-packed.cpp 23.6 KB
Newer Older
Gerard Ryan's avatar
Gerard Ryan committed
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
/*
 * @file 
 * @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.
 *
 */
 /*
BFV RNS testing programs
*/

#include <iostream>
#include <fstream>
#include <limits>

#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>

#include "palisade.h"

#include "cryptocontexthelper.h"
#include "encoding/encodings.h"

#include "utils/debug.h"

using namespace std;
using namespace lbcrypto;

#include <iterator>

#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"

void Sharpen();
void KeyGen(size_t size);
void Encrypt(size_t size);
void Evaluate(size_t size);
void Decrypt(size_t size);

int main(int argc, char **argv) {

	static int operation_flag;
	int opt;

	static struct option long_options[] =
	  {
		/* These options set a flag. */
		//{"verbose", no_argument,       &verbose_flag, 1},
		//{"brief",   no_argument,       &verbose_flag, 0},
		{"run", 	no_argument,       &operation_flag, 0},
		{"keygen", 	no_argument,       &operation_flag, 1},
		{"encrypt",   no_argument,     &operation_flag, 2},
		{"evaluate",   no_argument,     &operation_flag, 3},
		{"decrypt",   no_argument,     &operation_flag, 4},
		/* These options don�t set a flag.
		   We distinguish them by their indices. */
		{"size",  	required_argument, 			0, 's'},
		{"help",    no_argument, 0, 'h'},
		{0, 0, 0, 0}
	  };
	/* getopt_long stores the option index here. */
	int option_index = 0;

	size_t size = 0;

	while ((opt = getopt_long(argc, argv, "s:h", long_options, &option_index)) != -1) {
		switch (opt)
		{
			case 0:
				if (long_options[option_index].flag != 0)
					break;
				break;
			case 's':
				size = stoi(optarg);
				break;
			case 'h':
			default: /* '?' */
			  std::cerr<< "Usage: "<<argv[0]<<" <arguments> " <<std::endl
				   << "arguments:" <<std::endl
				   << "  --run simple run w/o serialization" <<std::endl
				   << "  --keygen --encrypt --evaluate --decrypt operation to run" <<std::endl
				   << "  -s --size size of the image"  <<std::endl
				   << "  -h --help prints this message" <<std::endl;
			  exit(EXIT_FAILURE);
		}
	}

	switch(operation_flag)
	{
		case 0:
			Sharpen();
			break;
		case 1:
			KeyGen(size);
			break;
		case 2:
			Encrypt(size);
			break;
		case 3:
			Evaluate(size);
			break;
		case 4:
			Decrypt(size);
			break;
		default:
			exit(EXIT_FAILURE);
	}

	//Sharpen();

	//cin.get();
	return 0;
}

#define PROFILE

CryptoContext<DCRTPoly> DeserializeContext(const string& ccFileName)
{

	std::cout << "Deserializing the crypto context...";

	Serialized	ccSer;
	if (SerializableHelper::ReadSerializationFromFile(ccFileName, &ccSer) == false) {
		cerr << "Could not read the cryptocontext file" << endl;
		return 0;
	}

	CryptoContext<DCRTPoly> cc = CryptoContextFactory<DCRTPoly>::DeserializeAndCreateContext(ccSer);

	std::cout << "Completed" << std::endl;

	return cc;
}


CryptoContext<DCRTPoly> DeserializeContextWithEvalKeys(const string& ccFileName, const string& eaFileName)
{

	std::cout << "Deserializing the crypto context...";

	Serialized	ccSer, eaSer;
	if (SerializableHelper::ReadSerializationFromFile(ccFileName, &ccSer) == false) {
		cerr << "Could not read the cryptocontext file" << endl;
		return 0;
	}

	if (SerializableHelper::ReadSerializationFromFile(eaFileName, &eaSer) == false) {
		cerr << "Could not read the eval automorphism key file" << endl;
		return 0;
	}

	CryptoContext<DCRTPoly> cc = CryptoContextFactory<DCRTPoly>::DeserializeAndCreateContext(ccSer);

	if( cc->DeserializeEvalAutomorphismKey(eaSer) == false ) {
		cerr << "Could not deserialize the eval automorphism key file" << endl;
		return 0;
	}

	std::cout << "Completed" << std::endl;

	return cc;

}

void KeyGen(size_t size) {

	TimeVar t1, t_total; //for TIC TOC

	TIC(t_total);

	double timeKeyGen(0.0), timeSer(0.0), timeTotal(0.0);

	usint ptm = 65537;
	double sigma = 3.19;
	double rootHermiteFactor = 1.004;

	std::cout << "Generating parameters...";

	EncodingParams encodingParams(new EncodingParamsImpl(ptm));

	CryptoContext<DCRTPoly> cryptoContext = CryptoContextFactory<DCRTPoly>::genCryptoContextBFVrns(
			encodingParams, rootHermiteFactor, sigma, 0, 1, 0, OPTIMIZED,2,30,60);

	uint32_t m = cryptoContext->GetCyclotomicOrder();
	PackedEncoding::SetParams(m, encodingParams);
	uint32_t batchSize = m/4;
	encodingParams->SetBatchSize(batchSize);

	std::cout << "p = " << cryptoContext->GetCryptoParameters()->GetPlaintextModulus() << std::endl;
	std::cout << "n = " << cryptoContext->GetCryptoParameters()->GetElementParams()->GetCyclotomicOrder() / 2 << std::endl;
	std::cout << "log2 q = " << cryptoContext->GetCryptoParameters()->GetElementParams()->GetModulus().GetMSB() << std::endl;

	// enable features that you wish to use
	cryptoContext->Enable(ENCRYPTION);
	cryptoContext->Enable(SHE);

	std::cout << "Completed" << std::endl;

	std::cout << "Generating keys...";

	// Key generation
	LPKeyPair<DCRTPoly> kp;

	TIC(t1);

	kp = cryptoContext->KeyGen();

	timeKeyGen = TOC(t1);

	// Read the image file
	int width, height, bpp;
	string path = "demoData/Baboon" + to_string(size) + ".png";
	const char *pathc = path.c_str();

	/*unsigned char* data =*/ stbi_load( pathc, &width, &height, &bpp, 1 );

	// Rotations we are supporting
	vector<int32_t> indexList = {-1-width,-width,-width+1,-1,1,width-1,width,width+1};

	TIC(t1);
	cryptoContext->EvalAtIndexKeyGen(kp.secretKey,indexList);
	timeKeyGen += TOC(t1);

	std::cout << "Completed" << std::endl;

	TIC(t1);

	std::cout << "Serializing crypto context...";

	Serialized ctxt;

	if (cryptoContext->Serialize(&ctxt)) {
		if (!SerializableHelper::WriteSerializationToFile(ctxt, "demoData/cryptocontext.txt")) {
			cerr << "Error writing serialization of the crypto context to cryptocontext.txt" << endl;
			return;
		}
	}
	else {
		cerr << "Error serializing the crypto context" << endl;
		return;
	}

	std::cout << "Completed" << std::endl;

	std::cout << "Serializing private and public keys...";

    if(kp.publicKey && kp.secretKey) {
		Serialized pubK, privK;

		if(kp.publicKey->Serialize(&pubK)) {
			if(!SerializableHelper::WriteSerializationToFile(pubK, "demoData/PUB.txt")) {
			cerr << "Error writing serialization of public key" << endl;
			return;
			}
		} else {
			cerr << "Error serializing public key" << endl;
			return;
		}

		if(kp.secretKey->Serialize(&privK)) {
			if(!SerializableHelper::WriteSerializationToFile(privK, "demoData/PRI.txt")) {
			cerr << "Error writing serialization of private key" << endl;
			return;
			}
		} else {
			cerr << "Error serializing private key" << endl;
			return;
		}
	} else {
		cerr << "Failure in generating keys" << endl;
	}

	std::cout << "Completed" << std::endl;

	std::cout << "Serializing eval automorphism keys...";

	Serialized eaKeys;

	if (cryptoContext->SerializeEvalAutomorphismKey(&eaKeys)) {
		if (!SerializableHelper::WriteSerializationToFile(eaKeys, "demoData/EVALAUTO.txt")) {
			cerr << "Error writing serialization of the eval automorphism keys" << endl;
			return;
		}
	}
	else {
		cerr << "Error serializing eval automorphism key" << endl;
		return;
	}

	std::cout << "Completed" << std::endl;

	timeSer = TOC(t1);

	timeTotal = TOC(t_total);

	std::cout << "\nTiming Summary" << std::endl;
	std::cout << "Key generation time:        " << "\t" << timeKeyGen << " ms" << std::endl;
	std::cout << "Serialization time: " << "\t" << timeSer << " ms" << std::endl;
	std::cout << "Total execution time:       " << "\t" << timeTotal << " ms" << std::endl;

}

void Encrypt(size_t size) {

	TimeVar t1, t_total; //for TIC TOC

	TIC(t_total);

	double timeEnc(0.0), timeSer(0.0), timeTotal(0.0);

	TIC(t1);

	CryptoContext<DCRTPoly> cryptoContext = DeserializeContext("demoData/cryptocontext.txt");

	const shared_ptr<LPCryptoParameters<DCRTPoly>> cryptoParams = cryptoContext->GetCryptoParameters();
	const auto encodingParams = cryptoParams->GetEncodingParams();
	uint32_t batchSize = encodingParams->GetBatchSize();

	string pubKeyLoc = "demoData/PUB.txt";
	Serialized kser;
	if(SerializableHelper::ReadSerializationFromFile(pubKeyLoc, &kser) == false) {
		cerr << "Could not read public key" << endl;
		return;
	}

	// Initialize the public key containers.
	LPPublicKey<DCRTPoly> pk = cryptoContext->deserializePublicKey(kser);

	timeSer = TOC(t1);

	// Read the image file
	int width, height, bpp;
	string path = "demoData/Baboon" + to_string(size) + ".png";
	const char *pathc = path.c_str();

	unsigned char* data = stbi_load( pathc, &width, &height, &bpp, 1 );

	vector<Plaintext> plaintext;

	vector<uint64_t> ptxtVector(batchSize);
	vector<uint64_t> ptxtVectorTemp(batchSize);

	size_t counter = 0;

	size_t slack = 0;

	for(int i = 0; i < height; i++)
	{
		std::cout << " [ ";
		for(int k = 0; k < width; k++) {
			std::cout << (unsigned int)(unsigned char)data[i*width+k] << " ";
			ptxtVector[(i*width+k)%batchSize] = (unsigned int)(unsigned char)data[i*width+k];
			counter++;
			if (((i*width+k+slack)%batchSize==0)&&(i+k)>0) {
				plaintext.push_back(cryptoContext->MakePackedPlaintext(ptxtVector));
				ptxtVectorTemp = std::vector<uint64_t>(batchSize);
				for (int j = 0; j < 2*width; j++)
					ptxtVectorTemp[j] = ptxtVector[batchSize-2*width+j];
				slack += 2*width;
				ptxtVector = ptxtVectorTemp;
				counter = 0;
			}
		}
		std::cout << " ] " << std::endl;
	}

	if (counter > 0)
		plaintext.push_back(cryptoContext->MakePackedPlaintext(ptxtVector));

	vector<Ciphertext<DCRTPoly>> image(plaintext.size());

	for(size_t i = 0; i < plaintext.size(); i++){

		TIC(t1);
		image[i] = cryptoContext->Encrypt(pk, plaintext[i]);
		timeEnc += TOC(t1);

		TIC(t1);

		string ciphertextname ="demoData/ciphertext-" + to_string(i+1) + ".txt";
		ofstream ctSer(ciphertextname, ios::binary);

		if (!ctSer.is_open()) {
			cerr << "could not open output file " << ciphertextname << endl;
			return;
		}

		Serialized cSer;
		if (image[i]->Serialize(&cSer)) {
			if (!SerializableHelper::WriteSerializationToFile(cSer, ciphertextname)) {
				cerr << "Error writing serialization of ciphertext to " + ciphertextname << endl;
				return;
			}
		}
		else {
			cerr << "Error serializing ciphertext" << endl;
			return;
		}

		timeSer += TOC(t1);
	}

	timeTotal = TOC(t_total);

	std::cout << "\nTiming Summary" << std::endl;
	std::cout << "Encryption time:        " << "\t" << timeEnc << " ms" << std::endl;
	std::cout << "Serialization time: " << "\t" << timeSer << " ms" << std::endl;
	std::cout << "Total execution time:       " << "\t" << timeTotal << " ms" << std::endl;

}

void Evaluate(size_t size)
{

	TimeVar t1, t_total; //for TIC TOC

	TIC(t_total);

	double timeEval(0.0), timeSer(0.0), timeTotal(0.0);

	TIC(t1);

    CryptoContext<DCRTPoly> cryptoContext = DeserializeContextWithEvalKeys("demoData/cryptocontext.txt","demoData/EVALAUTO.txt");

	const shared_ptr<LPCryptoParameters<DCRTPoly>> cryptoParams = cryptoContext->GetCryptoParameters();
	const auto encodingParams = cryptoParams->GetEncodingParams();
	int batchSize = encodingParams->GetBatchSize();

	int height = size;
	int width = size;

    int ciphertextCount;

    if (width*height < batchSize)
    	ciphertextCount = 1;
    else
    	ciphertextCount = ceil((double)batchSize/((double)(height+2)*width));

    std::cout << "Deserializing ciphertexts..." ;

    vector<Ciphertext<DCRTPoly>> image(ciphertextCount);

	for(int i = 0; i < ciphertextCount; i++)
	{

		string ciphertextname = "demoData/ciphertext-" + to_string(i+1) + ".txt";

		Serialized kser;
		if(SerializableHelper::ReadSerializationFromFile(ciphertextname, &kser) == false) {
				cerr << "Could not read ciphertext" << endl;
				return;
			}

		Ciphertext<DCRTPoly> ct = cryptoContext->deserializeCiphertext(kser);
		if(ct == NULL) {
			cerr << "Could not deserialize ciphertext" << endl;
			return;
		}
		else{
			image[i] = ct;
		}

	}

	std::cout << "Completed" << std::endl;

	timeSer = TOC(t1);

	std::cout << "Computing..." ;

	vector<int32_t> indexList = {-1-width,-width,-width+1,-1,1,width-1,width,width+1};

	TIC(t1);

	vector<uint64_t> eight(batchSize,8);
	Plaintext ptxtEight = cryptoContext->MakePackedPlaintext(eight);

	vector<uint64_t> two(batchSize,2);
	Plaintext ptxtTwo = cryptoContext->MakePackedPlaintext(two);

	vector<uint64_t> one(batchSize,1);
	Plaintext ptxtOne = cryptoContext->MakePackedPlaintext(one);

	vector<uint64_t> mask(batchSize);
	for (int i=0; i < batchSize; i++)
	{
		if ((i>width)&&(i<batchSize-width)&&(i%width!=0)&&(i%width!=width-1))
			mask[i]=1;
	}
	Plaintext ptxtMask = cryptoContext->MakePackedPlaintext(mask);

	vector<Ciphertext<DCRTPoly>> pixel_value(image.size());
	vector<Ciphertext<DCRTPoly>> image2(image.size());

	for (size_t k = 0; k < image.size(); k++) {
		for(size_t i = 0; i < indexList.size(); i++) {
			if (i == 0) {
				pixel_value[k] = cryptoContext->EvalAtIndex(image[k],indexList[i]);
			}
			else
				pixel_value[k] = cryptoContext->EvalAdd(pixel_value[k],cryptoContext->EvalAtIndex(image[k],indexList[i]));
		}
		pixel_value[k] = cryptoContext->EvalSub(pixel_value[k],cryptoContext->EvalMult(image[k],ptxtEight));
		pixel_value[k] = cryptoContext->EvalMult(pixel_value[k],ptxtMask);
		image2[k] = cryptoContext->EvalMult(image[k],ptxtTwo);
		image2[k] = cryptoContext->EvalAdd(image2[k],ptxtOne);
		image2[k] = cryptoContext->EvalSub(image2[k],pixel_value[k]);
	}

	timeEval = TOC(t1);

	std::cout << "Completed" << std::endl;

	std::cout << "Serializing the results..." ;

	TIC(t1);

	for(int i = 0; i < ciphertextCount; i++)
	{

		string ciphertextname ="demoData/ciphertext-result-" + to_string(i+1) + ".txt";
		ofstream ctSer(ciphertextname, ios::binary);

		if (!ctSer.is_open()) {
			cerr << "could not open output file " << ciphertextname << endl;
			return;
		}

		Serialized cSer;
		if (image2[i]->Serialize(&cSer)) {
			if (!SerializableHelper::WriteSerializationToFile(cSer, ciphertextname)) {
				cerr << "Error writing serialization of ciphertext to " + ciphertextname << endl;
				return;
			}
		}
		else {
			cerr << "Error serializing ciphertext" << endl;
			return;
		}

	}

	timeSer += TOC(t1);

	std::cout << "Completed" << std::endl;

	timeTotal = TOC(t_total);

	std::cout << "\nTiming Summary" << std::endl;
	std::cout << "Evaluation time:        " << "\t" << timeEval << " ms" << std::endl;
	std::cout << "Serialization time: " << "\t" << timeSer << " ms" << std::endl;
	std::cout << "Total execution time:       " << "\t" << timeTotal << " ms" << std::endl;

}

void Decrypt(size_t size) {

	TimeVar t1, t_total; //for TIC TOC

	TIC(t_total);

	double timeDec(0.0), timeSer(0.0), timeTotal(0.0);

	TIC(t1);

	CryptoContext<DCRTPoly> cryptoContext = DeserializeContext("demoData/cryptocontext.txt");

	const shared_ptr<LPCryptoParameters<DCRTPoly>> cryptoParams = cryptoContext->GetCryptoParameters();
	const auto encodingParams = cryptoParams->GetEncodingParams();
	size_t batchSize = encodingParams->GetBatchSize();
	usint ptm = cryptoParams->GetPlaintextModulus();

	size_t height = size;
	size_t width = size;

	string privKeyLoc = "demoData/PRI.txt";
	Serialized kser;
	if(SerializableHelper::ReadSerializationFromFile(privKeyLoc, &kser) == false) {
		cerr << "Could not read privatekey" << endl;
		return;
	}

	// Initialize the public key containers.
	LPPrivateKey<DCRTPoly> sk = cryptoContext->deserializeSecretKey(kser);

    int ciphertextCount;

    if (width*height < batchSize)
    		ciphertextCount = 1;
    else
    		ciphertextCount = ceil((double)batchSize/((double)(height+2)*width));

    std::cout << "Deserializing ciphertexts..." ;

    vector<Ciphertext<DCRTPoly>> image2(ciphertextCount);

	for(int i = 0; i < ciphertextCount; i++)
	{

		string ciphertextname = "demoData/ciphertext-result-" + to_string(i+1) + ".txt";

		Serialized kser;
		if(SerializableHelper::ReadSerializationFromFile(ciphertextname, &kser) == false) {
				cerr << "Could not read ciphertext" << endl;
				return;
			}

		Ciphertext<DCRTPoly> ct = cryptoContext->deserializeCiphertext(kser);
		if(ct == NULL) {
			cerr << "Could not deserialize ciphertext" << endl;
			return;
		}
		else{
			image2[i] = ct;
		}

	}

	std::cout << "Completed" << std::endl;

	timeSer = TOC(t1);

    std::cout << "Decrypting..." ;

   	vector<Plaintext> result(image2.size());

	TIC(t1);

	for(size_t i = 0; i < image2.size(); i++)
	{
		cryptoContext->Decrypt(sk, image2[i],&result[i]);
	}

	timeDec = TOC(t1);

	std::cout << "Completed" << std::endl;

	std::cout << "The result is" << std::endl;

	usint half = ptm >> 1;
	for(size_t i = 0; i < result.size(); i++)
	{
		vector<uint64_t> vectorRes = result[i]->GetPackedValue();

		std::cout << " [ ";
		for(size_t k = 0; k < std::min(batchSize,width*height); k++) {
			if (vectorRes[k] > half )
				std::cout << std::floor((int)(vectorRes[k]-ptm)/2) << " ";
			else
				std::cout << std::floor(vectorRes[k]/2) << " ";
		}
		std::cout << " ] " << std::endl;
	}

	timeTotal = TOC(t_total);

	string path = "demoData/Baboon" + to_string(size) + "OUT.png";
	const char *pathc = path.c_str();
	unsigned char *data = new unsigned char[height*width];
	for(size_t i = 0; i < height; i++)
	{
		vector<uint64_t> vectorRes = result[i]->GetPackedValue();
		for(size_t k = 0; k < width; k++) {
			if( vectorRes[k] > half )
				data[i*width + k] = std::floor((int)(vectorRes[k]-ptm)/2);
			else
				data[i*width + k] = std::floor(vectorRes[k]/2);
		}
	}

//	for(int i = 0; i < height; i++)
//	{
//		std::cout << " [ ";
//		for(int k = 0; k < width; k++) {
//			std::cout << (unsigned int)(unsigned char)data[i*width+k] << " ";
//			plaintext[i].push_back(cryptoContext->MakeFractionalPlaintext( (unsigned int)(unsigned char)data[i*width + k]));
//		}
//		std::cout << " ] " << std::endl;
//	}
	stbi_write_png( pathc, width, height, 1, data, width*1 );
	delete[] data;

	std::cout << "Number of ciphertexts: " << result.size() << std::endl;

	std::cout << "\nTiming Summary" << std::endl;
	std::cout << "Decryption time:        " << "\t" << timeDec << " ms" << std::endl;
	std::cout << "Serialization time: " << "\t" << timeSer << " ms" << std::endl;
	std::cout << "Total execution time:       " << "\t" << timeTotal << " ms" << std::endl;

}

void Sharpen() {

	std::cout << "\n===========SHARPENING DEMO===============: " << std::endl;

	std::cout << "\nThis code demonstrates the implementation of 8-neighbor Laplacian image sharpening algorithm using BFVrns. " << std::endl;

	usint ptm = 65537;
	double sigma = 3.19;
	double rootHermiteFactor = 1.004;

	EncodingParams encodingParams(new EncodingParamsImpl(ptm));

	CryptoContext<DCRTPoly> cryptoContext = CryptoContextFactory<DCRTPoly>::genCryptoContextBFVrns(
			encodingParams, rootHermiteFactor, sigma, 0, 1, 0, OPTIMIZED,2,30,60);

	uint32_t m = cryptoContext->GetCyclotomicOrder();
	PackedEncoding::SetParams(m, encodingParams);
	uint32_t batchSize = m/4;
	encodingParams->SetBatchSize(batchSize);

	std::cout << "p = " << cryptoContext->GetCryptoParameters()->GetPlaintextModulus() << std::endl;
	std::cout << "n = " << cryptoContext->GetCryptoParameters()->GetElementParams()->GetCyclotomicOrder() / 2 << std::endl;
	std::cout << "log2 q = " << cryptoContext->GetCryptoParameters()->GetElementParams()->GetModulus().GetMSB() << std::endl;

	// enable features that you wish to use
	cryptoContext->Enable(ENCRYPTION);
	cryptoContext->Enable(SHE);

	// Key generation
	LPKeyPair<DCRTPoly> keyPair;

	keyPair = cryptoContext->KeyGen();

	// Read the image file
	int width, height, bpp;
	unsigned char* data = stbi_load( "demoData/Baboon8.png", &width, &height, &bpp, 1 );

	// Rotations we are supporting
	vector<int32_t> indexList = {-1-width,-width,-width+1,-1,1,width-1,width,width+1};
	cryptoContext->EvalAtIndexKeyGen(keyPair.secretKey,indexList);

	vector<Plaintext> plaintext;

	vector<uint64_t> ptxtVector(batchSize);
	vector<uint64_t> ptxtVectorTemp(batchSize);

	size_t counter = 0;

	size_t slack = 0;

	for(int i = 0; i < height; i++)
	{
		std::cout << " [ ";
		for(int k = 0; k < width; k++) {
			std::cout << (unsigned int)(unsigned char)data[i*width+k] << " ";
			ptxtVector[(i*width+k)%batchSize] = (unsigned int)(unsigned char)data[i*width+k];
			counter++;
			if (((i*width+k+slack)%batchSize==0)&&(i+k)>0) {
				plaintext.push_back(cryptoContext->MakePackedPlaintext(ptxtVector));
				ptxtVectorTemp = std::vector<uint64_t>(batchSize);
				for (int j = 0; j < 2*width; j++)
					ptxtVectorTemp[j] = ptxtVector[batchSize-2*width+j];
				slack += 2*width;
				ptxtVector = ptxtVectorTemp;
				counter = 0;
			}
		}
		std::cout << " ] " << std::endl;
	}

	if (counter > 0)
		plaintext.push_back(cryptoContext->MakePackedPlaintext(ptxtVector));

	vector<Ciphertext<DCRTPoly>> image(plaintext.size());

	for(size_t i = 0; i < plaintext.size(); i++)
		image[i] = cryptoContext->Encrypt(keyPair.publicKey, plaintext[i]);

	vector<uint64_t> eight(batchSize,8);
	Plaintext ptxtEight = cryptoContext->MakePackedPlaintext(eight);

	vector<uint64_t> two(batchSize,2);
	Plaintext ptxtTwo = cryptoContext->MakePackedPlaintext(two);

	vector<uint64_t> one(batchSize,1);
	Plaintext ptxtOne = cryptoContext->MakePackedPlaintext(one);

	vector<uint64_t> mask(batchSize);
	for (size_t i = 0; i < batchSize; i++)
	{
		if ((i>(size_t)width)&&(i<batchSize-width)&&(i%width!=0)&&(i%width!=(size_t)width-1))
			mask[i]=1;
	}
	Plaintext ptxtMask = cryptoContext->MakePackedPlaintext(mask);

	vector<Ciphertext<DCRTPoly>> pixel_value(image.size());
	vector<Ciphertext<DCRTPoly>> image2(image.size());

	for (size_t k = 0; k < image.size(); k++) {
		for(size_t i = 0; i < indexList.size(); i++) {
			if (i == 0) {
				pixel_value[k] = cryptoContext->EvalAtIndex(image[k],indexList[i]);
			}
			else
				pixel_value[k] = cryptoContext->EvalAdd(pixel_value[k],cryptoContext->EvalAtIndex(image[k],indexList[i]));
		}
		pixel_value[k] = cryptoContext->EvalSub(pixel_value[k],cryptoContext->EvalMult(image[k],ptxtEight));
		pixel_value[k] = cryptoContext->EvalMult(pixel_value[k],ptxtMask);
		image2[k] = cryptoContext->EvalMult(image[k],ptxtTwo);
		image2[k] = cryptoContext->EvalAdd(image2[k],ptxtOne);
		image2[k] = cryptoContext->EvalSub(image2[k],pixel_value[k]);
	}

	vector<Plaintext> result(image2.size());

	for(size_t i = 0; i < image2.size(); i++)
	{
		cryptoContext->Decrypt(keyPair.secretKey, image2[i],&result[i]);
	}

	std::cout << "The result is" << std::endl;

	for(size_t i = 0; i < result.size(); i++)
	{
		vector<uint64_t> vectorRes = result[i]->GetPackedValue();

		usint half = ptm >> 1;

		std::cout << " [ ";
		for(size_t k = 0; k < (size_t)std::min((int)batchSize,width*height); k++) {
			if (vectorRes[k] > half )
				std::cout << std::floor((int)(vectorRes[k]-ptm)/2) << " ";
			else
				std::cout << std::floor(vectorRes[k]/2) << " ";
		}
		std::cout << " ] " << std::endl;
	}

	std::cout << "Number of ciphertexts: " << result.size() << std::endl;

}