Encoding.cpp 5.67 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
/*
 * @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.
 *
 */

#define _USE_MATH_DEFINES
#include "benchmark/benchmark_api.h"

bool runOnlyOnce = true;

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

#include "cryptocontexthelper.h"

Gerard Ryan's avatar
Gerard Ryan committed
37 38
#include "encoding/encodings.h"
#include "lattice/elemparamfactory.h"
Gerard Ryan's avatar
Gerard Ryan committed
39 40 41 42

using namespace std;
using namespace lbcrypto;

Gerard Ryan's avatar
Gerard Ryan committed
43
void BM_encoding_Scalar(benchmark::State& state) {
Gerard Ryan's avatar
Gerard Ryan committed
44 45 46
	usint	m = 1024;
	PlaintextModulus	ptm = 128;
	int64_t value = 47;
Gerard Ryan's avatar
Gerard Ryan committed
47
	Plaintext plaintext;
Gerard Ryan's avatar
Gerard Ryan committed
48 49
	shared_ptr<ILParams> lp = ElemParamFactory::GenElemParams<ILParams>(m);
	EncodingParams ep( new EncodingParamsImpl(ptm) );
Gerard Ryan's avatar
Gerard Ryan committed
50

Gerard Ryan's avatar
Gerard Ryan committed
51
	while (state.KeepRunning()) {
Gerard Ryan's avatar
Gerard Ryan committed
52
		plaintext.reset( new ScalarEncoding(lp, ep, value) );
Gerard Ryan's avatar
Gerard Ryan committed
53 54
		plaintext->Encode();
	}
Gerard Ryan's avatar
Gerard Ryan committed
55 56
}

Gerard Ryan's avatar
Gerard Ryan committed
57 58 59 60 61
BENCHMARK(BM_encoding_Scalar);


void BM_encoding_Integer(benchmark::State& state) { // benchmark
	Plaintext plaintext;
Gerard Ryan's avatar
Gerard Ryan committed
62 63 64
	usint	m = 1024;
	PlaintextModulus	ptm = 128;
	int64_t mv = 58;
Gerard Ryan's avatar
Gerard Ryan committed
65

Gerard Ryan's avatar
Gerard Ryan committed
66 67
	shared_ptr<ILParams> lp = ElemParamFactory::GenElemParams<ILParams>(m);
	EncodingParams ep( new EncodingParamsImpl(ptm) );
Gerard Ryan's avatar
Gerard Ryan committed
68 69

	while (state.KeepRunning()) {
Gerard Ryan's avatar
Gerard Ryan committed
70
		plaintext.reset( new IntegerEncoding(lp, ep, mv) );
Gerard Ryan's avatar
Gerard Ryan committed
71
		plaintext->Encode();
Gerard Ryan's avatar
Gerard Ryan committed
72 73 74
	}
}

Gerard Ryan's avatar
Gerard Ryan committed
75
BENCHMARK(BM_encoding_Integer);
Gerard Ryan's avatar
Gerard Ryan committed
76

Gerard Ryan's avatar
Gerard Ryan committed
77 78
void BM_encoding_CoefPacked(benchmark::State& state) {
	Plaintext plaintext;
Gerard Ryan's avatar
Gerard Ryan committed
79 80 81
	usint	m = 1024;
	PlaintextModulus	ptm = 128;
	PlaintextModulus half = ptm / 2;
Gerard Ryan's avatar
Gerard Ryan committed
82

Gerard Ryan's avatar
Gerard Ryan committed
83 84
	shared_ptr<ILParams> lp = ElemParamFactory::GenElemParams<ILParams>(m);
	EncodingParams ep( new EncodingParamsImpl(ptm) );
Gerard Ryan's avatar
Gerard Ryan committed
85

Gerard Ryan's avatar
Gerard Ryan committed
86 87 88
	vector<int64_t> intvec;
	for( usint ii=0; ii<m/2; ii++)
		intvec.push_back( rand() % half );
Gerard Ryan's avatar
Gerard Ryan committed
89 90

	while (state.KeepRunning()) {
Gerard Ryan's avatar
Gerard Ryan committed
91
		plaintext.reset( new CoefPackedEncoding(lp,ep,intvec) );
Gerard Ryan's avatar
Gerard Ryan committed
92
		plaintext->Encode();
Gerard Ryan's avatar
Gerard Ryan committed
93 94 95
	}
}

Gerard Ryan's avatar
Gerard Ryan committed
96
BENCHMARK(BM_encoding_CoefPacked);
Gerard Ryan's avatar
Gerard Ryan committed
97

Gerard Ryan's avatar
Gerard Ryan committed
98 99 100 101 102
void BM_encoding_PackedIntPlaintext(benchmark::State& state) {
	Plaintext plaintext;
	shared_ptr<ILParams> lp;
	EncodingParams ep;

Gerard Ryan's avatar
Gerard Ryan committed
103
	std::vector<int64_t> vectorOfInts1 = { 1,2,3,4,5,6,7,8,0,0 };
Gerard Ryan's avatar
Gerard Ryan committed
104

Gerard Ryan's avatar
Gerard Ryan committed
105 106 107 108 109 110 111
	usint m = 22;
	PlaintextModulus p = 89;
	BigInteger modulusP(p);
	BigInteger modulusQ("955263939794561");
	BigInteger squareRootOfRoot("941018665059848");
	BigInteger bigmodulus("80899135611688102162227204937217");
	BigInteger bigroot("77936753846653065954043047918387");
Gerard Ryan's avatar
Gerard Ryan committed
112

Gerard Ryan's avatar
Gerard Ryan committed
113 114
	auto cycloPoly = GetCyclotomicPolynomial<BigVector>(m, modulusQ);
	ChineseRemainderTransformArb<BigVector>::SetCylotomicPolynomial(cycloPoly, modulusQ);
Gerard Ryan's avatar
Gerard Ryan committed
115

Gerard Ryan's avatar
Gerard Ryan committed
116 117
	lp.reset(new ILParams(m, modulusQ, squareRootOfRoot, bigmodulus, bigroot));
	ep.reset(new EncodingParamsImpl(p,8));
Gerard Ryan's avatar
Gerard Ryan committed
118 119

	while (state.KeepRunning()) {
Gerard Ryan's avatar
Gerard Ryan committed
120
		plaintext.reset( new PackedEncoding(lp,ep,vectorOfInts1) );
Gerard Ryan's avatar
Gerard Ryan committed
121

Gerard Ryan's avatar
Gerard Ryan committed
122
		plaintext->Encode();
Gerard Ryan's avatar
Gerard Ryan committed
123 124 125
	}
}

