IntegerMath.cpp 7.72 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
/*
 * @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.
 *
 */
/*
  Description:	
  This code benchmarks functions of the math directory  of the PALISADE lattice encryption library.
*/
#include "benchmark/benchmark_api.h"

#include "AllBackends.h"

#include <iostream>
#include <vector>

using namespace std;
using namespace lbcrypto;

#define DO_BENCHMARK_TEMPLATE(X,Y) \
		BENCHMARK_TEMPLATE(X,Y)->Unit(benchmark::kMicrosecond);


//four simple benchmarks to test constructing BigInts
// typically the code to benchmark is in a 'function' that is then
// called within the actual benchmark.

// test BigInt constants
template<typename I>
static void make_BigInt_constants(void) {	// function
	I one(1);
}

template<typename I>
void BM_BigInt_constants(benchmark::State& state) { // benchmark
	while (state.KeepRunning()) {
		make_BigInt_constants<I>();
	}
}

Gerard Ryan's avatar
Gerard Ryan committed
60 61 62
DO_BENCHMARK_TEMPLATE(BM_BigInt_constants,M2Integer)
DO_BENCHMARK_TEMPLATE(BM_BigInt_constants,M4Integer)
DO_BENCHMARK_TEMPLATE(BM_BigInt_constants,M6Integer)
Gerard Ryan's avatar
Gerard Ryan committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
DO_BENCHMARK_TEMPLATE(BM_BigInt_constants,NativeInteger)

template<typename I>
static void make_BigInt_small_variables (void) {	// function
	I a("10403"), b("103");
}


template<typename I>
void BM_BigInt_small_variables(benchmark::State& state) { // benchmark
	while (state.KeepRunning()) {
		make_BigInt_small_variables<I>();
	}
}

Gerard Ryan's avatar
Gerard Ryan committed
78 79 80
DO_BENCHMARK_TEMPLATE(BM_BigInt_small_variables,M2Integer)
DO_BENCHMARK_TEMPLATE(BM_BigInt_small_variables,M4Integer)
DO_BENCHMARK_TEMPLATE(BM_BigInt_small_variables,M6Integer)
Gerard Ryan's avatar
Gerard Ryan committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94
DO_BENCHMARK_TEMPLATE(BM_BigInt_small_variables,NativeInteger)

template<typename I>
static void make_BigInt_large_variables (void) {	// function
	I a("18446744073709551616"), b("18446744073709551617");
}

template<typename I>
void BM_BigInt_large_variables(benchmark::State& state) { // benchmark
	while (state.KeepRunning()) {
		make_BigInt_large_variables<I>();
	}
}

Gerard Ryan's avatar
Gerard Ryan committed
95 96 97
DO_BENCHMARK_TEMPLATE(BM_BigInt_large_variables,M2Integer)
DO_BENCHMARK_TEMPLATE(BM_BigInt_large_variables,M4Integer)
DO_BENCHMARK_TEMPLATE(BM_BigInt_large_variables,M6Integer)
Gerard Ryan's avatar
Gerard Ryan committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118

static string smalla("10403"), smallb("103");
static string largea("18446744073709551616"), largeb("18446744073709551617");


// add
template<typename I>
static void add_BigInt(const I& a, const I& b) {	// function
	I c = a+b;
}

template<typename I>
static void BM_BigInt_Add(benchmark::State& state) { // benchmark
	I a( state.range(0) == 0 ? smalla : largea );
	I b( state.range(0) == 0 ? smallb : largeb );

	while (state.KeepRunning()) {
		add_BigInt(a, b);
	}
}

Gerard Ryan's avatar
Gerard Ryan committed
119 120 121 122 123 124
BENCHMARK_TEMPLATE(BM_BigInt_Add,M2Integer)->Unit(benchmark::kMicrosecond)->ArgName("Small")->Arg(0);
BENCHMARK_TEMPLATE(BM_BigInt_Add,M2Integer)->Unit(benchmark::kMicrosecond)->ArgName("Large")->Arg(1);
BENCHMARK_TEMPLATE(BM_BigInt_Add,M4Integer)->Unit(benchmark::kMicrosecond)->ArgName("Small")->Arg(0);
BENCHMARK_TEMPLATE(BM_BigInt_Add,M4Integer)->Unit(benchmark::kMicrosecond)->ArgName("Large")->Arg(1);
BENCHMARK_TEMPLATE(BM_BigInt_Add,M6Integer)->Unit(benchmark::kMicrosecond)->ArgName("Small")->Arg(0);
BENCHMARK_TEMPLATE(BM_BigInt_Add,M6Integer)->Unit(benchmark::kMicrosecond)->ArgName("Large")->Arg(1);
Gerard Ryan's avatar
Gerard Ryan committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
BENCHMARK_TEMPLATE(BM_BigInt_Add,NativeInteger)->Unit(benchmark::kMicrosecond)->ArgName("Small")->Arg(0);

// +=
template<typename I>
static void addeq_BigInt(I& a, const I& b) {	// function
	a += b;
}

template<typename I>
static void BM_BigInt_Addeq(benchmark::State& state) { // benchmark
	I b( state.range(0) == 0 ? smallb : largeb );

	while (state.KeepRunning()) {
		I a( state.range(0) == 0 ? smalla : largea );
		addeq_BigInt(a, b);
	}
}

