SHE.cpp 4.21 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
/*
 * SHE.cpp
 * @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.
 *
 */

Gerard Ryan's avatar
Gerard Ryan committed
27 28
#define PROFILE

Gerard Ryan's avatar
Gerard Ryan committed
29 30 31 32 33 34 35 36 37 38
#define _USE_MATH_DEFINES
#include "benchmark/benchmark_api.h"


#include <iostream>
#include <fstream>
#include "palisade.h"

#include "cryptocontexthelper.h"

Gerard Ryan's avatar
Gerard Ryan committed
39
#include "encoding/encodings.h"
Gerard Ryan's avatar
Gerard Ryan committed
40 41 42 43 44 45

#include "EncryptHelper.h"

using namespace std;
using namespace lbcrypto;

Gerard Ryan's avatar
Gerard Ryan committed
46
static std::vector<int64_t> makeIntVector(int siz, const PlaintextModulus& ptmi) {
Gerard Ryan's avatar
Gerard Ryan committed
47
	std::vector<int64_t>			elem;
Gerard Ryan's avatar
Gerard Ryan committed
48
	PlaintextModulus half = ptmi/2;
Gerard Ryan's avatar
Gerard Ryan committed
49 50

	for( int i=0; i<siz; i++ )
Gerard Ryan's avatar
Gerard Ryan committed
51
		elem.push_back(i%half);
Gerard Ryan's avatar
Gerard Ryan committed
52 53 54 55

	return elem;
}

Gerard Ryan's avatar
Gerard Ryan committed
56
static bool setup_SHE(CryptoContext<Poly> cc, Ciphertext<Poly>& ct1, Ciphertext<Poly>& ct2) {
Gerard Ryan's avatar
Gerard Ryan committed
57
	int nel = cc->GetCyclotomicOrder()/2;
Gerard Ryan's avatar
Gerard Ryan committed
58
	auto ptm = cc->GetCryptoParameters()->GetPlaintextModulus();
Gerard Ryan's avatar
Gerard Ryan committed
59 60 61

	LPKeyPair<Poly> kp = cc->KeyGen();

Gerard Ryan's avatar
Gerard Ryan committed
62 63
	Plaintext p1 = cc->MakeCoefPackedPlaintext( makeIntVector(nel, ptm) );
	Plaintext p2 = cc->MakeCoefPackedPlaintext( makeIntVector(nel, ptm) );
Gerard Ryan's avatar
Gerard Ryan committed
64

Gerard Ryan's avatar
Gerard Ryan committed
65 66
	ct1 = cc->Encrypt(kp.publicKey, p1);
	ct2 = cc->Encrypt(kp.publicKey, p2);
Gerard Ryan's avatar
Gerard Ryan committed
67

Gerard Ryan's avatar
Gerard Ryan committed
68 69 70 71 72
	try {
		cc->EvalMultKeyGen(kp.secretKey);
	} catch(...) {
		return false;
	}
Gerard Ryan's avatar
Gerard Ryan committed
73

Gerard Ryan's avatar
Gerard Ryan committed
74
	return true;
Gerard Ryan's avatar
Gerard Ryan committed
75 76 77
}

void BM_evalAdd_SHE(benchmark::State& state) { // benchmark
Gerard Ryan's avatar
Gerard Ryan committed
78 79
	CryptoContext<Poly> cc;
	Ciphertext<Poly> ct1, ct2;
Gerard Ryan's avatar
Gerard Ryan committed
80 81

	if( state.thread_index == 0 ) {
Gerard Ryan's avatar
Gerard Ryan committed
82 83 84 85
		try {
			cc = CryptoContextHelper::getNewContext(parms[state.range(0)]);
			cc->Enable(ENCRYPTION);
			cc->Enable(SHE);
Gerard Ryan's avatar
Gerard Ryan committed
86

Gerard Ryan's avatar
Gerard Ryan committed
87 88 89 90
			setup_SHE(cc, ct1, ct2);
		} catch( std::exception& e ) {
		state.SkipWithError( e.what() );
		return;
Gerard Ryan's avatar
Gerard Ryan committed
91
	}
Gerard Ryan's avatar
Gerard Ryan committed
92
}
Gerard Ryan's avatar
Gerard Ryan committed
93 94

	while (state.KeepRunning()) {
Gerard Ryan's avatar
Gerard Ryan committed
95
		Ciphertext<Poly> ctP = cc->EvalAdd(ct1, ct2);
Gerard Ryan's avatar
Gerard Ryan committed
96 97 98 99 100 101
	}
}

BENCHMARK_PARMS(BM_evalAdd_SHE)

void BM_evalMult_SHE(benchmark::State& state) { // benchmark
Gerard Ryan's avatar
Gerard Ryan committed
102 103
	CryptoContext<Poly> cc;
	Ciphertext<Poly> ct1, ct2;
Gerard Ryan's avatar
Gerard Ryan committed
104
	bool isSetup = false;
Gerard Ryan's avatar
Gerard Ryan committed
105 106

	if( state.thread_index == 0 ) {
Gerard Ryan's avatar
Gerard Ryan committed
107 108 109 110 111 112 113 114 115 116 117 118
		try {
			cc = CryptoContextHelper::getNewContext(parms[state.range(0)]);
			cc->Enable(ENCRYPTION);
			cc->Enable(SHE);

			isSetup = setup_SHE(cc, ct1, ct2);
		} catch( std::exception& e ) {
			state.SkipWithError( e.what() );
			return;
		}

		if( !isSetup ) {
Gerard Ryan's avatar
Gerard Ryan committed
119
			state.SkipWithError("Setup failed: EvalMultKeyGen not supported?");
Gerard Ryan's avatar
Gerard Ryan committed
120 121
			return;
		}
Gerard Ryan's avatar
Gerard Ryan committed
122 123 124
	}

	while (state.KeepRunning()) {
Gerard Ryan's avatar
Gerard Ryan committed
125
		Ciphertext<Poly> ctP = cc->EvalMult(ct1, ct2);
Gerard Ryan's avatar
Gerard Ryan committed
126 127 128 129 130 131
	}
}

BENCHMARK_PARMS(BM_evalMult_SHE)

void BM_baseDecompose_SHE(benchmark::State& state) { // benchmark
Gerard Ryan's avatar
Gerard Ryan committed
132 133
	CryptoContext<Poly> cc;
	Ciphertext<Poly> ct1, ct2;
Gerard Ryan's avatar
Gerard Ryan committed
134 135 136 137 138 139 140 141 142

	if( state.thread_index == 0 ) {
		try {
			cc = CryptoContextHelper::getNewContext(parms[state.range(0)]);
			cc->Enable(ENCRYPTION);
			cc->Enable(SHE);

			setup_SHE(cc, ct1, ct2);
		} catch( std::exception& e ) {
Gerard Ryan's avatar
Gerard Ryan committed
143 144
			state.SkipWithError( e.what() );
			return;
Gerard Ryan's avatar
Gerard Ryan committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
		}
	}

	while (state.KeepRunning()) {
		try {
			vector<Poly> ctP = ct1->GetElements()[0].BaseDecompose(2);
		} catch( std::exception& e ) {
			state.SkipWithError( e.what() );
			break;
		}
	}
}

BENCHMARK_PARMS(BM_baseDecompose_SHE)

//execute the benchmarks
BENCHMARK_MAIN()