Gerard Ryan's avatar
Gerard Ryan committed
126 127 128 129 130 131
BENCHMARK(BM_encoding_PackedIntPlaintext);

void BM_encoding_PackedIntPlaintext_SetParams(benchmark::State& state) {
	Plaintext plaintext;
	shared_ptr<ILParams> lp;
	EncodingParams ep;
Gerard Ryan's avatar
Gerard Ryan committed
132

Gerard Ryan's avatar
Gerard Ryan committed
133 134 135 136
	usint m = 22;
	PlaintextModulus p = 89;
	BigInteger modulusP(p);

Gerard Ryan's avatar
Gerard Ryan committed
137
	std::vector<int64_t> vectorOfInts1 = { 1,2,3,4,5,6,7,8,0,0 };
Gerard Ryan's avatar
Gerard Ryan committed
138 139

	if( state.thread_index == 0 ) {
Gerard Ryan's avatar
Gerard Ryan committed
140 141 142 143
		BigInteger modulusQ("955263939794561");
		BigInteger squareRootOfRoot("941018665059848");
		BigInteger bigmodulus("80899135611688102162227204937217");
		BigInteger bigroot("77936753846653065954043047918387");
Gerard Ryan's avatar
Gerard Ryan committed
144

Gerard Ryan's avatar
Gerard Ryan committed
145 146
		auto cycloPoly = GetCyclotomicPolynomial<BigVector>(m, modulusQ);
		ChineseRemainderTransformArb<BigVector>::SetCylotomicPolynomial(cycloPoly, modulusQ);
Gerard Ryan's avatar
Gerard Ryan committed
147 148


Gerard Ryan's avatar
Gerard Ryan committed
149 150
		lp.reset(new ILParams(m, modulusQ, squareRootOfRoot, bigmodulus, bigroot));
		ep.reset(new EncodingParamsImpl(p,8));
Gerard Ryan's avatar
Gerard Ryan committed
151 152 153
	}

	while (state.KeepRunning()) {
Gerard Ryan's avatar
Gerard Ryan committed
154
		PackedEncoding::SetParams(m, ep);
Gerard Ryan's avatar
Gerard Ryan committed
155
		state.PauseTiming();
Gerard Ryan's avatar
Gerard Ryan committed
156
		PackedEncoding::Destroy();
Gerard Ryan's avatar
Gerard Ryan committed
157 158 159 160
		state.ResumeTiming();
	}
}

Gerard Ryan's avatar
Gerard Ryan committed
161
BENCHMARK(BM_encoding_PackedIntPlaintext_SetParams);
Gerard Ryan's avatar
Gerard Ryan committed
162

Gerard Ryan's avatar
Gerard Ryan committed
163 164 165 166

void BM_Encoding_String(benchmark::State& state) { // benchmark
	CryptoContext<Poly> cc;
	Plaintext plaintext;
Gerard Ryan's avatar
Gerard Ryan committed
167

Gerard Ryan's avatar
Gerard Ryan committed
168 169
	usint	m = 1024;
	PlaintextModulus	ptm = 256;
Gerard Ryan's avatar
Gerard Ryan committed
170

Gerard Ryan's avatar
Gerard Ryan committed
171 172
	shared_ptr<ILParams> lp = ElemParamFactory::GenElemParams<ILParams>(m);
	EncodingParams ep( new EncodingParamsImpl(ptm) );
Gerard Ryan's avatar
Gerard Ryan committed
173

Gerard Ryan's avatar
Gerard Ryan committed
174 175 176 177 178 179 180 181
	auto randchar = []() -> char {
		const char charset[] =
				"0123456789"
				"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
				"abcdefghijklmnopqrstuvwxyz";
		const size_t max_index = (sizeof(charset) - 1);
		return charset[ rand() % max_index ];
	};
Gerard Ryan's avatar
Gerard Ryan committed
182

Gerard Ryan's avatar
Gerard Ryan committed
183 184
	string fullStr(m/2,0);
	std::generate_n(fullStr.begin(), m/2, randchar);
Gerard Ryan's avatar
Gerard Ryan committed
185 186

	while (state.KeepRunning()) {
Gerard Ryan's avatar
Gerard Ryan committed
187
		plaintext.reset( new StringEncoding(lp,ep,fullStr) );
Gerard Ryan's avatar
Gerard Ryan committed
188
		plaintext->Encode();
Gerard Ryan's avatar
Gerard Ryan committed
189 190 191
	}
}

Gerard Ryan's avatar
Gerard Ryan committed
192
BENCHMARK(BM_Encoding_String);
Gerard Ryan's avatar
Gerard Ryan committed
193 194 195 196 197 198

//execute the benchmarks
BENCHMARK_MAIN()