Gerard Ryan's avatar
Gerard Ryan committed
143 144 145 146 147 148
BENCHMARK_TEMPLATE(BM_BigInt_Addeq,M2Integer)->Unit(benchmark::kMicrosecond)->ArgName("Small")->Arg(0);
BENCHMARK_TEMPLATE(BM_BigInt_Addeq,M2Integer)->Unit(benchmark::kMicrosecond)->ArgName("Large")->Arg(1);
BENCHMARK_TEMPLATE(BM_BigInt_Addeq,M4Integer)->Unit(benchmark::kMicrosecond)->ArgName("Small")->Arg(0);
BENCHMARK_TEMPLATE(BM_BigInt_Addeq,M4Integer)->Unit(benchmark::kMicrosecond)->ArgName("Large")->Arg(1);
BENCHMARK_TEMPLATE(BM_BigInt_Addeq,M6Integer)->Unit(benchmark::kMicrosecond)->ArgName("Small")->Arg(0);
BENCHMARK_TEMPLATE(BM_BigInt_Addeq,M6Integer)->Unit(benchmark::kMicrosecond)->ArgName("Large")->Arg(1);
Gerard Ryan's avatar
Gerard Ryan committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
BENCHMARK_TEMPLATE(BM_BigInt_Addeq,NativeInteger)->Unit(benchmark::kMicrosecond)->ArgName("Small")->Arg(0);

// mult
template<typename I>
static void mult_BigInt(const I& a, const I& b) {	// function
	I c1 = a*b;
}

template<typename I>
static void BM_BigInt_Mult(benchmark::State& state) { // benchmark
	I a( state.range(0) == 0 ? smalla : largea );
	I b( state.range(0) == 0 ? smallb : largeb );

	while (state.KeepRunning()) {
		mult_BigInt(a,b);
	}
}

Gerard Ryan's avatar
Gerard Ryan committed
167 168 169 170 171 172
BENCHMARK_TEMPLATE(BM_BigInt_Mult,M2Integer)->Unit(benchmark::kMicrosecond)->ArgName("Small")->Arg(0);
BENCHMARK_TEMPLATE(BM_BigInt_Mult,M2Integer)->Unit(benchmark::kMicrosecond)->ArgName("Large")->Arg(1);
BENCHMARK_TEMPLATE(BM_BigInt_Mult,M4Integer)->Unit(benchmark::kMicrosecond)->ArgName("Small")->Arg(0);
BENCHMARK_TEMPLATE(BM_BigInt_Mult,M4Integer)->Unit(benchmark::kMicrosecond)->ArgName("Large")->Arg(1);
BENCHMARK_TEMPLATE(BM_BigInt_Mult,M6Integer)->Unit(benchmark::kMicrosecond)->ArgName("Small")->Arg(0);
BENCHMARK_TEMPLATE(BM_BigInt_Mult,M6Integer)->Unit(benchmark::kMicrosecond)->ArgName("Large")->Arg(1);
Gerard Ryan's avatar
Gerard Ryan committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
BENCHMARK_TEMPLATE(BM_BigInt_Mult,NativeInteger)->Unit(benchmark::kMicrosecond)->ArgName("Small")->Arg(0);

// *=
template<typename I>
static void multeq_BigInt(I& a, const I& b) {	// function
	a *= b;
}

template<typename I>
static void BM_BigInt_Multeq(benchmark::State& state) { // benchmark
	I b( state.range(0) == 0 ? smallb : largeb );

	while (state.KeepRunning()) {
		I a( state.range(0) == 0 ? smalla : largea );
		multeq_BigInt(a, b);
	}
}

Gerard Ryan's avatar
Gerard Ryan committed
191 192 193 194 195 196
BENCHMARK_TEMPLATE(BM_BigInt_Multeq,M2Integer)->Unit(benchmark::kMicrosecond)->ArgName("Small")->Arg(0);
BENCHMARK_TEMPLATE(BM_BigInt_Multeq,M2Integer)->Unit(benchmark::kMicrosecond)->ArgName("Large")->Arg(1);
BENCHMARK_TEMPLATE(BM_BigInt_Multeq,M4Integer)->Unit(benchmark::kMicrosecond)->ArgName("Small")->Arg(0);
BENCHMARK_TEMPLATE(BM_BigInt_Multeq,M4Integer)->Unit(benchmark::kMicrosecond)->ArgName("Large")->Arg(1);
BENCHMARK_TEMPLATE(BM_BigInt_Multeq,M6Integer)->Unit(benchmark::kMicrosecond)->ArgName("Small")->Arg(0);
BENCHMARK_TEMPLATE(BM_BigInt_Multeq,M6Integer)->Unit(benchmark::kMicrosecond)->ArgName("Large")->Arg(1);
Gerard Ryan's avatar
Gerard Ryan committed
197 198 199 200
BENCHMARK_TEMPLATE(BM_BigInt_Multeq,NativeInteger)->Unit(benchmark::kMicrosecond)->ArgName("Small")->Arg(0);

//execute the benchmarks
BENCHMARK_MAIN()